1 package com.dwipal; 2 3 import javax.swing.tree.DefaultMutableTreeNode ; 4 import javax.swing.JTree ; 5 import javax.swing.*; 6 import javax.swing.tree.DefaultTreeModel ; 7 import javax.swing.tree.TreeNode ; 8 import javax.swing.event.TreeSelectionListener ; 9 import javax.swing.event.TreeSelectionEvent ; 10 import javax.swing.tree.TreeSelectionModel ; 11 import java.util.Vector ; 12 import java.util.Hashtable ; 13 import java.util.Enumeration ; 14 import java.io.File ; 15 16 17 public class DwSnmpMibTreeBuilder implements DwMibParserInterface,Runnable 18 { 19 DwSnmpMibOutputHandler output=null; 20 private DefaultMutableTreeNode rootNode; 21 private DefaultMutableTreeNode treeRootNode; 22 private DefaultMutableTreeNode rootOrphan; 23 private DefaultMutableTreeNode rootVariable; 24 private DefaultMutableTreeNode rootVariableTable; 25 26 JTree tree; 27 28 private Vector fileVect; 29 private Vector orphanNodes; 30 31 private String 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 (treeRootRec); 44 45 DwSnmpMibRecord rootRec=new DwSnmpMibRecord(); 46 rootRec.name="root"; 47 rootRec.parent="MIB Browser"; 48 rootRec.number=1; 49 rootNode=new DefaultMutableTreeNode (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 (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 (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 (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 (); 80 fileVect=new Vector (); 81 clearError(); 82 } 83 84 public DefaultMutableTreeNode getRootNode() { 85 return rootNode; 86 } 87 88 public boolean addFile(String fName) { 89 if(fName==null) return false; 90 File mibFile=new File (fName); 91 if(mibFile.exists()!=true) return false; 92 fileVect.add(fName); 93 return true; 94 } 95 96 public boolean addDirectory(String dirName) { 97 System.out.println("Adding directory : " + dirName); 98 File dir=new File (dirName); 99 if(dir.isDirectory()!=true) return false; 100 File 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 [] getFiles() { 110 111 try { 112 Enumeration enu=fileVect.elements(); 113 String returnStr[]=new String [fileVect.size()]; 114 115 int i=0; 116 while(enu.hasMoreElements()) { 117 returnStr[i++] = (String )enu.nextElement(); 118 } 119 clearError(); 120 return returnStr; 121 } 122 catch(Exception 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 err) { 133 errorMsg=err; 134 } 135 136 public JTree buildTree() { 137 if(fileVect.size()==0) { 139 setError("Error : Please add files first"); 140 return null; 141 } 142 143 oidSupport=new DwSnmpOidSupport(); 144 Thread treeThread=new Thread (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 (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 Enumeration enu=fileVect.elements(); 161 String fileName=""; 162 JTree newTree=null; 163 while(enu.hasMoreElements()) { 164 fileName=(String ) enu.nextElement(); 165 loadFile(fileName); 166 } 167 updateOrphans(); 168 output.println("*****COMPLETED******"); 169 } 170 171 private void loadFile(String 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 fName) { 178 if(fName==null) return false; 179 File mibFile=new File (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 outputText("Updating orphans."); 195 DwSnmpMibRecord orphanRec=null; 196 Enumeration orphanEnu; 197 boolean contFlag=true; 198 199 while(contFlag==true) { 200 contFlag=false; 201 orphanEnu=orphanNodes.elements(); 202 while(orphanEnu.hasMoreElements()) { 203 DefaultMutableTreeNode orphanNode = (DefaultMutableTreeNode )orphanEnu.nextElement(); 204 205 if (addNode(orphanNode)==true) { 206 contFlag=true; 207 orphanNodes.remove(orphanNode); 208 continue; 209 } 210 218 } 219 output.print("."); 220 } 221 output.print("Done"); 222 output.print("\nBuilding OID Name resolution table..."); 223 oidSupport.buildOidToNameResolutionTable(rootNode); 224 225 orphanEnu=orphanNodes.elements(); 228 while(orphanEnu.hasMoreElements()) { 229 DefaultMutableTreeNode orphanNode = (DefaultMutableTreeNode )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 outputText("Updating variables table.."); 242 Enumeration enuVar=variableHash.elements(); 243 DwSnmpMibRecord varRec; 244 while(enuVar.hasMoreElements ()) { 245 varRec=(DwSnmpMibRecord) enuVar.nextElement(); 246 rootVariable.add(new DefaultMutableTreeNode (varRec)); 247 } 248 249 if(tree!=null && tree.getModel()!=null) { 250 ((DefaultTreeModel )tree.getModel()).reload(); 251 tree.revalidate(); 252 tree.repaint(); 253 } 254 outputText("Done"); 255 } 256 257 private int parseFile(String 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 newNode=new DefaultMutableTreeNode (childRec); 268 if(addNode(newNode)==false) { 269 orphanNodes.add(newNode); 271 orphanHash.put(childRec.name,newNode); 272 return false; 274 } 275 return true; 276 277 304 } 305 306 307 private boolean addNode(DefaultMutableTreeNode newNode) { 308 DwSnmpMibRecord newRec=(DwSnmpMibRecord) newNode.getUserObject(); 309 310 DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode )treeHash.get(newRec.parent); 311 if(parentNode==null) return false; 315 316 DwSnmpMibRecord parentRec=(DwSnmpMibRecord)parentNode.getUserObject(); 318 if(parentRec.recordType > 0) newRec.recordType =parentRec.recordType+1; 319 320 DefaultMutableTreeNode dupNode=isChildPresent(newNode); 321 if(dupNode == null){ try { 323 parentNode.add(newNode); 324 newNode.setParent(parentNode); 325 treeHash.put(newRec.name,newNode); 327 return true; 328 } catch(Exception e) { 329 System.out.println("Err in Child : " + newRec.name + "Parent : " + newRec.parent); 330 return false; 331 } 332 } 333 else { Enumeration dupChildren=newNode.children(); 335 while(dupChildren.hasMoreElements()) { 336 DefaultMutableTreeNode dupChildNode=(DefaultMutableTreeNode )dupChildren.nextElement(); 337 if(isChildPresent(dupChildNode)==null) dupNode.add(dupChildNode); 338 } 339 return true; 340 } 341 } 342 343 DefaultMutableTreeNode isChildPresent(DefaultMutableTreeNode childNode) { 344 DwSnmpMibRecord childRec=(DwSnmpMibRecord)childNode.getUserObject(); 345 return(isChildPresent(childRec)); 346 } 347 348 DefaultMutableTreeNode isChildPresent(DwSnmpMibRecord rec) { 349 DefaultMutableTreeNode parentNode =(DefaultMutableTreeNode ) treeHash.get(rec.parent); 350 if(parentNode==null) parentNode =(DefaultMutableTreeNode ) orphanHash.get(rec.parent); 351 if(parentNode==null) return null; 352 Enumeration enuChildren=parentNode.children(); 353 if (enuChildren!=null) { 354 DefaultMutableTreeNode childNode; 355 DwSnmpMibRecord childRec; 356 while(enuChildren.hasMoreElements()) { 357 childNode=(DefaultMutableTreeNode ) enuChildren.nextElement(); 358 childRec=(DwSnmpMibRecord )childNode.getUserObject(); 359 if(childRec.name.equals(rec.name)== true) { 360 return childNode; 362 } 363 } 364 } 365 return null; } 367 368 public void setOutput(DwSnmpMibOutputHandler output) { 369 this.output=output; 370 } 371 372 void outputText(String s) { 373 try { 374 output.println(s); 375 } catch(Exception e) { 376 System.out.println(s); 377 } 378 } 379 void outputError(String s) { 380 try { 381 output.printError(s); 382 } catch(Exception 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 s) { 394 outputError(s); 395 } 396 397 } 399 400 401 402 403 404 | Popular Tags |