You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2006/08/02 01:00:52 UTC
svn commit: r427782 - in /incubator/tuscany/java/sca/core/src:
main/java/org/apache/tuscany/core/loader/StAXUtil.java
test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java
Author: jboynes
Date: Tue Aug 1 16:00:52 2006
New Revision: 427782
URL: http://svn.apache.org/viewvc?rev=427782&view=rev
Log:
add a mechanism for creating a DOM from a StAX stream
this will be used by the property value handlers to create something that an xpath expression
can be evaluated against
Added:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java (with props)
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java
Modified: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java?rev=427782&r1=427781&r2=427782&view=diff
==============================================================================
--- incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java (original)
+++ incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java Tue Aug 1 16:00:52 2006
@@ -19,6 +19,16 @@
import java.util.HashMap;
import java.util.Map;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilder;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
import org.apache.tuscany.spi.model.InteractionScope;
import org.apache.tuscany.spi.model.Multiplicity;
@@ -66,4 +76,62 @@
}
}
+ public static Document createPropertyValue(XMLStreamReader reader,
+ QName type,
+ DocumentBuilder builder) throws XMLStreamException {
+ Document doc = builder.newDocument();
+
+ // root element has no namespace and local name "value"
+ Element root = doc.createElementNS(null, "value");
+ loadPropertyValue(reader, root);
+
+ return doc;
+ }
+
+ /**
+ * Load a property value specification from an StAX stream into a DOM Document.
+ * Only elements, text and attributes are processed; all comments and other whitespace are ignored.
+ *
+ * @param reader the stream to read from
+ * @param root the DOM node to load
+ */
+ public static void loadPropertyValue(XMLStreamReader reader, Node root) throws XMLStreamException {
+ Document document = root.getOwnerDocument();
+ Node current = root;
+ while (true) {
+ switch (reader.next()) {
+ case XMLStreamConstants.START_ELEMENT:
+ QName name = reader.getName();
+ Element child = document.createElementNS(name.getNamespaceURI(), name.getLocalPart());
+
+ // add the attributes for this element
+ int count = reader.getAttributeCount();
+ for (int i = 0; i < count; i++) {
+ String ns = reader.getAttributeNamespace(i);
+ String localPart = reader.getAttributeLocalName(i);
+ String value = reader.getAttributeValue(i);
+ child.setAttributeNS(ns, localPart, value);
+ }
+
+ // push the new element and make it the current one
+ current.appendChild(child);
+ current = child;
+ break;
+ case XMLStreamConstants.CDATA:
+ current.appendChild(document.createCDATASection(reader.getText()));
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ current.appendChild(document.createTextNode(reader.getText()));
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ // if we are back at the root then we are done
+ if (current == root) {
+ return;
+ }
+
+ // pop the element off the stack
+ current = current.getParentNode();
+ }
+ }
+ }
}
Added: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java?rev=427782&view=auto
==============================================================================
--- incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java (added)
+++ incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java Tue Aug 1 16:00:52 2006
@@ -0,0 +1,74 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ * 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.apache.tuscany.core.loader;
+
+import java.io.StringReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+
+import junit.framework.TestCase;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class PropertyParsingTestCase extends TestCase {
+ private XMLInputFactory xmlFactory;
+ private DocumentBuilder docBuilder;
+ private Element root;
+
+ public void testComplexProperty() throws XMLStreamException {
+ String xml = "<property xmlns:foo='http://foo.com'>"
+ + "<foo:a>aValue</foo:a>"
+ + "<foo:b>InterestingURI</foo:b>"
+ + "</property>";
+
+ XMLStreamReader reader = getReader(xml);
+ StAXUtil.loadPropertyValue(reader, root);
+ NodeList childNodes = root.getChildNodes();
+ assertEquals(2, childNodes.getLength());
+
+ Element e = (Element) childNodes.item(0);
+ assertEquals("http://foo.com", e.getNamespaceURI());
+ assertEquals("a", e.getLocalName());
+ assertEquals("aValue", e.getTextContent());
+ e = (Element) childNodes.item(1);
+ assertEquals("http://foo.com", e.getNamespaceURI());
+ assertEquals("b", e.getLocalName());
+ assertEquals("InterestingURI", e.getTextContent());
+ }
+
+ public XMLStreamReader getReader(String xml) throws XMLStreamException {
+ XMLStreamReader reader = xmlFactory.createXMLStreamReader(new StringReader(xml));
+ reader.next();
+ return reader;
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ xmlFactory = XMLInputFactory.newInstance();
+ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+ docBuilder = docFactory.newDocumentBuilder();
+ Document doc = docBuilder.newDocument();
+ root = doc.createElement("value");
+ }
+}
Propchange: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/PropertyParsingTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org