KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piaget > analyze > Parser


1 /*
2  * FileParser.java
3  *
4  * Created on April 12, 2005, 8:05 PM
5  */

6
7 package org.netbeans.modules.piaget.analyze;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileReader JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.Reader JavaDoc;
15
16
17
18 /**
19  *
20  * @author loicsegapelli
21  *
22  * Opens a file and gets content as tokens.
23  */

24 public abstract class Parser {
25     
26     protected Scanner scanner;
27     
28     protected static final int EOF = Scanner.EOF;
29     protected static final int LONG = Scanner.LONG;
30     protected static final int WORD = Scanner.WORD;
31     
32     protected int ttype;
33     protected long timestamp,startTime;
34     
35     protected String JavaDoc filename;
36     
37     
38     
39     /** Creates a new instance of FileParser */
40     public Parser(File JavaDoc f) {
41         init();
42         filename = f.getName();
43         timestamp = 0;
44         openFile(f);
45     }
46     
47     private void openFile(File JavaDoc f){
48         try{
49             FileReader JavaDoc reader = new FileReader JavaDoc(f);
50             scanner = new Scanner(reader);
51             firstTimeStamp();
52             while(ttype!=EOF){
53                 try{
54                     analyzeTokenInput();
55                 } catch(SyntaxException se){
56                     String JavaDoc msg = se.getMessage();
57                     // if msg.length == 0 it means the error was thrown by
58
// method "recover"
59
if(msg.length()>0) recover(msg);
60                 }
61             }
62             eof();
63         } catch(java.io.FileNotFoundException JavaDoc e){
64             System.out.println("file not found:"+filename);
65             e.printStackTrace(System.out);
66         } catch(Exception JavaDoc e){
67             e.printStackTrace(System.out);
68         }
69     }
70     
71     private void firstTimeStamp(){
72         try{
73             nextLONG();
74             startTime = scanner.lval;
75         }catch(SyntaxException e){
76             if(ttype != EOF){
77                 System.out.println(filename);
78                 System.out.println("first ts not found");
79                 startTime = -1;
80             }
81         }
82         scanner.pushBack();
83     }
84     
85     private String JavaDoc ttype(int ttype){
86         switch(ttype){
87             case EOF: return "EOF";
88             case LONG: return "LONG "+Long.toString(scanner.lval);
89             case WORD: return "WORD"+scanner.sval;
90             default: return "UNKNOWN!";
91         }
92     }
93     
94     protected void nextWORD() throws SyntaxException{
95         next();
96         if(ttype!=WORD) throw(new SyntaxException("WORD expected, found: "+ttype(ttype)));
97     }
98     
99     protected void nextLONG() throws SyntaxException{
100         next();
101         if(ttype!=LONG) throw(new SyntaxException("LONG expected, found: "+ttype(ttype)));
102     }
103     
104     protected void next() throws SyntaxException{
105         try{
106             ttype = scanner.nextToken();
107             while(ttype!=WORD && ttype!=LONG && ttype!=EOF) ttype = scanner.nextToken();
108         }catch(Exception JavaDoc e){
109             e.printStackTrace();
110         }
111         if(ttype==EOF) throw(new SyntaxException(""));
112     }
113     
114     private void recover(String JavaDoc msg){
115         try{
116             if(msg.length()>0)error(msg);
117             while(ttype!=LONG && ttype!=EOF) next();
118             scanner.pushBack();
119         }catch(SyntaxException e){
120             String JavaDoc myMSG = e.getMessage();
121             // this should not happen
122
if(myMSG.length()!=0) System.out.println("mystake, myMSG:"+myMSG);;
123         }
124     }
125     
126     protected void error(String JavaDoc msg){
127         System.out.println(filename+":"+scanner.lineno()+":"+msg);
128     }
129     
130     public static String JavaDoc prettyTime(long l){
131         int i = new Long JavaDoc(l/1000).intValue();
132         int h = i/3600;
133         String JavaDoc hs = new Integer JavaDoc(h).toString();
134         if(h<10) hs = "0"+hs;
135         int m = (i%3600)/60;
136         String JavaDoc ms = new Integer JavaDoc(m).toString();
137         if(m<10) ms = "0"+ms;
138         int s = i%60;
139         String JavaDoc ss = new Integer JavaDoc(s).toString();
140         if(s<10) ss = "0"+ss;
141         return hs+":"+ms+":"+ss;
142     }
143     
144     protected boolean svalIs(String JavaDoc s){
145         return scanner.sval.equalsIgnoreCase(s);
146     }
147     
148     abstract protected void eof();
149     
150     abstract protected void init();
151     
152     abstract protected void analyzeTokenInput() throws SyntaxException;
153     
154 }
155
Popular Tags