KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dwipal > DwSnmpMibTreeBuilder


1 package com.dwipal;
2
3 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
4 import javax.swing.JTree JavaDoc;
5 import javax.swing.*;
6 import javax.swing.tree.DefaultTreeModel JavaDoc;
7 import javax.swing.tree.TreeNode JavaDoc;
8 import javax.swing.event.TreeSelectionListener JavaDoc;
9 import javax.swing.event.TreeSelectionEvent JavaDoc;
10 import javax.swing.tree.TreeSelectionModel JavaDoc;
11 import java.util.Vector JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.io.File JavaDoc;
15
16
17 public class DwSnmpMibTreeBuilder implements DwMibParserInterface,Runnable JavaDoc
18 {
19     DwSnmpMibOutputHandler output=null;
20     private DefaultMutableTreeNode JavaDoc rootNode;
21     private DefaultMutableTreeNode JavaDoc treeRootNode;
22     private DefaultMutableTreeNode JavaDoc rootOrphan;
23     private DefaultMutableTreeNode JavaDoc rootVariable;
24     private DefaultMutableTreeNode JavaDoc rootVariableTable;
25
26     JTree JavaDoc tree;
27
28     private Vector JavaDoc fileVect;
29     private Vector JavaDoc orphanNodes;
30
31     private String JavaDoc errorMsg="";
32
33     public DwSnmpOidSupport oidSupport=new DwSnmpOidSupport();
34     DwSnmpMibTreeHash treeHash;
35     DwSnmpMibTreeHash variableHash;
36     DwSnmpMibTreeHash orphanHash;
37
38     public DwSnmpMibTreeBuilder() {
39         DwSnmpMibRecord treeRootRec=new DwSnmpMibRecord();
40         treeRootRec.name="MIB Browser";
41         treeRootRec.parent="MIB Browser";
42         treeRootRec.number=0;
43         treeRootNode=new DefaultMutableTreeNode JavaDoc(treeRootRec);
44
45         DwSnmpMibRecord rootRec=new DwSnmpMibRecord();
46         rootRec.name="root";
47         rootRec.parent="MIB Browser";
48         rootRec.number=1;
49         rootNode=new DefaultMutableTreeNode JavaDoc(rootRec);
50
51         DwSnmpMibRecord rootOrphanRec=new DwSnmpMibRecord();
52         rootOrphanRec.name="Orphans";
53         rootOrphanRec.parent="MIB Browser";
54         rootOrphanRec.description = "This subtree contains MIB Records whose parent cannot be traced";
55         rootOrphanRec.number=10;
56         rootOrphan =new DefaultMutableTreeNode JavaDoc(rootOrphanRec);
57
58
59         DwSnmpMibRecord rootVariableRec=new DwSnmpMibRecord();
60         rootVariableRec.name="Variables/Textual Conventions";
61         rootVariableRec.parent="MIB Browser";
62         rootVariableRec.description = "This subtree contains all the variables which map to the standard variables.";
63         rootVariableRec.number=11;
64         rootVariable =new DefaultMutableTreeNode JavaDoc(rootVariableRec);
65
66         DwSnmpMibRecord rootVariableTableRec=new DwSnmpMibRecord();
67         rootVariableTableRec.name="Table Entries";
68         rootVariableTableRec.parent="Variables/Textual Conventions";
69         rootVariableTableRec.description = "This branch contains a list of sequences for all the tables ";
70         rootVariableTableRec.number=12;
71         rootVariableTable =new DefaultMutableTreeNode JavaDoc(rootVariableTableRec);
72
73         treeHash=new DwSnmpMibTreeHash();
74         treeHash.put(rootRec.name,rootNode);
75
76         variableHash=new DwSnmpMibTreeHash();
77         orphanHash =new DwSnmpMibTreeHash();
78
79         orphanNodes=new Vector JavaDoc();
80         fileVect=new Vector JavaDoc();
81         clearError();
82     }
83
84     public DefaultMutableTreeNode JavaDoc getRootNode() {
85         return rootNode;
86     }
87
88     public boolean addFile(String JavaDoc fName) {
89         if(fName==null) return false;
90         File JavaDoc mibFile=new File JavaDoc(fName);
91         if(mibFile.exists()!=true) return false;
92         fileVect.add(fName);
93         return true;
94     }
95
96     public boolean addDirectory(String JavaDoc dirName) {
97         System.out.println("Adding directory : " + dirName);
98         File JavaDoc dir=new File JavaDoc(dirName);
99         if(dir.isDirectory()!=true) return false;
100         File JavaDoc files[]=dir.listFiles();
101         if(files==null) return false;
102         for(int i=0;i<files.length;i++) {
103             fileVect.add(files[i].getPath());
104         }
105         return true;
106     }
107
108
109     public String JavaDoc[] getFiles() {
110
111         try {
112             Enumeration JavaDoc enu=fileVect.elements();
113             String JavaDoc returnStr[]=new String JavaDoc[fileVect.size()];
114
115             int i=0;
116             while(enu.hasMoreElements()) {
117                 returnStr[i++] = (String JavaDoc)enu.nextElement();
118             }
119             clearError();
120             return returnStr;
121         }
122         catch(Exception JavaDoc e) {
123             setError("Error in getting filenames..\n" + e.toString());
124             return null;
125         }
126     }
127
128     private void clearError() {
129         errorMsg = "";
130     }
131
132     private void setError(String JavaDoc err) {
133         errorMsg=err;
134     }
135
136     public JTree JavaDoc buildTree() {
137         // Check if files have been added to list
138
if(fileVect.size()==0) {
139             setError("Error : Please add files first");
140             return null;
141         }
142
143         oidSupport=new DwSnmpOidSupport();
144         Thread JavaDoc treeThread=new Thread JavaDoc(this);
145         treeThread.setPriority(Thread.MAX_PRIORITY-1);
146         treeThread.start();
147
148         treeRootNode.add(rootNode);
149         treeRootNode.add(rootOrphan);
150         rootVariable.add(rootVariableTable);
151         treeRootNode.add(rootVariable);
152         tree=new JTree JavaDoc(treeRootNode);
153         tree.putClientProperty("JTree.lineStyle", "Angled");
154         tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION);
155         return(tree);
156     }
157
158     public void run() {
159         // Get filenames and add nodes to rootnode
160
Enumeration JavaDoc enu=fileVect.elements();
161         String JavaDoc fileName="";
162         JTree JavaDoc newTree=null;
163         while(enu.hasMoreElements()) {
164             fileName=(String JavaDoc) enu.nextElement();
165             loadFile(fileName);
166         }
167         updateOrphans();
168         output.println("*****COMPLETED******");
169     }
170
171     private void loadFile(String JavaDoc fileName) {
172         output.print("Adding file " + fileName);
173         if(parseFile(fileName)<0) outputError(".. Error");
174         else output.print("..Done\n");
175     }
176
177     public boolean loadNewFile(String JavaDoc fName) {
178         if(fName==null) return false;
179         File JavaDoc mibFile=new File JavaDoc(fName);
180         if(mibFile.exists()!=true) return false;
181         if(fileVect.indexOf(fName)==-1) {
182             tree.collapsePath(tree.getSelectionPath());
183             fileVect.add(fName);
184             loadFile(fName);
185             updateOrphans();
186             return true;
187         }
188         return false;
189     }
190
191     private void updateOrphans() {
192         // Check if orphan's parents have arrived. if yes, remove them from orphan list
193
//outputText("Scanning orphans..");
194
outputText("Updating orphans.");
195         DwSnmpMibRecord orphanRec=null;
196         Enumeration JavaDoc orphanEnu;
197         boolean contFlag=true;
198
199         while(contFlag==true) {
200             contFlag=false;
201             orphanEnu=orphanNodes.elements();
202             while(orphanEnu.hasMoreElements()) {
203                 DefaultMutableTreeNode JavaDoc orphanNode = (DefaultMutableTreeNode JavaDoc)orphanEnu.nextElement();
204
205                 if (addNode(orphanNode)==true) {
206                     contFlag=true;
207                     orphanNodes.remove(orphanNode);
208                     continue;
209                 }
210                 // If orphan rec. type=1, then it is a table variable.
211
/*
212                 orphanRec = (DwSnmpMibRecord) orphanNode.getUserObject();
213                 if(orphanRec.recordType==orphanRec.recTable) {
214                     rootVariableTable.add(orphanNode);
215                     orphanNodes.remove(orphanNode);
216                 }
217                 */

218             }
219             output.print(".");
220         }
221         output.print("Done");
222         output.print("\nBuilding OID Name resolution table...");
223         oidSupport.buildOidToNameResolutionTable(rootNode);
224
225         //Add remaining orphans to treeroot.orphans
226
//System.out.print("Updating orphan table....");
227
orphanEnu=orphanNodes.elements();
228         while(orphanEnu.hasMoreElements()) {
229             DefaultMutableTreeNode JavaDoc orphanNode = (DefaultMutableTreeNode JavaDoc)orphanEnu.nextElement();
230             orphanRec=(DwSnmpMibRecord) orphanNode.getUserObject();
231             if(orphanRec.recordType==orphanRec.recVariable) continue;
232             if(orphanRec.recordType==orphanRec.recTable) {
233                 rootVariable.add(orphanNode);
234                 continue;
235             }
236             if(treeHash.containsKey(orphanRec.name)!=true) rootOrphan.add(orphanNode);
237         }
238
239         //System.out.print("Completed!\n");
240
// Add variables to varroot
241
outputText("Updating variables table..");
242         Enumeration JavaDoc enuVar=variableHash.elements();
243         DwSnmpMibRecord varRec;
244         while(enuVar.hasMoreElements ()) {
245             varRec=(DwSnmpMibRecord) enuVar.nextElement();
246             rootVariable.add(new DefaultMutableTreeNode JavaDoc(varRec));
247         }
248
249         if(tree!=null && tree.getModel()!=null) {
250             ((DefaultTreeModel JavaDoc)tree.getModel()).reload();
251             tree.revalidate();
252             tree.repaint();
253         }
254         outputText("Done");
255     }
256
257     private int parseFile(String JavaDoc fName) {
258
259         DwSnmpMibParser fParser=new DwSnmpMibParser(fName,this);
260         if(output!=null) fParser.setOutput(output);
261         return fParser.parseMibFile();
262     }
263
264     private boolean addRecord(DwSnmpMibRecord childRec) {
265         int parseStatus=0;
266         if(childRec==null) return false;
267         DefaultMutableTreeNode JavaDoc newNode=new DefaultMutableTreeNode JavaDoc(childRec);
268         if(addNode(newNode)==false) {
269 // if(orphanHash.containsKey(childRec.name)==false) {
270
orphanNodes.add(newNode);
271                 orphanHash.put(childRec.name,newNode);
272 // }
273
return false;
274         }
275         return true;
276
277         /*
278         // See if parent exists. if no parent, add it to orphans
279         if (treeHash.containsKey(childRec.parent) == false) {
280         // outputText("Orphan : " + childRec.name + " Parent : " + childRec.parent );
281             DefaultMutableTreeNode orphanNode=new DefaultMutableTreeNode(childRec,true);
282             treeHash.put(childRec.name,orphanNode);
283             orphanNodes.add(orphanNode);
284             return false;
285         }
286         // Get the parent node (current node will be added to it)
287         DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode) treeHash.get(childRec.parent);
288
289         // Check if parent node contains a child of same name as new node
290         // If child exists, return true
291         if(isChildPresent(childRec)!=null) return true;
292
293         // Check if parent is a Table, and set the node tableEntry accordingly
294         DwSnmpMibRecord parentRec=(DwSnmpMibRecord)parentNode.getUserObject();
295         if(parentRec.recordType > 0) childRec.recordType =parentRec.recordType+1;
296         //outputText("Added Child : " + childRec.name + " Parent : " + childRec.parent );
297         DefaultMutableTreeNode childNode=new DefaultMutableTreeNode (childRec,true);
298         // Add node to its parent
299         parentNode.add(childNode);
300         childNode.setParent(parentNode);
301         treeHash.put(childRec.name,childNode);
302         return true;
303         */

304     }
305
306
307     private boolean addNode(DefaultMutableTreeNode JavaDoc newNode) {
308         DwSnmpMibRecord newRec=(DwSnmpMibRecord) newNode.getUserObject();
309
310         DefaultMutableTreeNode JavaDoc parentNode = (DefaultMutableTreeNode JavaDoc)treeHash.get(newRec.parent);
311         //if(parentNode==null) { // See if parent is an orphan
312
// parentNode = (DefaultMutableTreeNode)orphanHash.get(newRec.parent);
313
//}
314
if(parentNode==null) return false;
315
316         // Check if parent is a Table, and set the node tableEntry accordingly
317
DwSnmpMibRecord parentRec=(DwSnmpMibRecord)parentNode.getUserObject();
318         if(parentRec.recordType > 0) newRec.recordType =parentRec.recordType+1;
319
320         DefaultMutableTreeNode JavaDoc dupNode=isChildPresent(newNode);
321         if(dupNode == null){ // Add node to its parent
322
try {
323                 parentNode.add(newNode);
324                 newNode.setParent(parentNode);
325                 // See if parent is not an orphan
326
treeHash.put(newRec.name,newNode);
327                 return true;
328             } catch(Exception JavaDoc e) {
329                 System.out.println("Err in Child : " + newRec.name + "Parent : " + newRec.parent);
330                 return false;
331             }
332         }
333         else { // Node already present. add all its children to the existing node
334
Enumeration JavaDoc dupChildren=newNode.children();
335             while(dupChildren.hasMoreElements()) {
336                 DefaultMutableTreeNode JavaDoc dupChildNode=(DefaultMutableTreeNode JavaDoc)dupChildren.nextElement();
337                 if(isChildPresent(dupChildNode)==null) dupNode.add(dupChildNode);
338             }
339             return true;
340         }
341     }
342
343     DefaultMutableTreeNode JavaDoc isChildPresent(DefaultMutableTreeNode JavaDoc childNode) {
344         DwSnmpMibRecord childRec=(DwSnmpMibRecord)childNode.getUserObject();
345         return(isChildPresent(childRec));
346     }
347
348     DefaultMutableTreeNode JavaDoc isChildPresent(DwSnmpMibRecord rec) {
349         DefaultMutableTreeNode JavaDoc parentNode =(DefaultMutableTreeNode JavaDoc) treeHash.get(rec.parent);
350         if(parentNode==null) parentNode =(DefaultMutableTreeNode JavaDoc) orphanHash.get(rec.parent);
351         if(parentNode==null) return null;
352         Enumeration JavaDoc enuChildren=parentNode.children();
353         if (enuChildren!=null) {
354             DefaultMutableTreeNode JavaDoc childNode;
355             DwSnmpMibRecord childRec;
356             while(enuChildren.hasMoreElements()) {
357                 childNode=(DefaultMutableTreeNode JavaDoc) enuChildren.nextElement();
358                 childRec=(DwSnmpMibRecord )childNode.getUserObject();
359                 if(childRec.name.equals(rec.name)== true) {
360                     //outputText("ChildCheck, Rec. Present.. Parent : " + rec.parent + " Name : " + rec.name);
361
return childNode;
362                 }
363             }
364         }
365         return null; // Child does not exist
366
}
367
368     public void setOutput(DwSnmpMibOutputHandler output) {
369         this.output=output;
370     }
371
372     void outputText(String JavaDoc s) {
373         try {
374         output.println(s);
375         } catch(Exception JavaDoc e) {
376             System.out.println(s);
377         }
378     }
379     void outputError(String JavaDoc s) {
380         try {
381         output.printError(s);
382         } catch(Exception JavaDoc e) {
383             System.out.println(s);
384         }
385     }
386
387     public void newMibParseToken(DwSnmpMibRecord rec) {
388         addRecord(rec);
389
390     }
391
392
393     public void parseMibError(String JavaDoc s) {
394         outputError(s);
395     }
396
397 }// End of class.
398

399
400
401
402
403
404
Popular Tags