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