View Javadoc
1   package org.apache.maven.doxia.module.apt;
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  import java.io.IOException;
23  import java.io.LineNumberReader;
24  import java.io.Reader;
25  
26  import org.codehaus.plexus.util.IOUtil;
27  
28  /**
29   * Reader for apt source documents.
30   */
31  public class AptReaderSource
32      implements AptSource
33  {
34      /** A reader. */
35      private LineNumberReader reader;
36  
37      /** lineNumber. */
38      private int lineNumber;
39  
40      /** The name, e.g. the filename. */
41      private String name;
42  
43      /**
44       * Constructor: initialize reader.
45       *
46       * @param in the reader.
47       */
48      public AptReaderSource( Reader in )
49      {
50          reader = new LineNumberReader( in );
51  
52          lineNumber = -1;
53      }
54  
55      /**
56       * Constructor: initialize reader.
57       *
58       * @param in the reader.
59       * @param name the name of the source
60       */
61      public AptReaderSource( Reader in, String name )
62      {
63          this( in );
64  
65          this.name = name;
66      }
67  
68      /**
69       * {@inheritDoc}
70       *
71       * @return a {@link java.lang.String} object.
72       * @throws org.apache.maven.doxia.module.apt.AptParseException if any.
73       */
74      public String getNextLine()
75          throws AptParseException
76      {
77          if ( reader == null )
78          {
79              return null;
80          }
81  
82          String line;
83  
84          try
85          {
86              line = reader.readLine();
87              if ( line == null )
88              {
89                  reader.close();
90                  reader = null;
91              }
92              else
93              {
94                  lineNumber = reader.getLineNumber();
95              }
96          }
97          catch ( IOException e )
98          {
99              // 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 }