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.util;
020
021/*
022 * Originally from org.apache.doxia.module.apt.AptReaderSource. It was modified
023 * to get unget support
024 */
025
026import java.io.IOException;
027import java.io.LineNumberReader;
028import java.io.Reader;
029import java.util.Objects;
030
031import org.apache.maven.doxia.parser.ParseException;
032
033/**
034 * {@link ByLineSource} default implementation
035 */
036public class ByLineReaderSource implements ByLineSource {
037    /**
038     * reader
039     */
040    private LineNumberReader reader;
041
042    /**
043     * current line number
044     */
045    private int lineNumber;
046
047    /**
048     * holds the last line returned by getNextLine()
049     */
050    private String lastLine;
051
052    /**
053     * <code>true</code> if ungetLine() was called and no getNextLine() was
054     * called
055     */
056    private boolean ungetted = false;
057
058    private String name;
059
060    /**
061     * Creates the ByLineReaderSource.
062     *
063     * @param in real source :)
064     */
065    public ByLineReaderSource(final Reader in) {
066        this(in, "");
067    }
068
069    /**
070     * <p>Constructor for ByLineReaderSource.</p>
071     *
072     * @param in a {@link java.io.Reader} object.
073     * @param name a {@link java.lang.String} object.
074     */
075    public ByLineReaderSource(final Reader in, final String name) {
076        this.reader = new LineNumberReader(in);
077
078        this.name = name;
079
080        this.lineNumber = -1;
081    }
082
083    /**
084     * {@inheritDoc}
085     *
086     * @return a {@link java.lang.String} object.
087     * @throws org.apache.maven.doxia.parser.ParseException if any.
088     */
089    public final String getNextLine() throws ParseException {
090        if (reader == null) {
091            return null;
092        }
093
094        if (ungetted) {
095            ungetted = false;
096            return lastLine;
097        }
098
099        String line;
100
101        try {
102            line = reader.readLine();
103            if (line == null) {
104                reader.close();
105                reader = null;
106            } else {
107                lineNumber = reader.getLineNumber();
108            }
109        } catch (IOException e) {
110            throw new ParseException(e, lineNumber, 0);
111        }
112
113        lastLine = line;
114
115        return line;
116    }
117
118    /**
119     * {@inheritDoc}
120     *
121     * @return a {@link java.lang.String} object.
122     */
123    public final String getName() {
124        return name;
125    }
126
127    /**
128     * {@inheritDoc}
129     *
130     * @return a int.
131     */
132    public final int getLineNumber() {
133        return lineNumber;
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    public final void close() {
140        if (reader == null) {
141            return;
142        }
143
144        try {
145            reader.close();
146        } catch (IOException ex) {
147            // ignore
148        }
149        reader = null;
150    }
151
152    /**
153     * {@inheritDoc}
154     */
155    public final void ungetLine() {
156        if (ungetted) {
157            throw new IllegalStateException("we support only one level of ungetLine()");
158        }
159        ungetted = true;
160    }
161
162    /** {@inheritDoc} */
163    public final void unget(final String s) {
164        Objects.requireNonNull(s, "s cannot be null");
165
166        if (s.length() != 0) {
167            ungetLine();
168            lastLine = s;
169        }
170    }
171}