View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.module.apt;
20  
21  import java.io.IOException;
22  import java.io.LineNumberReader;
23  import java.io.Reader;
24  
25  /**
26   * Reader for apt source documents.
27   */
28  public class AptReaderSource implements AptSource {
29      /** A reader. */
30      private LineNumberReader reader;
31  
32      /** lineNumber. */
33      private int lineNumber;
34  
35      /** The name, e.g. the filename. */
36      private String name;
37  
38      /**
39       * Constructor: initialize reader.
40       *
41       * @param in the reader.
42       */
43      public AptReaderSource(Reader in) {
44          reader = new LineNumberReader(in);
45  
46          lineNumber = -1;
47      }
48  
49      /**
50       * Constructor: initialize reader.
51       *
52       * @param in the reader.
53       * @param name the name of the source
54       */
55      public AptReaderSource(Reader in, String name) {
56          this(in);
57  
58          this.name = name;
59      }
60  
61      /**
62       * {@inheritDoc}
63       *
64       * @return a {@link java.lang.String} object.
65       * @throws org.apache.maven.doxia.module.apt.AptParseException if any.
66       */
67      public String getNextLine() throws AptParseException {
68          if (reader == null) {
69              return null;
70          }
71  
72          String line;
73  
74          try {
75              line = reader.readLine();
76              if (line == null) {
77                  reader.close();
78                  reader = null;
79              } else {
80                  lineNumber = reader.getLineNumber();
81              }
82          } catch (IOException e) {
83              // TODO handle column number
84              throw new AptParseException(null, e, lineNumber, -1);
85          }
86  
87          return line;
88      }
89  
90      /**
91       * {@inheritDoc}
92       *
93       * @return a {@link java.lang.String} object.
94       */
95      public String getName() {
96          // never return null
97          return name != null ? name : "";
98      }
99  
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 }