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 *
031 * @version $Id: AptReaderSource.html 905940 2014-04-12 16:27:29Z hboutemy $
032 */
033public class AptReaderSource
034    implements AptSource
035{
036    /** A reader. */
037    private LineNumberReader reader;
038
039    /** lineNumber. */
040    private int lineNumber;
041
042    /** The name, e.g. the filename. */
043    private String name;
044
045    /**
046     * Constructor: initialize reader.
047     *
048     * @param in the reader.
049     */
050    public AptReaderSource( Reader in )
051    {
052        reader = new LineNumberReader( in );
053
054        lineNumber = -1;
055    }
056
057    /**
058     * Constructor: initialize reader.
059     *
060     * @param in the reader.
061     * @param name the name of the source
062     */
063    public AptReaderSource( Reader in, String name )
064    {
065        this( in );
066
067        this.name = name;
068    }
069
070    /** {@inheritDoc} */
071    public String getNextLine()
072        throws AptParseException
073    {
074        if ( reader == null )
075        {
076            return null;
077        }
078
079        String line;
080
081        try
082        {
083            line = reader.readLine();
084            if ( line == null )
085            {
086                reader.close();
087                reader = null;
088            }
089            else
090            {
091                lineNumber = reader.getLineNumber();
092            }
093        }
094        catch ( IOException e )
095        {
096            // TODO handle column number
097            throw new AptParseException( "IOException: " + e.getMessage(), e, lineNumber, -1 );
098        }
099
100        return line;
101    }
102
103    /** {@inheritDoc} */
104    public String getName()
105    {
106        // never return null
107        return name != null ? name : "";
108    }
109
110    /** {@inheritDoc} */
111    public int getLineNumber()
112    {
113        return lineNumber;
114    }
115
116    /**
117     * Closes the reader associated with this AptReaderSource.
118     */
119    public void close()
120    {
121        IOUtil.close( reader );
122        reader = null;
123    }
124}