Debian Codegraph @

src/main/java/org/freehep/util/io/LineNumberWriter.java

raw
1 // Copyright 2003, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.IOException;
5 import java.io.Writer;
6 import java.util.EventListener;
7 import java.util.EventObject;
8 import java.util.TooManyListenersException;
9
10 /**
11 * Counts line numbers, based on the first cr-lf, cr or lf it finds. Informs a
12 * listener when the linenumber exceeds a threshold.
13 *
14 * Listeners can only be informed from the second line only.
15 *
16 * @author Mark Donszelmann
17 * @version $Id: LineNumberWriter.java 8584 2006-08-10 23:06:37Z duns $
18 */
19 public class LineNumberWriter extends Writer {
20
21 private final static int UNKNOWN = 0;
22
23 private final static int CR = 1;
24
25 private final static int CRLF = 2;
26
27 private final static int LF = 3;
28
29 private final static int LFCR = 4;
30
31 private int lineSeparator = UNKNOWN;
32
33 private Writer out;
34
35 private int lineNo = 0;
36
37 private LineNumberListener listener;
38
39 private int lineNoLimit;
40
41 private int previous = -1;
42
43 /**
44 * Creates a Line Number Writer
45 *
46 * @param out writer to write to
47 */
48 public LineNumberWriter(Writer out) {
49 this.out = out;
50 }
51
52 public void write(char cbuf[]) throws IOException {
53 write(cbuf, 0, cbuf.length);
54 }
55
56 public void write(char cbuf[], int off, int len) throws IOException {
57 for (int i = 0; i < len; i++) {
58 write(cbuf[off + i]);
59 }
60 }
61
62 public void write(String str) throws IOException {
63 write(str, 0, str.length());
64 }
65
66 public void write(String str, int off, int len) throws IOException {
67 for (int i = 0; i < len; i++) {
68 write(str.charAt(off + i));
69 }
70 }
71
72 public void write(int c) throws IOException {
73 boolean newLine = false;
74 synchronized (lock) {
75 out.write(c);
76
77 switch (lineSeparator) {
78 default:
79 case UNKNOWN:
80 switch (previous) {
81 case '\r':
82 lineNo++;
83 lineSeparator = (c == '\n') ? CRLF : CR;
84 if (c == '\r')
85 lineNo++;
86 break;
87 case '\n':
88 lineNo++;
89 lineSeparator = (c == '\r') ? LFCR : LF;
90 if (c == '\n')
91 lineNo++;
92 break;
93 default:
94 break;
95 }
96 break;
97 case CR:
98 if (c == '\r') {
99 lineNo++;
100 newLine = true;
101 }
102 break;
103 case CRLF:
104 if ((previous == '\r') && (c == '\n')) {
105 lineNo++;
106 newLine = true;
107 }
108 break;
109 case LF:
110 if (c == '\n') {
111 lineNo++;
112 newLine = true;
113 }
114 break;
115 case LFCR:
116 if ((previous == '\n') && (c == '\r')) {
117 lineNo++;
118 newLine = true;
119 }
120 break;
121 }
122 previous = c;
123 }
124
125 if ((listener != null) && newLine && (lineNo >= lineNoLimit)) {
126 listener.lineNumberReached(new LineNumberEvent(this, lineNo));
127 }
128 }
129
130 public void close() throws IOException {
131 out.close();
132 }
133
134 public void flush() throws IOException {
135 out.flush();
136 }
137
138 /**
139 * Returns the line number that is currently being written.
140 *
141 * @return current line number
142 */
143 public int getLineNumber() {
144 return lineNo;
145 }
146
147 /**
148 * Set the current line number
149 *
150 * @param lineNo new line number
151 */
152 public void setLineNumber(int lineNo) {
153 this.lineNo = lineNo;
154 }
155
156 /**
157 * Add a LineNumberListener
158 *
159 * @param listener new listener
160 * @param lineNoLimit line number for which to generate a LineNumberEvent
161 * @throws TooManyListenersException if there is more than one listener
162 */
163 public void addLineNumberListener(LineNumberListener listener,
164 int lineNoLimit) throws TooManyListenersException {
165 if (this.listener != null)
166 throw new TooManyListenersException();
167 if (lineNoLimit < 2)
168 throw new IllegalArgumentException(
169 "LineNoLimit cannot be less than 2");
170
171 this.listener = listener;
172 this.lineNoLimit = lineNoLimit;
173 }
174
175 /**
176 * LineNumberListener interface can inform a listener about changes in the
177 * line number, or when a linenumber limit has been reached.
178 *
179 * @author duns
180 * @version $Id: LineNumberWriter.java 8584 2006-08-10 23:06:37Z duns $
181 */
182 public static interface LineNumberListener extends EventListener {
183
184 /**
185 * Called when the line number limit has been reached.
186 *
187 * @param event line number event
188 */
189 public void lineNumberReached(LineNumberEvent event);
190 }
191
192 /**
193 * Event to be used by the LineNumberListener interface.
194 *
195 * @author duns
196 * @version $Id: LineNumberWriter.java 8584 2006-08-10 23:06:37Z duns $
197 */
198 public static class LineNumberEvent extends EventObject {
199 /**
200 *
201 */
202 private static final long serialVersionUID = 2821724279014031198L;
203
204 private int lineNo;
205
206 /**
207 * Create a LineNumberEvent
208 *
209 * @param source event source
210 * @param lineNo current line number
211 */
212 public LineNumberEvent(Object source, int lineNo) {
213 super(source);
214 this.lineNo = lineNo;
215 }
216
217 /**
218 * @return current line number
219 */
220 public int getLineNumber() {
221 return lineNo;
222 }
223
224 public String toString() {
225 return "LineNumberEvent: line=" + lineNo + "; "
226 + getSource().toString();
227 }
228 }
229 }