001package org.apache.maven.doxia.module.apt;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.io.LineNumberReader;
024import java.io.Reader;
025
026import org.codehaus.plexus.util.IOUtil;
027
028/**
029 * Reader for apt source documents.
030 */
031public class AptReaderSource
032    implements AptSource
033{
034    /** A reader. */
035    private LineNumberReader reader;
036
037    /** lineNumber. */
038    private int lineNumber;
039
040    /** The name, e.g. the filename. */
041    private String name;
042
043    /**
044     * Constructor: initialize reader.
045     *
046     * @param in the reader.
047     */
048    public AptReaderSource( Reader in )
049    {
050        reader = new LineNumberReader( in );
051
052        lineNumber = -1;
053    }
054
055    /**
056     * Constructor: initialize reader.
057     *
058     * @param in the reader.
059     * @param name the name of the source
060     */
061    public AptReaderSource( Reader in, String name )
062    {
063        this( in );
064
065        this.name = name;
066    }
067
068    /**
069     * {@inheritDoc}
070     *
071     * @return a {@link java.lang.String} object.
072     * @throws org.apache.maven.doxia.module.apt.AptParseException if any.
073     */
074    public String getNextLine()
075        throws AptParseException
076    {
077        if ( reader == null )
078        {
079            return null;
080        }
081
082        String line;
083
084        try
085        {
086            line = reader.readLine();
087            if ( line == null )
088            {
089                reader.close();
090                reader = null;
091            }
092            else
093            {
094                lineNumber = reader.getLineNumber();
095            }
096        }
097        catch ( IOException e )
098        {
099            // TODO handle column number
100            throw new AptParseException( "IOException: " + e.getMessage(), e, lineNumber, -1 );
101        }
102
103        return line;
104    }
105
106    /**
107     * {@inheritDoc}
108     *
109     * @return a {@link java.lang.String} object.
110     */
111    public String getName()
112    {
113        // never return null
114        return name != null ? name : "";
115    }
116
117    /**
118     * {@inheritDoc}
119     *
120     * @return a int.
121     */
122    public int getLineNumber()
123    {
124        return lineNumber;
125    }
126
127    /**
128     * Closes the reader associated with this AptReaderSource.
129     */
130    public void close()
131    {
132        IOUtil.close( reader );
133        reader = null;
134    }
135}