KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dwipal > DwSnmpMibParser


1 package com.dwipal;
2
3 import java.util.Vector JavaDoc;
4 import java.io.*;
5
6 public class DwSnmpMibParser
7 {
8     
9     String JavaDoc fileName;
10     DwSnmpMibRecord currentRec;
11     String JavaDoc prevToken,tokenVal="xx",tokenVal2="";
12     PipedReader pr;
13     PipedWriter pw;
14     BufferedReader r;
15     FileReader inFile;
16     public static int TT_EOL=StreamTokenizer.TT_EOL;
17     DwSnmpMibOutputHandler output;
18     DwMibParserInterface tokens;
19     
20     
21     DwSnmpMibParser(String JavaDoc fileName,DwMibParserInterface intf)
22     {
23         this.fileName = fileName;
24         this.tokens=intf;
25     }
26     
27     
28     int parseMibFile()
29     {
30         DwSnmpMibRecord mibRec;
31         
32         BufferedReader in=null;
33         StreamTokenizer st;
34         String JavaDoc tok1=" " ,tok2= " ";
35         
36         try {
37             inFile= new FileReader (new File(fileName));
38             r = new BufferedReader(inFile);
39             st = new StreamTokenizer(r);
40         }
41         catch (Exception JavaDoc e) {
42             outputError("File open error : Cannot open file.\n" + e.toString());
43             return -1;
44         }
45         
46         String JavaDoc line=" ";
47     
48         st.resetSyntax();
49         st.eolIsSignificant(true);
50         st.wordChars(33,126);
51         
52         //st.quoteChar('"');
53

54         //st.quoteChar('"');
55
//st.ordinaryChar(',');
56
//st.ordinaryChar(';');
57

58         String JavaDoc t1="a";
59         int parseStatus=0;
60         int parseStatusTemp=0;
61         
62         try {
63             int flag=0;
64             while (getNextToken(st).trim().length() > 0 || st.ttype == TT_EOL) {
65                 t1=getTokenVal(st);
66                 //if(t1.indexOf("AtmTrafficDescrParamIndex") != -1) flag++;
67
//if(flag>0) System.out.println(t1 + " " + parseStatus );
68
switch (parseStatus) {
69                 case 0: {
70                         currentRec=new DwSnmpMibRecord();
71                         if(t1.indexOf("IMPORT")!=-1) { // Skip till ;
72
parseStatus =100;
73                         }
74                         else
75                         if(t1.equals("MODULE-IDENTITY")==true) {
76                             currentRec.name = prevToken;
77                             parseStatus =1;
78                         }
79                         else
80                         if(t1.equals("OBJECT-TYPE")==true) {
81                             String JavaDoc temp=new String JavaDoc (prevToken.trim());
82                             temp=temp.substring(0,1);
83                             if (temp.toLowerCase().equals(temp)) {
84                                 parseStatus=1;
85                                 currentRec.name=prevToken;
86                             }
87                             //System.out.print("*"+prevToken);
88
}
89                         else if(t1.indexOf("OBJECT-GROUP")!=-1) { // Skip till ;
90
String JavaDoc temp=new String JavaDoc (prevToken.trim());
91                             temp=temp.substring(0,1);
92                             if (temp.toLowerCase().equals(temp)) {
93                                 parseStatus=1;
94                                 currentRec.name=prevToken;
95                             }
96                         }
97                         else if(t1.equals("OBJECT")==true) {
98                             currentRec.name=prevToken;
99                             parseStatus=2;
100                         }
101                         else if(t1.equals("::=")==true)
102                         {
103                             currentRec.init();
104                             currentRec.name=prevToken;
105                             parseStatus=9; // Its a variable
106
}
107                         continue;
108                     }
109                     
110                         
111                 case 1: { // GET " ::= " Token
112
if(t1.equals("::=") == true)
113                             parseStatus=3;
114                         else if(t1.equals("SYNTAX") == true)
115                             parseStatus=5;
116                         else if(t1.indexOf("ACCESS")!= -1 )
117                             parseStatus=6;
118                         else if(t1.equals("STATUS") == true)
119                             parseStatus=7;
120                         else if(t1.equals("DESCRIPTION") == true)
121                             parseStatus=8;
122                         else if(t1.equals("INDEX") == true)
123                             parseStatus=11;
124                         else if(t1.equals("OBJECTS") == true)
125                             parseStatus=14;
126                         continue;
127                     }
128                         
129                 case 2: { // GET "IDENTIFIER " else reset
130
if(t1.equals("IDENTIFIER")==true) {
131                             parseStatus = 1; // GET ::= next
132
}
133                         else {
134                             parseStatus=0;
135                         }
136                         continue;
137                     }
138                         
139                 case 3: { // get Group Name
140
if(t1.trim().startsWith("{")==true || t1.trim().length()==0) continue;
141                         currentRec.parent=t1;
142                         parseStatus =4; // next=GET Number.
143
continue;
144                     }
145                         
146                 case 4: { // Get sub-Group number
147
try {
148                             if(t1.trim().endsWith(")")==true) { // for chained server entries
149
String JavaDoc numStr="";
150                                     DwSnmpMibRecord newRec=new DwSnmpMibRecord();
151                                     numStr=t1.substring(t1.indexOf((int)'(')+1,t1.indexOf((int)')'));
152                                     //System.out.println("Adding T1: " + t1 + " Number Str : " + numStr);
153
try {
154                                     newRec.number =Integer.parseInt(numStr.trim());
155                                 }
156                                 catch(Exception JavaDoc ne2) {
157                                     outputError("Error in line " + st.lineno());
158                                     continue;
159                                 }
160                                 newRec.name=t1.substring(0,t1.indexOf("("));
161                                 newRec.parent =currentRec.parent;
162                                 currentRec.parent = newRec.name;
163                                 //System.out.println("Chained Rec. : " + newRec.name + " Parent : " + newRec.parent + "." + newRec.number );
164
addToken(newRec);
165                                 continue;
166                             }
167                             //System.out.println("Name : "+currentRec.name +"T1 : " + t1);
168
currentRec.number = Integer.parseInt(t1.trim());
169                             //System.out.println("Rec. Added : " + currentRec.name + " " + currentRec.parent + "." + currentRec.number );
170
addToken(currentRec);
171                             parseStatus=0;
172                             continue;
173                         }
174                         catch(NumberFormatException JavaDoc ne) {
175                             outputError("Error in getting number.."+t1+"\n" + ne.toString());
176                         }
177                     }
178                         //System.out.println("Record Added...." + currentRec.name );
179

180                 case 5: { // Get Syntax data till EOL
181
if(t1.indexOf((int)'{') != -1) {
182                             parseStatus =12;
183                             currentRec.syntax =currentRec.syntax.concat(" " +t1);
184                             continue;
185                         }
186                                 
187                         if (st.ttype==TT_EOL || st.ttype==st.TT_EOF) {
188                             currentRec.syntax =currentRec.syntax.concat(t1);
189                             if(parseStatusTemp==1 ) {
190                                 //System.out.println("Syntax : "+ currentRec.name + " , " + currentRec.syntax );
191
if(currentRec.syntax.indexOf('{') != -1){
192                                     parseStatus=12;
193                                     continue;
194                                 }
195                                 // See if it is a table. if so, set recordtype to 1
196
if(currentRec.syntax.trim().startsWith("SEQUENCE") == true) {
197                                     currentRec.recordType=1;
198                                     currentRec.tableEntry=1;
199                                 }
200                                 //addToken(currentRec);
201
parseStatus =1;
202                                 parseStatusTemp =0;
203                             }
204                             continue;
205                         }
206                         //System.out.println("Variable Found : " + currentRec.name+ " Type : " + currentRec.syntax);
207
currentRec.syntax = currentRec.syntax.concat(" " + t1);
208                         if(currentRec.syntax.trim().length()>0) parseStatusTemp=1;
209                         continue;
210                             
211                             
212                         /*
213                         if (st.ttype==TT_EOL) {
214                             parseStatus=1;
215                             continue;
216                         }
217                         currentRec.syntax = currentRec.syntax.concat(" " + t1);
218                         continue;
219 */

220                     }
221                 case 6: { // Get Access Mode Data till EOL
222
if (st.ttype==TT_EOL) {
223                             parseStatus=1;
224                             continue;
225                         }
226                         currentRec.access = currentRec.access.concat(" " + t1);
227                         continue;
228
229                     }
230                 case 7: { // Get Status data till EOL
231
if (st.ttype==TT_EOL) {
232                             parseStatus=1;
233                             continue;
234                         }
235                         currentRec.status = currentRec.status.concat(" " + t1);
236                         continue;
237
238                     }
239                         
240                 case 8: { // Get Description till EOL
241
if (st.ttype==st.TT_EOF) {
242                             break;
243                         }
244                         currentRec.description = currentRec.description.concat(" " + t1 );
245                         if(t1.trim().length()!=0) parseStatus=1;
246                         continue;
247                     }
248                 case 9: { // Record is a variable
249
currentRec.recordType = currentRec.recVariable;
250                         if(t1.indexOf((int)'{') != -1) {
251                             parseStatus =10;
252                             currentRec.syntax =currentRec.syntax.concat(" " + t1);
253                             continue;
254                         }
255                     
256                         if (st.ttype==TT_EOL || st.ttype==st.TT_EOF) {
257                             currentRec.syntax =currentRec.syntax.concat(t1);
258                             if(parseStatusTemp==1 ) {
259                                 //System.out.println("InVar : "+ currentRec.name + " , " + currentRec.syntax );
260
if(currentRec.syntax.indexOf('{') != -1){
261                                     parseStatus=10;
262                                     continue;
263                                 }
264                                 // See if it is a table. if so, set recordtype to 1
265
if(currentRec.syntax.trim().startsWith("SEQUENCE") == true) {
266                                     currentRec.recordType=1;
267                                 }
268                                 
269                                 //System.out.println("Var added : " + currentRec.name+ " SYN : " + currentRec.syntax );
270
addToken(currentRec);
271                                 parseStatus=0;
272                                 parseStatusTemp =0;
273                             }
274                             continue;
275                         }
276                         //System.out.println("Variable Found : " + currentRec.name+ " Type : " + currentRec.syntax);
277
currentRec.syntax = currentRec.syntax.concat(" " + t1);
278                         if(currentRec.syntax.trim().length()>0) parseStatusTemp=1;
279                         continue;
280                     }
281                 case 10: { // Variable Data in { }
282
// System.out.println(t1);
283
currentRec.syntax=currentRec.syntax.concat(t1);
284                          if(t1.indexOf((int)'}') != -1) {
285                              parseStatus =0;
286                              parseStatusTemp =0;
287                             // See if it is a table. if so, set recordtype to 1
288
if(currentRec.syntax.trim().startsWith("SEQUENCE")==true) {
289                                  currentRec.recordType=1;
290                              }
291                              addToken(currentRec);
292                              //System.out.println(currentRec.name +" " + currentRec.syntax );
293
continue;
294                          }
295                          continue;
296                     }
297                          
298                 case 11: { // INDEX (For tables)
299

300                         if(t1.trim().startsWith("{")==true) continue;
301                         if(t1.indexOf((int)'}') != -1) {
302                             parseStatus = 1;
303                             continue;
304                         }
305                         currentRec.index=currentRec.index.concat(t1);
306                         continue;
307                     }
308                          
309                          
310                 case 12: {
311                         currentRec.syntax=currentRec.syntax.concat(t1);
312                         if(t1.indexOf((int)'}') != -1) {
313                             parseStatus =1;
314                             parseStatusTemp =0;
315                             
316                             // See if it is a table. if so, set recordtype to 1
317
if(currentRec.syntax.trim().startsWith("SEQUENCE")==true) {
318                                  currentRec.recordType=1;
319                                  currentRec.tableEntry=1;
320                             }
321                         }
322                          //if(t1.indexOf((int)'}') != -1)
323
continue;
324                     }
325                 // case 13: Not used because unlucky :(
326
case 14 : { // OBJECT-GROUP OBJECTS...
327
currentRec.syntax=currentRec.syntax.concat(t1);
328                         if(t1.indexOf((int)'}') != -1) {
329                             parseStatus =1;
330                         }
331                          //if(t1.indexOf((int)'}') != -1)
332
continue;
333                     }
334                 case 100: {
335                         if(t1.indexOf(';')!= -1) parseStatus =0;
336                     }
337                 case 101: {
338                         if(t1.indexOf('}')!= -1) parseStatus =0;
339                     }
340                 
341                 }
342             }
343         }
344         catch (Exception JavaDoc e) {
345             System.out.println("Error in parsing.. \n" + e.toString());
346         }
347         return 0;
348     }
349     
350     
351     // returns the next non blank token
352
String JavaDoc getNextToken(StreamTokenizer st)
353     {
354         String JavaDoc tok="";
355         prevToken=getTokenVal(st);
356         
357         while(tok.equals("") == true )
358         {
359             try {
360                 if(tokenVal.equals("xx")!=true) return(tokenVal);
361                 if(tokenVal2.equals("")!=true) {
362                     setTokenVal(tokenVal2);
363                     tokenVal2="";
364                     return tokenVal;
365                 }
366                 if(st.nextToken()!= StreamTokenizer.TT_EOF) {
367                     if(st.ttype==TT_EOL) return getTokenVal(st);
368                     if(st.ttype==StreamTokenizer.TT_WORD ) {
369                         tok=st.sval;
370                         //System.out.println(tok);
371
// if { is combined with something, seperate them
372
if(tok.startsWith("{")==true) {
373                             if(tok.trim().length() !=1) {
374                                 setTokenVal("{");
375                                 tokenVal2=new String JavaDoc(tok.substring(1));
376                                 return ("{");
377                             }
378                         }
379                         if(tok.endsWith("}")==true) {
380                             if(tok.trim().length() !=1) {
381                                 setTokenVal(tok.replace('}',' '));
382                                 tokenVal2="}";
383                                 return tok.replace('}',' ');
384                             }
385                         }
386                         
387                         // Get "Quoted Text" as whole tokens :)
388
if(tok.startsWith("\"")==true) {
389                             //System.out.println("Comment.");
390
String JavaDoc strQuote =new String JavaDoc(tok);
391                             st.nextToken();
392                             tok=getTokenVal(st);
393                             while(tok!=null && tok.indexOf('"')==-1 ) {
394                                 String JavaDoc temp=getTokenVal(st);
395                                 if(temp.trim().length()>0) strQuote=strQuote.concat(" "+temp);
396                                 if(st.nextToken()==st.TT_EOF) return tok;
397                                 tok=getTokenVal(st);
398                             }
399                             strQuote=strQuote.concat(getTokenVal(st));
400                             //System.out.println (strQuote);
401
if(strQuote.trim().length() > 0) tokenVal =strQuote;
402                         }
403                             
404                         if(tok.equals("--")==true) {
405                             //System.out.print("-- ");
406
while(st.ttype != st.TT_EOL )
407                                 st.nextToken();
408                             //System.out.println("..." + getTokenVal(st));
409
break;
410                             //continue;
411
}
412                         //System.out.println("++ "+ tok+" ++");
413

414                         if(st.ttype == TT_EOL) return(" "); //st.ttype ;
415
else
416                             continue;
417                     }
418                     else if(st.ttype == StreamTokenizer.TT_NUMBER ) {
419                         tok=String.valueOf(st.nval );
420                         if(tok.trim().length()>0) {
421                             //System.out.println("No : "+ tok);
422
return tok;
423                         }
424                         else
425                             continue;
426                     }
427                     else tok="";
428                 }
429                 else return "";
430             }
431             catch (Exception JavaDoc e) {
432                 if(e.getMessage().startsWith("Write end dead") != true)
433                     System.out.println("Error in reading file..." + e.toString());
434                 //System.out.println("Errorin getting next token...\n" + e.toString());
435
return "";
436             }
437         }
438         return tok;
439     }
440     
441     void setTokenVal(String JavaDoc t) {
442         tokenVal=t;
443     }
444                      
445     String JavaDoc getTokenVal(StreamTokenizer st)
446     {
447         try {
448             if(tokenVal != "xx") {
449                 String JavaDoc temp=tokenVal.toString();
450                 tokenVal="xx";
451                 return temp;
452             }
453             if(st.ttype == TT_EOL) return String.valueOf('\n');
454             if(st.ttype==StreamTokenizer.TT_WORD )
455                 return(st.sval);
456             else if(st.ttype == StreamTokenizer.TT_NUMBER )
457                 return(String.valueOf ((int)st.nval) );
458             else return ("");
459         }
460         catch(Exception JavaDoc e) {
461             System.out.println("Error in retrieving token value..\n" + e.toString());
462             
463         }
464         return("");
465     }
466                       
467     
468     void setOutput(DwSnmpMibOutputHandler output) {
469         this.output=output;
470     }
471     
472     void outputText(String JavaDoc s) {
473         output.println(s);
474     }
475     void outputError(String JavaDoc s) {
476         output.printError(s);
477     }
478     
479     void addToken(DwSnmpMibRecord rec) {
480         tokens.newMibParseToken(rec);
481     }
482 /*
483     public static void main(String mainArgs[])
484     {
485         if(mainArgs.length ==0 ) return;
486         DwSnmpMibParser sp=new DwSnmpMibParser(mainArgs[0]);
487         sp.parseMibFile();
488     }
489     */

490 }
491
Popular Tags