simplenode.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,164 行 · 第 1/3 页

JAVA
1,164
字号
// This file is part of the OpenNMS(R) MIB Parser.//// Copyright (C) 2002-2003 John Rodriguez//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.// // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU// Lesser General Public License for more details.// // You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA//// See: http://www.fsf.org/copyleft/lesser.html///* Generated By:JJTree: Do not edit this line. SimpleNode.java */import java.util.Hashtable;import java.lang.StringBuffer;import java.util.Enumeration;import java.util.TreeMap;import java.util.Vector;/** * The MIB parser was made by writing 2 files  <br> * <blockquote> *   ParseMib.jjt - a grammar for ASN1.  <br>     *   SimpleNode.java - initially generated by JavaCC with additional fields  <br> *                     added that save important information.  <br> * </blockquote>     * These are the only 2 files that should need to be edited for changes. <br><br> * The parser.jjt specified the important tokens to recognize. * When a token is recognized, a method can be called from this class. You can * use existing methods or add your own. For example, here is a typical example * of how information is saved from a token that has been scanned in the ASN.1 * <c> <br><br> *      void SequenceOfVars() #TableOidVars : <br> *      { <br> *          Token t; <br> *      } <br> *      { <br> *          STUFF DELETED ... <br> *          { <br> *              jjtThis.setName(t.image); <br> *          } <br> *      } <br> * </c> <br><br> * The code above is called when a token is recognized in the ASN.1. This * would recognize something like: <br> * <c>SysOREntry ::= SEQUENCE {  </c><br> * and save the "image" field "SysOrentry" in the AST that is being generated * when tokens are found. * Note that not every token needs to have information saved from it. This * is because a small subset of information is output to the XML file that * OpenNms uses. <br> * When fields are saved in the jjt file, they can call methods in this file, * SimpleNode.java. This saves information to the Abstract Syntax Tree (AST)     * which is built by the parser and can be later accessed through these methods. * Any number of new fields or methods can be added to SimpleNode.java if you * want to extend the parser. The initial SimpleNode.java was nearly just stub * code. The reason for using AST is that it can be repeatedly walked and more * information added as needed. This is in contrast to the small number of * tokens that may be on the stack at any time during lexical analysis or * parsing. This is useful because not all information can be determined at * parse time. It is simpler and more clear to build it up gradually. For * example, consider the AST walk methods, which must be called in this order * because the AST is decorated on each walk. More Symbol Table information * is added on each walk. <br> * <c> * collectTableInfo() <br> * collectTableIndexInfo() <br> * collectSequenceInfo() <br> * collectOids() <br> * collectTextualConventionsInfo() <br> * </c> * Note that all the walks could probably have been combined into 1 or 2 * walks but this would have made the code less clear. Cpu time is cheap, * engineering time to understand code is expensive. The AST is not that * big so the walks are not really that expensive. * * The SimpleNode class has methods that:<br> * <blockquote> *    Walk the Abstract Syntax Tree (AST) to decorate the nodes.<br> *    Add information to the Symbol Table (ST) - various hash tables.<br> *    Emit the final XML representation of the important Object Identifiers (OIDS)<br> * </blockquote> * * @author John Rodriguez * @version 1.0 * @since 8/23/2003 <br><br> * * For any problems with this code, please email <br><br> * *   John Rodriguez  "snmpfactotum@yahoo.com" * * and include the MIBs that this compiler could not handle. */public class SimpleNode    implements Node {  /**   * The symbol table (ST) is made up of several different Hashtables   * of which oidNames is one.   * This hashtable contains all the object identifier names.   * The key is the oid, e.g. "lightsurf", the value is the full OID   * This is so that they can be built up from the base oid str   *   .iso.org.dod.internet.private   * As a simplifying assumption, everything is assumed to be under the above.   */  static Hashtable oidNames = new Hashtable();  /**       * Keep every variable that was in a ASN.1 table in a symbol table (hashtable).   * This is because variables in an ASN.1 table have different xml printed.   * Saving them in a symbol table allows gathering them when discovered in   * the ASN.1   * For example, in a non-table it would appear as: <br>   * <c>instance=".0"</c>   * while in a table it would appear as (depending on the index variable name): <br>   * <c>instance="ifTableIndex"</c> <br>   * name/value oidName/tableName   */  static Hashtable oidVarsTableName = new Hashtable();  /**   * A MIB table declaration will have an entry like: <br>   *   auiTable OBJECT-TYPE  <br>   *      SYNTAX SEQUENCE OF AuiEntry ... <br>   * Save the entry ("AuiEntry") as the  key, and  the name ("auiTable")   * as the value.   */  static Hashtable entryNameAndTableName = new Hashtable();  /**   * Save the index OID of a table as key=tableName, value=indexName   */  static Hashtable tableAndIndex = new Hashtable();  /**   * Save TEXTUAL-CONVENTION names and the base type in this table as   * key=type before TEXTUAL-CONVENTION, value=baseType   */  static Hashtable typeNameTable = new Hashtable();  /**   * This is an ordered list of OIDS used for output at the end. As   * the OIDS are discovered in the AST, they are added to the ST and this   * list. The list is necessary because Hashtables do not maintain the order   * that the OIDS were inserted.   */  static Vector orderList = new Vector();  /**   * This String just gives a shortcut so that we do not have to parse everything.   */  static final String rfc1155TextOID = ".iso.org.dod.internet";  /**   * This String just gives a shortcut so that we do not have to parse everything.   */  static final String rfc1155NumericOID = ".1.3.6.1";  /**   * This String just gives a shortcut so that we do not have to parse everything.   */  static final String baseTextOID = ".iso.org.dod.internet.private";  /**   * This String just gives a shortcut so that we do not have to parse everything.   */  static final String baseNumericOID = ".1.3.6.1.4";  /**   * This string is catenated on an alias that is greater then 19 chars   * This is an rrd limitation.   */  static final String TOO_LONG = "TOOLONG";  static final int tooLongSize = 19;  /**   * Every node has 1 and only 1 parent.   */  protected Node parent;  /**   * A node may have 0 or more children. This is kept in an array for easy   * walking.   */  protected Node[] children;  /**   * Every node has a unique id.   */  protected int id;  protected ParseMib parser;  /**   * Some String fields have this default String so we can tell if   * they have been assigned or not.   */  static final String DEFAULT = "default";  /**   * The name found in the MIB for an OID. This is for the string part   * of the OID. By saving every text OID name, we can later output the   * entire text name of an OID by gradually building up the name based   * on ancestors text names.   */  protected String identifier_text = DEFAULT;  protected String identifier_parent_text = DEFAULT;  /**   *  for searches of the tree to see whether this node has been visited   */  protected boolean visited = false;  /**   *  mark every OID variable that is in a table, done with a walk of the AST   */  protected boolean isOidInTable = false;  protected static boolean printDebug = false;  static {    try {      if (printDebug) {        System.out.println("building oidNames of oids");        // put the numeric and text oid bases in the hashtable        // eventually, these could be parsed from imports        //        // this can get things started with a few basic declarations        // that are commonly used        //   .iso/.1        //   .iso.org.dod.internet.private.enterprises/.1.3.6.1.4.1      }      OidValues oids = new OidValues();      oids.setTextOid(".iso");      oids.setNumericOid(".1");      oidNames.put("iso", oids);      // build up some of the oids that are from RFC1155-SMI      // directory, mgmt, experimental, private, enterprises      oids = new OidValues();      oids.setTextOid(rfc1155TextOID + "." + "directory");      oids.setNumericOid(rfc1155NumericOID + ".1");      oidNames.put("directory", oids);      oids = new OidValues();      oids.setTextOid(rfc1155TextOID + "." + "mgmt");      oids.setNumericOid(rfc1155NumericOID + ".2");      oidNames.put("mgmt", oids);      oids = new OidValues();      oids.setTextOid(rfc1155TextOID + "." + "experimental");      oids.setNumericOid(rfc1155NumericOID + ".3");      oidNames.put("experimental", oids);      oids = new OidValues();      oids.setTextOid(rfc1155TextOID + "." + "private");      oids.setNumericOid(rfc1155NumericOID + ".4");      oidNames.put("private", oids);      oids = new OidValues();      oids.setTextOid(baseTextOID + "." + "enterprises");      oids.setNumericOid(baseNumericOID + ".1");      oidNames.put("enterprises", oids);    }    catch (Exception e) {      System.err.println("exception during oidNames init" + e.getMessage());    }  }  /**   * turn debug on or off.   */  public static void setDebug(boolean b) {    printDebug = b;  }  /**   * Constructor for a node.   *   * @param i is a unique id   */  public SimpleNode(int i) {    id = i;  }  /**   * Constructor for a node.   *   * @param p is the parser.   * @param i is the unique id.   */  public SimpleNode(ParseMib p, int i) {    this(i);    parser = p;  }  /**   * Get the unique id of the node.   */  public int getId() {    return id;  }  /**   * Return a boolean indicating whether this node has been visited previously or not. The visited boolean may be reset   * between walks.   */  public boolean getVisited() {    return visited;  }  /**   * Set whether this node has been visited previously.   *   * @param b is the value to set visited   */  public void setVisited(boolean b) {    visited = b;  }  /**   * For some nodes, the parser will save information found in the MIB   * declaration. The jjt will have a construct like Token t, and this   * will call this method with a text name to save.   *   * @param str is the text from the MIB construct to save.   */  public void setName(String str) {    identifier_text = str;  }  /**   * During parsing, certain MIB constructs will have caused a save of the   * text name. This routine gets that text.   */  public String getName() {    return identifier_text;  }  /**   * This is a convenience method to save the name of the parent node. It   * would have been easy to walk back up the tree and get the parent.   *   * @param str is the name of the parent found in the MIB.   */  public void setParentName(String str) {    identifier_parent_text = str;  }  /**   * Get the name of the parent node found during MIB parsing.   */  public String getParentName() {    return identifier_parent_text;  }  /**   * In order to declare a MIB table, one has to use a SEQUENCE declaration   * followed by 1 or more OIDS. This just records the fact an OID is to   * be part of a table.   *   * @param b set true or false whether this OID is part of a table.   */  public void setIsOidInTable(boolean b) {    isOidInTable = b;  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?