You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2006/10/05 22:08:07 UTC

svn commit: r453346 - in /incubator/tuscany/java: sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/ sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/ sca/services/bindings/binding.axis2/src/main/java/org/apache...

Author: rfeng
Date: Thu Oct  5 13:08:06 2006
New Revision: 453346

URL: http://svn.apache.org/viewvc?view=rev&rev=453346
Log:
Fix the handling of "xmlns" and add more test cases

Added:
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java   (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java   (with props)
    incubator/tuscany/java/sdo/impl/src/test/resources/anytype.xsd   (with props)
Modified:
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/StAXHelper.java
    incubator/tuscany/java/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2Service.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLStreamSerializer.java

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java?view=diff&rev=453346&r1=453345&r2=453346
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/DOMXMLStreamReader.java Thu Oct  5 13:08:06 2006
@@ -605,6 +605,11 @@
         }
         this.rootElementName = rootElement.getLocalName();
         this.rootElementURI = rootElement.getNamespaceURI();
+
+        declaredNamespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace");
+        declaredNamespaceMap.put("xmlns", "http://www.w3.org/2000/xmlns/");
+        declaredNamespaceMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
+        
         populateProperties();
     }
 
@@ -1216,6 +1221,7 @@
             Attr attr = (Attr)nodeMap.item(i);
             if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) {
                 // Skip xmlns:xxx
+                registerNamespace(attr.getLocalName(), attr.getValue());
                 continue;
             }
             QName attrName = new QName(attr.getNamespaceURI(), attr.getLocalName());

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/StAXHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/StAXHelper.java?view=diff&rev=453346&r1=453345&r2=453346
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/StAXHelper.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/databinding/xml/StAXHelper.java Thu Oct  5 13:08:06 2006
@@ -597,9 +597,12 @@
                         // being written previously also. So we need to generate
                         // a prefix
                         // here
-                    } else {
+                    } else if (prefix == null || prefix.equals("")) {
                         prefix = generateUniquePrefix(writer.getNamespaceContext());
                         writer.writeNamespace(prefix, namespaceName);
+                        writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader
+                            .getAttributeValue(i));
+                    } else {
                         writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader
                             .getAttributeValue(i));
                     }

Added: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java?view=auto&rev=453346
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java Thu Oct  5 13:08:06 2006
@@ -0,0 +1,47 @@
+/*
+ * 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.core.databinding.xml;
+
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+/**
+ * Test Case for StAXHelper
+ */
+public class StAXHelperTestCase extends TestCase {
+    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>";
+
+    /**
+     * @see junit.framework.TestCase#setUp()
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void testHelper() throws Exception {
+        XMLStreamReader reader = StAXHelper.createXMLStreamReader(XML);
+        String xml = StAXHelper.save(reader);
+        reader = StAXHelper.createXMLStreamReader(xml);
+    }
+
+}

Propchange: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/databinding/xml/StAXHelperTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2Service.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2Service.java?view=diff&rev=453346&r1=453345&r2=453346
==============================================================================
--- incubator/tuscany/java/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2Service.java (original)
+++ incubator/tuscany/java/sca/services/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/Axis2Service.java Thu Oct  5 13:08:06 2006
@@ -189,15 +189,14 @@
             msg.setBody(args);
             Message resp;
             // dispatch the wire down the chain and get the response
-            //TODO http://issues.apache.org/jira/browse/TUSCANY-777
-             ClassLoader oldtccl = Thread.currentThread().getContextClassLoader();
-             try{
-             Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()) ;   
-             resp = headInterceptor.invoke(msg);
-             
-             }finally{
-                 Thread.currentThread().setContextClassLoader(oldtccl) ;
-             }
+            // TODO http://issues.apache.org/jira/browse/TUSCANY-777
+            ClassLoader oldtccl = Thread.currentThread().getContextClassLoader();
+            try {
+                Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+                resp = headInterceptor.invoke(msg);
+            } finally {
+                Thread.currentThread().setContextClassLoader(oldtccl);
+            }
             Object body = resp.getBody();
             if (resp.isFault()) {
                 throw new InvocationTargetException((Throwable) body);

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java?view=diff&rev=453346&r1=453345&r2=453346
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java Thu Oct  5 13:08:06 2006
@@ -221,6 +221,11 @@
     }
 
     public void populateProperties() {
+        declaredNamespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace");
+        declaredNamespaceMap.put("xmlns", "http://www.w3.org/2000/xmlns/");
+        declaredNamespaceMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
+        
+        
         if (properties != null)
             return;
         if (elementQName == null)

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLStreamSerializer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLStreamSerializer.java?view=diff&rev=453346&r1=453345&r2=453346
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLStreamSerializer.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLStreamSerializer.java Thu Oct  5 13:08:06 2006
@@ -184,7 +184,8 @@
      * @param writer
      * @throws XMLStreamException
      */
-    protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {
+    protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer)
+        throws XMLStreamException {
         int count = reader.getAttributeCount();
         String prefix = null;
         String namespaceName = null;
@@ -192,29 +193,42 @@
         for (int i = 0; i < count; i++) {
             prefix = reader.getAttributePrefix(i);
             namespaceName = reader.getAttributeNamespace(i);
-            if (namespaceName != null)
+            if (namespaceName != null) {
                 writerPrefix = writer.getNamespaceContext().getPrefix(namespaceName);
+            }
 
             if (!"".equals(namespaceName)) {
-                // prefix has already being declared but this particular attrib has a
+                // prefix has already being declared but this particular attrib
+                // has a
                 // no prefix attached. So use the prefix provided by the writer
                 if (writerPrefix != null && (prefix == null || prefix.equals(""))) {
-                    writer.writeAttribute(writerPrefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i));
+                    writer.writeAttribute(writerPrefix,
+                                          namespaceName,
+                                          reader.getAttributeLocalName(i),
+                                          reader.getAttributeValue(i));
 
                     // writer prefix is available but different from the current
-                    // prefix of the attrib. We should be decalring the new prefix
+                    // prefix of the attrib. We should be decalring the new
+                    // prefix
                     // as a namespace declaration
                 } else if (prefix != null && !"".equals(prefix) && !prefix.equals(writerPrefix)) {
                     writer.writeNamespace(prefix, namespaceName);
-                    writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i));
+                    writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader
+                        .getAttributeValue(i));
 
-                    // prefix is null (or empty), but the namespace name is valid! it has not
-                    // being written previously also. So we need to generate a prefix
+                    // prefix is null (or empty), but the namespace name is
+                    // valid! it has not
+                    // being written previously also. So we need to generate a
+                    // prefix
                     // here
-                } else {
+                } else if (prefix == null || prefix.equals("")) {
                     prefix = generateUniquePrefix(writer.getNamespaceContext());
                     writer.writeNamespace(prefix, namespaceName);
-                    writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i));
+                    writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader
+                        .getAttributeValue(i));
+                } else {
+                    writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader
+                        .getAttributeValue(i));
                 }
             } else {
                 // empty namespace is equal to no namespace!

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java?view=auto&rev=453346
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java Thu Oct  5 13:08:06 2006
@@ -0,0 +1,81 @@
+package org.apache.tuscany.sdo.test;
+
+import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.URL;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.helper.XMLStreamHelper;
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.TypeHelper;
+import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XSDHelper;
+
+public class AnyTypeTest extends TestCase {
+    private static TypeHelper typeHelper;
+    private static DataFactory dataFactory;
+    private static XMLStreamHelper streamHelper;
+    private static XSDHelper xsdHelper;
+
+    private static final String TEST_MODEL = "/anytype.xsd";
+    private static final String TEST_NAMESPACE = "http://www.example.com/anytype";
+
+    public void testAnySimpleType() throws Exception {
+        Property property = typeHelper.getOpenContentProperty(TEST_NAMESPACE, "globalElement");
+        Type propertyType = property.getType();
+
+        DataObject dataObject = dataFactory.create(TEST_NAMESPACE, "Person");
+        dataObject.set("firstName", "Fuhwei");
+
+        DataObject rootObject = dataFactory.create(propertyType);
+        rootObject.set("anyTypeElement", dataObject);
+
+        dataObject = dataFactory.create(TEST_NAMESPACE, "Person");
+        dataObject.set("firstName", "Mindy");
+        rootObject.set("personElement", dataObject);
+
+        // XMLStreamHelper.saveObject has a problem to serialize the any type
+        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+        StringWriter writer = new StringWriter();
+        XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
+        streamHelper.saveObject(rootObject, streamWriter);
+        streamWriter.flush();
+        System.out.println(writer.toString());
+
+        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+        StringReader reader = new StringReader(writer.toString());
+        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(reader);
+        XMLDocument doc = streamHelper.load(streamReader);
+        rootObject = doc.getRootObject();
+        DataObject testObject = rootObject.getDataObject("anyTypeElement");
+        System.out.println("anyTypeElement dataobject: " + testObject);
+        testObject = rootObject.getDataObject("personElement");
+        System.out.println("personElement dataobject: " + testObject);
+    }
+
+    protected void setUp() throws Exception {
+        typeHelper = SDOUtil.createTypeHelper();
+        dataFactory = SDOUtil.createDataFactory(typeHelper);
+        streamHelper = SDOUtil.createXMLStreamHelper(typeHelper);
+        xsdHelper = SDOUtil.createXSDHelper(typeHelper);
+
+        // Populate the meta data for the test (Stock Quote) model
+        URL url = getClass().getResource(TEST_MODEL);
+        InputStream inputStream = url.openStream();
+        xsdHelper.define(inputStream, url.toString());
+        inputStream.close();
+    }
+
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sdo/impl/src/test/resources/anytype.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/anytype.xsd?view=auto&rev=453346
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/anytype.xsd (added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/anytype.xsd Thu Oct  5 13:08:06 2006
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ *  Copyright (c) 2005-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.
+ -->
+<xsd:schema xmlns:sdot="http://www.example.com/anytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/anytype"> 
+  
+  <xsd:element name="globalElement">
+  	<xsd:complexType>
+  		<xsd:sequence>
+  			<xsd:element name="anyTypeElement" type="xsd:anyType"/>
+  			<xsd:element name="personElement" type="sdot:Person"/>
+  		</xsd:sequence>
+  	</xsd:complexType>
+  </xsd:element>
+  
+  <xsd:complexType name="Person">
+  	<xsd:sequence>
+  		<xsd:element name="firstName" type="xsd:string"/>
+  	</xsd:sequence>
+  </xsd:complexType>
+</xsd:schema>

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/anytype.xsd
------------------------------------------------------------------------------
    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