X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxml%2FDomUtils.java;h=8c8128c42f67360bcb9695796d4f852087850d1d;hb=e72743b6a9fac5a99b842f92b1687fba65ef3210;hp=3825bacda36adbb32ab3c00d5ff3f2185b5f9afe;hpb=32a62ca2c752e33a7873ac868a7a1f289caedcd4;p=utils diff --git a/support/general/src/main/java/org/wamblee/xml/DomUtils.java b/support/general/src/main/java/org/wamblee/xml/DomUtils.java index 3825bacd..8c8128c4 100644 --- a/support/general/src/main/java/org/wamblee/xml/DomUtils.java +++ b/support/general/src/main/java/org/wamblee/xml/DomUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2005 the original author or authors. + * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,19 +12,38 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ - + */ package org.wamblee.xml; +import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; + +import org.dom4j.DocumentException; + +import org.dom4j.io.DOMReader; +import org.dom4j.io.DOMWriter; + +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import org.xml.sax.SAXException; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; + import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; @@ -34,29 +53,13 @@ import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.xml.serialize.OutputFormat; -import org.apache.xml.serialize.XMLSerializer; -import org.dom4j.DocumentException; -import org.dom4j.io.DOMReader; -import org.dom4j.io.DOMWriter; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - /** * Some basic XML utilities for common reoccuring tasks for DOM documents. - * + * * @author Erik Brakkee */ public final class DomUtils { - - private static final Log LOG = LogFactory.getLog(DomUtils.class); + private static final Logger LOG = Logger.getLogger(DomUtils.class.getName()); /** * Disabled default constructor. @@ -71,10 +74,13 @@ public final class DomUtils { * * @param aDocument * document. + * * @return + * */ public static Document read(String aDocument) throws XMLException { ByteArrayInputStream is = new ByteArrayInputStream(aDocument.getBytes()); + return read(is); } @@ -83,14 +89,17 @@ public final class DomUtils { * * @param aIs * Input stream. + * * @return + * */ public static Document read(InputStream aIs) throws XMLException { try { DocumentBuilder builder = DocumentBuilderFactory.newInstance() - .newDocumentBuilder(); + .newDocumentBuilder(); + return builder.parse(aIs); - } catch (SAXException e) { + } catch (SAXException e) { throw new XMLException(e.getMessage(), e); } catch (IOException e) { throw new XMLException(e.getMessage(), e); @@ -100,7 +109,7 @@ public final class DomUtils { try { aIs.close(); } catch (Exception e) { - LOG.warn("Error closing XML file", e); + LOG.log(Level.WARNING, "Error closing XML file", e); } } } @@ -112,17 +121,19 @@ public final class DomUtils { * Input stream. * @param aSchema * Schema. + * * @return Parsed and validated document. + * */ public static Document readAndValidate(InputStream aIs, InputStream aSchema) - throws XMLException { - + throws XMLException { try { final Schema schema = SchemaFactory.newInstance( - XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( - new StreamSource(aSchema)); + XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( + new StreamSource(aSchema)); - final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + final DocumentBuilderFactory factory = DocumentBuilderFactory + .newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); factory.setSchema(schema); @@ -138,15 +149,15 @@ public final class DomUtils { try { aSchema.close(); } catch (Exception e) { - LOG.warn("Error closing schema", e); + LOG.log(Level.WARNING, "Error closing schema", e); } + try { aIs.close(); } catch (Exception e) { - LOG.warn("Error closing XML file", e); + LOG.log(Level.WARNING, "Error closing XML file", e); } } - } /** @@ -156,9 +167,10 @@ public final class DomUtils { * Document to serialize. * @param aOs * Output stream. + * */ public static void serialize(Document aDocument, OutputStream aOs) - throws IOException { + throws IOException { XMLSerializer serializer = new XMLSerializer(aOs, new OutputFormat()); serializer.serialize(aDocument); } @@ -168,11 +180,14 @@ public final class DomUtils { * * @param aDocument * Document to serialize. + * * @return Serialized document. + * */ public static String serialize(Document aDocument) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); serialize(aDocument, os); + return os.toString(); } @@ -181,10 +196,12 @@ public final class DomUtils { * * @param aDocument * Document to convert. + * * @return W3C DOM document. + * */ public static Document convert(org.dom4j.Document aDocument) - throws DocumentException { + throws DocumentException { return new DOMWriter().write(aDocument); } @@ -193,6 +210,7 @@ public final class DomUtils { * * @param aDocument * Document to convert. + * * @return Dom4j document. */ public static org.dom4j.Document convert(Document aDocument) { @@ -210,8 +228,10 @@ public final class DomUtils { */ public static void removeDuplicateAttributes(Node aNode) { NodeList list = aNode.getChildNodes(); + for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); + if (node instanceof Element) { removeDuplicateAttributes((Element) node); removeDuplicateAttributes(node); @@ -229,19 +249,24 @@ public final class DomUtils { NamedNodeMap attributes = aElement.getAttributes(); Map uniqueAttributes = new TreeMap(); List attlist = new ArrayList(); + for (int i = 0; i < attributes.getLength(); i++) { Attr attribute = (Attr) attributes.item(i); + if (uniqueAttributes.containsKey(attribute.getNodeName())) { - LOG.info("Detected duplicate attribute (will be removed)'" - + attribute.getNodeName() + "'"); + LOG.info("Detected duplicate attribute (will be removed)'" + + attribute.getNodeName() + "'"); } + uniqueAttributes.put(attribute.getNodeName(), attribute); attlist.add(attribute); } + // Remove all attributes from the element. for (Attr att : attlist) { aElement.removeAttributeNode(att); } + // Add the unique attributes back to the element. for (Attr att : uniqueAttributes.values()) { aElement.setAttributeNode(att);