001: package com.dwipal;
002:
003: import java.util.Vector;
004: import java.io.*;
005:
006: public class DwSnmpMibParser {
007:
008: String fileName;
009: DwSnmpMibRecord currentRec;
010: String prevToken, tokenVal = "xx", tokenVal2 = "";
011: PipedReader pr;
012: PipedWriter pw;
013: BufferedReader r;
014: FileReader inFile;
015: public static int TT_EOL = StreamTokenizer.TT_EOL;
016: DwSnmpMibOutputHandler output;
017: DwMibParserInterface tokens;
018:
019: DwSnmpMibParser(String fileName, DwMibParserInterface intf) {
020: this .fileName = fileName;
021: this .tokens = intf;
022: }
023:
024: int parseMibFile() {
025: DwSnmpMibRecord mibRec;
026:
027: BufferedReader in = null;
028: StreamTokenizer st;
029: String tok1 = " ", tok2 = " ";
030:
031: try {
032: inFile = new FileReader(new File(fileName));
033: r = new BufferedReader(inFile);
034: st = new StreamTokenizer(r);
035: } catch (Exception e) {
036: outputError("File open error : Cannot open file.\n"
037: + e.toString());
038: return -1;
039: }
040:
041: String line = " ";
042:
043: st.resetSyntax();
044: st.eolIsSignificant(true);
045: st.wordChars(33, 126);
046:
047: //st.quoteChar('"');
048:
049: //st.quoteChar('"');
050: //st.ordinaryChar(',');
051: //st.ordinaryChar(';');
052:
053: String t1 = "a";
054: int parseStatus = 0;
055: int parseStatusTemp = 0;
056:
057: try {
058: int flag = 0;
059: while (getNextToken(st).trim().length() > 0
060: || st.ttype == TT_EOL) {
061: t1 = getTokenVal(st);
062: //if(t1.indexOf("AtmTrafficDescrParamIndex") != -1) flag++;
063: //if(flag>0) System.out.println(t1 + " " + parseStatus );
064: switch (parseStatus) {
065: case 0: {
066: currentRec = new DwSnmpMibRecord();
067: if (t1.indexOf("IMPORT") != -1) { // Skip till ;
068: parseStatus = 100;
069: } else if (t1.equals("MODULE-IDENTITY") == true) {
070: currentRec.name = prevToken;
071: parseStatus = 1;
072: } else if (t1.equals("OBJECT-TYPE") == true) {
073: String temp = new String(prevToken.trim());
074: temp = temp.substring(0, 1);
075: if (temp.toLowerCase().equals(temp)) {
076: parseStatus = 1;
077: currentRec.name = prevToken;
078: }
079: //System.out.print("*"+prevToken);
080: } else if (t1.indexOf("OBJECT-GROUP") != -1) { // Skip till ;
081: String temp = new String(prevToken.trim());
082: temp = temp.substring(0, 1);
083: if (temp.toLowerCase().equals(temp)) {
084: parseStatus = 1;
085: currentRec.name = prevToken;
086: }
087: } else if (t1.equals("OBJECT") == true) {
088: currentRec.name = prevToken;
089: parseStatus = 2;
090: } else if (t1.equals("::=") == true) {
091: currentRec.init();
092: currentRec.name = prevToken;
093: parseStatus = 9; // Its a variable
094: }
095: continue;
096: }
097:
098: case 1: { // GET " ::= " Token
099: if (t1.equals("::=") == true)
100: parseStatus = 3;
101: else if (t1.equals("SYNTAX") == true)
102: parseStatus = 5;
103: else if (t1.indexOf("ACCESS") != -1)
104: parseStatus = 6;
105: else if (t1.equals("STATUS") == true)
106: parseStatus = 7;
107: else if (t1.equals("DESCRIPTION") == true)
108: parseStatus = 8;
109: else if (t1.equals("INDEX") == true)
110: parseStatus = 11;
111: else if (t1.equals("OBJECTS") == true)
112: parseStatus = 14;
113: continue;
114: }
115:
116: case 2: { // GET "IDENTIFIER " else reset
117: if (t1.equals("IDENTIFIER") == true) {
118: parseStatus = 1; // GET ::= next
119: } else {
120: parseStatus = 0;
121: }
122: continue;
123: }
124:
125: case 3: { // get Group Name
126: if (t1.trim().startsWith("{") == true
127: || t1.trim().length() == 0)
128: continue;
129: currentRec.parent = t1;
130: parseStatus = 4; // next=GET Number.
131: continue;
132: }
133:
134: case 4: { // Get sub-Group number
135: try {
136: if (t1.trim().endsWith(")") == true) { // for chained server entries
137: String numStr = "";
138: DwSnmpMibRecord newRec = new DwSnmpMibRecord();
139: numStr = t1.substring(
140: t1.indexOf((int) '(') + 1, t1
141: .indexOf((int) ')'));
142: //System.out.println("Adding T1: " + t1 + " Number Str : " + numStr);
143: try {
144: newRec.number = Integer.parseInt(numStr
145: .trim());
146: } catch (Exception ne2) {
147: outputError("Error in line "
148: + st.lineno());
149: continue;
150: }
151: newRec.name = t1.substring(0, t1
152: .indexOf("("));
153: newRec.parent = currentRec.parent;
154: currentRec.parent = newRec.name;
155: //System.out.println("Chained Rec. : " + newRec.name + " Parent : " + newRec.parent + "." + newRec.number );
156: addToken(newRec);
157: continue;
158: }
159: //System.out.println("Name : "+currentRec.name +"T1 : " + t1);
160: currentRec.number = Integer.parseInt(t1.trim());
161: //System.out.println("Rec. Added : " + currentRec.name + " " + currentRec.parent + "." + currentRec.number );
162: addToken(currentRec);
163: parseStatus = 0;
164: continue;
165: } catch (NumberFormatException ne) {
166: outputError("Error in getting number.." + t1
167: + "\n" + ne.toString());
168: }
169: }
170: //System.out.println("Record Added...." + currentRec.name );
171:
172: case 5: { // Get Syntax data till EOL
173: if (t1.indexOf((int) '{') != -1) {
174: parseStatus = 12;
175: currentRec.syntax = currentRec.syntax
176: .concat(" " + t1);
177: continue;
178: }
179:
180: if (st.ttype == TT_EOL || st.ttype == st.TT_EOF) {
181: currentRec.syntax = currentRec.syntax
182: .concat(t1);
183: if (parseStatusTemp == 1) {
184: //System.out.println("Syntax : "+ currentRec.name + " , " + currentRec.syntax );
185: if (currentRec.syntax.indexOf('{') != -1) {
186: parseStatus = 12;
187: continue;
188: }
189: // See if it is a table. if so, set recordtype to 1
190: if (currentRec.syntax.trim().startsWith(
191: "SEQUENCE") == true) {
192: currentRec.recordType = 1;
193: currentRec.tableEntry = 1;
194: }
195: //addToken(currentRec);
196: parseStatus = 1;
197: parseStatusTemp = 0;
198: }
199: continue;
200: }
201: //System.out.println("Variable Found : " + currentRec.name+ " Type : " + currentRec.syntax);
202: currentRec.syntax = currentRec.syntax.concat(" "
203: + t1);
204: if (currentRec.syntax.trim().length() > 0)
205: parseStatusTemp = 1;
206: continue;
207:
208: /*
209: if (st.ttype==TT_EOL) {
210: parseStatus=1;
211: continue;
212: }
213: currentRec.syntax = currentRec.syntax.concat(" " + t1);
214: continue;
215: */
216: }
217: case 6: { // Get Access Mode Data till EOL
218: if (st.ttype == TT_EOL) {
219: parseStatus = 1;
220: continue;
221: }
222: currentRec.access = currentRec.access.concat(" "
223: + t1);
224: continue;
225:
226: }
227: case 7: { // Get Status data till EOL
228: if (st.ttype == TT_EOL) {
229: parseStatus = 1;
230: continue;
231: }
232: currentRec.status = currentRec.status.concat(" "
233: + t1);
234: continue;
235:
236: }
237:
238: case 8: { // Get Description till EOL
239: if (st.ttype == st.TT_EOF) {
240: break;
241: }
242: currentRec.description = currentRec.description
243: .concat(" " + t1);
244: if (t1.trim().length() != 0)
245: 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
253: .concat(" " + t1);
254: continue;
255: }
256:
257: if (st.ttype == TT_EOL || st.ttype == st.TT_EOF) {
258: currentRec.syntax = currentRec.syntax
259: .concat(t1);
260: if (parseStatusTemp == 1) {
261: //System.out.println("InVar : "+ currentRec.name + " , " + currentRec.syntax );
262: if (currentRec.syntax.indexOf('{') != -1) {
263: parseStatus = 10;
264: continue;
265: }
266: // See if it is a table. if so, set recordtype to 1
267: if (currentRec.syntax.trim().startsWith(
268: "SEQUENCE") == true) {
269: currentRec.recordType = 1;
270: }
271:
272: //System.out.println("Var added : " + currentRec.name+ " SYN : " + currentRec.syntax );
273: addToken(currentRec);
274: parseStatus = 0;
275: parseStatusTemp = 0;
276: }
277: continue;
278: }
279: //System.out.println("Variable Found : " + currentRec.name+ " Type : " + currentRec.syntax);
280: currentRec.syntax = currentRec.syntax.concat(" "
281: + t1);
282: if (currentRec.syntax.trim().length() > 0)
283: parseStatusTemp = 1;
284: continue;
285: }
286: case 10: { // Variable Data in { }
287: // System.out.println(t1);
288: currentRec.syntax = currentRec.syntax.concat(t1);
289: if (t1.indexOf((int) '}') != -1) {
290: parseStatus = 0;
291: parseStatusTemp = 0;
292: // See if it is a table. if so, set recordtype to 1
293: if (currentRec.syntax.trim().startsWith(
294: "SEQUENCE") == true) {
295: currentRec.recordType = 1;
296: }
297: addToken(currentRec);
298: //System.out.println(currentRec.name +" " + currentRec.syntax );
299: continue;
300: }
301: continue;
302: }
303:
304: case 11: { // INDEX (For tables)
305:
306: if (t1.trim().startsWith("{") == true)
307: continue;
308: if (t1.indexOf((int) '}') != -1) {
309: parseStatus = 1;
310: continue;
311: }
312: currentRec.index = currentRec.index.concat(t1);
313: continue;
314: }
315:
316: case 12: {
317: currentRec.syntax = currentRec.syntax.concat(t1);
318: if (t1.indexOf((int) '}') != -1) {
319: parseStatus = 1;
320: parseStatusTemp = 0;
321:
322: // See if it is a table. if so, set recordtype to 1
323: if (currentRec.syntax.trim().startsWith(
324: "SEQUENCE") == true) {
325: currentRec.recordType = 1;
326: currentRec.tableEntry = 1;
327: }
328: }
329: //if(t1.indexOf((int)'}') != -1)
330: continue;
331: }
332: // case 13: Not used because unlucky :(
333: case 14: { // OBJECT-GROUP OBJECTS...
334: currentRec.syntax = currentRec.syntax.concat(t1);
335: if (t1.indexOf((int) '}') != -1) {
336: parseStatus = 1;
337: }
338: //if(t1.indexOf((int)'}') != -1)
339: continue;
340: }
341: case 100: {
342: if (t1.indexOf(';') != -1)
343: parseStatus = 0;
344: }
345: case 101: {
346: if (t1.indexOf('}') != -1)
347: parseStatus = 0;
348: }
349:
350: }
351: }
352: } catch (Exception e) {
353: System.out.println("Error in parsing.. \n" + e.toString());
354: }
355: return 0;
356: }
357:
358: // returns the next non blank token
359: String getNextToken(StreamTokenizer st) {
360: String tok = "";
361: prevToken = getTokenVal(st);
362:
363: while (tok.equals("") == true) {
364: try {
365: if (tokenVal.equals("xx") != true)
366: return (tokenVal);
367: if (tokenVal2.equals("") != true) {
368: setTokenVal(tokenVal2);
369: tokenVal2 = "";
370: return tokenVal;
371: }
372: if (st.nextToken() != StreamTokenizer.TT_EOF) {
373: if (st.ttype == TT_EOL)
374: return getTokenVal(st);
375: if (st.ttype == StreamTokenizer.TT_WORD) {
376: tok = st.sval;
377: //System.out.println(tok);
378: // if { is combined with something, seperate them
379: if (tok.startsWith("{") == true) {
380: if (tok.trim().length() != 1) {
381: setTokenVal("{");
382: tokenVal2 = new String(tok.substring(1));
383: return ("{");
384: }
385: }
386: if (tok.endsWith("}") == true) {
387: if (tok.trim().length() != 1) {
388: setTokenVal(tok.replace('}', ' '));
389: tokenVal2 = "}";
390: return tok.replace('}', ' ');
391: }
392: }
393:
394: // Get "Quoted Text" as whole tokens :)
395: if (tok.startsWith("\"") == true) {
396: //System.out.println("Comment.");
397: String strQuote = new String(tok);
398: st.nextToken();
399: tok = getTokenVal(st);
400: while (tok != null
401: && tok.indexOf('"') == -1) {
402: String temp = getTokenVal(st);
403: if (temp.trim().length() > 0)
404: strQuote = strQuote.concat(" "
405: + temp);
406: if (st.nextToken() == st.TT_EOF)
407: return tok;
408: tok = getTokenVal(st);
409: }
410: strQuote = strQuote.concat(getTokenVal(st));
411: //System.out.println (strQuote);
412: if (strQuote.trim().length() > 0)
413: tokenVal = strQuote;
414: }
415:
416: if (tok.equals("--") == true) {
417: //System.out.print("-- ");
418: while (st.ttype != st.TT_EOL)
419: st.nextToken();
420: //System.out.println("..." + getTokenVal(st));
421: break;
422: //continue;
423: }
424: //System.out.println("++ "+ tok+" ++");
425:
426: if (st.ttype == TT_EOL)
427: return (" "); //st.ttype ;
428: else
429: continue;
430: } else if (st.ttype == StreamTokenizer.TT_NUMBER) {
431: tok = String.valueOf(st.nval);
432: if (tok.trim().length() > 0) {
433: //System.out.println("No : "+ tok);
434: return tok;
435: } else
436: continue;
437: } else
438: tok = "";
439: } else
440: return "";
441: } catch (Exception e) {
442: if (e.getMessage().startsWith("Write end dead") != true)
443: System.out.println("Error in reading file..."
444: + e.toString());
445: //System.out.println("Errorin getting next token...\n" + e.toString());
446: return "";
447: }
448: }
449: return tok;
450: }
451:
452: void setTokenVal(String t) {
453: tokenVal = t;
454: }
455:
456: String getTokenVal(StreamTokenizer st) {
457: try {
458: if (tokenVal != "xx") {
459: String temp = tokenVal.toString();
460: tokenVal = "xx";
461: return temp;
462: }
463: if (st.ttype == TT_EOL)
464: return String.valueOf('\n');
465: if (st.ttype == StreamTokenizer.TT_WORD)
466: return (st.sval);
467: else if (st.ttype == StreamTokenizer.TT_NUMBER)
468: return (String.valueOf((int) st.nval));
469: else
470: return ("");
471: } catch (Exception e) {
472: System.out.println("Error in retrieving token value..\n"
473: + e.toString());
474:
475: }
476: return ("");
477: }
478:
479: void setOutput(DwSnmpMibOutputHandler output) {
480: this .output = output;
481: }
482:
483: void outputText(String s) {
484: output.println(s);
485: }
486:
487: void outputError(String s) {
488: output.printError(s);
489: }
490:
491: void addToken(DwSnmpMibRecord rec) {
492: tokens.newMibParseToken(rec);
493: }
494: /*
495: public static void main(String mainArgs[])
496: {
497: if(mainArgs.length ==0 ) return;
498: DwSnmpMibParser sp=new DwSnmpMibParser(mainArgs[0]);
499: sp.parseMibFile();
500: }
501: */
502: }
|