View Javadoc
1   package org.apache.maven.doxia.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /*
23   * Originally from org.apache.doxia.module.apt.AptReaderSource. It was modified
24   * to get unget support
25   */
26  
27  import java.io.IOException;
28  import java.io.LineNumberReader;
29  import java.io.Reader;
30  
31  import org.apache.maven.doxia.parser.ParseException;
32  import org.codehaus.plexus.util.IOUtil;
33  
34  /**
35   * {@link ByLineSource} default implementation
36   */
37  public class ByLineReaderSource implements ByLineSource
38  {
39      /**
40       * reader
41       */
42      private LineNumberReader reader;
43  
44      /**
45       * current line number
46       */
47      private int lineNumber;
48  
49      /**
50       * holds the last line returned by getNextLine()
51       */
52      private String lastLine;
53  
54      /**
55       * <code>true</code> if ungetLine() was called and no getNextLine() was
56       * called
57       */
58      private boolean ungetted = false;
59      
60      private String name;
61  
62      /**
63       * Creates the ByLineReaderSource.
64       *
65       * @param in real source :)
66       */
67      public ByLineReaderSource( final Reader in )
68      {
69          this( in, "" );
70      }
71      
72      /**
73       * <p>Constructor for ByLineReaderSource.</p>
74       *
75       * @param in a {@link java.io.Reader} object.
76       * @param name a {@link java.lang.String} object.
77       */
78      public ByLineReaderSource( final Reader in, final String name )
79      {
80          this.reader = new LineNumberReader( in );
81          
82          this.name = name;
83  
84          this.lineNumber = -1;
85      }
86  
87      /**
88       * {@inheritDoc}
89       *
90       * @return a {@link java.lang.String} object.
91       * @throws org.apache.maven.doxia.parser.ParseException if any.
92       */
93      public final String getNextLine() throws ParseException
94      {
95          if ( reader == null )
96          {
97              return null;
98          }
99  
100         if ( ungetted )
101         {
102             ungetted = false;
103             return lastLine;
104         }
105 
106         String line;
107 
108         try
109         {
110             line = reader.readLine();
111             if ( line == null )
112             {
113                 reader.close();
114                 reader = null;
115             }
116             else
117             {
118                 lineNumber = reader.getLineNumber();
119             }
120         }
121         catch ( IOException e )
122         {
123             throw new ParseException( e, lineNumber, 0 );
124         }
125 
126         lastLine = line;
127 
128         return line;
129     }
130 
131     /**
132      * {@inheritDoc}
133      *
134      * @return a {@link java.lang.String} object.
135      */
136     public final String getName()
137     {
138         return name;
139     }
140 
141     /**
142      * {@inheritDoc}
143      *
144      * @return a int.
145      */
146     public final int getLineNumber()
147     {
148         return lineNumber;
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     public final void close()
155     {
156         IOUtil.close( reader );
157         reader = null;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     public final void ungetLine()
164     {
165         if ( ungetted )
166         {
167             throw new IllegalStateException( "we support only one level of ungetLine()" );
168         }
169         ungetted = true;
170     }
171 
172     /** {@inheritDoc} */
173     public final void unget( final String s )
174     {
175         if ( s == null )
176         {
177             throw new IllegalArgumentException( "argument can't be null" );
178         }
179         if ( s.length() != 0 )
180         {
181             ungetLine();
182             lastLine = s;
183         }
184     }
185 }