/**
*
*/
package com.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* @author Administrator
*
*/
public class MarkupImpl implements Markup {
private Document doc = null;
private String charSet = "gb2312";
private Element parentEle = null;
private Element nowEle = null;
private Element childEle = null;
public MarkupImpl() {
}
public MarkupImpl(String fileName) throws Exception {
loadXml(fileName);
}
public MarkupImpl(String fileName,String charSet) throws Exception{
this.charSet = charSet;
loadXml(fileName);
}
/* (non-Javadoc)
* @see admServer.Markup#findElem(java.lang.String)
*/
public boolean findElem(String name) {
if (null == parentEle) {
if (null == doc)
return false;
else {
Iterator<Element> it = doc.nodeIterator();
while (it.hasNext()) {
Object obj = it.next();
if (!(obj instanceof Element))continue;
Element ele = (Element)obj;
if (ele.getName().equals(name)) {
nowEle = ele;
childEle = null;
return true;
}
}
return false;
}
}
Element ele = null;
if (null == nowEle || !nowEle.getName().equals(name))
ele = parentEle.element(name);
else {
List<Element> lstEle = parentEle.elements(name);
int i = 0;
while (i < lstEle.size()) {
ele = lstEle.get(i++);
if (ele.equals(nowEle)) break;
}
if (i < lstEle.size())
ele = lstEle.get(i);
else
ele = null;
}
if (null == ele)return false;
nowEle = ele;
childEle = null;
return true;
}
/* (non-Javadoc)
* @see admServer.Markup#findChildElem(java.lang.String)
*/
public boolean findChildElem(String name) {
if (null == nowEle) {
if (null == parentEle) {
if (null == doc)
return false;
else
parentEle = doc.getRootElement();
}
Iterator<Element> it = parentEle.elementIterator();
while (it.hasNext()) {
Element ele = it.next();
if (ele.getName().equals(name)) {
nowEle = parentEle;
childEle = ele;
return true;
}
}
return false;
}
Element ele = null;
if (null == childEle || !childEle.getName().equals(name))
ele = nowEle.element(name);
else {
List<Element> lstEle = nowEle.elements(name);
int i = 0;
while (i < lstEle.size()) {
ele = lstEle.get(i++);
if (ele.equals(childEle)) break;
}
if (i < lstEle.size())
ele = lstEle.get(i);
else
ele = null;
}
if (null == ele)return false;
childEle = ele;
return true;
}
/* (non-Javadoc)
* @see admServer.Markup#intoElem()
*/
public boolean intoElem() {
if (null == nowEle) {
if (null == parentEle) {
parentEle = doc.getRootElement();
nowEle = parentEle;
return true;
}
nowEle = getFirstEle(parentEle);
return (null != nowEle);
}
Element ele = null;
if (null == childEle)
ele = getFirstEle(nowEle);
else
ele = childEle;
if (null == ele) return false;
parentEle = nowEle;
nowEle = ele;
childEle = null;
return true;
}
/* (non-Javadoc)
* @see admServer.Markup#outOfElem()
*/
public boolean outOfElem() {
if (null == nowEle) return false;
childEle = nowEle;
nowEle = parentEle;
parentEle = nowEle.getParent();
if (null == parentEle)
parentEle= nowEle;
return true;
}
/* (non-Javadoc)
* @see admServer.Markup#resetChildPos()
*/
public void resetChildPos() {
childEle = null;
}
/* (non-Javadoc)
* @see admServer.Markup#resetMainPos()
*/
public void resetMainPos() {
nowEle = childEle = null;
}
/* (non-Javadoc)
* @see admServer.Markup#resetPos()
*/
public void resetPos() {
parentEle = nowEle = childEle = null;
}
/* (non-Javadoc)
* @see admServer.Markup#getData(java.lang.String)
*/
public String getData() {
String strRet;
if (null == nowEle || !nowEle.isTextOnly()) return null;
strRet = nowEle.getText();
return strRet;
}
/* (non-Javadoc)
* @see admServer.Markup#getChildData(java.lang.String)
*/
public String getChildData() {
String strRet;
if (null == childEle || !childEle.isTextOnly()) return null;
strRet = childEle.getText();
return strRet;
}
/* (non-Javadoc)
* @see admServer.Markup#getAttribute(java.lang.String)
*/
public String getAttrib(String name) {
String strRet;
if (null == nowEle) return null;
strRet = nowEle.attributeValue(name);
return strRet;
}
/* (non-Javadoc)
* @see admServer.Markup#getChildAttrubute(java.lang.String)
*/
public String getChildAttrib(String name) {
String strRet;
if (null == childEle) return null;
strRet = childEle.attributeValue(name);
return strRet;
}
/* (non-Javadoc)
* @see admServer.Markup#getComment()
*/
public String getComment() {
if (null == nowEle) return null;
Element eleTmp = getPreEle(nowEle);
if (eleTmp.getNodeType() != Element.COMMENT_NODE)return null;
return eleTmp.getText();
//return false;
}
/* (non-Javadoc)
* @see admServer.Markup#addElem(java.lang.String)
*/
public boolean addElem(String name) {
if (null == parentEle) {
if (null == doc) {
doc = DocumentFactory.getInstance().createDocument();
parentEle = doc.addElement(name);
return (null != parentEle);
}
parentEle = doc.getRootElement();
}
nowEle = parentEle.addElement(name);
return (null != nowEle);
}
/* (non-Javadoc)
* @see admServer.Markup#addElem(java.lang.String, java.lang.String)
*/
public boolean addElem(String name, String value) {
if (null == parentEle) {
if (null == doc) {
doc = DocumentFactory.getInstance().createDocument();
parentEle = doc.addElement(name,value);
return (null != parentEle);
}
parentEle = doc.getRootElement();
}
nowEle = parentEle.addElement(name);
if (null == nowEle)
return false;
nowEle.setText(value);
return true;
}
/* (non-Javadoc)
* @see admServer.Markup#addElem(java.lang.String, int)
*/
public boolean addElem(String name, int value) {
return addElem(name,"" + value);
}
/* (non-Javadoc)
* @see admServer.Markup#addChildElem(java.lang.String)
*/
public boolean addChildElem(String name) {
if (null == nowEle) {
if (null == parentEle) {
if (null == doc)return false;
parentEle = doc.getRootElement();
}
nowEle = getFirstEle(parentEle);
if (null == nowEle) return false;
}
childEle = nowEle.addElement(name);
return (null != childEle);
}
/* (non-Javadoc)
* @see admServer.Markup#addChildElem(java.lang.String, java.lang.String)
*/
public boolean addChildElem(String name, String value) {
if (null == nowEle) {
if (null == parentEle) {
if (null == doc)return false;
parentEle = doc.getRootElement();
}
nowEle = getFirstEle(parentEle);
if