1 package org.apache.maven.doxia.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
36
37 public class ByLineReaderSource implements ByLineSource
38 {
39
40
41
42 private LineNumberReader reader;
43
44
45
46
47 private int lineNumber;
48
49
50
51
52 private String lastLine;
53
54
55
56
57
58 private boolean ungetted = false;
59
60 private String name;
61
62
63
64
65
66
67 public ByLineReaderSource( final Reader in )
68 {
69 this( in, "" );
70 }
71
72
73
74
75
76
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
89
90
91
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
133
134
135
136 public final String getName()
137 {
138 return name;
139 }
140
141
142
143
144
145
146 public final int getLineNumber()
147 {
148 return lineNumber;
149 }
150
151
152
153
154 public final void close()
155 {
156 IOUtil.close( reader );
157 reader = null;
158 }
159
160
161
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
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 }