package com.hisoft.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.hisoft.vo.XmlAnis;
/**
*
* @ClassName:Parser
* @Description:TODO
* @author weizhao.dong
* @date 2014-1-13 下午06:37:52
*/
public class Parser {
/**
* 核心解析类
*
* @author weizhao.dong
* @Date 2014-1-13 下午06:41:03
* @return
* @throws DocumentException
* @throws FileNotFoundException
*/
public Document parser(String file) throws FileNotFoundException,
DocumentException {
Map<String, XmlAnis> xmlMap = new HashMap<String, XmlAnis>();
Map<String, Integer> parentflag = new HashMap<String, Integer>();
/**
*解析schema
*/
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new FileInputStream(file));
List<Element> lists = document.selectNodes("/xs:schema/xs:element");
for (Iterator iterator = lists.listIterator(); iterator.hasNext();) {
Element element = (Element) iterator.next();
XmlAnis xmlAnis = new XmlAnis();
List<Attribute> attributes = element.attributes();
for (Attribute attribute : attributes) {
if (attribute.getName().equals("name")) {
xmlAnis.setName(attribute.getValue());
}
if (attribute.getName().equals("type")) {
xmlAnis.setType(attribute.getValue());
}
}
List<String> childs = new ArrayList<String>();
List<com.hisoft.vo.Attribute> xmlAttributes = new ArrayList<com.hisoft.vo.Attribute>();
unpackScheam(element, childs, parentflag, xmlAttributes);
xmlAnis.setChilds(childs); // 增加子节点
xmlAnis.setAttributes(xmlAttributes);// 增加属性
xmlMap.put(xmlAnis.getName(), xmlAnis);
}
/**
* 组装xml
*/
Document gendocument = null;
for (XmlAnis xmlAnis2 : xmlMap.values()) {
if (parentflag.get(xmlAnis2.getName()) == null) {
gendocument = DocumentHelper.createDocument();
Element root = gendocument.addElement(xmlAnis2.getName());
packXml(xmlMap, xmlAnis2, root);
}
}
return gendocument;
}
/**
* 解析schema
*
* @author weizhao.dong
* @Date 2014-1-13 下午07:50:53
* @param element
* @param childs
* @param parentflag
* @param xmlAttributes
*/
private void unpackScheam(Element element, List<String> childs,
Map<String, Integer> parentflag,
List<com.hisoft.vo.Attribute> xmlAttributes) {
for (Iterator subiterator = element.elementIterator(); subiterator
.hasNext();) {
Element element1 = (Element) subiterator.next();
if (element1.attributes().size() > 0) {
com.hisoft.vo.Attribute xmlattribute = null;
List<Attribute> subattributes = element1.attributes();
for (Attribute attribute : subattributes) {
if (element1.getName().equals("attribute")) {
if (xmlattribute == null) {
xmlattribute = new com.hisoft.vo.Attribute();
}
if (attribute.getName().equals("name")) {
xmlattribute.setName(attribute.getValue());
}
if (attribute.getName().equals("default")) {
xmlattribute.setDefaultvalue(attribute.getValue());
}
} else {
if (attribute.getName().equals("ref")) {
childs.add(attribute.getValue());
if (parentflag.get(attribute.getValue()) == null) {
parentflag.put(attribute.getValue(), 1);
}
}
}
}
if (xmlattribute != null) {
xmlAttributes.add(xmlattribute);
}
}
unpackScheam(element1, childs, parentflag, xmlAttributes);
}
}
/**
* 组装XML
*
* @author weizhao.dong
* @Date 2014-1-13 下午07:57:45
* @param xmlMap
* @param xmlAnis
* @param element
*/
private void packXml(Map<String, XmlAnis> xmlMap, XmlAnis xmlAnis,
Element element) {
System.out.println(xmlAnis.getName());
List<String> genchilds = xmlAnis.getChilds();
if (genchilds != null && genchilds.size() > 0) {
for (String str : genchilds) {
Element subElement = element.addElement(str);
XmlAnis subAnis = xmlMap.get(str);
packXml(xmlMap, subAnis, subElement);
}
}
List<com.hisoft.vo.Attribute> xmlAttributes = xmlAnis.getAttributes();
if (xmlAttributes != null && xmlAttributes.size() > 0) {
for (com.hisoft.vo.Attribute attribute : xmlAttributes) {
if (attribute.getDefaultvalue() == null) {
element.addAttribute(attribute.getName(), "");
} else {
element.addAttribute(attribute.getName(), attribute
.getDefaultvalue());
}
}
}
}
}
- 1
- 2
前往页