package com.huawei.emdc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
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));
SAXBuilder builder = new SAXBuilder();
doc = builder.build(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");
List<Element> vs = root.getChildren("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");
List<Element> elms = root.getChildren("EMDC");
if (elms != null && !elms.isEmpty())
{
Emdc emdc = null;
String v = null;
for (Element el : elms)
{
emdc = new Emdc();
emdc.setIp(el.getAttributeValue("ip"));
emdc.setName(el.getAttributeValue("name"));
v = el.getAttributeValue("VerID");
emdc.setVerID(v);
if (versions.isEmpty() || (!versions.contains(v)))
{
versions.add(v);
}
emdc.setItfID(el.getAttributeValue("ItfID"));
emdcs.add(emdc);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void delete(String ip)
{
// List<Element> elms = root.elements("EMDC");
List<Element> elms = root.getChildren("EMDC");
if (elms != null && !elms.isEmpty())
{
for (Element el : elms)
{
if (ip.equals(el.getAttributeValue("ip")))
{
// root.remove(el);
root.removeChild(el.getName());
}
}
}
rewriteXML();
}
public void deleteAll()
{
// List<Element> elms = root.elements("EMDC");
// //List<Element> elms = root.getChildren("EMDC");
// if (elms != null && !elms.isEmpty())
// {
// for (Element el : elms)
// {
// // root.remove(el);
// root.removeChild(el.getName());
// }
// }
root.removeChildren("EMDC");
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());
Element elm = new Element("EMDC");
elm.setAttribute("name", emdc.getNEName());
elm.setAttribute("ip", emdc.getIP());
elm.setAttribute("VerID", emdc.getVerID());
elm.setAttribute("ItfID", emdc.getItfID());
root.addContent(elm);
}
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();
// }
XMLOutputter XMLOut = new XMLOutputter();
try
{
XMLOut.output(doc, new FileOutputStream(path));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException 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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
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));
SAXBuilder builder = new SAXBuilder();
doc = builder.build(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");
List<Element> vs = root.getChildren("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");
List<Element> elms = root.getChildren("EMDC");
if (elms != null && !elms.isEmpty())
{
Emdc emdc = null;
String v = null;
for (Element el : elms)
{
emdc = new Emdc();
emdc.setIp(el.getAttributeValue("ip"));
emdc.setName(el.getAttributeValue("name"));
v = el.getAttributeValue("VerID");
emdc.setVerID(v);
if (versions.isEmpty() || (!versions.contains(v)))
{
versions.add(v);
}
emdc.setItfID(el.getAttributeValue("ItfID"));
emdcs.add(emdc);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void delete(String ip)
{
// List<Element> elms = root.elements("EMDC");
List<Element> elms = root.getChildren("EMDC");
if (elms != null && !elms.isEmpty())
{
for (Element el : elms)
{
if (ip.equals(el.getAttributeValue("ip")))
{
// root.remove(el);
root.removeChild(el.getName());
}
}
}
rewriteXML();
}
public void deleteAll()
{
// List<Element> elms = root.elements("EMDC");
// //List<Element> elms = root.getChildren("EMDC");
// if (elms != null && !elms.isEmpty())
// {
// for (Element el : elms)
// {
// // root.remove(el);
// root.removeChild(el.getName());
// }
// }
root.removeChildren("EMDC");
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());
Element elm = new Element("EMDC");
elm.setAttribute("name", emdc.getNEName());
elm.setAttribute("ip", emdc.getIP());
elm.setAttribute("VerID", emdc.getVerID());
elm.setAttribute("ItfID", emdc.getItfID());
root.addContent(elm);
}
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();
// }
XMLOutputter XMLOut = new XMLOutputter();
try
{
XMLOut.output(doc, new FileOutputStream(path));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException 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;
}
}