You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by dk...@apache.org on 2009/04/08 18:40:44 UTC

svn commit: r763308 - in /webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src: main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java test/java/tests/NamespaceContextTest.java

Author: dkulp
Date: Wed Apr  8 16:40:44 2009
New Revision: 763308

URL: http://svn.apache.org/viewvc?rev=763308&view=rev
Log:
[WSCOMMONS-447, WSCOMMONS-426] Fix some issues that occur if schema has not namespace context.

Modified:
    webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
    webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/java/tests/NamespaceContextTest.java

Modified: webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
URL: http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java?rev=763308&r1=763307&r2=763308&view=diff
==============================================================================
--- webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java (original)
+++ webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java Wed Apr  8 16:40:44 2009
@@ -74,8 +74,8 @@
 
     private Hashtable schema_ns;
 
-    static String xsdPrefix = "xs";
-    public static final String xsdNamespace = "http://www.w3.org/2001/XMLSchema";
+    String xsdPrefix = "xs";
+    String xsdNamespace = "http://www.w3.org/2001/XMLSchema";
     ArrayList docs;
     Element schemaElement;
 
@@ -151,8 +151,8 @@
                 }
                 if (prefix == null) {
                     //find a usable prefix
-                    int count = 1;
-                    prefix = "tns" + count;
+                    int count = 0;
+                    prefix = "tns";
                     String ns = serializedSchema.getAttribute("xmlns:" + prefix);
                     while (ns != null && !"".equals(ns)) {
                         ++count;
@@ -162,10 +162,10 @@
                 } 
                 if ("".equals(prefix)) {
                     serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
-                                                    "xmlns", targetNS);
+                                                    "xmlns", schemaObj.syntacticalTargetNamespace);
                 } else {
                     serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
-                                                    "xmlns:" + prefix, targetNS);
+                                                    "xmlns:" + prefix, schemaObj.syntacticalTargetNamespace);
                 }
                 schema_ns.put(schemaObj.syntacticalTargetNamespace, prefix);
             }
@@ -216,18 +216,6 @@
             serializedSchema.setAttribute("version", schemaObj.version);
         }
 
-        //add the extra namespace decalarations if any are available
-        NamespacePrefixList ctx = schemaObj.getNamespaceContext();
-        String[] prefixes = ctx.getDeclaredPrefixes();
-        for (int i = 0;  i < prefixes.length;  i++) {
-            String prefix = prefixes[i];
-            String uri = ctx.getNamespaceURI(prefix);
-            if (!Constants.DEFAULT_NS_PREFIX.equals(prefix)) {
-                serializedSchema.setAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI,
-                        Constants.XMLNS_ATTRIBUTE + ":" + prefix, uri);
-            }
-        }
-
         //after serialize the schema add into documentation
         //and add to document collection array  which at the end 
         //returned
@@ -322,32 +310,48 @@
      */
     private Element setupNamespaces(Document schemaDocs, XmlSchema schemaObj) {
         NamespacePrefixList ctx = schemaObj.getNamespaceContext();
-        schemaObj.schema_ns_prefix = xsdPrefix = ctx.getPrefix(xsdNamespace);
+        schemaObj.schema_ns_prefix = xsdPrefix = ctx == null ? null : ctx.getPrefix(xsdNamespace);
         if(xsdPrefix == null) {
-            schemaObj.schema_ns_prefix = xsdPrefix = "";
+            //find a prefix to use
+            xsdPrefix = "";
+            if (ctx != null && ctx.getNamespaceURI(xsdPrefix) != null) {
+                xsdPrefix = "xsd";
+            }
+            int count = 0;
+            while (ctx != null && ctx.getNamespaceURI(xsdPrefix) != null) {
+                xsdPrefix = "xsd" + ++count;
+            }
+            schemaObj.schema_ns_prefix = xsdPrefix;
         }
 
         Element schemaEl = createNewElement(schemaDocs, "schema",
                                             schemaObj.schema_ns_prefix, XmlSchema.SCHEMA_NS);
 
-        String[] prefixes = ctx.getDeclaredPrefixes();
-        for (int i = 0;  i < prefixes.length;  i++) {
-            String prefix = prefixes[i];
-            String uri = ctx.getNamespaceURI(prefix);
-            if (uri != null && prefix != null) {
-                if ("".equals(prefix) || !schema_ns.containsKey(uri)) {
-                    schema_ns.put(uri, prefix);
+        if (ctx != null) {
+            String[] prefixes = ctx.getDeclaredPrefixes();
+            for (int i = 0;  i < prefixes.length;  i++) {
+                String prefix = prefixes[i];
+                String uri = ctx.getNamespaceURI(prefix);
+                if (uri != null && prefix != null) {
+                    if ("".equals(prefix) || !schema_ns.containsKey(uri)) {
+                        schema_ns.put(uri, prefix);
+                    }
+                    prefix = (prefix.length() > 0) ? "xmlns:" + prefix : "xmlns";                
+                    schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
+                                            prefix, uri);
                 }
-                prefix = (prefix.length() > 0) ? "xmlns:" + prefix : "xmlns";                
-                schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
-                                        prefix, uri);
             }
         }
         //for schema that not set the xmlns attrib member
         if (schema_ns.get(xsdNamespace) == null) {
             schema_ns.put(xsdNamespace, xsdPrefix);
-            schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
-                                    "xmlns:" + xsdPrefix, xsdNamespace);
+            if ("".equals(xsdPrefix)) {
+                schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
+                                        "xmlns", xsdNamespace);
+            } else {
+                schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
+                                        "xmlns:" + xsdPrefix, xsdNamespace);                
+            }
             schemaObj.schema_ns_prefix = xsdPrefix;
         }
         return schemaEl;

Modified: webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/java/tests/NamespaceContextTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/java/tests/NamespaceContextTest.java?rev=763308&r1=763307&r2=763308&view=diff
==============================================================================
--- webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/java/tests/NamespaceContextTest.java (original)
+++ webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/test/java/tests/NamespaceContextTest.java Wed Apr  8 16:40:44 2009
@@ -112,4 +112,44 @@
         String ns = el.getAttribute("xmlns");
         assertEquals("http://www.w3.org/2001/XMLSchema", ns);
     }
+    public void testNullNamespaceCtx() throws Exception {
+        String schema = "\t\t<xsd:schema targetNamespace=\"http://example.org/getBalance/\"\n" +
+            "attributeFormDefault=\"unqualified\" elementFormDefault=\"unqualified\"" +
+            " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
+            " xmlns=\"http://www.w3.org/2001/XMLSchema\"" +
+            " xmlns:xsd1=\"http://example.org/getBalance/\">" +
+            "\t\t\t<xsd:include schemaLocation=\"getBalance.xsd\" />\n" +
+            "\n" +
+            "\t\t\t<xsd:element name=\"newCustomer\">\n" +
+            "\t\t\t\t<xsd:complexType>\n" +
+            "\t\t\t\t\t<xsd:sequence>\n" +
+            "\t\t\t\t\t\t<xsd:element name=\"details\" type=\"xsd1:cinfoct\" />\n" +
+            "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"string\" />\n" +
+            "\t\t\t\t\t</xsd:sequence>\n" +
+            "\t\t\t\t</xsd:complexType>\n" +
+            "\t\t\t</xsd:element>\n" +
+            "\n" +
+            "\t\t\t<xsd:element name=\"customerId\">\n" +
+            "\t\t\t\t<xsd:complexType>\n" +
+            "\t\t\t\t\t<xsd:sequence>\n" +
+            "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"string\" />\n" +
+            "\t\t\t\t\t</xsd:sequence>\n" +
+            "\t\t\t\t</xsd:complexType>\n" +
+            "\t\t\t</xsd:element>\n" +
+            "\n" +
+            "\t\t</xsd:schema>";
+        org.xml.sax.InputSource schemaInputSource = new InputSource(new StringReader(schema));
+        XmlSchemaCollection xsc = new XmlSchemaCollection();
+        xsc.setBaseUri(Resources.TEST_RESOURCES);
+
+        //Set the namespaces explicitly
+        XmlSchema schemaDef = xsc.read(schemaInputSource, null);
+        schemaDef.setNamespaceContext(null);
+        Document doc = schemaDef.getSchemaDocument();
+        Element el = doc.getDocumentElement();
+        String ns = el.getAttribute("xmlns");
+        assertEquals("http://www.w3.org/2001/XMLSchema", ns);
+        ns = el.getAttribute("xmlns:tns");
+        assertEquals("http://example.org/getBalance/", ns);
+    }
 }