001: package com.dwipal;
002:
003: import javax.swing.tree.DefaultMutableTreeNode;
004: import javax.swing.JTree;
005: import javax.swing.*;
006: import javax.swing.tree.DefaultTreeModel;
007: import javax.swing.tree.TreeNode;
008: import javax.swing.event.TreeSelectionListener;
009: import javax.swing.event.TreeSelectionEvent;
010: import javax.swing.tree.TreeSelectionModel;
011: import java.util.Vector;
012: import java.util.Hashtable;
013: import java.util.Enumeration;
014: import java.io.File;
015:
016: public class DwSnmpMibTreeBuilder implements DwMibParserInterface,
017: Runnable {
018: DwSnmpMibOutputHandler output = null;
019: private DefaultMutableTreeNode rootNode;
020: private DefaultMutableTreeNode treeRootNode;
021: private DefaultMutableTreeNode rootOrphan;
022: private DefaultMutableTreeNode rootVariable;
023: private DefaultMutableTreeNode rootVariableTable;
024:
025: JTree tree;
026:
027: private Vector fileVect;
028: private Vector orphanNodes;
029:
030: private String errorMsg = "";
031:
032: public DwSnmpOidSupport oidSupport = new DwSnmpOidSupport();
033: DwSnmpMibTreeHash treeHash;
034: DwSnmpMibTreeHash variableHash;
035: DwSnmpMibTreeHash orphanHash;
036:
037: public DwSnmpMibTreeBuilder() {
038: DwSnmpMibRecord treeRootRec = new DwSnmpMibRecord();
039: treeRootRec.name = "MIB Browser";
040: treeRootRec.parent = "MIB Browser";
041: treeRootRec.number = 0;
042: treeRootNode = new DefaultMutableTreeNode(treeRootRec);
043:
044: DwSnmpMibRecord rootRec = new DwSnmpMibRecord();
045: rootRec.name = "root";
046: rootRec.parent = "MIB Browser";
047: rootRec.number = 1;
048: rootNode = new DefaultMutableTreeNode(rootRec);
049:
050: DwSnmpMibRecord rootOrphanRec = new DwSnmpMibRecord();
051: rootOrphanRec.name = "Orphans";
052: rootOrphanRec.parent = "MIB Browser";
053: rootOrphanRec.description = "This subtree contains MIB Records whose parent cannot be traced";
054: rootOrphanRec.number = 10;
055: rootOrphan = new DefaultMutableTreeNode(rootOrphanRec);
056:
057: DwSnmpMibRecord rootVariableRec = new DwSnmpMibRecord();
058: rootVariableRec.name = "Variables/Textual Conventions";
059: rootVariableRec.parent = "MIB Browser";
060: rootVariableRec.description = "This subtree contains all the variables which map to the standard variables.";
061: rootVariableRec.number = 11;
062: rootVariable = new DefaultMutableTreeNode(rootVariableRec);
063:
064: DwSnmpMibRecord rootVariableTableRec = new DwSnmpMibRecord();
065: rootVariableTableRec.name = "Table Entries";
066: rootVariableTableRec.parent = "Variables/Textual Conventions";
067: rootVariableTableRec.description = "This branch contains a list of sequences for all the tables ";
068: rootVariableTableRec.number = 12;
069: rootVariableTable = new DefaultMutableTreeNode(
070: rootVariableTableRec);
071:
072: treeHash = new DwSnmpMibTreeHash();
073: treeHash.put(rootRec.name, rootNode);
074:
075: variableHash = new DwSnmpMibTreeHash();
076: orphanHash = new DwSnmpMibTreeHash();
077:
078: orphanNodes = new Vector();
079: fileVect = new Vector();
080: clearError();
081: }
082:
083: public DefaultMutableTreeNode getRootNode() {
084: return rootNode;
085: }
086:
087: public boolean addFile(String fName) {
088: if (fName == null)
089: return false;
090: File mibFile = new File(fName);
091: if (mibFile.exists() != true)
092: return false;
093: fileVect.add(fName);
094: return true;
095: }
096:
097: public boolean addDirectory(String dirName) {
098: System.out.println("Adding directory : " + dirName);
099: File dir = new File(dirName);
100: if (dir.isDirectory() != true)
101: return false;
102: File files[] = dir.listFiles();
103: if (files == null)
104: return false;
105: for (int i = 0; i < files.length; i++) {
106: fileVect.add(files[i].getPath());
107: }
108: return true;
109: }
110:
111: public String[] getFiles() {
112:
113: try {
114: Enumeration enu = fileVect.elements();
115: String returnStr[] = new String[fileVect.size()];
116:
117: int i = 0;
118: while (enu.hasMoreElements()) {
119: returnStr[i++] = (String) enu.nextElement();
120: }
121: clearError();
122: return returnStr;
123: } catch (Exception e) {
124: setError("Error in getting filenames..\n" + e.toString());
125: return null;
126: }
127: }
128:
129: private void clearError() {
130: errorMsg = "";
131: }
132:
133: private void setError(String err) {
134: errorMsg = err;
135: }
136:
137: public JTree buildTree() {
138: // Check if files have been added to list
139: if (fileVect.size() == 0) {
140: setError("Error : Please add files first");
141: return null;
142: }
143:
144: oidSupport = new DwSnmpOidSupport();
145: Thread treeThread = new Thread(this );
146: treeThread.setPriority(Thread.MAX_PRIORITY - 1);
147: treeThread.start();
148:
149: treeRootNode.add(rootNode);
150: treeRootNode.add(rootOrphan);
151: rootVariable.add(rootVariableTable);
152: treeRootNode.add(rootVariable);
153: tree = new JTree(treeRootNode);
154: tree.putClientProperty("JTree.lineStyle", "Angled");
155: tree.getSelectionModel().setSelectionMode(
156: TreeSelectionModel.SINGLE_TREE_SELECTION);
157: return (tree);
158: }
159:
160: public void run() {
161: // Get filenames and add nodes to rootnode
162: Enumeration enu = fileVect.elements();
163: String fileName = "";
164: JTree newTree = null;
165: while (enu.hasMoreElements()) {
166: fileName = (String) enu.nextElement();
167: loadFile(fileName);
168: }
169: updateOrphans();
170: output.println("*****COMPLETED******");
171: }
172:
173: private void loadFile(String fileName) {
174: output.print("Adding file " + fileName);
175: if (parseFile(fileName) < 0)
176: outputError(".. Error");
177: else
178: output.print("..Done\n");
179: }
180:
181: public boolean loadNewFile(String fName) {
182: if (fName == null)
183: return false;
184: File mibFile = new File(fName);
185: if (mibFile.exists() != true)
186: return false;
187: if (fileVect.indexOf(fName) == -1) {
188: tree.collapsePath(tree.getSelectionPath());
189: fileVect.add(fName);
190: loadFile(fName);
191: updateOrphans();
192: return true;
193: }
194: return false;
195: }
196:
197: private void updateOrphans() {
198: // Check if orphan's parents have arrived. if yes, remove them from orphan list
199: //outputText("Scanning orphans..");
200: outputText("Updating orphans.");
201: DwSnmpMibRecord orphanRec = null;
202: Enumeration orphanEnu;
203: boolean contFlag = true;
204:
205: while (contFlag == true) {
206: contFlag = false;
207: orphanEnu = orphanNodes.elements();
208: while (orphanEnu.hasMoreElements()) {
209: DefaultMutableTreeNode orphanNode = (DefaultMutableTreeNode) orphanEnu
210: .nextElement();
211:
212: if (addNode(orphanNode) == true) {
213: contFlag = true;
214: orphanNodes.remove(orphanNode);
215: continue;
216: }
217: // If orphan rec. type=1, then it is a table variable.
218: /*
219: orphanRec = (DwSnmpMibRecord) orphanNode.getUserObject();
220: if(orphanRec.recordType==orphanRec.recTable) {
221: rootVariableTable.add(orphanNode);
222: orphanNodes.remove(orphanNode);
223: }
224: */
225: }
226: output.print(".");
227: }
228: output.print("Done");
229: output.print("\nBuilding OID Name resolution table...");
230: oidSupport.buildOidToNameResolutionTable(rootNode);
231:
232: //Add remaining orphans to treeroot.orphans
233: //System.out.print("Updating orphan table....");
234: orphanEnu = orphanNodes.elements();
235: while (orphanEnu.hasMoreElements()) {
236: DefaultMutableTreeNode orphanNode = (DefaultMutableTreeNode) orphanEnu
237: .nextElement();
238: orphanRec = (DwSnmpMibRecord) orphanNode.getUserObject();
239: if (orphanRec.recordType == orphanRec.recVariable)
240: continue;
241: if (orphanRec.recordType == orphanRec.recTable) {
242: rootVariable.add(orphanNode);
243: continue;
244: }
245: if (treeHash.containsKey(orphanRec.name) != true)
246: rootOrphan.add(orphanNode);
247: }
248:
249: //System.out.print("Completed!\n");
250: // Add variables to varroot
251: outputText("Updating variables table..");
252: Enumeration enuVar = variableHash.elements();
253: DwSnmpMibRecord varRec;
254: while (enuVar.hasMoreElements()) {
255: varRec = (DwSnmpMibRecord) enuVar.nextElement();
256: rootVariable.add(new DefaultMutableTreeNode(varRec));
257: }
258:
259: if (tree != null && tree.getModel() != null) {
260: ((DefaultTreeModel) tree.getModel()).reload();
261: tree.revalidate();
262: tree.repaint();
263: }
264: outputText("Done");
265: }
266:
267: private int parseFile(String fName) {
268:
269: DwSnmpMibParser fParser = new DwSnmpMibParser(fName, this );
270: if (output != null)
271: fParser.setOutput(output);
272: return fParser.parseMibFile();
273: }
274:
275: private boolean addRecord(DwSnmpMibRecord childRec) {
276: int parseStatus = 0;
277: if (childRec == null)
278: return false;
279: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
280: childRec);
281: if (addNode(newNode) == false) {
282: // if(orphanHash.containsKey(childRec.name)==false) {
283: orphanNodes.add(newNode);
284: orphanHash.put(childRec.name, newNode);
285: // }
286: return false;
287: }
288: return true;
289:
290: /*
291: // See if parent exists. if no parent, add it to orphans
292: if (treeHash.containsKey(childRec.parent) == false) {
293: // outputText("Orphan : " + childRec.name + " Parent : " + childRec.parent );
294: DefaultMutableTreeNode orphanNode=new DefaultMutableTreeNode(childRec,true);
295: treeHash.put(childRec.name,orphanNode);
296: orphanNodes.add(orphanNode);
297: return false;
298: }
299: // Get the parent node (current node will be added to it)
300: DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode) treeHash.get(childRec.parent);
301:
302: // Check if parent node contains a child of same name as new node
303: // If child exists, return true
304: if(isChildPresent(childRec)!=null) return true;
305:
306: // Check if parent is a Table, and set the node tableEntry accordingly
307: DwSnmpMibRecord parentRec=(DwSnmpMibRecord)parentNode.getUserObject();
308: if(parentRec.recordType > 0) childRec.recordType =parentRec.recordType+1;
309: //outputText("Added Child : " + childRec.name + " Parent : " + childRec.parent );
310: DefaultMutableTreeNode childNode=new DefaultMutableTreeNode (childRec,true);
311: // Add node to its parent
312: parentNode.add(childNode);
313: childNode.setParent(parentNode);
314: treeHash.put(childRec.name,childNode);
315: return true;
316: */
317: }
318:
319: private boolean addNode(DefaultMutableTreeNode newNode) {
320: DwSnmpMibRecord newRec = (DwSnmpMibRecord) newNode
321: .getUserObject();
322:
323: DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) treeHash
324: .get(newRec.parent);
325: //if(parentNode==null) { // See if parent is an orphan
326: // parentNode = (DefaultMutableTreeNode)orphanHash.get(newRec.parent);
327: //}
328: if (parentNode == null)
329: return false;
330:
331: // Check if parent is a Table, and set the node tableEntry accordingly
332: DwSnmpMibRecord parentRec = (DwSnmpMibRecord) parentNode
333: .getUserObject();
334: if (parentRec.recordType > 0)
335: newRec.recordType = parentRec.recordType + 1;
336:
337: DefaultMutableTreeNode dupNode = isChildPresent(newNode);
338: if (dupNode == null) { // Add node to its parent
339: try {
340: parentNode.add(newNode);
341: newNode.setParent(parentNode);
342: // See if parent is not an orphan
343: treeHash.put(newRec.name, newNode);
344: return true;
345: } catch (Exception e) {
346: System.out.println("Err in Child : " + newRec.name
347: + "Parent : " + newRec.parent);
348: return false;
349: }
350: } else { // Node already present. add all its children to the existing node
351: Enumeration dupChildren = newNode.children();
352: while (dupChildren.hasMoreElements()) {
353: DefaultMutableTreeNode dupChildNode = (DefaultMutableTreeNode) dupChildren
354: .nextElement();
355: if (isChildPresent(dupChildNode) == null)
356: dupNode.add(dupChildNode);
357: }
358: return true;
359: }
360: }
361:
362: DefaultMutableTreeNode isChildPresent(
363: DefaultMutableTreeNode childNode) {
364: DwSnmpMibRecord childRec = (DwSnmpMibRecord) childNode
365: .getUserObject();
366: return (isChildPresent(childRec));
367: }
368:
369: DefaultMutableTreeNode isChildPresent(DwSnmpMibRecord rec) {
370: DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) treeHash
371: .get(rec.parent);
372: if (parentNode == null)
373: parentNode = (DefaultMutableTreeNode) orphanHash
374: .get(rec.parent);
375: if (parentNode == null)
376: return null;
377: Enumeration enuChildren = parentNode.children();
378: if (enuChildren != null) {
379: DefaultMutableTreeNode childNode;
380: DwSnmpMibRecord childRec;
381: while (enuChildren.hasMoreElements()) {
382: childNode = (DefaultMutableTreeNode) enuChildren
383: .nextElement();
384: childRec = (DwSnmpMibRecord) childNode.getUserObject();
385: if (childRec.name.equals(rec.name) == true) {
386: //outputText("ChildCheck, Rec. Present.. Parent : " + rec.parent + " Name : " + rec.name);
387: return childNode;
388: }
389: }
390: }
391: return null; // Child does not exist
392: }
393:
394: public void setOutput(DwSnmpMibOutputHandler output) {
395: this .output = output;
396: }
397:
398: void outputText(String s) {
399: try {
400: output.println(s);
401: } catch (Exception e) {
402: System.out.println(s);
403: }
404: }
405:
406: void outputError(String s) {
407: try {
408: output.printError(s);
409: } catch (Exception e) {
410: System.out.println(s);
411: }
412: }
413:
414: public void newMibParseToken(DwSnmpMibRecord rec) {
415: addRecord(rec);
416:
417: }
418:
419: public void parseMibError(String s) {
420: outputError(s);
421: }
422:
423: }// End of class.
|