Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/trapezium/parse/TextLineParser.java


1   /*
2    * @(#)TextLineParser.java
3    *
4    * Copyright (c) 1998 by Trapezium Development LLC.  All Rights Reserved.
5    *
6    * The information in this file is the property of Trapezium Development LLC
7    * and may be used only in accordance with the terms of the license granted
8    * by Trapezium.
9    *
10   */
11  package com.trapezium.parse;
12  
13  import java.io.*;
14  import java.util.*;
15  
16  /**
17   *  Converts InputStream into a vector of lines.  Works with 
18   *  TextLineEnumerator.  No longer used with VRML files, Strings too
19   *  memory intensive.  Should be removed.
20   *
21   *  @author          Johannes N. Johannsen
22   *  @version         1.0, 31 Oct 1997
23   *
24   *  @since           1.0
25   */
26  public class TextLineParser {
27    Vector lines;
28  
29      /**
30       *  Class constructor, creates vector of String lines from an InputStream
31       *
32       *  @param  inStream  an InputStream containing text
33       */
34    public TextLineParser( InputStream inStream ) {
35      lines = new Vector( 5000, 5000 );
36        loadLines( inStream );
37      }
38  
39      /**
40       *  Class Constructor, create vector of String lines from a file
41       *
42       *  @param  source  a File containing text
43       */
44    public TextLineParser( File source ) throws FileNotFoundException, IOException {
45      lines = new Vector( 100, 1000 );
46      BufferedInputStream inStream = new BufferedInputStream( new FileInputStream( source ));
47      loadLines( inStream );
48    }
49  
50      /**
51       *  Get the vector of Strings that were loaded from the file
52       *
53       *  @return  a vector of String objects
54       */
55    public Vector getLines() {
56      return( lines );
57    }
58  
59      /**
60       *  Initialize the vector of string objects from text read from an InputStream
61       *
62       *  @param  inStream   the InputStream containing the text
63       */
64    void loadLines( InputStream inStream ) {
65      int pushChar = 0;
66      int pushChar2 = 0;
67      int lineCount = 0;
68      try {
69        while ( true ) {
70          // The StringBuffer has the contents of the line
71          StringBuffer buf = new StringBuffer();
72  
73          // flag indicates whether the line was added to the list, used only
74          // when end of file is reached, otherwise \n or \r result in line being 
75          // added
76          boolean addedBuf = false;
77  
78          // The following formats for end of line have been encountered:
79          //    \r -- Late Night VRML 2.0
80          //    \r\r\n -- file from michael
81          //    \r\n -- vi, notepad
82          //
83          // General rule seems to be if there is a \n, we can ignore the \r's.
84          // Problem then becomes detecting when there are \r's or not.
85          int x = pushChar;
86          if (( x != 0 ) && ( x != -1 )) {
87            buf.append( (char)x );
88          }
89          if (( buf.length() == 1 ) && (( buf.charAt( 0 ) == '\n' ) || ( buf.charAt( 0 ) == '\r' ))) {
90            String l = new String( buf );
91            lines.addElement( l );
92            lineCount++;
93            buf = new StringBuffer();
94          }
95          x = pushChar2;
96          if (( x != 0 ) && ( x != -1 )) {
97            buf.append( (char)x );
98          }
99  
100         pushChar = 0;
101         pushChar2 = 0;
102         // read the stream until:  end of file, \r or \n
103         while (( x = inStream.read() ) != -1 ) {
104           // we have a \r character, 
105           if ( x == '\r' ) {
106             String l = new String( buf );
107             lines.addElement( l );
108             lineCount++;
109             x = inStream.read();  // skip other one
110             if ( x != '\n' ) {
111               // Some files have lines ending with \r\r\n!
112               if ( x == '\r' ) {
113                 x = inStream.read();
114                 if ( x != '\n' ) {
115                   pushChar = '\r';
116                   pushChar2 = x;
117                 }
118               } else {
119                 pushChar = x;
120               }
121             }
122             addedBuf = true;
123             break;
124           }
125           if ( x == '\n' ) {
126             String l = new String( buf );
127             lines.addElement( l );
128             lineCount++;
129             x = inStream.read();  // skip other one
130             if ( x != '\r' ) {
131               pushChar = x;
132             }
133             addedBuf = true;
134             break;
135           }
136           buf.append( (char)x );
137         }
138         if (( x == -1 ) && !addedBuf ) {
139           String l = new String( buf );
140           lines.addElement( l );
141           lineCount++;
142           break;
143         }
144       }
145 
146       inStream.close();
147     } catch ( Exception e ) {
148       System.out.println( "Parser load lines failed: " + e );
149     }
150   }
151 
152 
153     /**
154      *  Debugging dump of the TextLineParser
155      */
156   public void dump() {
157     for ( int i = 0; i < lines.size(); i++ ) {
158       String s = (String)lines.elementAt( i );
159       if ( s == null ) {
160         System.out.println( "" );
161       } else {
162         System.out.println( (String)lines.elementAt( i ) );
163       }
164     }
165   }
166 }
167 
168