You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ke...@apache.org on 2007/02/20 13:53:03 UTC

svn commit: r509538 - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/util/resource/ test/java/org/apache/tuscany/sdo/test/ test/resources/

Author: kelvingoodson
Date: Tue Feb 20 04:53:00 2007
New Revision: 509538

URL: http://svn.apache.org/viewvc?view=rev&rev=509538
Log:
TUSCANY-1091 Applying Yang's patch

Added:
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java   (with props)
    incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd   (with props)
    incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd   (with props)
Modified:
    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/XMLFragmentStreamReader.java

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=509538&r1=509537&r2=509538
==============================================================================
--- 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 Tue Feb 20 04:53:00 2007
@@ -65,7 +65,7 @@
     private QName elementQName;
 
     // we always create a new namespace context
-    private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
+    private NameSpaceContext namespaceContext;
 
     private Map declaredNamespaceMap = new HashMap();
 
@@ -104,16 +104,22 @@
         this.typeHelper = typeHelper == null ? TypeHelper.INSTANCE : typeHelper;
         this.xsdHelper = (xsdHelper != null) ? xsdHelper : ((typeHelper == null) ? XSDHelper.INSTANCE : SDOUtil.createXSDHelper(typeHelper));
         rootElement = this.xsdHelper.getGlobalProperty(rootElmentURI, rootElementName, true);
+        namespaceContext = new NameSpaceContext();
         populateProperties();
     }
 
-    public DataObjectXMLStreamReader(Property rootElement, DataObject dataObject, TypeHelper typeHelper, XSDHelper xsdHelper) {
+    protected DataObjectXMLStreamReader(TypeHelper typeHelper, XSDHelper xsdHelper, Property rootElement, DataObject dataObject) {
         this.typeHelper = typeHelper == null ? TypeHelper.INSTANCE : typeHelper;
         this.xsdHelper = (xsdHelper != null) ? xsdHelper : ((typeHelper == null) ? XSDHelper.INSTANCE : SDOUtil.createXSDHelper(typeHelper));
         this.rootElement = rootElement;
         this.dataObject = dataObject;
         this.rootElementURI = xsdHelper.getNamespaceURI(rootElement);
         this.rootElementName = xsdHelper.getLocalName(rootElement);
+    }
+    
+    public DataObjectXMLStreamReader(Property rootElement, DataObject dataObject, TypeHelper typeHelper, XSDHelper xsdHelper) {
+        this(typeHelper, xsdHelper, rootElement, dataObject);
+        namespaceContext = new NameSpaceContext();
         populateProperties();
     }
     
@@ -123,6 +129,7 @@
         this.rootElementURI = document.getRootElementURI();
         this.typeHelper = typeHelper;
         this.xsdHelper = typeHelper == null ? XSDHelper.INSTANCE : SDOUtil.createXSDHelper(typeHelper);
+        namespaceContext = new NameSpaceContext();
         populateProperties();
     }
 
@@ -135,7 +142,7 @@
         this.properties = properties;
         this.elementQName = elementQName;
         this.attributes = attributes;
-
+        namespaceContext = new NameSpaceContext();
     }
 
     private void addProperty(Property property, Object value, List propertyList) {
@@ -216,7 +223,9 @@
             Map.Entry entry = new NameValuePair(qname, SDOUtil.convertToString(propertyType, value));
             propertyList.add(entry);
         } else {
-            DataObjectXMLStreamReader childReader = new DataObjectXMLStreamReader(property, (DataObject) value, typeHelper, xsdHelper);
+            DataObjectXMLStreamReader childReader = new DataObjectXMLStreamReader(typeHelper, xsdHelper, property, (DataObject) value);
+            childReader.namespaceContext = namespaceContext;
+            childReader.populateProperties();
             childReader.rootElement = property;
             Map.Entry entry = new NameValuePair(qname, childReader);
             propertyList.add(entry);
@@ -302,16 +311,6 @@
     }
 
     /**
-     * add the namespace context
-     */
-
-    public void setParentNamespaceContext(NamespaceContext nsContext) {
-        // register the namespace context passed in to this
-        this.namespaceContext.setParent(nsContext);
-
-    }
-
-    /**
      * we need to split out the calling to the populate namespaces seperately since this needs to be done *after* setting the parent namespace
      * context. We cannot assume it will happen at construction!
      */
@@ -1009,8 +1008,7 @@
             return CHARACTERS;
         } else if (propertyValue == null || propertyValue instanceof String) {
             // strings are handled by the NameValuePairStreamReader
-            childReader = new SimpleElementStreamReader(propertyQName, (String) propertyValue);
-            childReader.setParentNamespaceContext(this.namespaceContext);
+            childReader = new SimpleElementStreamReader(propertyQName, (String) propertyValue, namespaceContext);
             childReader.init();
         } else if (propertyValue instanceof DataObjectXMLStreamReader) {
             // ADBbean has it's own method to get a reader
@@ -1018,7 +1016,6 @@
             // we know for sure that this is an ADB XMLStreamreader.
             // However we need to make sure that it is compatible
             childReader = reader;
-            childReader.setParentNamespaceContext(this.namespaceContext);
             childReader.init();
         } else {
             // all special possiblilities has been tried! Let's treat
@@ -1083,7 +1080,7 @@
 
         private static final QName XSI_NIL_QNAME = new QName("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi");
 
-        private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext(null);
+        private final NameSpaceContext namespaceContext;
 
         private QName name;
 
@@ -1091,11 +1088,12 @@
 
         private int state = START_ELEMENT_STATE;
 
-        public SimpleElementStreamReader(QName name, String value) {
+        public SimpleElementStreamReader(QName name, String value, NameSpaceContext nameSpaces) {
             this.name = name;
             this.value = value;
             if (value == null)
                 state = START_ELEMENT_STATE_WITH_NULL;
+            namespaceContext = nameSpaces;
         }
 
         public Object getProperty(String key) throws IllegalArgumentException {
@@ -1328,10 +1326,6 @@
             return (state == END_ELEMENT_STATE);
         }
 
-        public void setParentNamespaceContext(NamespaceContext nsContext) {
-            this.namespaceContext.setParent(nsContext);
-        }
-
         public void init() {
             // just add the current elements namespace and prefix to the this
             // elements nscontext
@@ -1460,15 +1454,10 @@
 
     }
 
-    protected static class DelegatingNamespaceContext implements NamespaceContext {
-        private NamespaceContext parent;
-
+    protected class NameSpaceContext implements NamespaceContext {
         private Map prefixToNamespaceMapping = new HashMap();
 
-        public DelegatingNamespaceContext(NamespaceContext parent) {
-            super();
-            this.parent = parent;
-
+        public NameSpaceContext() {
             prefixToNamespaceMapping.put("xml", "http://www.w3.org/XML/1998/namespace");
             prefixToNamespaceMapping.put("xmlns", "http://www.w3.org/2000/xmlns/");
             prefixToNamespaceMapping.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
@@ -1481,8 +1470,6 @@
             String ns = (String) prefixToNamespaceMapping.get(prefix);
             if (ns != null)
                 return ns;
-            else if (parent != null)
-                return parent.getNamespaceURI(prefix);
             else
                 return null;
         }
@@ -1496,10 +1483,7 @@
                     return (String) entry.getKey();
                 }
             }
-            if (parent != null)
-                return parent.getPrefix(nsURI);
-            else
-                return null;
+            return null;
         }
 
         public Iterator getPrefixes(String nsURI) {
@@ -1510,11 +1494,6 @@
                     prefixList.add(entry.getKey());
                 }
             }
-            if (parent != null) {
-                for (Iterator i = parent.getPrefixes(nsURI); i.hasNext();) {
-                    prefixList.add(i.next());
-                }
-            }
             return prefixList.iterator();
         }
 
@@ -1530,17 +1509,15 @@
                 prefix = "p" + (counter++);
             if (prefix == null)
                 prefix = "";
-            if (nsURI != null)
+            if (nsURI != null) {
                 prefixToNamespaceMapping.put(prefix, nsURI);
+                declaredNamespaceMap.put(prefix, nsURI);
+            }
             return new QName(nsURI, name, prefix);
         }
 
         public void removeMapping(String prefix) {
             prefixToNamespaceMapping.remove(prefix);
-        }
-
-        public void setParent(NamespaceContext parent) {
-            this.parent = parent;
         }
     }
 

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLFragmentStreamReader.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLFragmentStreamReader.java?view=diff&rev=509538&r1=509537&r2=509538
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLFragmentStreamReader.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/XMLFragmentStreamReader.java Tue Feb 20 04:53:00 2007
@@ -19,7 +19,6 @@
  */
 package org.apache.tuscany.sdo.util.resource;
 
-import javax.xml.namespace.NamespaceContext;
 import javax.xml.stream.XMLStreamReader;
 
 interface XMLFragmentStreamReader extends XMLStreamReader {
@@ -35,11 +34,6 @@
      * @return
      */
      boolean isEndOfFragment();
-
-    /**
-     * add the parent namespace context to this parser
-     */
-     void setParentNamespaceContext(NamespaceContext nsContext);
 
     /**
      * Initiate the parser - this will do whatever the needed

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java?view=auto&rev=509538
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java Tue Feb 20 04:53:00 2007
@@ -0,0 +1,91 @@
+/**
+ *
+ *  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.sdo.test;
+
+
+import java.io.*;
+import java.math.BigDecimal;
+import java.net.URL;
+
+import javax.xml.stream.*;
+
+import org.apache.tuscany.sdo.helper.XMLStreamHelper;
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import junit.framework.TestCase;
+
+import commonj.sdo.*;
+import commonj.sdo.helper.*;
+
+
+public class XMLStreamHelperPerformanceTestCase extends TestCase {
+    private final String TEST_MODEL = "/XMLStreamHelper.xsd";
+    private final String TEST_NAMESPACE = "http://www.example.com/simple";
+
+    HelperContext hc;
+    
+    /**
+     * Simple Dynamic SDO 2 test.
+     */
+    public void testDynamic() throws Exception {
+        TypeHelper typeHelper = hc.getTypeHelper();
+        Type quoteType = typeHelper.getType(TEST_NAMESPACE, "Quote");
+        DataObject quote = hc.getDataFactory().create(quoteType);
+
+        quote.setString("symbol", "fbnt");
+        quote.setString("companyName", "FlyByNightTechnology");
+        quote.setBigDecimal("price", new BigDecimal("1000.0"));
+        quote.setBigDecimal("open1", new BigDecimal("1000.0"));
+        quote.setBigDecimal("high", new BigDecimal("1000.0"));
+        quote.setBigDecimal("low", new BigDecimal("1000.0"));
+        quote.setDouble("volume", 1000);
+        quote.setDouble("change1", 1000);
+
+        DataObject child = quote.createDataObject("quotes");
+        child.setBigDecimal("price", new BigDecimal("2000.0"));
+        
+        DataObject quote3 = quote.createDataObject("quotes3");
+        quote3.setString("symbol3", "IBM");
+        quote3.setString("company3", "IBM Corp.");
+        
+        XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(typeHelper);
+        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+        StringWriter writer = new StringWriter();
+        XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
+        
+        XMLDocument doc = hc.getXMLHelper().createDocument(quote, TEST_NAMESPACE, "stockQuote");
+        streamHelper.save(doc, streamWriter);
+        streamWriter.flush();
+        assertEquals("<p0:stockQuote xmlns:p0=\"http://www.example.com/simple\" xmlns:p1=\"http://www.example.com/simple3\"><p0:symbol>fbnt</p0:symbol><p0:companyName>FlyByNightTechnology</p0:companyName><p0:price>1000.0</p0:price><p0:open1>1000.0</p0:open1><p0:high>1000.0</p0:high><p0:low>1000.0</p0:low><p0:volume>1000.0</p0:volume><p0:change1>1000.0</p0:change1><p0:quotes><p0:price>2000.0</p0:price></p0:quotes><p0:quotes3><p1:symbol3>IBM</p1:symbol3><p1:company3>IBM Corp.</p1:company3></p0:quotes3></p0:stockQuote>",writer.toString());
+    }
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        hc = SDOUtil.createHelperContext();
+
+        // Populate the meta data for the test (Stock Quote) model
+        URL url = getClass().getResource(TEST_MODEL);
+        InputStream inputStream = url.openStream();
+        hc.getXSDHelper().define(inputStream, url.toString());
+        inputStream.close();
+
+    }
+
+}

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

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

Added: incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd?view=auto&rev=509538
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd (added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd Tue Feb 20 04:53:00 2007
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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
+ -->
+<xsd:schema xmlns:simple3="http://www.example.com/simple3" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xshtd="http://www.example.com/simple" elementFormDefault="qualified" targetNamespace="http://www.example.com/simple">
+  
+  <xsd:import namespace="http://www.example.com/simple3" schemaLocation="simple3.xsd"/>
+  
+   <xsd:element name="stockQuote" type="xshtd:Quote"/>
+
+   <xsd:complexType name="Quote">
+       <xsd:sequence>
+          <xsd:element name="symbol" type="xsd:string"/>
+          <xsd:element name="companyName" type="xsd:string"/>
+          <xsd:element name="price" type="xsd:decimal"/>
+          <xsd:element name="open1" type="xsd:decimal"/>
+          <xsd:element name="high" type="xsd:decimal"/>
+          <xsd:element name="low" type="xsd:decimal"/>
+          <xsd:element name="volume" type="xsd:double"/>
+          <xsd:element name="change1" type="xsd:double"/>
+          <xsd:element maxOccurs="unbounded" minOccurs="0" name="quotes" type="xshtd:Quote"/>
+          <xsd:element maxOccurs="unbounded" minOccurs="0" name="quotes3" type="simple3:Quote3"/>
+       </xsd:sequence>
+   </xsd:complexType>
+
+</xsd:schema>

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/XMLStreamHelper.xsd
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd?view=auto&rev=509538
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd (added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd Tue Feb 20 04:53:00 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.    
+ -->
+<xsd:schema 
+  targetNamespace="http://www.example.com/simple3"
+  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
+  xmlns:simple3="http://www.example.com/simple3"
+elementFormDefault="qualified">
+<!--   <xsd:element name="stockQuote" type="simple3:Quote3"/>-->
+
+   <xsd:complexType name="Quote3">
+       <xsd:sequence>
+          <xsd:element name="symbol3" type="xsd:string"/>
+          <xsd:element name="company3" type="xsd:string"/>
+       </xsd:sequence>
+   </xsd:complexType>
+
+</xsd:schema>

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/simple3.xsd
------------------------------------------------------------------------------
    svn:mime-type = text/xml



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org