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 import org.codehaus.plexus.util.IOUtil;
26
27 /**
28 * Reader for apt source documents.
29 */
30 public class AptReaderSource implements AptSource {
31 /** A reader. */
32 private LineNumberReader reader;
33
34 /** lineNumber. */
35 private int lineNumber;
36
37 /** The name, e.g. the filename. */
38 private String name;
39
40 /**
41 * Constructor: initialize reader.
42 *
43 * @param in the reader.
44 */
45 public AptReaderSource(Reader in) {
46 reader = new LineNumberReader(in);
47
48 lineNumber = -1;
49 }
50
51 /**
52 * Constructor: initialize reader.
53 *
54 * @param in the reader.
55 * @param name the name of the source
56 */
57 public AptReaderSource(Reader in, String name) {
58 this(in);
59
60 this.name = name;
61 }
62
63 /**
64 * {@inheritDoc}
65 *
66 * @return a {@link java.lang.String} object.
67 * @throws org.apache.maven.doxia.module.apt.AptParseException if any.
68 */
69 public String getNextLine() throws AptParseException {
70 if (reader == null) {
71 return null;
72 }
73
74 String line;
75
76 try {
77 line = reader.readLine();
78 if (line == null) {
79 reader.close();
80 reader = null;
81 } else {
82 lineNumber = reader.getLineNumber();
83 }
84 } catch (IOException e) {
85 // TODO handle column number
86 throw new AptParseException(null, e, lineNumber, -1);
87 }
88
89 return line;
90 }
91
92 /**
93 * {@inheritDoc}
94 *
95 * @return a {@link java.lang.String} object.
96 */
97 public String getName() {
98 // never return null
99 return name != null ? name : "";
100 }
101
102 /**
103 * {@inheritDoc}
104 *
105 * @return a int.
106 */
107 public int getLineNumber() {
108 return lineNumber;
109 }
110
111 /**
112 * Closes the reader associated with this AptReaderSource.
113 */
114 public void close() {
115 IOUtil.close(reader);
116 reader = null;
117 }
118 }