Author: dims
Date: Mon Sep 11 10:18:40 2006
New Revision: 442265
URL: http://svn.apache.org/viewvc?view=rev&rev=442265
Log:
Fix and test case for WSCOMMONS-88 - XMLSchema cannot figure out external
namespaces when the schema is read from an InputSource
Added:
webservices/commons/trunk/modules/XmlSchema/src/test/java/tests/NamespaceContextTest.java
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/getBalance.xsd
Modified:
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NamespaceMap.java
Modified:
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java?view=diff&rev=442265&r1=442264&r2=442265
==============================================================================
---
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
(original)
+++
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
Mon Sep 11 10:18:40 2006
@@ -498,7 +498,7 @@
private QName getRefQName(String pName, NamespaceContext pContext) {
final int offset = pName.indexOf(':');
- final String uri;
+ String uri;
final String localName;
final String prefix;
if (offset == -1) {
@@ -511,8 +511,14 @@
} else {
prefix = pName.substring(0, offset);
uri = pContext.getNamespaceURI(prefix);
+ if (uri == null || Constants.NULL_NS_URI.equals(uri)) {
+ if(schema.parent != null) {
+ uri =
schema.parent.getNamespaceContext().getNamespaceURI(prefix);
+ }
+ }
+
if (uri == null || Constants.NULL_NS_URI.equals(uri)) {
- throw new IllegalStateException("The prefix " + prefix
+ " is not bound.");
+ throw new IllegalStateException("The prefix " + prefix + " is
not bound.");
}
localName = pName.substring(offset+1);
}
Modified:
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java?view=diff&rev=442265&r1=442264&r2=442265
==============================================================================
---
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
(original)
+++
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
Mon Sep 11 10:18:40 2006
@@ -38,6 +38,7 @@
import org.apache.ws.commons.schema.resolver.DefaultURIResolver;
import org.apache.ws.commons.schema.resolver.URIResolver;
import org.apache.ws.commons.schema.utils.TargetNamespaceValidator;
+import org.apache.ws.commons.schema.utils.NamespacePrefixList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
@@ -91,7 +92,7 @@
/**
* In-scope namespaces for XML processing
*/
- Map inScopeNamespaces = new HashMap();
+ private NamespacePrefixList namespaceContext;
/**
* An org.xml.sax.EntityResolver that is used to
@@ -400,5 +401,13 @@
receiver.setType(type);
}
unresolvedTypes.remove(typeName);
+ }
+
+ public NamespacePrefixList getNamespaceContext() {
+ return namespaceContext;
+ }
+
+ public void setNamespaceContext(NamespacePrefixList namespaceContext) {
+ this.namespaceContext = namespaceContext;
}
}
Modified:
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java?view=diff&rev=442265&r1=442264&r2=442265
==============================================================================
---
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
(original)
+++
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
Mon Sep 11 10:18:40 2006
@@ -104,7 +104,17 @@
if (targetNS == null) {
serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
"xmlns", schemaObj.syntacticalTargetNamespace);
- schema_ns.put(schemaObj.syntacticalTargetNamespace, "");
+ String prefix = null;
+ if(schemaObj.getNamespaceContext() != null) {
+ prefix =
schemaObj.getNamespaceContext().getPrefix(schemaObj.syntacticalTargetNamespace);
+ }
+ if(prefix == null && schemaObj.parent != null &&
schemaObj.parent.getNamespaceContext() != null) {
+ prefix =
schemaObj.parent.getNamespaceContext().getPrefix(schemaObj.syntacticalTargetNamespace);
+ }
+ if(prefix == null) {
+ prefix = "";
+ }
+ schema_ns.put(schemaObj.syntacticalTargetNamespace, prefix);
}
}
Modified:
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NamespaceMap.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NamespaceMap.java?view=diff&rev=442265&r1=442264&r2=442265
==============================================================================
---
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NamespaceMap.java
(original)
+++
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NamespaceMap.java
Mon Sep 11 10:18:40 2006
@@ -25,14 +25,14 @@
}
public String getNamespaceURI(String prefix) {
- return (String) get(prefix);
+ return get(prefix).toString();
}
public String getPrefix(String namespaceURI) {
Iterator iterator = entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
- if (entry.getValue().equals(namespaceURI)) {
+ if (entry.getValue().toString().equals(namespaceURI)) {
return (String) entry.getKey();
}
}
@@ -44,7 +44,7 @@
Iterator iterator = entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
- if (entry.getValue().equals(namespaceURI)) {
+ if (entry.getValue().toString().equals(namespaceURI)) {
list.add(entry.getKey());
}
}
Added:
webservices/commons/trunk/modules/XmlSchema/src/test/java/tests/NamespaceContextTest.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/test/java/tests/NamespaceContextTest.java?view=auto&rev=442265
==============================================================================
---
webservices/commons/trunk/modules/XmlSchema/src/test/java/tests/NamespaceContextTest.java
(added)
+++
webservices/commons/trunk/modules/XmlSchema/src/test/java/tests/NamespaceContextTest.java
Mon Sep 11 10:18:40 2006
@@ -0,0 +1,63 @@
+package tests;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.apache.ws.commons.schema.utils.NamespaceMap;
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.xml.sax.InputSource;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+public class NamespaceContextTest extends XMLTestCase {
+ protected boolean whitespace = true;
+ protected void setUp() throws Exception {
+ whitespace = XMLUnit.getIgnoreWhitespace();
+ XMLUnit.setIgnoreWhitespace(true);
+ }
+ protected void tearDown() throws java.lang.Exception {
+ XMLUnit.setIgnoreWhitespace(whitespace);
+ }
+ public void testNamespaceContext() throws Exception {
+ Map namespaceMapFromWSDL = new HashMap();
+ namespaceMapFromWSDL.put("tns", new
URI("http://example.org/getBalance/"));
+ 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\">" +
+ "\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</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=\"xsd: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
+ NamespaceMap prefixmap = new NamespaceMap(namespaceMapFromWSDL);
+ xsc.setNamespaceContext(prefixmap);
+ XmlSchema schemaDef = xsc.read(schemaInputSource, null);
+ StringWriter sw = new StringWriter();
+ schemaDef.write(sw);
+
+ assertXMLEqual(sw.toString(), schema);
+ }
+}
Added:
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/getBalance.xsd
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/getBalance.xsd?view=auto&rev=442265
==============================================================================
---
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/getBalance.xsd
(added)
+++
webservices/commons/trunk/modules/XmlSchema/src/test/test-resources/getBalance.xsd
Mon Sep 11 10:18:40 2006
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+ <xsd:complexType name="cinfoct">
+ <xsd:sequence>
+ <xsd:element name="customerSurname" type="xsd:string" />
+ <xsd:element name="customerForname" type="xsd:string" />
+ <xsd:element name="branchCode">
+ <xsd:simpleType>
+ <xsd:restriction base="xsd:string">
+ <xsd:pattern
+
value="[0-9]{2}[\-][0-9]{2}[\-][0-9]{2}" />
+ </xsd:restriction>
+ </xsd:simpleType>
+ </xsd:element>
+ </xsd:sequence>
+ </xsd:complexType>
+ <xsd:element name="custInfo" type="cinfoct" />
+ <xsd:complexType name="accinfct">
+ <xsd:sequence maxOccurs="unbounded" minOccurs="0">
+ <xsd:element name="accName">
+ <xsd:simpleType>
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="Cheque"
/>
+ <xsd:enumeration value="Credit"
/>
+ <xsd:enumeration
value="Savings" />
+ <xsd:enumeration value="Loan" />
+ </xsd:restriction>
+ </xsd:simpleType>
+ </xsd:element>
+ <xsd:element name="accNumber">
+ <xsd:simpleType>
+ <xsd:restriction base="xsd:string">
+ <xsd:pattern value="[0-9]{12}"
/>
+ </xsd:restriction>
+ </xsd:simpleType>
+ </xsd:element>
+ <xsd:element name="accBalance">
+ <xsd:simpleType>
+ <xsd:restriction base="xsd:decimal">
+ <xsd:fractionDigits value="2" />
+ </xsd:restriction>
+ </xsd:simpleType>
+ </xsd:element>
+ </xsd:sequence>
+ </xsd:complexType>
+ <xsd:element name="accInfo" type="accinfct" />
+</xsd:schema>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]