package org.xins.common.xml;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xins.common.MandatoryArgumentChecker;
import org.znerd.xmlenc.XMLOutputter;
public class ElementFormatter {
private ElementFormatter() {
}
public static String format(Element element) {
MandatoryArgumentChecker.check("element", element);
StringWriter output = new StringWriter();
try {
XMLOutputter xmlout = new XMLOutputter(output, "UTF-8");
format(element, xmlout);
return output.toString();
} catch (IOException iex) {
return "<" + element.getTagName() + "/>";
}
}
public static void format(Element element, XMLOutputter xmlout) throws IOException {
MandatoryArgumentChecker.check("element", element, "xmlout", xmlout);
xmlout.startTag(element.getTagName());
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
xmlout.attribute(attribute.getNodeName(), attribute.getNodeValue());
}
String pcdata = element.getTextContent();
if (!pcdata.equals("")) {
xmlout.pcdata(pcdata);
}
ElementList children = new ElementList(element);
for (Element child : children) {
format(child, xmlout);
}
xmlout.endTag();
}
public static Element parse(String xml) throws SAXException {
return parse(new StringReader(xml));
}
public static Element parseURI(String uri) throws SAXException {
try {
DocumentBuilder builder = DocumentBuilderPool.getInstance().getBuilder();
Document document = builder.parse(uri);
Element parsedElement = document.getDocumentElement();
DocumentBuilderPool.getInstance().releaseBuilder(builder);
return parsedElement;
} catch (IOException ex) {
throw new SAXParseException("IO error occured while parsing " + uri + ": " + ex.getMessage(), null);
}
}
public static Element parse(Reader reader) throws SAXException {
try {
DocumentBuilder builder = DocumentBuilderPool.getInstance().getBuilder();
InputSource source = new InputSource();
source.setCharacterStream(reader);
Document document = builder.parse(source);
Element parsedElement = document.getDocumentElement();
DocumentBuilderPool.getInstance().releaseBuilder(builder);
return parsedElement;
} catch (IOException ex) {
throw new SAXParseException("IO error occured while parsing: " + ex.getMessage(), null);
}
}
public static Element parse(InputStream stream) throws SAXException {
try {
DocumentBuilder builder = DocumentBuilderPool.getInstance().getBuilder();
Document document = builder.parse(stream);
Element rootElement = document.getDocumentElement();
DocumentBuilderPool.getInstance().releaseBuilder(builder);
return rootElement;
} catch (IOException ex) {
throw new SAXParseException("IO error occured while parsing: " + ex.getMessage(), null);
}
}
public static Element createMainElement(String name) {
return createMainElementNS(null, name);
}
public static Element createMainElementNS(String namespaceURI, String qualifiedName) {
DocumentBuilder documentBuilder = DocumentBuilderPool.getInstance().getBuilder();
Document document = documentBuilder.newDocument();
Element element = null;
if (namespaceURI == null) {
element = document.createElement(qualifiedName);
} else {
element = document.createElementNS(namespaceURI, qualifiedName);
}
document.appendChild(element);
Element createdElement = document.getDocumentElement();
DocumentBuilderPool.getInstance().releaseBuilder(documentBuilder);
return createdElement;
}
}