diff options
Diffstat (limited to '')
4 files changed, 196 insertions, 8 deletions
diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java index 89682badc8..923d290f36 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java @@ -18,10 +18,21 @@ */ package org.apache.tuscany.sca.common.xml.dom; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; + import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamResult; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; @@ -30,6 +41,10 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; +import org.xml.sax.ContentHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; /** * Helper for DOM @@ -38,11 +53,13 @@ import org.w3c.dom.Node; */ public final class DOMHelper { private DocumentBuilderFactory documentBuilderFactory; + private TransformerFactory transformerFactory; public DOMHelper(ExtensionPointRegistry registry) { FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); documentBuilderFactory = factories.getFactory(DocumentBuilderFactory.class); documentBuilderFactory.setNamespaceAware(true); + transformerFactory = factories.getFactory(TransformerFactory.class); } public Document newDocument() { @@ -58,6 +75,51 @@ public final class DOMHelper { } } + public Document load(String xmlString) throws IOException, SAXException { + DocumentBuilder builder = newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(xmlString)); + return builder.parse(is); + } + + public NodeContentHandler createContentHandler(Node root) { + if (root == null) { + root = newDocument(); + } + return new SAX2DOMAdapter(root); + } + + public String saveAsString(Node node) { + Transformer transformer = newTransformer(); + StringWriter sw = new StringWriter(); + StreamResult result = new StreamResult(sw); + try { + transformer.transform(new DOMSource(node), result); + } catch (TransformerException e) { + throw new IllegalArgumentException(e); + } + return result.getWriter().toString(); + } + + private Transformer newTransformer() { + Transformer transformer = null; + try { + transformer = transformerFactory.newTransformer(); + } catch (TransformerConfigurationException e) { + throw new IllegalArgumentException(e); + } + return transformer; + } + + public void saveAsSAX(Node node, ContentHandler contentHandler) { + Transformer transformer = newTransformer(); + SAXResult result = new SAXResult(contentHandler); + try { + transformer.transform(new DOMSource(node), result); + } catch (TransformerException e) { + throw new IllegalArgumentException(e); + } + } + public static QName getQName(Node node) { String ns = node.getNamespaceURI(); String prefix = node.getPrefix(); @@ -116,4 +178,8 @@ public final class DOMHelper { return doc; } + public static interface NodeContentHandler extends ContentHandler, LexicalHandler { + Node getNode(); + } + } diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java index 78449298dd..861107e6a4 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java @@ -20,8 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Stack; -import javax.xml.parsers.ParserConfigurationException; - import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.w3c.dom.Comment; @@ -31,17 +29,15 @@ import org.w3c.dom.Node; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; -import org.xml.sax.ext.LexicalHandler; /** * SAX2DOM adapter * * @version $Rev$ $Date$ */ -public class SAX2DOMAdapter implements ContentHandler, LexicalHandler { +public class SAX2DOMAdapter implements DOMHelper.NodeContentHandler { public static final String EMPTYSTRING = ""; public static final String XML_PREFIX = "xml"; public static final String XMLNS_PREFIX = "xmlns"; @@ -67,7 +63,7 @@ public class SAX2DOMAdapter implements ContentHandler, LexicalHandler { this.root = document; } - public SAX2DOMAdapter(Node root, Node nextSibling) throws ParserConfigurationException { + public SAX2DOMAdapter(Node root, Node nextSibling) { this.root = root; if (root instanceof Document) { this.document = (Document)root; @@ -78,11 +74,11 @@ public class SAX2DOMAdapter implements ContentHandler, LexicalHandler { this.nextSibling = nextSibling; } - public SAX2DOMAdapter(Node root) throws ParserConfigurationException { + public SAX2DOMAdapter(Node root) { this(root, null); } - public Node getDOM() { + public Node getNode() { return root; } diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java new file mode 100644 index 0000000000..55635a258c --- /dev/null +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.common.xml.sax; + +import java.io.IOException; +import java.io.StringReader; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.xml.sax.ContentHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; + +/** + * + */ +public class SAXHelper { + private SAXParserFactory saxParserFactory; + + public SAXHelper(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + saxParserFactory = factories.getFactory(SAXParserFactory.class); + saxParserFactory.setNamespaceAware(true); + } + + public SAXParser newSAXParser() throws SAXException { + try { + return saxParserFactory.newSAXParser(); + } catch (ParserConfigurationException e) { + throw new IllegalArgumentException(e); + } + } + + public XMLReader newXMLReader() throws SAXException { + return newSAXParser().getXMLReader(); + } + + public void parse(String xmlString, ContentHandler handler) throws SAXException, IOException { + XMLReader reader = newXMLReader(); + reader.setContentHandler(handler); + reader.parse(new InputSource(new StringReader(xmlString))); + } +} diff --git a/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java b/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java new file mode 100644 index 0000000000..5b4cb701d8 --- /dev/null +++ b/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.common.xml.dom; + +import static org.junit.Assert.assertNotNull; + +import org.apache.tuscany.sca.common.xml.sax.SAXHelper; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.custommonkey.xmlunit.XMLAssert; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.ContentHandler; + +/** + * Test Case for StAXHelper + * + * @version $Rev$ $Date$ + */ +public class DOMHelperTestCase { + private static final String XML = + "<a:foo xmlns:a='http://a' name='foo'><bar name='bar'>" + "<doo a:name='doo' xmlns:a='http://doo'/>" + + "</bar></a:foo>"; + + @Test + public void testHelper() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + DOMHelper helper = new DOMHelper(registry); + Document document = helper.load(XML); + String xml = helper.saveAsString(document); + XMLAssert.assertXMLEqual(XML, xml); + + Document root = helper.newDocument(); + ContentHandler handler = helper.createContentHandler(root); + + SAXHelper saxHelper = new SAXHelper(registry); + saxHelper.parse(XML, handler); + + assertNotNull(root.getFirstChild()); + xml = helper.saveAsString(root); + XMLAssert.assertXMLEqual(XML, xml); + } + +} |