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/19 12:42:22 UTC

svn commit: r509168 - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/ main/java/org/apache/tuscany/sdo/helper/ main/java/org/apache/tuscany/sdo/util/ main/java/org/apache/tuscany/sdo/util/resource/ test/java/org/apache/tuscan...

Author: kelvingoodson
Date: Mon Feb 19 03:42:17 2007
New Revision: 509168

URL: http://svn.apache.org/viewvc?view=rev&rev=509168
Log:
TUSCANY-1088 committed Yang's patch -- Default behaviour is now to tolerate bad XML unless disabled by provision of options using the SDOUtil.XML_LOAD_LaxForm (or by the system property XML.load.form.lax -- not tested in this patch)

Added:
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java   (with props)
Modified:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOExtendedMetaData.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOExtendedMetaData.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOExtendedMetaData.java?view=diff&rev=509168&r1=509167&r2=509168
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOExtendedMetaData.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOExtendedMetaData.java Mon Feb 19 03:42:17 2007
@@ -35,4 +35,5 @@
   
   void setAliasNames(EModelElement eModelElement, String aliasNames);
 
+  void setFeatureNamespaceMatchingLax(boolean featureNamespaceMatchingLax);
 }

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java?view=diff&rev=509168&r1=509167&r2=509168
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/SDOExtendedMetaDataImpl.java Mon Feb 19 03:42:17 2007
@@ -26,8 +26,10 @@
 
 import org.apache.tuscany.sdo.SDOExtendedMetaData;
 import org.eclipse.emf.ecore.EAnnotation;
+import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.EModelElement;
 import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EStructuralFeature;
 import org.eclipse.emf.ecore.EPackage.Registry;
 import org.eclipse.emf.ecore.util.BasicExtendedMetaData;
 
@@ -108,5 +110,170 @@
   {
     return registry;
   }
+
+  boolean featureNamespaceMatchingLax = true;
+
+  public void setFeatureNamespaceMatchingLax(boolean b) {
+    featureNamespaceMatchingLax = b;
+  }
+
+  protected boolean isFeatureNamespaceMatchingLax() {
+    return featureNamespaceMatchingLax;
+  }
+
+  /* getLocalAttribute & getLocalElement are
+   * TEMPORARILY COPIED FROM BASE CLASS - DO NOT EDIT - WILL BE REMOVED WHEN WE MOVE TO EMF 2.3
+   */
   
+  public EStructuralFeature getLocalAttribute(EClass eClass, String namespace, String name)
+  {
+    EStructuralFeature result = null;
+    if (isFeatureKindSpecific())
+    {
+      List allAttributes = getAllAttributes(eClass);
+      for (int i = 0, size = allAttributes.size(); i < size; ++i)
+      {
+        EStructuralFeature eStructuralFeature = (EStructuralFeature) allAttributes.get(i);
+        if (name.equals(getName(eStructuralFeature)))
+        {
+          String featureNamespace = getNamespace(eStructuralFeature);
+          if (namespace == null) 
+          {
+            if (featureNamespace == null)
+            {
+              return eStructuralFeature;
+            }
+            else if (result == null)
+            {
+              result = eStructuralFeature;
+            }
+          }
+          else if (namespace.equals(featureNamespace))
+          {
+            return eStructuralFeature;
+          }
+          else if (featureNamespace == null && result == null)
+          {
+            result = eStructuralFeature;
+          }
+        }
+      }
+    }
+    else
+    {
+      for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i)
+      {
+        EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i);
+        switch (getFeatureKind(eStructuralFeature))
+        {
+          case UNSPECIFIED_FEATURE:
+          case ATTRIBUTE_FEATURE:
+          {
+            if (name.equals(getName(eStructuralFeature)))
+            {
+              String featureNamespace = getNamespace(eStructuralFeature);
+              if (namespace == null) 
+              {
+                if (featureNamespace == null)
+                {
+                  return eStructuralFeature;
+                }
+                else if (result == null)
+                {
+                  result = eStructuralFeature;
+                }
+              }
+              else if (namespace.equals(featureNamespace))
+              {
+                return eStructuralFeature;
+              }
+              else if (featureNamespace == null && result == null)
+              {
+                result = eStructuralFeature;
+              }
+            }
+            break;
+          }
+        }
+      }
+    }
+
+    return isFeatureNamespaceMatchingLax() ? result : null;
+  }
+
+  protected EStructuralFeature getLocalElement(EClass eClass, String namespace, String name)
+  {
+    EStructuralFeature result = null;
+    if (isFeatureKindSpecific())
+    {
+      List allElements = getAllElements(eClass);
+      for (int i = 0, size = allElements.size(); i < size; ++i)
+      {
+        EStructuralFeature eStructuralFeature = (EStructuralFeature) allElements.get(i);
+        if (name.equals(getName(eStructuralFeature)))
+        {
+          String featureNamespace = getNamespace(eStructuralFeature);
+          if (namespace == null) 
+          {
+            if (featureNamespace == null)
+            {
+              return eStructuralFeature;
+            }
+            else if (result == null)
+            {
+              result = eStructuralFeature;
+            }
+          }
+          else if (namespace.equals(featureNamespace))
+          {
+            return eStructuralFeature;
+          }
+          else if (featureNamespace == null && result == null)
+          {
+            result = eStructuralFeature;
+          }
+        }
+      }
+    }
+    else
+    {
+      for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i)
+      {
+        EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i);
+        switch (getFeatureKind(eStructuralFeature))
+        {
+          case UNSPECIFIED_FEATURE:
+          case ELEMENT_FEATURE:
+          {
+            if (name.equals(getName(eStructuralFeature)))
+            {
+              String featureNamespace = getNamespace(eStructuralFeature);
+              if (namespace == null) 
+              {
+                if (featureNamespace == null)
+                {
+                  return eStructuralFeature;
+                }
+                else if (result == null)
+                {
+                  result = eStructuralFeature;
+                }
+              }
+              else if (namespace.equals(featureNamespace))
+              {
+                return eStructuralFeature;
+              }
+              else if (featureNamespace == null && result == null)
+              {
+                result = eStructuralFeature;
+              }
+            }
+            break;
+          }
+        }
+      }
+    }
+
+    return isFeatureNamespaceMatchingLax() ? result : null;
+  }
 }

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java?view=diff&rev=509168&r1=509167&r2=509168
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java Mon Feb 19 03:42:17 2007
@@ -122,7 +122,11 @@
   /**
    * Boolean to process the schemaLocation/noNamespaceSchemaLocation attributes occurring in the instance document to {@link XSDHelper#define convert XSD(s) to Types} 
    */
-  XML_LOAD_SCHEMA = "ProcessSchemaLocations";
+  XML_LOAD_SCHEMA = "ProcessSchemaLocations",
+  /**
+   * To tolerate malformed elements and attributes (default unless set by System property XML.load.form.lax). 0 not to.  
+   */
+  XML_LOAD_LaxForm = "load malform";
 
   /**
    * Create a DataObject wrapper for an instance of the specified dataType.

Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java?view=diff&rev=509168&r1=509167&r2=509168
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java Mon Feb 19 03:42:17 2007
@@ -41,7 +41,9 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.tuscany.sdo.SDOExtendedMetaData;
 import org.apache.tuscany.sdo.helper.HelperContextImpl;
+import org.apache.tuscany.sdo.helper.SDOExtendedMetaDataImpl;
 import org.apache.tuscany.sdo.helper.XMLStreamHelper;
 import org.apache.tuscany.sdo.helper.XSDHelperImpl;
 import org.apache.tuscany.sdo.util.SDOUtil;
@@ -390,39 +392,76 @@
         return new SDOXMLLoadImpl(createXMLHelper());
     }
 
+    static protected int loadLaxForm;
+    static {
+        int defaultLaxForm = 0x4242;
+        String property = System.getProperty("XML.load.form.lax");
+        if (property == null)
+            loadLaxForm = defaultLaxForm;
+        else
+            try {
+                loadLaxForm = Integer.decode(property).intValue();
+            } catch (NumberFormatException eNumberFormat) {
+                loadLaxForm = defaultLaxForm;
+            }
+    }
+    
     public void doLoad(InputSource inputSource, Map options) throws IOException {
-        if (options != null && Boolean.TRUE.equals(options.get(SDOUtil.XML_LOAD_SCHEMA))) {
-            XMLOptions option = (XMLOptions) options.get(OPTION_XML_OPTIONS);
-            if (option == null) {
-                option = new XMLOptionsImpl();
-                options.put(OPTION_XML_OPTIONS, option);
-            }
-            option.setProcessSchemaLocations(true);
-            Object emd = options.get(OPTION_EXTENDED_META_DATA);
-            if (emd == null)
-                emd = getDefaultLoadOptions().get(OPTION_EXTENDED_META_DATA);
-            ExtendedMetaData extendedMetaData;
-            final XSDHelper xsdHelper;
-            if (emd == null) {
-                extendedMetaData = ExtendedMetaData.INSTANCE;
-                xsdHelper = XSDHelper.INSTANCE;
-            } else {
-                extendedMetaData = (ExtendedMetaData) emd;
-                xsdHelper = new XSDHelperImpl(extendedMetaData, null);
-            }
-            option.setEcoreBuilder(new DefaultEcoreBuilder(extendedMetaData) {
-                public Collection generate(Map targetNamespaceToURI) throws IOException {
-                    for (Iterator iterator = targetNamespaceToURI.values().iterator(); iterator.hasNext();) {
-                        String uri = iterator.next().toString();
-                        xsdHelper.define(uri.indexOf(":/") == -1 ? Thread.currentThread().getContextClassLoader().getResourceAsStream(uri) : new URL(
-                                uri).openStream(), uri);
-                    }
-                    return null; // XMLHandler#processSchemaLocations doesn't take the result
+        if (options != null) {
+            /*
+             * Tolerates element/attribute malform unless indicated not to
+             */
+            Object option = options.get(SDOUtil.XML_LOAD_LaxForm);
+            int tolerance = option == null ? loadLaxForm : ((Number) option).intValue();
+            option = options.get(OPTION_EXTENDED_META_DATA);
+            if (tolerance == 0) {
+                if (option instanceof SDOExtendedMetaData)
+                    ((SDOExtendedMetaData) option).setFeatureNamespaceMatchingLax(false);
+            } else if (option instanceof SDOExtendedMetaData)
+                ((SDOExtendedMetaData) option).setFeatureNamespaceMatchingLax(true);
+            else
+                options.put(OPTION_EXTENDED_META_DATA, option = new SDOExtendedMetaDataImpl()); // TODO copy (BasicExtendedMetaData)option
+            /*
+             * Loads schema if necessary
+             */
+            if (Boolean.TRUE.equals(options.get(SDOUtil.XML_LOAD_SCHEMA))) {
+                XMLOptions xmlOptions = (XMLOptions) options.get(OPTION_XML_OPTIONS);
+                if (xmlOptions == null) {
+                    xmlOptions = new XMLOptionsImpl();
+                    options.put(OPTION_XML_OPTIONS, xmlOptions);
+                }
+                xmlOptions.setProcessSchemaLocations(true);
+                if (option == null)
+                    option = getDefaultLoadOptions().get(OPTION_EXTENDED_META_DATA);
+                ExtendedMetaData extendedMetaData;
+                final XSDHelper xsdHelper;
+                if (option == null) {
+                    extendedMetaData = ExtendedMetaData.INSTANCE;
+                    xsdHelper = XSDHelper.INSTANCE;
+                } else {
+                    extendedMetaData = (ExtendedMetaData) option;
+                    xsdHelper = new XSDHelperImpl(extendedMetaData, null);
                 }
-            });
+                xmlOptions.setEcoreBuilder(new DefaultEcoreBuilder(extendedMetaData) {
+                    public Collection generate(Map targetNamespaceToURI) throws IOException {
+                        for (Iterator iterator = targetNamespaceToURI.values().iterator(); iterator.hasNext();) {
+                            String uri = iterator.next().toString();
+                            xsdHelper.define(uri.indexOf(":/") == -1 ? Thread.currentThread().getContextClassLoader().getResourceAsStream(uri)
+                                    : new URL(uri).openStream(), uri);
+                        }
+                        return null; // XMLHandler#processSchemaLocations doesn't take the result
+                    }
+                });
+            }
+        } else if (loadLaxForm != 0) {
+            /*
+             * Tolerates element/attribute malform
+             */
+            options = new HashMap();
+            options.put(OPTION_EXTENDED_META_DATA, new SDOExtendedMetaDataImpl());
         }
         super.doLoad(inputSource, options);
-        // TODO consider whether we should restore options here
+        // TODO there is some thinking to be done about the restoration of options
     }
 
     /**

Modified: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java?view=diff&rev=509168&r1=509167&r2=509168
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java (original)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java Mon Feb 19 03:42:17 2007
@@ -17,6 +17,7 @@
         suite.addTestSuite(DateConversionTestCase.class);
         suite.addTestSuite(DefineOpenContentPropertyTestCase.class);
         suite.addTestSuite(DefineTypeTestCase.class);
+        suite.addTestSuite(FormTestCase.class);
         suite.addTestSuite(MixedTypeTestCase.class);
         suite.addTestSuite(OpenTypeTestCase.class);
         suite.addTestSuite(SerializeTypesTestCase.class);

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java?view=auto&rev=509168
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java Mon Feb 19 03:42:17 2007
@@ -0,0 +1,112 @@
+/**
+ *
+ *  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.IOException;
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.eclipse.emf.ecore.resource.Resource;
+
+import commonj.sdo.helper.HelperContext;
+import commonj.sdo.helper.XMLHelper;
+
+public class FormTestCase extends TestCase {
+    XMLHelper xmlHelper;
+    Map options;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        HelperContext hc = SDOUtil.createHelperContext();
+        hc.getXSDHelper().define(
+"<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"testNS\">"+
+  "<element name=\"root\">"+
+    "<complexType>"+
+      "<sequence>"+
+        "<element name=\"unqualifiedElement\" type=\"string\"/>"+
+        "<element name=\"qualifiedElement\" form=\"qualified\" type=\"string\"/>"+
+      "</sequence>"+
+      "<attribute name=\"unqualifiedAttribute\" type=\"string\"/>"+
+      "<attribute name=\"qualifiedAttribute\" form=\"qualified\" type=\"string\"/>"+
+    "</complexType>"+
+  "</element>"+
+"</schema>");
+        xmlHelper = hc.getXMLHelper();
+        options = new HashMap();
+        /*
+         * turn off default behaviour of tolerating malformed xml
+         * tests using this option and bad xml should demonstrate failure to load
+         */
+        options.put(SDOUtil.XML_LOAD_LaxForm, new Integer(0));
+    }
+
+    void load(String xml, Object options) throws IOException {
+        assertNotNull(xmlHelper.load(new StringReader(xml), null, options).getRootObject());
+    }
+
+    /*
+     * this malformed xml will load, as it uses the default lax approach
+     */
+    public void testLaxForm() throws IOException {
+        load(
+"<p:root xmlns:p=\"testNS\" p:unqualifiedAttribute=\"u\" qualifiedAttribute=\"q\">"+
+  "<p:unqualifiedElement/>"+
+  "<qualifiedElement/>"+
+"</p:root>", null);
+    }
+
+    /*
+     * any malformed xml through this method will fail to load
+     * since the options instance member disables lax processing
+     */
+    protected final void load(String xml) throws IOException {
+        try {
+            load(xml, options);
+            fail();
+        } catch (Resource.IOWrappedException featureNotFound) {
+        }
+    }
+
+    public void testUnqualifiedAttribute() throws IOException {
+        load("<p:root xmlns:p=\"testNS\" p:unqualifiedAttribute=\"u\"/>");
+    }
+
+    public void testQualifiedAttribute() throws IOException {
+        load("<p:root xmlns:p=\"testNS\" qualifiedAttribute=\"q\"/>");
+    }
+
+    public void testUnqualifiedElement() throws IOException {
+        load(
+"<p:root xmlns:p=\"testNS\">"+
+  "<p:unqualifiedElement/>"+
+"</p:root>");
+    }
+
+    public void testQualifiedElement() throws IOException {
+        load(
+"<p:root xmlns:p=\"testNS\">"+
+  "<qualifiedElement/>"+
+"</p:root>");
+    }
+}

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

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.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