KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > parser > FileTokenizer


1 /*
2  * $Id: FileTokenizer.java,v 1.1 2005/07/13 07:27:49 kofron Exp $
3  *
4  * Copyright 2005
5  * Formal Methods In Software Engineering Group
6  * Institute of Computer Science
7  * Academy of Sciences of the Czech Republic
8  *
9  * This code was developed by Jan Kofron <kofron@nenya.ms.mff.cuni.cz>
10  */

11
12 package SOFA.SOFAnode.Util.DFSRChecker.parser;
13
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 /**
19  * This class implements the ProtocolReader and is
20  * able to read the input protocols from a file
21  */

22 public class FileTokenizer extends ProtocolReader {
23     
24     /**
25      * ctor.
26      *
27      * @param filename the name of a file containg the input protocols
28      * @throws IOException when the file cannot be opened
29      */

30     public FileTokenizer(String JavaDoc filename) throws IOException JavaDoc {
31         
32         try {
33             reader = new FileReader JavaDoc(filename);
34         } catch (FileNotFoundException JavaDoc e) {
35             throw new IOException JavaDoc();
36         }
37     }
38
39     /**
40      * Reads the next character
41      *
42      * @throws IOException whenever a problem with reading the input file occurs
43      * @return the character read
44      */

45     public int read() throws IOException JavaDoc {
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                 ;//read = (char)reader.read();
52

53             else if (read == '#') {
54                 String JavaDoc line = getLine();
55                 if (line.equals("eop"))
56                     return -1;
57                 else
58                     ;// a comment, read further
59
}
60             else // a real character
61
break;
62                 
63         }
64         
65         if (read != -1) {
66             ++index;
67             return read;
68         }
69         else
70             return -1;
71     }
72     
73     
74     /**
75      * Checks whether the source string is not yet parsed completelly
76      *
77      * @return true if there are character left, false otherwise
78      */

79     public boolean ready() throws IOException JavaDoc {
80         return reader.ready();
81     }
82
83
84
85     
86     /**
87      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.parser.ProtocolReader#getIndex()
88      */

89     public int getIndex() {
90         return index;
91     }
92
93     /**
94      * @see java.io.Reader#read(char[], int, int)
95      */

96     public int read(char[] arg0, int arg1, int arg2) throws IOException JavaDoc {
97         //not implemented
98
throw new IOException JavaDoc("Not implemented");
99     }
100
101     /**
102      * JDK 1.5.0: java.io.Closeable#close()
103      */

104     public void close() throws IOException JavaDoc {
105         reader.close();
106     }
107     
108     
109     /**
110      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.parser.ProtocolReader#resetIndex()
111      */

112     public void resetIndex() {
113         index = 0;
114     }
115
116     
117     private String JavaDoc getLine() throws IOException JavaDoc {
118         String JavaDoc line = new String JavaDoc();
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     /**
136      * File reader
137      */

138     private FileReader JavaDoc reader;
139     
140     /**
141      * Output index
142      */

143     private int index = 0;
144
145 }
146
Popular Tags