tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r801843 - in /tuscany/java/sca/modules/common-xml/src: main/java/org/apache/tuscany/sca/common/xml/dom/ main/java/org/apache/tuscany/sca/common/xml/sax/ test/java/org/apache/tuscany/sca/common/xml/dom/
Date Thu, 06 Aug 2009 23:34:29 GMT
Author: rfeng
Date: Thu Aug  6 23:34:29 2009
New Revision: 801843

URL: http://svn.apache.org/viewvc?rev=801843&view=rev
Log:
Add SAX utilities

Added:
    tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/
    tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java
  (with props)
    tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/
    tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java
  (with props)
Modified:
    tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java
    tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java

Modified: tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java?rev=801843&r1=801842&r2=801843&view=diff
==============================================================================
--- tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java
(original)
+++ tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java
Thu Aug  6 23:34:29 2009
@@ -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.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 @@
  */
 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 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 @@
         return doc;
     }
 
+    public static interface NodeContentHandler extends ContentHandler, LexicalHandler {
+        Node getNode();
+    }
+
 }

Modified: tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java?rev=801843&r1=801842&r2=801843&view=diff
==============================================================================
--- tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java
(original)
+++ tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/SAX2DOMAdapter.java
Thu Aug  6 23:34:29 2009
@@ -20,8 +20,6 @@
 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.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 @@
         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 @@
         this.nextSibling = nextSibling;
     }
 
-    public SAX2DOMAdapter(Node root) throws ParserConfigurationException {
+    public SAX2DOMAdapter(Node root) {
         this(root, null);
     }
 
-    public Node getDOM() {
+    public Node getNode() {
         return root;
     }
 

Added: tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java?rev=801843&view=auto
==============================================================================
--- tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java
(added)
+++ tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java
Thu Aug  6 23:34:29 2009
@@ -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)));
+    }
+}

Propchange: tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/sax/SAXHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java?rev=801843&view=auto
==============================================================================
--- tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java
(added)
+++ tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java
Thu Aug  6 23:34:29 2009
@@ -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);
+    }
+
+}

Propchange: tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message