import org.xml.sax.SAXException;
import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
-public class XmlUtil {
+public final class XmlUtil {
public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
+ private static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
+ private static final DocumentBuilderFactory BUILDERFACTORY;
+
+ static {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+ factory.setCoalescing(true);
+ factory.setIgnoringElementContentWhitespace(true);
+ factory.setIgnoringComments(true);
+ BUILDERFACTORY = factory;
+ }
+
+ private XmlUtil() {}
public static Element readXmlToElement(String xmlContent) throws SAXException, IOException {
Document doc = readXmlToDocument(xmlContent);
return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
}
+ // TODO improve exceptions throwing
+ // along with XmlElement
+
public static Document readXmlToDocument(InputStream xmlContent) throws SAXException, IOException {
- DocumentBuilderFactory factory = getDocumentBuilderFactory();
DocumentBuilder dBuilder;
try {
- dBuilder = factory.newDocumentBuilder();
+ dBuilder = BUILDERFACTORY.newDocumentBuilder();
} catch (ParserConfigurationException e) {
- throw new RuntimeException(e);
+ throw new RuntimeException("Failed to parse XML document", e);
}
Document doc = dBuilder.parse(xmlContent);
return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement();
}
- private static final DocumentBuilderFactory getDocumentBuilderFactory() {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(true);
- factory.setCoalescing(true);
- factory.setIgnoringElementContentWhitespace(true);
- factory.setIgnoringComments(true);
- return factory;
- }
-
public static Document newDocument() {
- DocumentBuilderFactory factory = getDocumentBuilderFactory();
try {
- DocumentBuilder builder = factory.newDocumentBuilder();
+ DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder();
Document document = builder.newDocument();
return document;
} catch (ParserConfigurationException e) {
- throw new RuntimeException(e);
+ throw new RuntimeException("Failed to create document", e);
}
}
- public static Element createTextElement(Document document, String name, String content) {
- Element typeElement = document.createElement(name);
- typeElement.appendChild(document.createTextNode(content));
- return typeElement;
- }
-
- public static void addNamespaceAttr(Element root, String namespace) {
- root.setAttribute(XMLNS_ATTRIBUTE_KEY, namespace);
+ public static Element createElement(final Document document, String qName, Optional<String> namespaceURI) {
+ if(namespaceURI.isPresent()) {
+ final Element element = document.createElementNS(namespaceURI.get(), qName);
+ String name = XMLNS_ATTRIBUTE_KEY;
+ if(element.getPrefix() != null) {
+ name += ":" + element.getPrefix();
+ }
+ element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
+ return element;
+ }
+ return document.createElement(qName);
}
- public static void addPrefixedNamespaceAttr(Element root, String prefix, String namespace) {
- root.setAttribute(concat(XMLNS_ATTRIBUTE_KEY, prefix), namespace);
+ public static Element createTextElement(Document document, String qName, String content, Optional<String> namespaceURI) {
+ Element typeElement = createElement(document, qName, namespaceURI);
+ typeElement.appendChild(document.createTextNode(content));
+ return typeElement;
}
- public static Element createPrefixedTextElement(Document document, String key, String prefix, String moduleName) {
- return createTextElement(document, key, concat(prefix, moduleName));
+ public static Element createPrefixedTextElement(Document document, String qName, String prefix, String content, Optional<String> namespace) {
+ return createTextElement(document, qName, createPrefixedValue(prefix, content), namespace);
}
- private static String concat(String prefix, String value) {
+ public static String createPrefixedValue(String prefix, String value) {
return prefix + ":" + value;
}
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration == true ? "no" : "yes");
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(xml);
transformer.transform(source, result);
- String xmlString = result.getWriter().toString();
- return xmlString;
+ return result.getWriter().toString();
} catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
throw new RuntimeException("Unable to serialize xml element " + xml, e);
}