X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=inline;f=support%2Fsrc%2Forg%2Fwamblee%2Fxml%2FDomUtils.java;fp=support%2Fsrc%2Forg%2Fwamblee%2Fxml%2FDomUtils.java;h=4fdb52e752b6517440ed68538c36c1a86b2a9be9;hb=336db50f7988a7a10533703c1fdcd758e1ed01d3;hp=0000000000000000000000000000000000000000;hpb=5700e961ac3e85ddfa3d95e2d2bcb1dac9ccf854;p=utils diff --git a/support/src/org/wamblee/xml/DomUtils.java b/support/src/org/wamblee/xml/DomUtils.java new file mode 100644 index 00000000..4fdb52e7 --- /dev/null +++ b/support/src/org/wamblee/xml/DomUtils.java @@ -0,0 +1,99 @@ +/* + * Copyright 2005 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * 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 java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +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.Document; +import org.xml.sax.SAXException; + +/** + * Some basic XML utilities for common reoccuring tasks for + * DOM documents. + */ +public final class DomUtils { + + /** + * Disabled default constructor. + * + */ + private DomUtils() { + // Empty. + } + + + /** + * Parses an XML document from a stream. + * @param aIs Input stream. + * @return + */ + public static Document read(InputStream aIs) throws SAXException, ParserConfigurationException, IOException { + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + return builder.parse(aIs); + } + + /** + * Serializes an XML document to a stream. + * @param aDocument Document to serialize. + * @param aOs Output stream. + */ + public static void serialize(Document aDocument, OutputStream aOs) throws IOException { + XMLSerializer serializer = new XMLSerializer(aOs, new OutputFormat()); + serializer.serialize(aDocument); + } + + /** + * Serializes an XML document. + * @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(); + } + + /** + * Converts a dom4j document into a w3c DOM document. + * @param aDocument Document to convert. + * @return W3C DOM document. + */ + public static Document convert(org.dom4j.Document aDocument) throws DocumentException { + return new DOMWriter().write(aDocument); + } + + /** + * Converts a W3C DOM document into a dom4j document. + * @param aDocument Document to convert. + * @return Dom4j document. + */ + public static org.dom4j.Document convert(Document aDocument) { + return new DOMReader().read(aDocument); + } +}