www.pudn.com > SnmpMaster.rar > ParseConfig.java
package snmpmaster;
import org.xml.sax.*;
import org.w3c.dom.*;
//import org.apache.xerces.parsers.DOMParser;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.xml.parsers.*;
public class ParseConfig {
Document xmlDoc;
public ParseConfig() {
}
public Hashtable GetOidConfig()
{
String RootOid = "";
Vector NameVector = new Vector();
Hashtable TypeTable = new Hashtable();
Hashtable OidTable = new Hashtable();
Hashtable OidConfig = new Hashtable(); //存储Oid的所有配置
try{
/////////////////创建一个DOM//////////////////
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
xmlDoc = builder.parse("E:\\网络监控系统\\代码设计\\SnmpMaster\\Config.xml"); //解析xml文件
///////////////////////////////////////////
NodeList ChildNodes = xmlDoc.getDocumentElement().getChildNodes();
Node OidNode = null;
for(int i = 0 ; i < ChildNodes.getLength(); i ++)
{
if(ChildNodes.item(i).getNodeType() == Node.TEXT_NODE)
continue; //如果当前节点是文本节点则执行下一个循环
if(ChildNodes.item(i).getNodeName().equals("OidConfig")){
//如果找到节点
OidNode = ChildNodes.item(i);
break;
}//if
}//for
ChildNodes = OidNode.getChildNodes(); //获得"OidConfig"的子节点
Node CurrentNode = null;
int index = 0;
for(int i = 0; i < ChildNodes.getLength(); i ++ )
{
if(ChildNodes.item(i).getNodeType() == Node.TEXT_NODE)
continue; //如果当前节点是文本节点则执行下一个循环
CurrentNode = ChildNodes.item(i);
if(ChildNodes.item(i).getNodeName().equals("RootOid"))
{
Node InnterText = CurrentNode.getFirstChild(); //获得它的文本节点
RootOid = InnterText.getNodeValue();//获得文本节点的值 RootOid;
continue;
}
if(ChildNodes.item(i).getNodeName().equals("Table")) //如果是表节点
{
NamedNodeMap AttrNodeMap = CurrentNode.getAttributes();
Node AttrNode = AttrNodeMap.getNamedItem("Name");
String Name = AttrNode.getNodeValue(); //得到Name属性的值
NameVector.add(index,Name); //存储该节点的名称
index ++ ;
TypeTable.put(Name,"table"); //存储该节点的类型
Hashtable tableConfig = GetTableConfig(RootOid,CurrentNode.getChildNodes());
OidTable.put(Name,tableConfig); //存储该节点配置
continue;
} //if
if(ChildNodes.item(i).getNodeName().equals("Node")) //如果是表节点
{
NamedNodeMap AttrNodeMap = CurrentNode.getAttributes();
Node AttrNode = AttrNodeMap.getNamedItem("Name");
String Name = AttrNode.getNodeValue(); //得到start属性的值
AttrNode = AttrNodeMap.getNamedItem("Oid");
String Oid = AttrNode.getNodeValue(); //得到Oid属性的值
Oid = RootOid + "." + Oid + ".0";
NameVector.add(index,Name); //存储该节点的名称
index ++ ;
TypeTable.put(Name,"node"); //存储该节点的类型
OidTable.put(Name,Oid); //存储该节点Oid配置
continue;
} //if
}//for
System.out.println(TypeTable.toString());
System.out.println(NameVector.toString());
System.out.println(OidTable.toString());
/*如果OidTable中对应项类型为table则,值为一个Hashtable否则为String
*/
OidConfig.put("name",NameVector);
OidConfig.put("type",TypeTable);
OidConfig.put("oidTable",OidTable);
return OidConfig;
}//try
catch(IOException ioe) {
ioe.printStackTrace();
}
catch(DOMException dome) {
dome.printStackTrace();
}
catch(SAXException saxe) {
saxe.printStackTrace();
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
return OidConfig;
} ///////////////////////////////////GetOidConfig
public Hashtable GetTableConfig(String RootOid , NodeList TableList)
{
Hashtable TableConfig = new Hashtable();
Vector NameVector = new Vector();
Hashtable OidTable = new Hashtable();
Node CurrentNode = null;
int index = 0;
for(int i = 0; i < TableList.getLength(); i++)
{
CurrentNode = TableList.item(i);
if(TableList.item(i).getNodeName().equals("Item"))
{
NamedNodeMap AttrNodeMap = CurrentNode.getAttributes();
Node AttrNode = AttrNodeMap.getNamedItem("Name");
String Name = AttrNode.getNodeValue(); //得到Name属性的值
AttrNode = AttrNodeMap.getNamedItem("Oid");
String Oid = AttrNode.getNodeValue(); // 得到Oid属性的值
Oid = RootOid + "." + Oid;
NameVector.add(index,Name); //存储该节点的名称
index ++ ;
OidTable.put(Name,Oid);
}//if
} ////for
TableConfig.put("name",NameVector); //Vector
TableConfig.put("Oid",OidTable); //Hashtable
System.out.println(NameVector.toString());
System.out.println(OidTable.toString());
return TableConfig;
}////////////////////////////////////////GetTableConfig//////////
public Hashtable GetCommamdConfig(String CmdCode)
{
Vector ObjectName = new Vector(); //需要操纵的对象名称列表
Vector returnName = new Vector(); //需要返回的对象名列表
String Action = ""; //需要执行的操作
Hashtable CmdConfig = new Hashtable();//返回的命令配置列表
try
{
/////////////////创建一个DOM//////////////////
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.parse("SnmpConfig.xml"); //解析xml文件
//////////找到对应CmdCode的节点/////////////
NodeList ChildNodes = xmlDoc.getDocumentElement().getChildNodes();
Node CurrentNode = null;
boolean FindInterface = false;
for(int i = 0 ; i < ChildNodes.getLength(); i ++)
{
if(ChildNodes.item(i).getNodeType() == Node.TEXT_NODE)
continue; //如果当前节点是文本节点则执行下一个循环
CurrentNode = ChildNodes.item(i);
NodeList CmdList = CurrentNode.getChildNodes();
if(ChildNodes.item(i).getNodeName().equals("CommandInterface"))
{ //如果找到节点
for(int j = 0; j < CmdList.getLength(); j ++)
{
if(CmdList.item(j).getNodeName().equals("CmdCode"))
{
if(CmdList.item(j).getFirstChild().getNodeValue().equals(CmdCode))
FindInterface = true;
break;
}//if
}//for
}//if
if(FindInterface == true)break; //如果找到对节点
}//for
///////////////////////////////////////////
//////////获取CommandInterface的配置//////////
ChildNodes = CurrentNode.getChildNodes();
/*
Hashtable Oidtable = new Hashtable();//存储返回项对应的Oid列表
*/
for(int i = 0; i < ChildNodes.getLength();i++)
{
CurrentNode = ChildNodes.item(i);
if(CurrentNode.getNodeName().equals("Action"))
{
Action = CurrentNode.getFirstChild().getNodeValue();
continue;
}
if(CurrentNode.getNodeName().equals("Object"))
{
NamedNodeMap AttrNodeMap = CurrentNode.getAttributes();
Node AttrNode = AttrNodeMap.getNamedItem("Name");
String name = AttrNode.getNodeValue(); //得到name属性的值
ObjectName.add(name);
continue;
}
if(CurrentNode.getNodeName().equals("returnValue"))
{
NamedNodeMap AttrNodeMap = CurrentNode.getAttributes();
Node AttrNode = AttrNodeMap.getNamedItem("Name");
String name = AttrNode.getNodeValue(); //得到name属性的值
returnName.add(name);
continue;
}
}//for
///////////////////////////////////////////////////////////////////
CmdConfig.put("Action",Action);
CmdConfig.put("ObjectName",ObjectName);
CmdConfig.put("returnName",returnName);
return CmdConfig;
}
catch(IOException ioe) {
ioe.printStackTrace();
}
catch(DOMException dome) {
dome.printStackTrace();
}
catch(SAXException saxe) {
saxe.printStackTrace();
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
return CmdConfig;
}
///////////////////////////////////////////
//////////////////////////////////////////
/////////////GetCommamdConfig///////////////////////////
public static void main(String[] args) {
ParseConfig parseConfig1 = new ParseConfig();
Hashtable OidConfig = parseConfig1.GetOidConfig();
Hashtable CmdConfig = parseConfig1.GetCommamdConfig("002");
// Hashtable OidValue = parseConfig1.GetOidValue("10.40.40.8",OidConfig,CmdConfig);
System.out.println(CmdConfig.toString());
}
}