Author: dkulp
Date: Wed Apr  8 16:07:43 2009
New Revision: 763297

URL: http://svn.apache.org/viewvc?rev=763297&view=rev
Log:
[WSCOMMONS-460] Fix problems with default namespace being reset if schema 
doesn't have a namespace defined for the targetNamespace

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=763297&r1=763296&r2=763297&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:07:43 2009
@@ -125,16 +125,12 @@
         if (schemaObj.syntacticalTargetNamespace != null) {
             serializedSchema.setAttribute("targetNamespace", 
schemaObj.syntacticalTargetNamespace);
 
-            Object targetNS =
-                    schema_ns.get(schemaObj.syntacticalTargetNamespace);
+            String targetNS =
+                    
(String)schema_ns.get(schemaObj.syntacticalTargetNamespace);
 
             //if the namespace is not entered then add 
-            //the targetNamespace as its
+            //the targetNamespace
             if (targetNS == null) {
-                
if(!Constants.XMLNS_URI.equals(schemaObj.syntacticalTargetNamespace)){
-                    serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
-                            "xmlns", schemaObj.syntacticalTargetNamespace);
-                }
                 String prefix = null;
                 if(schemaObj.getNamespaceContext() != null) {
                     prefix = 
schemaObj.getNamespaceContext().getPrefix(schemaObj.syntacticalTargetNamespace);
@@ -142,8 +138,34 @@
                 if(prefix == null && schemaObj.parent != null && 
schemaObj.parent.getNamespaceContext() != null) {
                     prefix = 
schemaObj.parent.getNamespaceContext().getPrefix(schemaObj.syntacticalTargetNamespace);
                 }
+                //check if the chosen prefix is ok
                 if(prefix == null) {
-                    prefix = "";
+                    if (serializedSchema.getAttributeNode("xmlns") == null) {
+                        prefix = "";
+                    }
+                } else {
+                    String ns = serializedSchema.getAttribute("xmlns:" + 
prefix);
+                    if (ns != null && !"".equals(ns)) {
+                        prefix = null;
+                    }                    
+                }
+                if (prefix == null) {
+                    //find a usable prefix
+                    int count = 1;
+                    prefix = "tns" + count;
+                    String ns = serializedSchema.getAttribute("xmlns:" + 
prefix);
+                    while (ns != null && !"".equals(ns)) {
+                        ++count;
+                        prefix = "tns" + count;
+                        ns = serializedSchema.getAttribute("xmlns:" + prefix);
+                    }
+                } 
+                if ("".equals(prefix)) {
+                    serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
+                                                    "xmlns", targetNS);
+                } else {
+                    serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
+                                                    "xmlns:" + prefix, 
targetNS);
                 }
                 schema_ns.put(schemaObj.syntacticalTargetNamespace, prefix);
             }
@@ -304,34 +326,29 @@
         if(xsdPrefix == null) {
             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) {
-                schema_ns.put(uri, 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);
             }
         }
         //for schema that not set the xmlns attrib member
         if (schema_ns.get(xsdNamespace) == null) {
             schema_ns.put(xsdNamespace, xsdPrefix);
-            schemaObj.schema_ns_prefix = xsdPrefix;
-        }
-
-        Element schemaEl = createNewElement(schemaDocs, "schema",
-                schemaObj.schema_ns_prefix, XmlSchema.SCHEMA_NS);
-
-        Iterator entries = schema_ns.entrySet().iterator();
-
-        while (entries.hasNext()) {
-            //let it crash for null pointer because then either the schema
-            //is wrong(namespace not set properly or bug in setting ns)
-            Map.Entry entry = (Map.Entry) entries.next();
-            String key = entry.getKey().toString();
-            String value = entry.getValue().toString();
-            value = (value.length() > 0) ? "xmlns:" + value : "xmlns";
             schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
-                    value, key);
+                                    "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=763297&r1=763296&r2=763297&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:07:43 2009
@@ -30,6 +30,9 @@
 import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 public class NamespaceContextTest extends XMLTestCase {
     protected boolean whitespace = true;
     protected void setUp() throws Exception {
@@ -45,14 +48,16 @@
         namespaceMapFromWSDL.put("xsd", new 
URI("http://www.w3.org/2001/XMLSchema";));
         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: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=\"tns:cinfoct\" />\n" +
-                "\t\t\t\t\t\t<xsd:element name=\"id\" type=\"xsd:string\" 
/>\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" +
@@ -60,7 +65,7 @@
                 "\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=\"xsd:string\" 
/>\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" +
@@ -76,11 +81,35 @@
         XmlSchema schemaDef = xsc.read(schemaInputSource, null);
         StringWriter sw = new StringWriter();
         schemaDef.write(sw);
-
         try {
-            assertXMLEqual(sw.toString(), schema);
+            assertXMLEqual(sw.toString(), schema.replaceAll("tns:", "xsd1:"));
         } catch (NullPointerException ex) {
             System.out.println(">>>> NPE, ignoring assertXMLEqual");
         }
     }
+    public void testNamespaceCount() throws Exception {
+        String schema =  "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\""; +
+                       " 
xmlns:addressing=\"http://schemas.foo.com/references\""; +
+                       " xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\""; 
+
+                       " xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\""; 
+
+                       " 
xmlns:tns=\"http://schemas.foo.com/idl/isfx_authn_service.idl\"; " +
+                       " xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\""; +
+                       " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""; +
+                       " 
xmlns:xsd1=\"http://schemas.foo.com/idltypes/isfx_authn_service.idl\""; +
+                       " 
targetNamespace=\"http://schemas.foo.com/idltypes/isf_suppl_info.idl\";>" +
+                       "<complexType name=\"IT_ISF.DomainRealmFilter\">" +
+                       "<sequence><element name=\"domain_name\" 
type=\"string\"/>" +
+                       "<element name=\"realm\" type=\"string\"/></sequence>" +
+                       "</complexType></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);
+        Document doc = schemaDef.getSchemaDocument();
+        Element el = doc.getDocumentElement();
+        String ns = el.getAttribute("xmlns");
+        assertEquals("http://www.w3.org/2001/XMLSchema";, ns);
+    }
 }


Reply via email to