package com.huawei.emdc;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.huawei.vne.ne.SimuEMDC;
public class EmdcConfig
{
private static EmdcConfig ins = new EmdcConfig();
private List<Emdc> emdcs = null;
private List<String> versions = null;
private Document doc = null;
private String path = null;
private Element root = null;
private EmdcConfig()
{
path = System.getProperty("user.dir") + File.separator + "config"
+ File.separator + "EMDC" + File.separator;
String fileName = "emdcConfig.xml";
path += fileName;
load(path);
}
public static EmdcConfig getInstance()
{
return ins;
}
private void load(String path)
{
try
{
SAXReader sar = new SAXReader();
doc = sar.read(new File(path));
if (null == doc)
{
System.out.println("Read eappConfig.xml error");
}
root = doc.getRootElement();
emdcs = new ArrayList<Emdc>();
versions = new ArrayList<String>();
List<Element> vs = root.elements("VerID");
if (vs != null && !vs.isEmpty())
{
String vn = null;
for (Element el : vs)
{
vn = el.getText();
if (versions.isEmpty() || (!versions.contains(vn)))
{
versions.add(vn);
}
}
}
List<Element> elms = root.elements("EMDC");
if (elms != null && !elms.isEmpty())
{
Emdc emdc = null;
String v = null;
for (Element el : elms)
{
emdc = new Emdc();
emdc.setIp(el.attributeValue("ip"));
emdc.setName(el.attributeValue("name"));
v = el.attributeValue("VerID");
emdc.setVerID(v);
if (versions.isEmpty() || (!versions.contains(v)))
{
versions.add(v);
}
emdc.setItfID(el.attributeValue("ItfID"));
emdcs.add(emdc);
}
}
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
public void delete(String ip)
{
// Element root = doc.getRootElement();
List<Element> elms = root.elements("EMDC");
if (elms != null && !elms.isEmpty())
{
for (Element el : elms)
{
if (ip.equals(el.attribute("ip").getValue()))
{
root.remove(el);
}
}
}
rewriteXML();
}
public void deleteAll()
{
// Element root = doc.getRootElement();
List<Element> elms = root.elements("EMDC");
if (elms != null && !elms.isEmpty())
{
for (Element el : elms)
{
root.remove(el);
}
}
rewriteXML();
}
public void addEMDC(SimuEMDC emdc)
{
// Element root = doc.getRootElement();
addEmdc(emdc);
rewriteXML();
}
public void addEMDCs(List<SimuEMDC> emdcs)
{
// Element root = doc.getRootElement();
for (SimuEMDC emdc : emdcs)
{
addEmdc(emdc);
}
rewriteXML();
}
private void addEmdc(SimuEMDC emdc)
{
Element elm = root.addElement("EMDC");
elm.addAttribute("name", emdc.getNEName());
elm.addAttribute("ip", emdc.getIP());
elm.addAttribute("VerID", emdc.getVerID());
elm.addAttribute("ItfID", emdc.getItfID());
}
private void rewriteXML()
{
OutputFormat format = OutputFormat.createPrettyPrint();
// 利用格式化类对编码进行设置
// format.setEncoding("GBK");
FileOutputStream output;
try
{
output = new FileOutputStream(new File(path));
XMLWriter writer = new XMLWriter(output, format);
writer.write(doc);
writer.flush();
writer.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Emdc> getEmdcs()
{
load(path);
return emdcs;
}
public void setEmdcs(List<Emdc> emdcs)
{
this.emdcs = emdcs;
}
public List<String> getVersions()
{
load(path);
return versions;
}
public void setVersions(List<String> versions)
{
this.versions = versions;
}
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.huawei.vne.ne.SimuEMDC;
public class EmdcConfig
{
private static EmdcConfig ins = new EmdcConfig();
private List<Emdc> emdcs = null;
private List<String> versions = null;
private Document doc = null;
private String path = null;
private Element root = null;
private EmdcConfig()
{
path = System.getProperty("user.dir") + File.separator + "config"
+ File.separator + "EMDC" + File.separator;
String fileName = "emdcConfig.xml";
path += fileName;
load(path);
}
public static EmdcConfig getInstance()
{
return ins;
}
private void load(String path)
{
try
{
SAXReader sar = new SAXReader();
doc = sar.read(new File(path));
if (null == doc)
{
System.out.println("Read eappConfig.xml error");
}
root = doc.getRootElement();
emdcs = new ArrayList<Emdc>();
versions = new ArrayList<String>();
List<Element> vs = root.elements("VerID");
if (vs != null && !vs.isEmpty())
{
String vn = null;
for (Element el : vs)
{
vn = el.getText();
if (versions.isEmpty() || (!versions.contains(vn)))
{
versions.add(vn);
}
}
}
List<Element> elms = root.elements("EMDC");
if (elms != null && !elms.isEmpty())
{
Emdc emdc = null;
String v = null;
for (Element el : elms)
{
emdc = new Emdc();
emdc.setIp(el.attributeValue("ip"));
emdc.setName(el.attributeValue("name"));
v = el.attributeValue("VerID");
emdc.setVerID(v);
if (versions.isEmpty() || (!versions.contains(v)))
{
versions.add(v);
}
emdc.setItfID(el.attributeValue("ItfID"));
emdcs.add(emdc);
}
}
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
public void delete(String ip)
{
// Element root = doc.getRootElement();
List<Element> elms = root.elements("EMDC");
if (elms != null && !elms.isEmpty())
{
for (Element el : elms)
{
if (ip.equals(el.attribute("ip").getValue()))
{
root.remove(el);
}
}
}
rewriteXML();
}
public void deleteAll()
{
// Element root = doc.getRootElement();
List<Element> elms = root.elements("EMDC");
if (elms != null && !elms.isEmpty())
{
for (Element el : elms)
{
root.remove(el);
}
}
rewriteXML();
}
public void addEMDC(SimuEMDC emdc)
{
// Element root = doc.getRootElement();
addEmdc(emdc);
rewriteXML();
}
public void addEMDCs(List<SimuEMDC> emdcs)
{
// Element root = doc.getRootElement();
for (SimuEMDC emdc : emdcs)
{
addEmdc(emdc);
}
rewriteXML();
}
private void addEmdc(SimuEMDC emdc)
{
Element elm = root.addElement("EMDC");
elm.addAttribute("name", emdc.getNEName());
elm.addAttribute("ip", emdc.getIP());
elm.addAttribute("VerID", emdc.getVerID());
elm.addAttribute("ItfID", emdc.getItfID());
}
private void rewriteXML()
{
OutputFormat format = OutputFormat.createPrettyPrint();
// 利用格式化类对编码进行设置
// format.setEncoding("GBK");
FileOutputStream output;
try
{
output = new FileOutputStream(new File(path));
XMLWriter writer = new XMLWriter(output, format);
writer.write(doc);
writer.flush();
writer.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Emdc> getEmdcs()
{
load(path);
return emdcs;
}
public void setEmdcs(List<Emdc> emdcs)
{
this.emdcs = emdcs;
}
public List<String> getVersions()
{
load(path);
return versions;
}
public void setVersions(List<String> versions)
{
this.versions = versions;
}
}
====================例子xml===============================
<?xml version="1.0" encoding="UTF-8"?>
<config>
<IP>10.148.142.72</IP>
<VerID>eAPP610V100R002C00B110</VerID>
<ItfID>eAPP610V100R002C00B110</ItfID>
<EMDC name="EMDC_10_148_142_72" ip="10.148.142.72" VerID="eAPP610V100R002C00B110" ItfID="V100R003C00B040041716" />
</config>
=========================================================