⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customxmloperator.java

📁 这是对XML文件进行操作的一个类
💻 JAVA
字号:
/**
 * <p>Description:operate topo xml</p>
 * <p>Company: dhcc.com</p>
 * @author 聂成海
 * @project dhcnms
 * @date 2006-09-25
 */

package com.dhcnms.topology.util;

import java.io.*;
import java.util.*;

import org.jdom.Element;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.dhcnms.topology.model.CustomViewIP;
import com.dhcnms.common.util.SysLogger;
import com.dhcnms.initialize.ResourceCenter;
import com.dhcnms.polling.*;

public class CustomXmlOperator 
{
	private final String headBytes = "<%@page contentType=\"text/html; charset=GB2312\"%>\r\n";
	private SAXBuilder builder;
	private FileInputStream fis;
	private FileOutputStream fos;
	private Document doc;
	private XMLOutputter serializer;
	private String fullPath;
	
	private static List tempXmlInfo = new ArrayList();
	private static int xmlID = 0;
	
	public CustomXmlOperator()
	{
	}
	
	public void setFileName(String fileName)
	{
		fullPath = ResourceCenter.getInstance().getSysPath() + "resource\\xml\\" + fileName;			
	}
	
	/**
	 * 打开文件,创建xml解析对象,为读或写做准备
	 */
	public void openXMLFile()
	{
		try
 		{		
  		    fis = new FileInputStream(fullPath);
		    int start = headBytes.getBytes().length;
		    fis.skip(start);			
		    builder = new SAXBuilder();
		    doc = builder.build(fis);
		}
		catch(Exception e)
		{
			 SysLogger.error("Error in CustomXmlOperator.openXMLFile()" + e.getMessage());
			 SysLogger.error("------Error FileName=" + fullPath + "-------------");
		}   
	}
	
	public void delXMLFile()
	{
		try
		{
			File delFile = new File(fullPath);
			delFile.delete();
		}
		catch (Exception e)
		{
			SysLogger.error("删除文件操作出错",e);
		}
	}
	
	/**
	 * 创建文件,创建xml解析对象,为写做准备
	 */
	public void createXMLFile()
	{
		Element root = new Element("root");
		Element nodes = new Element("nodes");
		Element lines = new Element("lines");
		root.addContent(nodes);
		root.addContent(lines);
		  
		try
	    {
			Format format = Format.getCompactFormat();
		    format.setEncoding("GB2312");
		    format.setIndent("	");
		    serializer = new XMLOutputter(format);
		    fos = new FileOutputStream(fullPath);
		    fos.write(headBytes.getBytes());
		    
		    doc = new Document(root);
		    
		    serializer.output(doc, fos);
		    fos.close();
		    serializer = null;
		    fos = null;		
		}
		catch(Exception e)
		{
			SysLogger.error("--------Error in createXMLFile()----------------"); 
		} 
	}
	
	/**
	 * 为编辑结点作准备,把所有结点的部分信息先放到内存中
	 */
	public void prepare4editNodes()
	{
		openXMLFile();
		Element root = doc.getRootElement();
		Element oldNodes = root.getChild("nodes");
		List list = oldNodes.getChildren();
		tempXmlInfo.clear();
		xmlID = 0;
		int tempId = 0;
		for(int i=0;i<list.size();i++)
		{
			Element element = (Element)list.get(i);
			XmlInfo xmlInfo = new XmlInfo();
			xmlInfo.setXmlId(element.getChildText("id"));
			xmlInfo.setHostId(Integer.parseInt(element.getChildText("host_id")));
			xmlInfo.setCategory(element.getChild("id").getAttributeValue("category"));
			xmlInfo.setIpAddress(element.getChildText("ip"));
			xmlInfo.setExist(false);
			tempXmlInfo.add(xmlInfo);
			
			tempId = Integer.parseInt(element.getChildText("id"));
			if(tempId>xmlID)  xmlID = tempId; //找到最大的Id
		}	
		xmlID++;
	}
	
	/**
	 * 检查节点是否已经存在(对于网络设备和服务器)
	 */
	public boolean isExist(int hostId)
	{
		boolean exist = false;
		for(int i=0;i<tempXmlInfo.size();i++)
		{
			XmlInfo xmlInfo = (XmlInfo)tempXmlInfo.get(i);
			if(!xmlInfo.getCategory().equals("address")&&xmlInfo.getHostId()==hostId)
			{
		        exist = true;
		        xmlInfo.setExist(true);
		        break;
			}
		}
		return exist;
	}

	/**
	 * 检查节点是否已经存在(对于任意IP)
	 */
	public boolean isExist(String address)
	{
		boolean exist = false;
		for(int i=0;i<tempXmlInfo.size();i++)
		{
			XmlInfo xmlInfo = (XmlInfo)tempXmlInfo.get(i);
			if(xmlInfo.getCategory().equals("address")&&xmlInfo.getIpAddress().equals(address))
			{
		        exist = true;
		        xmlInfo.setExist(true);
		        break;
			}
		}
		return exist;
	}
	
	/**
	 * 编辑完结点后,删除没用的结点
	 */
	public void deleteNodes()
	{
		for(int i=0;i<tempXmlInfo.size();i++)
		{
			XmlInfo xmlInfo = (XmlInfo)tempXmlInfo.get(i);
			if(!xmlInfo.isExist())
			{
				deleteNodeByID(xmlInfo.getXmlId());
				System.out.println("----" + xmlInfo.getXmlId());
			}
		}
	}
	
	/**
	 * 按xmlid删除一个结点
	 */
	public void deleteNodeByID(String xmlId)
	{
		Element root = doc.getRootElement();
		List list = root.getChild("nodes").getChildren("node");		
		for (int i = 0; i < list.size(); i++)
		{
			Element node = (Element) list.get(i);
			if(node.getChildText("id").equals(xmlId)) 
			{
			    node.getParentElement().removeContent(node);
			    deleteLinesByID(xmlId); //删除结点,必然删除与它相关的连线
			    break;
			}     
		}
	}
	
	/**
	 * 删除连线
	 */
	public void deleteLinesByID(String xmlId)
	{
		Element root = doc.getRootElement();
		List list = root.getChild("lines").getChildren("line");	
		for(int i=0; i<list.size(); i+=1)
		{
			Element line = (Element)list.get(i);
			if(line.getChildText("a").equals(xmlId)
			||line.getChildText("b").equals(xmlId))
			  line.getParentElement().removeContent(line);						
		}				
	}
	
	public void prepare4AddLines()
	{
		openXMLFile();
		Element root = doc.getRootElement();
		Element oldlines = root.getChild("lines");
		root.removeContent(oldlines);
		Element nodes = new Element("lines");
		root.addContent(nodes);
		
	}
			
	/**
	 * 保存文件
	 */
	public void close()
	{		
	   try
	   {
		   Format format = Format.getCompactFormat();
		   format.setEncoding("GB2312");
		   format.setIndent("	");
		   serializer = new XMLOutputter(format);
		   fos = new FileOutputStream(fullPath);
		   fos.write(headBytes.getBytes());
		   serializer.output(doc, fos);
		   fos.close();
		   
		   doc = null;
		   builder = null;
		   fis = null;
		   serializer = null;
		   
		}
	    catch(Exception e)
		{
	    	SysLogger.error("Error in CustomXmlOperator.close()",e);
		}		
	}
	 
	/**
	 * 保存xml文件(用于拓扑图上的"保存"按钮)
	 */
	public void imageToXml(String content,String fileName)
	{
		String fullPath = ResourceCenter.getInstance().getSysPath() + "resource\\xml\\" + fileName;
		FileOutputStream fos = null;
	    OutputStreamWriter osw = null;	   
	    try
	    {
	        fos = new FileOutputStream(fullPath);
	        
	        osw = new OutputStreamWriter(fos, "GB2312");	        
	        osw.write("<%@page contentType=\"text/html; charset=GB2312\"%>\r\n" + content);
	        
	    }
	    catch(Exception e)
	    {   
	        SysLogger.error("CustomXmlOperator.imageToXml()",e);
	    }
	    finally
	    {
	       try
	       {
	           osw.close();
	       }
	       catch(Exception ee)
	       {}
	    }
	}
		
	/**
	 * 增加结点(从已有的资源中增加)
	 */
	public void add(int hostId)
	{
		Element root = doc.getRootElement();
		Element nodes = root.getChild("nodes");		
		Element node = new Element("node");
		Element id = new Element("id");		
		Element host_id = new Element("host_id");
		Element img = new Element("img");
		Element x = new Element("x");
		Element y = new Element("y");
		Element ip = new Element("ip");
		Element alias = new Element("alias");
		Element info = new Element("info");
		Element menu = new Element("menu");
		 
		Host host = PollingEngine.getInstance().getHostByID(hostId);	
		if(host==null) System.out.println("host is null=" + hostId);
		
		id.setText(String.valueOf(xmlID)); // 1 id项
		if(host.getCategory()==4)
		   id.setAttribute("category","server");
		else
		   id.setAttribute("category","network");
		host_id.setText(String.valueOf(hostId)); //2 host_id项		 				 	
		img.setText(host.getImage());
							
		x.setText(String.valueOf(xmlID*10));  //4 x、y项
		y.setText("10");		
		ip.setText(host.getIpAddress());// 5 ip项		
		alias.setText(host.getAlias()); // 6. alias项
		info.setText("名称:" + host.getAlias() + "<br>IP地址:" + host.getIpAddress());  //7.info项信息

		String menuItem = 
				"<a class=\"ping_menu_out\" onmouseover=\"pingMenuOver();\" onmouseout=\"pingMenuOut();\"" +
				" onclick=\"javascript:resetProcDlg();window.showModelessDialog('ping.jsp?ip=" + host.getIpAddress() + "'," +
				" window, 'dialogHeight:210px;dialogWidth:500px;status:0;help:0;edge:sunken;center:yes;scroll:0');" +
				"timingCloseProDlg(8000);\" title=\"ping " + host.getIpAddress() + "\">&nbsp;&nbsp;&nbsp;&nbsp;Ping </a><br/>"+
				"<a class=\"detail_menu_out\" onmouseover=\"detailMenuOver();\" onmouseout=\"detailMenuOut();\" " +
				"href=\"server.do?action=detail&id=" + host.getId() + "\">&nbsp;&nbsp;&nbsp;&nbsp;查看信息 </a><br/>";
		menu.setText(menuItem); //8. menu项信息		
		node.addContent(id);
		node.addContent(host_id);
		node.addContent(img);
		node.addContent(x);
		node.addContent(y);
		node.addContent(ip);
		node.addContent(alias);
		node.addContent(info);
		node.addContent(menu);
		nodes.addContent(node);	
		xmlID++;
	}
	
	/**
	 * 增加结点(增加任意IP)
	 */
	public void add(int hostId,String hostAlias,String hostAddress)
	{
		Element root = doc.getRootElement();
		Element nodes = root.getChild("nodes");	
		
		Element node = new Element("node");
		Element id = new Element("id");		
		Element host_id = new Element("host_id");
		Element img = new Element("img");
		Element x = new Element("x");
		Element y = new Element("y");
		Element ip = new Element("ip");
		Element alias = new Element("alias");
		Element info = new Element("info");
		Element menu = new Element("menu");
		 			
		id.setText(String.valueOf(xmlID)); // 1 id项		
		id.setAttribute("category","address");
		host_id.setText(String.valueOf(hostId)); //2 host_id项		 				 	
		img.setText("image/topo/terminal.gif");
							
		x.setText(String.valueOf(xmlID*10));  //4 x、y项
		y.setText("10");		
		ip.setText(hostAddress);// 5 ip项		
		alias.setText(hostAlias); // 6. alias项
		info.setText("名称:" + hostAlias + "<br>IP地址:" + hostAddress);  //7.info项信息

		String menuItem = 
				"<a class=\"ping_menu_out\" onmouseover=\"pingMenuOver();\" onmouseout=\"pingMenuOut();\"" +
				" onclick=\"javascript:resetProcDlg();window.showModelessDialog('ping.jsp?ip=" + hostAddress + "'," +
				" window, 'dialogHeight:210px;dialogWidth:500px;status:0;help:0;edge:sunken;center:yes;scroll:0');" +
				"timingCloseProDlg(8000);\" title=\"ping " + hostAddress + "\">&nbsp;&nbsp;&nbsp;&nbsp;Ping </a>";
		menu.setText(menuItem); //8. menu项信息		
		node.addContent(id);
		node.addContent(host_id);
		node.addContent(img);
		node.addContent(x);
		node.addContent(y);
		node.addContent(ip);
		node.addContent(alias);
		node.addContent(info);
		node.addContent(menu);
		nodes.addContent(node);	
		xmlID++;
	}
		
	public void addLine(String id1, String id2)
	{
		Element root = doc.getRootElement();
		Element lines = root.getChild("lines");	
		
		Element line = new Element("line");
	  	Element a = new Element("a");
		Element b = new Element("b");
		Element color = new Element("color");
		Element dash = new Element("dash");
		
		a.setText(id1);
		b.setText(id2);
	    color.setText("blue");
		dash.setText("Solid");
		
		line.addContent(a);
		line.addContent(b);
		line.addContent(color);
		line.addContent(dash);
		lines.addContent(line);		
	}
	
	/**
	 * 更新所有info项
	 */
	public void updateInfo()
	{	
		openXMLFile();
		Element root = doc.getRootElement();
		Element nodes = root.getChild("nodes");
		List list = nodes.getChildren();
		
		//改变结点的info和image
		Host host = null;
		CustomViewIP address = null;
		for (int i = 0; i < list.size(); i++)
		{			
			Element node = (Element) list.get(i);			
			int id = Integer.valueOf(node.getChild("host_id").getText()).intValue();
			String category = node.getChild("id").getAttributeValue("category").toString();
			
			if(category.equals("address"))
			{
			   address = CustomViewEngine.getInstance().getCustomIPByID(id);
			   if(address==null) continue;
			   
			   node.getChild("info").setText(address.getShowMessage());
			   if(address.isAlarm()) //报警
				  node.getChild("img").setText("image/topo/terminal_alarm.gif");
			   else
				  node.getChild("img").setText("image/topo/terminal.gif");			   			   
			}
			else
			{	
			   host = PollingEngine.getInstance().getHostByID(id);			
			   if(host==null) continue;
															
			   node.getChild("info").setText(host.getHostMessage());						
			   if(host.getCategory()==4)
			   {	
			      if(host.isAlarmTag()) //报警
			         node.getChild("img").setText("image/topo/server_alarm.gif");
			      else
			         node.getChild("img").setText(host.getImage());			   
			   }
			   else
			   {	
				  if(host.isAlarmTag()) //报警
				     node.getChild("img").setText(TopoHelper.getNetworkAlarmImage(host.getCategory()));   
				  else
				     node.getChild("img").setText(host.getImage());
		       }			
		    }
		}	
		close();		
	}	
}

⌨️ 快捷键说明

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