import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import com.cns.certservice.vo.license.LicenseVO; public class LicenseUtil { private static LicenseUtil instance; // 存储xml元素信息的容器 private List<XmlNode> elemList = null; public static synchronized LicenseUtil getInstance(){ if(instance == null) instance = new LicenseUtil(); return instance; } /** * 生成license文件 * @param vo * @param filepath * @throws IOException */ public void createLisence(LicenseVO vo, String filepath) throws IOException { Document document = DocumentHelper.createDocument(); Element root = document.addElement("lisence"); Element lisence_info = root.addElement("lisence-info"); Element system_id = lisence_info.addElement("system-id"); Element ou_info = lisence_info.addElement("ou-info"); Element name = ou_info.addElement("name"); Element address = ou_info.addElement("address"); Element phone = ou_info.addElement("phone"); Element email = ou_info.addElement("email"); Element laid_default = lisence_info.addElement("laid-default"); Element laid_info = lisence_info.addElement("laid-info"); if(vo.getLaids() != null && vo.getLaids().size() > 0) { for (Iterator iterator = vo.getLaids().iterator(); iterator.hasNext();) { String laidvalue = (String) iterator.next(); Element laid = laid_info.addElement("laid"); laid.setText(laidvalue); } } Element config = lisence_info.addElement("config"); Element lock = config.addElement("lock"); Element validity_date = config.addElement("validity-date"); Element begin = validity_date.addElement("begin"); Element end = validity_date.addElement("end"); Element user_config = config.addElement("user-config"); Element max_number = user_config.addElement("max-number"); Element cert_config = config.addElement("cert-config"); Element cert_number = cert_config.addElement("cert-number"); Element validtime = cert_config.addElement("validtime"); Element ipass_config = config.addElement("ipass-config"); Element ipassnum = ipass_config.addElement("ipassnum"); Element usernum = ipass_config.addElement("usernum"); Element racert = config.addElement("racert"); Element sign_info = root.addElement("sign-info"); Element sign_data = sign_info.addElement("sign-data"); Element sign_date = sign_info.addElement("sign-date"); Element public_key = sign_info.addElement("public-key"); Element authorizer_info = root.addElement("authorizer-info"); Element company_name = authorizer_info.addElement("company-name"); Element company_addr = authorizer_info.addElement("company-addr"); Element company_tele = authorizer_info.addElement("company-tele"); Element company_postcode = authorizer_info.addElement("company-postcode"); Element declaration = authorizer_info.addElement("declaration"); laid_info.setAttributeValue("count", vo.getLaidcount()); laid_info.setAttributeValue("max", vo.getLaidmax()); if(vo.getSystemId() != null) system_id.setText(vo.getSystemId()); if(vo.getOuName() != null) name.setText(vo.getOuName()); if(vo.getOuAddress() != null) address.setText(vo.getOuAddress()); if(vo.getOuPhone() != null) phone.setText(vo.getOuPhone()); if(vo.getOuEmail() != null) email.setText(vo.getOuEmail()); if(vo.getLaidDefault() != null) laid_default.setText(vo.getLaidDefault()); if(vo.getLock() != null) lock.setText(vo.getLock()); if(vo.getBegin() != null) begin.setText(vo.getBegin()); if(vo.getEnd() != null) end.setText(vo.getEnd()); if(vo.getMaxnumber() != null) max_number.setText(vo.getMaxnumber()); if(vo.getCertnumber() != null) cert_number.setText(vo.getCertnumber()); cert_number.setAttributeValue("unit", vo.getCertUnit()); if(vo.getValidtime() != null) validtime.setText(vo.getValidtime()); validtime.setAttributeValue("max", vo.getValidtimemax()); validtime.setAttributeValue("unit", vo.getValidtimeunit()); if(vo.getIpassnum() != null) ipassnum.setText(vo.getIpassnum()); if(vo.getUsernum() != null) usernum.setText(vo.getUsernum()); if(vo.getRacert() != null) racert.setText(vo.getRacert()); racert.setAttributeValue("conding-type", vo.getCondingtype()); if(vo.getSignData() != null) sign_data.setText(vo.getSignData()); sign_data.setAttributeValue("conding-type", vo.getCondingtype()); if(vo.getSignDate() != null) sign_date.setText(vo.getSignDate()); if(vo.getPublicKey() != null) public_key.setText(vo.getPublicKey()); public_key.setAttributeValue("conding-type", vo.getCondingtype()); if(vo.getCompanyName() != null) company_name.setText(vo.getCompanyName()); if(vo.getCompanyAddr() != null) company_addr.setText(vo.getCompanyAddr()); if(vo.getCompanyPostCode() != null) company_postcode.setText(vo.getCompanyPostCode()); if(vo.getCompanyTel() != null) company_tele.setText(vo.getCompanyTel()); if(vo.getDeclaration() != null) declaration.setText(vo.getDeclaration()); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK");//根据需要设置编码 Writer fileWriter = new FileWriter(filepath); XMLWriter xmlWriter = new XMLWriter(fileWriter, format); xmlWriter.write(document); xmlWriter.close(); } /** * 文件解析 * * @param fileName * 文件路径 * @return * @throws DocumentException */ public LicenseVO parserXmlByFile(String fileName) throws DocumentException { File inputXml = new File(fileName); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputXml); Element elem = document.getRootElement(); elemList = new ArrayList<XmlNode>(); getElementList(elem); return readLicense(); } /** * 字符串解析 * * @param srcXml * 文件内容 * @return * @throws DocumentException */ public LicenseVO parserXmlByStr(String srcXml) throws DocumentException { Document srcdoc = DocumentHelper.parseText(srcXml); Element elem = srcdoc.getRootElement(); elemList = new ArrayList<XmlNode>(); getElementList(elem); return readLicense(); } private LicenseVO readLicense() { LicenseVO vo = new LicenseVO(); List laids = new ArrayList(); for (Iterator iterator = elemList.iterator(); iterator.hasNext();) { XmlNode node = (XmlNode) iterator.next(); String nodename = node.getNodename(); String value = node.getValue(); if (nodename.equals("system-id")) { vo.setSystemId(value); } else if (nodename.equals("name")) { vo.setOuName(value); } else if (nodename.equals("address")) { vo.setOuAddress(value); } else if (nodename.equals("email")){ vo.setOuEmail(value); }else if (nodename.equals("phone")) { vo.setOuPhone(value); } else if (nodename.equals("laid-default")) { vo.setLaidDefault(value); } else if (nodename.equals("laid")) { laids.add(value); } else if (nodename.equals("lock")) { vo.setLock(value); } else if (nodename.equals("begin")) { vo.setBegin(value); } else if (nodename.equals("end")) { vo.setEnd(value); } else if (nodename.equals("max-number")) { vo.setMaxnumber(value); } else if (nodename.equals("cert-number")) { vo.setCertnumber(value); } else if (nodename.equals("validtime")) { vo.setValidtime(value); } else if (nodename.equals("ipassnum")) { vo.setIpassnum(value); } else if (nodename.equals("usernum")) { vo.setUsernum(value); } else if (nodename.equals("racert")) { vo.setRacert(value); } else if (nodename.equals("sign-data")) { vo.setSignData(value); } else if (nodename.equals("sign-date")) { vo.setSignDate(value); } else if (nodename.equals("public-key")) { vo.setPublicKey(value); } else if (nodename.equals("company-name")) { vo.setCompanyName(value); } else if (nodename.equals("company-addr")) { vo.setCompanyAddr(value); } else if (nodename.equals("company-tele")) { vo.setCompanyTel(value); } else if (nodename.equals("company-postcode")) { vo.setCompanyPostCode(value); } else if (nodename.equals("declaration")) { vo.setDeclaration(value); } } vo.setLaids(laids); return vo; } /** * 递归遍历方法 * * @param element */ private void getElementList(Element element) { List elements = element.elements(); if (elements.size() == 0) { // 没有子元素 String nodename = element.getName(); String value = element.getTextTrim(); elemList.add(new XmlNode(nodename, value)); } else { // 有子元素 for (Iterator it = elements.iterator(); it.hasNext();) { Element elem = (Element) it.next(); // 递归遍历 getElementList(elem); } } } } class XmlNode { private String nodename; private String value; public XmlNode(String nodename, String value) { this.nodename = nodename; this.value = value; } public String getNodename() { return nodename; } public void setNodename(String nodename) { this.nodename = nodename; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }