01: package org.jical;
02:
03: /*
04: * Never used..
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.io.InputStreamReader;
08: import java.io.BufferedReader;
09: import java.io.UnsupportedEncodingException;
10: */
11: import java.io.Reader;
12: import java.util.Iterator;
13: import java.util.List;
14: import java.util.ArrayList;
15:
16: public class UnfoldingLineIterator implements Iterator {
17:
18: private Iterator m_iterator;
19: private List m_lines = new ArrayList();
20:
21: public UnfoldingLineIterator(Reader reader) {
22: this (new LineIterator(reader));
23: }
24:
25: public UnfoldingLineIterator(Iterator iterator) {
26: m_iterator = iterator;
27: }
28:
29: public boolean hasNext() {
30: checkLines();
31: return (!m_lines.isEmpty());
32: }
33:
34: public Object next() {
35: if (hasNext()) {
36: return m_lines.remove(0);
37: }
38: return null;
39: }
40:
41: public void remove() throws UnsupportedOperationException {
42: throw new UnsupportedOperationException();
43: }
44:
45: private void checkLines() {
46: synchronized (m_lines) {
47: if (m_lines.size() < 2 && m_iterator.hasNext()) {
48: StringBuffer line = (StringBuffer) m_iterator.next();
49: if (line != null) {
50: m_lines.add(line);
51: unfoldLines();
52: checkLines();
53: }
54: }
55: }
56: }
57:
58: private void unfoldLines() {
59: synchronized (m_lines) {
60: int i = 1;
61: while (i < m_lines.size()) {
62: StringBuffer line = (StringBuffer) m_lines.get(i);
63: char c = line.charAt(0);
64: if (c == ' ' || c == '\t') {
65: m_lines.remove(i);
66: StringBuffer pline = (StringBuffer) m_lines
67: .get(i - 1);
68: line.deleteCharAt(0);
69: pline.append(line);
70: } else {
71: i++;
72: }
73: }
74: }
75: }
76:
77: }
|