View Javadoc

1   package net.sf.quarrel.file;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.ArrayList;
6   import java.util.Collections;
7   import java.util.Iterator;
8   import java.util.List;
9   
10  /***
11   * This class is responsible for parsing an InputStream into a set of ElementText objects.  Step to using it are as
12   * follows
13   * <p/>
14   * <ul>
15   * <li> create an instance passing in the InputStream
16   * <li> call process() which does the actual parsing
17   * <li> call elements which will give you back an Iterator of ElementText object
18   * <li> iterate over the ElementText and have fun
19   * </ul>
20   *
21   * @deprecated Use StreamParser
22   */
23  public class StreamProcessor {
24  
25      private final InputStream _inputStream;
26      private final List _elements = new ArrayList();
27      private boolean _processed = false;
28  
29      public StreamProcessor(InputStream inputStream) {
30          _inputStream = inputStream;
31      }
32  
33      public void process() throws IOException {
34  
35          String contents = readStreamContents();
36  
37          final String[] strings = contents.split("\r\n\r\n");
38          for (int i = 0; i < strings.length; i++) {
39              String string = strings[i];
40              final ElementText elementText = new ElementText(string);
41              _elements.add(elementText);
42          }
43  
44          _processed = true;
45  
46      }
47  
48      private String readStreamContents() throws IOException {
49  
50          StringBuffer buffer = new StringBuffer(2000);
51  
52          int i;
53          while ((i = _inputStream.read()) != -1) {
54              buffer.append((char) i);
55          }
56  
57          return buffer.toString();
58  
59      }
60  
61      public Iterator elements() {
62  
63          if (!_processed) {
64              throw new IllegalStateException();
65          }
66  
67          return Collections.unmodifiableList(_elements).iterator();
68  
69      }
70  
71  }