Author: ajith
Date: Fri Nov 25 02:03:09 2005
New Revision: 348922
URL: http://svn.apache.org/viewcvs?rev=348922&view=rev
Log:
1. Updated the code to handle imported schemas with a base URI.
2. Added a new testcase/resources to test whether the imports are working
Added:
webservices/commons/trunk/XmlSchema/test-resources/importAux.xsd
webservices/commons/trunk/XmlSchema/test-resources/importBase.xsd
webservices/commons/trunk/XmlSchema/test/tests/ImportTest.java
Modified:
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
Modified:
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
URL:
http://svn.apache.org/viewcvs/webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java?rev=348922&r1=348921&r2=348922&view=diff
==============================================================================
---
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
(original)
+++
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
Fri Nov 25 02:03:09 2005
@@ -46,6 +46,8 @@
schema = new XmlSchema(collection);
}
+
+
XmlSchema build(Document doc, ValidationEventHandler veh) {
Element schemaEl = doc.getDocumentElement();
return handleXmlSchemaElement(schemaEl);
@@ -1443,7 +1445,7 @@
populateElementNamespaces(el, elementNameSpaceMap);
Object elementNs = elementNameSpaceMap.get(args[0]);
String result =
elementNs!=null?elementNs.toString():schema.getNamespace(args[0]);
-
+
if (result == null)
throw new XmlSchemaException(
"Couldn't map prefix '" + args[0] +
@@ -1857,7 +1859,30 @@
}
XmlSchema getXmlSchemaFromLocation(String schemaLocation) {
- XmlSchema s = collection.read(new InputSource(schemaLocation), null);
- return s;
+ //check and determine the nature of the schema reference
+ //if it's relative and a base URI is present, then the schema
+ //location needs to be taken by concatanting the base URI with the
+ //relative path
+
+ String baseURI = collection.baseUri;
+ if (baseURI!=null){
+ if (!isAbsoulte(schemaLocation)){
+ schemaLocation = baseURI +
+ (schemaLocation.startsWith("/")?"":"/")+
+ schemaLocation;
+ }
+ }
+
+
+ return collection.read(new InputSource(schemaLocation), null);
+ }
+
+ /**
+ * Find whether a given uri is relative or not
+ * @param uri
+ * @return
+ */
+ private boolean isAbsoulte(String uri){
+ return uri.startsWith("http://");
}
}
Modified:
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
URL:
http://svn.apache.org/viewcvs/webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java?rev=348922&r1=348921&r2=348922&view=diff
==============================================================================
---
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
(original)
+++
webservices/commons/trunk/XmlSchema/src/org/apache/ws/commons/schema/XmlSchemaCollection.java
Fri Nov 25 02:03:09 2005
@@ -48,13 +48,26 @@
* Namespaces we know about. Each one has an equivalent XmlSchema.
*/
Map namespaces = new HashMap();
-
+ /**
+ * base URI is used as the base for loading the
+ * imports
+ */
+ String baseUri = null;
/**
* In-scope namespaces for XML processing
*/
Map inScopeNamespaces = new HashMap();
XmlSchema xsd = new XmlSchema(XmlSchema.SCHEMA_NS, this);
+
+ /**
+ * Set the base URI. This is used when schemas need to be
+ * loaded from relative locations
+ * @param baseUri
+ */
+ public void setBaseUri(String baseUri){
+ this.baseUri = baseUri;
+ }
public void init() {
XmlSchemaSimpleType type;
Added: webservices/commons/trunk/XmlSchema/test-resources/importAux.xsd
URL:
http://svn.apache.org/viewcvs/webservices/commons/trunk/XmlSchema/test-resources/importAux.xsd?rev=348922&view=auto
==============================================================================
--- webservices/commons/trunk/XmlSchema/test-resources/importAux.xsd (added)
+++ webservices/commons/trunk/XmlSchema/test-resources/importAux.xsd Fri Nov 25
02:03:09 2005
@@ -0,0 +1,14 @@
+<schema targetNamespace="http://soapinterop.org/xsd2"
+ xmlns="http://www.w3.org/2001/XMLSchema"
+ xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:xsd1="http://soapinterop.org/xsd2"
+ elementFormDefault="qualified">
+ <complexType name="SOAPStruct">
+ <all>
+ <element name="varFloat" type="xsd:float"/>
+ <element name="varInt" type="xsd:int"/>
+ <element name="varString" type="xsd:string"/>
+ </all>
+ </complexType>
+</schema>
\ No newline at end of file
Added: webservices/commons/trunk/XmlSchema/test-resources/importBase.xsd
URL:
http://svn.apache.org/viewcvs/webservices/commons/trunk/XmlSchema/test-resources/importBase.xsd?rev=348922&view=auto
==============================================================================
--- webservices/commons/trunk/XmlSchema/test-resources/importBase.xsd (added)
+++ webservices/commons/trunk/XmlSchema/test-resources/importBase.xsd Fri Nov
25 02:03:09 2005
@@ -0,0 +1,10 @@
+<schema targetNamespace="http://soapinterop.org/xsd"
+ xmlns="http://www.w3.org/2001/XMLSchema"
+ xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:xsd1="http://soapinterop.org/xsd"
+ xmlns:xsd2="http://soapinterop.org/xsd2"
+ elementFormDefault="qualified">
+ <import namespace="http://soapinterop.org/xsd2"
schemaLocation="importAux.xsd"></import>
+ <element name="echoStructParam" type="xsd2:SOAPStruct"/>
+</schema>
\ No newline at end of file
Added: webservices/commons/trunk/XmlSchema/test/tests/ImportTest.java
URL:
http://svn.apache.org/viewcvs/webservices/commons/trunk/XmlSchema/test/tests/ImportTest.java?rev=348922&view=auto
==============================================================================
--- webservices/commons/trunk/XmlSchema/test/tests/ImportTest.java (added)
+++ webservices/commons/trunk/XmlSchema/test/tests/ImportTest.java Fri Nov 25
02:03:09 2005
@@ -0,0 +1,56 @@
+package tests;
+
+import junit.framework.TestCase;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.w3c.dom.Document;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class ImportTest extends TestCase {
+
+ public void testSchemaImport() throws Exception{
+ //create a DOM document
+ DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
+ documentBuilderFactory.setNamespaceAware(true);
+ Document doc = documentBuilderFactory.newDocumentBuilder().
+ parse("test-resources/importBase.xsd");
+
+ XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+ schemaCol.setBaseUri("test-resources");
+ XmlSchema schema = schemaCol.read(doc,null);
+ assertNotNull(schema);
+
+ }
+
+
+// public void testSchemaImportRemote() throws Exception{
+// //create a DOM document
+// String schemaLocation =
"http://131.107.72.15/SoapWsdl_BaseDataTypes_XmlFormatter_Service_Indigo/BaseDataTypesDocLitB.svc?xsd=xsd1";
+// java.net.URL u = new java.net.URL(schemaLocation);
+// InputStream uStream = u.openConnection().getInputStream();
+//
+// XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+// XmlSchema schema = schemaCol.read(new
InputStreamReader(uStream),null);
+// assertNotNull(schema);
+//
+// }
+}