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 di...@apache.org on 2006/02/02 21:36:34 UTC
svn commit: r374486 - in /webservices/commons/modules/XmlSchema:
src/org/apache/ws/commons/schema/SchemaBuilder.java
test/tests/ImportTest.java
Author: dims
Date: Thu Feb 2 12:36:31 2006
New Revision: 374486
URL: http://svn.apache.org/viewcvs?rev=374486&view=rev
Log:
Fix for WSCOMMONS-1 - XmlSchema not resolving schemaLocation with a relative path
Modified:
webservices/commons/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
webservices/commons/modules/XmlSchema/test/tests/ImportTest.java
Modified: webservices/commons/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java?rev=374486&r1=374485&r2=374486&view=diff
==============================================================================
--- webservices/commons/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java (original)
+++ webservices/commons/modules/XmlSchema/src/org/apache/ws/commons/schema/SchemaBuilder.java Thu Feb 2 12:36:31 2006
@@ -33,6 +33,10 @@
import java.util.StringTokenizer;
import java.util.Map;
import java.util.HashMap;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.io.IOException;
+import java.io.File;
public class SchemaBuilder {
Document doc;
@@ -63,6 +67,7 @@
"attributeFormDefault"));
schema.setBlockDefault(this.getDerivation(schemaEl, "blockDefault"));
schema.setFinalDefault(this.getDerivation(schemaEl, "finalDefault"));
+ schema.setSourceURI(schemaEl.getOwnerDocument().getBaseURI());
/***********
* for ( each childElement)
@@ -251,7 +256,7 @@
}
// only populate it if it isn't already in there
if(!collection.namespaces.containsKey(schema.targetNamespace)){
- collection.namespaces.put(schema.targetNamespace, schema);
+ collection.namespaces.put(schema.targetNamespace, schema);
}
}
@@ -1687,8 +1692,14 @@
schemaImport.schemaLocation =
importEl.getAttribute("schemaLocation");
- if ((schemaImport.schemaLocation != null) && (!schemaImport.schemaLocation.equals("")))
- schemaImport.schema = getXmlSchemaFromLocation(schemaImport.schemaLocation);
+ if ((schemaImport.schemaLocation != null) && (!schemaImport.schemaLocation.equals(""))) {
+ if(schema.getSourceURI()!=null) {
+ schemaImport.schema =
+ getXmlSchemaFromLocation(schemaImport.schemaLocation, schema.getSourceURI());
+ } else {
+ schemaImport.schema = getXmlSchemaFromLocation(schemaImport.schemaLocation);
+ }
+ }
return schemaImport;
}
@@ -1711,9 +1722,13 @@
includeEl.getAttribute("schemaLocation");
- include.schema =
- getXmlSchemaFromLocation(include.schemaLocation);
-
+ if(schema.getSourceURI()!=null) {
+ include.schema =
+ getXmlSchemaFromLocation(include.schemaLocation, schema.getSourceURI());
+ } else {
+ include.schema =
+ getXmlSchemaFromLocation(include.schemaLocation);
+ }
XmlSchemaObjectCollection coll = include.schema.getItems();
return include;
@@ -1861,30 +1876,102 @@
}
XmlSchema getXmlSchemaFromLocation(String schemaLocation) {
- //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
+ return getXmlSchemaFromLocation(schemaLocation, collection.baseUri);
+ }
- String baseURI = collection.baseUri;
+ XmlSchema getXmlSchemaFromLocation(String schemaLocation, String baseURI) {
if (baseURI!=null){
- if (!isAbsoulte(schemaLocation)){
- schemaLocation = baseURI +
- (schemaLocation.startsWith("/")?"":"/")+
- schemaLocation;
+ if (!isAbsolute(schemaLocation)){
+ try {
+ schemaLocation = getURL(new URL(baseURI), schemaLocation).toString();
+ } catch (Exception e) {
+ schemaLocation = baseURI +
+ (schemaLocation.startsWith("/")?"":"/")+
+ schemaLocation;
+ }
}
}
-
-
return collection.read(new InputSource(schemaLocation), null);
}
/**
* Find whether a given uri is relative or not
+ *
* @param uri
- * @return
+ * @return boolean
*/
- private boolean isAbsoulte(String uri){
+ private boolean isAbsolute(String uri) {
return uri.startsWith("http://");
}
+
+ /**
+ * This is essentially a call to "new URL(contextURL, spec)" with extra handling in case spec is
+ * a file.
+ *
+ * @param contextURL
+ * @param spec
+ * @return
+ * @throws java.io.IOException
+ */
+ private static URL getURL(URL contextURL, String spec) throws IOException {
+
+ // First, fix the slashes as windows filenames may have backslashes
+ // in them, but the URL class wont do the right thing when we later
+ // process this URL as the contextURL.
+ String path = spec.replace('\\', '/');
+
+ // See if we have a good URL.
+ URL url = null;
+
+ try {
+
+ // first, try to treat spec as a full URL
+ url = new URL(contextURL, path);
+
+ // if we are deail with files in both cases, create a url
+ // by using the directory of the context URL.
+ if ((contextURL != null) && url.getProtocol().equals("file")
+ && contextURL.getProtocol().equals("file")) {
+ url = getFileURL(contextURL, path);
+ }
+ } catch (MalformedURLException me) {
+
+ // try treating is as a file pathname
+ url = getFileURL(contextURL, path);
+ }
+
+ // Everything is OK with this URL, although a file url constructed
+ // above may not exist. This will be caught later when the URL is
+ // accessed.
+ return url;
+ } // getURL
+
+ /**
+ * Method getFileURL
+ *
+ * @param contextURL
+ * @param path
+ * @return
+ * @throws IOException
+ */
+ private static URL getFileURL(URL contextURL, String path)
+ throws IOException {
+
+ if (contextURL != null) {
+
+ // get the parent directory of the contextURL, and append
+ // the spec string to the end.
+ String contextFileName = contextURL.getFile();
+ URL parent = null;
+ File parentFile = new File(contextFileName).getParentFile();
+ if (parentFile != null) {
+ parent = parentFile.toURL();
+ }
+ if (parent != null) {
+ return new URL(parent, path);
+ }
+ }
+
+ return new URL("file", "", path);
+ } // getFileURL
}
Modified: webservices/commons/modules/XmlSchema/test/tests/ImportTest.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/XmlSchema/test/tests/ImportTest.java?rev=374486&r1=374485&r2=374486&view=diff
==============================================================================
--- webservices/commons/modules/XmlSchema/test/tests/ImportTest.java (original)
+++ webservices/commons/modules/XmlSchema/test/tests/ImportTest.java Thu Feb 2 12:36:31 2006
@@ -1,14 +1,11 @@
package tests;
import junit.framework.TestCase;
-import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
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.
*
@@ -41,16 +38,20 @@
}
+ /**
+ * variation of above don't set the base uri.
+ * @throws Exception
+ */
+ public void testSchemaImport2() 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();
+ 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);
-//
-// }
+ }
}