1 package com.dwipal; 2 3 import java.util.Vector ; 4 import java.io.*; 5 6 public class DwSnmpMibParser 7 { 8 9 String fileName; 10 DwSnmpMibRecord currentRec; 11 String 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 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 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 e) { 42 outputError("File open error : Cannot open file.\n" + e.toString()); 43 return -1; 44 } 45 46 String line=" "; 47 48 st.resetSyntax(); 49 st.eolIsSignificant(true); 50 st.wordChars(33,126); 51 52 54 58 String 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 switch (parseStatus) { 69 case 0: { 70 currentRec=new DwSnmpMibRecord(); 71 if(t1.indexOf("IMPORT")!=-1) { 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 temp=new String (prevToken.trim()); 82 temp=temp.substring(0,1); 83 if (temp.toLowerCase().equals(temp)) { 84 parseStatus=1; 85 currentRec.name=prevToken; 86 } 87 } 89 else if(t1.indexOf("OBJECT-GROUP")!=-1) { String temp=new String (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; } 107 continue; 108 } 109 110 111 case 1: { 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: { if(t1.equals("IDENTIFIER")==true) { 131 parseStatus = 1; } 133 else { 134 parseStatus=0; 135 } 136 continue; 137 } 138 139 case 3: { if(t1.trim().startsWith("{")==true || t1.trim().length()==0) continue; 141 currentRec.parent=t1; 142 parseStatus =4; continue; 144 } 145 146 case 4: { try { 148 if(t1.trim().endsWith(")")==true) { String numStr=""; 150 DwSnmpMibRecord newRec=new DwSnmpMibRecord(); 151 numStr=t1.substring(t1.indexOf((int)'(')+1,t1.indexOf((int)')')); 152 try { 154 newRec.number =Integer.parseInt(numStr.trim()); 155 } 156 catch(Exception 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 addToken(newRec); 165 continue; 166 } 167 currentRec.number = Integer.parseInt(t1.trim()); 169 addToken(currentRec); 171 parseStatus=0; 172 continue; 173 } 174 catch(NumberFormatException ne) { 175 outputError("Error in getting number.."+t1+"\n" + ne.toString()); 176 } 177 } 178 180 case 5: { 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 if(currentRec.syntax.indexOf('{') != -1){ 192 parseStatus=12; 193 continue; 194 } 195 if(currentRec.syntax.trim().startsWith("SEQUENCE") == true) { 197 currentRec.recordType=1; 198 currentRec.tableEntry=1; 199 } 200 parseStatus =1; 202 parseStatusTemp =0; 203 } 204 continue; 205 } 206 currentRec.syntax = currentRec.syntax.concat(" " + t1); 208 if(currentRec.syntax.trim().length()>0) parseStatusTemp=1; 209 continue; 210 211 212 220 } 221 case 6: { 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: { 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: { 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: { 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 if(currentRec.syntax.indexOf('{') != -1){ 261 parseStatus=10; 262 continue; 263 } 264 if(currentRec.syntax.trim().startsWith("SEQUENCE") == true) { 266 currentRec.recordType=1; 267 } 268 269 addToken(currentRec); 271 parseStatus=0; 272 parseStatusTemp =0; 273 } 274 continue; 275 } 276 currentRec.syntax = currentRec.syntax.concat(" " + t1); 278 if(currentRec.syntax.trim().length()>0) parseStatusTemp=1; 279 continue; 280 } 281 case 10: { currentRec.syntax=currentRec.syntax.concat(t1); 284 if(t1.indexOf((int)'}') != -1) { 285 parseStatus =0; 286 parseStatusTemp =0; 287 if(currentRec.syntax.trim().startsWith("SEQUENCE")==true) { 289 currentRec.recordType=1; 290 } 291 addToken(currentRec); 292 continue; 294 } 295 continue; 296 } 297 298 case 11: { 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 if(currentRec.syntax.trim().startsWith("SEQUENCE")==true) { 318 currentRec.recordType=1; 319 currentRec.tableEntry=1; 320 } 321 } 322 continue; 324 } 325 case 14 : { currentRec.syntax=currentRec.syntax.concat(t1); 328 if(t1.indexOf((int)'}') != -1) { 329 parseStatus =1; 330 } 331 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 e) { 345 System.out.println("Error in parsing.. \n" + e.toString()); 346 } 347 return 0; 348 } 349 350 351 String getNextToken(StreamTokenizer st) 353 { 354 String 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 if(tok.startsWith("{")==true) { 373 if(tok.trim().length() !=1) { 374 setTokenVal("{"); 375 tokenVal2=new String (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 if(tok.startsWith("\"")==true) { 389 String strQuote =new String (tok); 391 st.nextToken(); 392 tok=getTokenVal(st); 393 while(tok!=null && tok.indexOf('"')==-1 ) { 394 String 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 if(strQuote.trim().length() > 0) tokenVal =strQuote; 402 } 403 404 if(tok.equals("--")==true) { 405 while(st.ttype != st.TT_EOL ) 407 st.nextToken(); 408 break; 410 } 412 414 if(st.ttype == TT_EOL) return(" "); 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 return tok; 423 } 424 else 425 continue; 426 } 427 else tok=""; 428 } 429 else return ""; 430 } 431 catch (Exception e) { 432 if(e.getMessage().startsWith("Write end dead") != true) 433 System.out.println("Error in reading file..." + e.toString()); 434 return ""; 436 } 437 } 438 return tok; 439 } 440 441 void setTokenVal(String t) { 442 tokenVal=t; 443 } 444 445 String getTokenVal(StreamTokenizer st) 446 { 447 try { 448 if(tokenVal != "xx") { 449 String 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 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 s) { 473 output.println(s); 474 } 475 void outputError(String s) { 476 output.printError(s); 477 } 478 479 void addToken(DwSnmpMibRecord rec) { 480 tokens.newMibParseToken(rec); 481 } 482 490 } 491 | Popular Tags |