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
38
39 public class ByLineReaderSource implements ByLineSource
40 {
41
42
43
44 private LineNumberReader reader;
45
46
47
48
49 private int lineNumber;
50
51
52
53
54 private String lastLine;
55
56
57
58
59
60 private boolean ungetted = false;
61
62
63
64
65
66
67 public ByLineReaderSource( final Reader in )
68 {
69 reader = new LineNumberReader( in );
70
71 lineNumber = -1;
72 }
73
74
75 public final String getNextLine() throws ParseException
76 {
77 if ( reader == null )
78 {
79 return null;
80 }
81
82 if ( ungetted )
83 {
84 ungetted = false;
85 return lastLine;
86 }
87
88 String line;
89
90 try
91 {
92 line = reader.readLine();
93 if ( line == null )
94 {
95 reader.close();
96 reader = null;
97 }
98 else
99 {
100 lineNumber = reader.getLineNumber();
101 }
102 }
103 catch ( IOException e )
104 {
105 throw new ParseException( e, lineNumber, 0 );
106 }
107
108 lastLine = line;
109
110 return line;
111 }
112
113
114 public final String getName()
115 {
116 return "";
117 }
118
119
120 public final int getLineNumber()
121 {
122 return lineNumber;
123 }
124
125
126 public final void close()
127 {
128 IOUtil.close( reader );
129 reader = null;
130 }
131
132
133 public final void ungetLine()
134 {
135 if ( ungetted )
136 {
137 throw new IllegalStateException( "we support only one level of ungetLine()" );
138 }
139 ungetted = true;
140 }
141
142
143 public final void unget( final String s )
144 {
145 if ( s == null )
146 {
147 throw new IllegalArgumentException( "argument can't be null" );
148 }
149 if ( s.length() != 0 )
150 {
151 ungetLine();
152 lastLine = s;
153 }
154 }
155 }