1 11 12 package SOFA.SOFAnode.Util.DFSRChecker.parser; 13 14 import java.io.FileNotFoundException ; 15 import java.io.FileReader ; 16 import java.io.IOException ; 17 18 22 public class FileTokenizer extends ProtocolReader { 23 24 30 public FileTokenizer(String filename) throws IOException { 31 32 try { 33 reader = new FileReader (filename); 34 } catch (FileNotFoundException e) { 35 throw new IOException (); 36 } 37 } 38 39 45 public int read() throws IOException { 46 char read = (char)-1; 47 while (reader.ready()) { 48 read = (char)reader.read(); 49 50 if ((read == ' ') || (read == '\t') || (read == '\n') || (read == '\r')) 51 ; 53 else if (read == '#') { 54 String line = getLine(); 55 if (line.equals("eop")) 56 return -1; 57 else 58 ; } 60 else break; 62 63 } 64 65 if (read != -1) { 66 ++index; 67 return read; 68 } 69 else 70 return -1; 71 } 72 73 74 79 public boolean ready() throws IOException { 80 return reader.ready(); 81 } 82 83 84 85 86 89 public int getIndex() { 90 return index; 91 } 92 93 96 public int read(char[] arg0, int arg1, int arg2) throws IOException { 97 throw new IOException ("Not implemented"); 99 } 100 101 104 public void close() throws IOException { 105 reader.close(); 106 } 107 108 109 112 public void resetIndex() { 113 index = 0; 114 } 115 116 117 private String getLine() throws IOException { 118 String line = new String (); 119 char read; 120 121 while (reader.ready()) { 122 read = (char)reader.read(); 123 if ((read != '\n') && (read != '\r')) 124 line += read; 125 else 126 break; 127 } 128 129 return line; 130 } 131 132 133 134 135 138 private FileReader reader; 139 140 143 private int index = 0; 144 145 } 146 | Popular Tags |