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