You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by tl...@apache.org on 2006/11/09 11:38:09 UTC

svn commit: r472849 - in /incubator/cxf/trunk: common/common/src/main/java/org/apache/cxf/resource/ rt/core/src/test/resources/org/apache/cxf/wsdl11/s1/s3/

Author: tli
Date: Thu Nov  9 02:38:09 2006
New Revision: 472849

URL: http://svn.apache.org/viewvc?view=rev&rev=472849
Log:
refactoring URIResolver...

Modified:
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/URIResolver.java
    incubator/cxf/trunk/rt/core/src/test/resources/org/apache/cxf/wsdl11/s1/s3/schema3.xsd

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java?view=diff&rev=472849&r1=472848&r2=472849
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/ExtendedURIResolver.java Thu Nov  9 02:38:09 2006
@@ -21,74 +21,19 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.Stack;
 
 import org.xml.sax.InputSource;
 
-
 public class ExtendedURIResolver {
 
-    private Stack<ResolverInfo> stack = new Stack<ResolverInfo>();
-    private org.apache.cxf.resource.URIResolver currentResolver;
+    private URIResolver currentResolver;
     private Stack<InputStream> resourceOpened = new Stack<InputStream>();
 
-    private class ResolverInfo {
-        String uri;
-        org.apache.cxf.resource.URIResolver resolver;
-        public ResolverInfo(String uri, org.apache.cxf.resource.URIResolver resolver) {
-            this.uri = uri;
-            this.resolver = resolver;
-        }
-        public String getUri() {
-            return uri;
-        }
-        public org.apache.cxf.resource.URIResolver getResolver() {
-            return resolver;
-        }
-    }
-    
+
     public InputSource resolve(String schemaLocation, String baseUri) {
         try {
-            if (baseUri != null) {
-                URI check = null;
-                if (baseUri.startsWith("classpath:")) {
-                    check = new URI(baseUri.substring(10));
-                } else if (baseUri.startsWith("jar:")) {
-                    int i = baseUri.indexOf("!");
-                    if (i != -1) {
-                        String bu = baseUri.substring(i + 1);
-                        check = new URI(bu.startsWith("file:") ? bu : "file:" + bu);
-                    } else {
-                        check = new URI(baseUri);
-                    }
-                } else {
-                    baseUri = baseUri.startsWith("file:") ? baseUri : "file:" + baseUri;
-                    check = new URI(baseUri);
-                }
-                if (check.isAbsolute()) {
-                    currentResolver = new org.apache.cxf.resource.URIResolver();
-                    stack.addElement(new ResolverInfo(schemaLocation, currentResolver));            
-                } else {
-                    while (!stack.isEmpty()) {
-                        ResolverInfo ri = stack.pop();
-                        if (ri.getUri().equals(baseUri)) {
-                            currentResolver = ri.getResolver();
-                            stack.addElement(ri);
-                            break;
-                        }
-                    }
-                    stack.addElement(new ResolverInfo(schemaLocation, currentResolver));            
-                }
-                if (currentResolver == null) {
-                    throw new RuntimeException("invalidate schema import");
-                }
-            } else {
-                if (currentResolver == null) {
-                    currentResolver = new org.apache.cxf.resource.URIResolver();
-                }
-            }
+            currentResolver = new URIResolver();
             currentResolver.resolveStateful(baseUri, schemaLocation, getClass());
             if (currentResolver.isResolved()) {
                 if (currentResolver.getURI() != null && currentResolver.getURI().isAbsolute()) {
@@ -105,14 +50,12 @@
                 return source;
             }
 
-        } catch (IOException e) {
-            // move on...
-        } catch (URISyntaxException use) {            
+        } catch (Exception e) {
             // move on...
         }
         return new InputSource(schemaLocation);
     }
-    
+
     public void close() {
         try {
             while (!resourceOpened.isEmpty()) {
@@ -123,9 +66,9 @@
             // move on...
         }
     }
-    
+
     public String getLatestImportURI() {
         return currentResolver.getURI().toString();
     }
-    
+
 }

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/URIResolver.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/URIResolver.java?view=diff&rev=472849&r1=472848&r2=472849
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/URIResolver.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource/URIResolver.java Thu Nov  9 02:38:09 2006
@@ -27,7 +27,6 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.Stack;
 
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 
@@ -46,23 +45,8 @@
     private URI uri;
     private InputStream is;
     private Class calling;
-    
-    private Stack<Location> history = new Stack<Location>();
 
-    private class Location {
-        private String base;
-        private String relative;
-        public Location(String base, String relative) {
-            this.base = base;
-            this.relative = relative;
-        }
-        public String getBase() {
-            return base;
-        }
-        public String getRelative() {
-            return relative;
-        }
-    }
+    private String lastImportUrl;
 
     public URIResolver() throws IOException {
     }
@@ -74,7 +58,7 @@
     public URIResolver(String baseUriStr, String uriStr) throws IOException {
         this(baseUriStr, uriStr, null);
     }
-    
+
     public URIResolver(String baseUriStr, String uriStr, Class calling) throws IOException {
         this.calling = (calling != null) ? calling : getClass();
 
@@ -89,97 +73,73 @@
         }
     }
 
-    
-    public void resolveStateful(String baseUriStr, String uriStr, Class callingCls) throws IOException {
-        this.calling = (callingCls != null) ? callingCls : getClass();
-
-        if (uriStr.startsWith("classpath:")) {
-            tryClasspath(uriStr);
-        } else if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
-            tryJarState(baseUriStr, uriStr);
-        } else if (uriStr.startsWith("jar:")) {
-            tryJar(uriStr);
-        } else {
-            tryFileSystemState(baseUriStr, uriStr);
-        }
-    }
-
-    private URI getAbsoluteFileStr(String baseUriStr, String uriStr) throws MalformedURLException {
+    private String getAbsoluteUrlStr(String baseUriStr, String uriStr) throws MalformedURLException {
         URI relative;
         URI base;
-        try {            
-            File uriFile = new File(uriStr);
-            uriFile = new File(uriFile.getAbsolutePath());   
-            if (uriFile.exists()) {
-                relative = uriFile.toURI();
-            } else {
-                relative = new URI(uriStr);
-            }            
+        try {
+            relative = new URI(uriStr);
             if (relative.isAbsolute()) {
-                return new URI(uriStr);
+                return new URI(uriStr).toString();
             } else if (baseUriStr != null) {
+                String prefix = "file:";
+                if (baseUriStr.startsWith("classpath:")) {
+                    baseUriStr = baseUriStr.substring(10);
+                    prefix = "";
+                } else if (baseUriStr.startsWith("jar:")) {
+                    int i = baseUriStr.indexOf("!");
+                    if (i != -1) {
+                        baseUriStr = baseUriStr.substring(i + 1);
+                    } else {
+                        baseUriStr = baseUriStr.substring(4);
+                    }
+                    prefix = "";
+                } else if (baseUriStr.startsWith("file:")) {
+                    prefix = "";
+                }
                 base = new URI(baseUriStr);
                 if (base.isAbsolute()) {
-                    return base.resolve(relative);
+                    return prefix + base.resolve(relative).toString();
                 } else {
-                    Location location = null;
                     // assume that the outmost element of history is parent
-                    while (!history.empty()) {
-                        location = history.pop();
-                        if (location.getRelative().equals(baseUriStr)) {
-                            break;
-                        } else {
-                            location = null;
-                        }
-                    }
-                    if (location != null) {
-                        URI result = getAbsoluteFileStr(location.base, location.relative).resolve(relative);
-                        history.push(location);
-                        return result;
+                    if (lastImportUrl != null) {
+                        URI result = new URI(lastImportUrl).resolve(relative);
+                        return prefix + result.toString();
                     } else {
                         return null;
                     }
                 }
-            }            
+            }
         } catch (URISyntaxException e) {
             return null;
         }
         return null;
     }
-    
-    private void tryFileSystemState(String baseUriStr, String uriStr) 
-        throws IOException, MalformedURLException {
+
+    public void resolveStateful(String baseUriStr, String uriStr, Class callingCls) 
+        throws IOException, MalformedURLException, URISyntaxException {
+
+        this.calling = (callingCls != null) ? callingCls : getClass();
+
         if (baseUriStr == null && uriStr == null) {
             return;
-        }                
-        URI finalRelative = getAbsoluteFileStr(baseUriStr, uriStr);
-        try {
-            if (!(new URI(uriStr)).isAbsolute()) {
-                history.push(new Location(baseUriStr, uriStr));
-            } 
-            if (finalRelative != null) {
-                File targetFile = new File(finalRelative.toString().startsWith("file:") ? finalRelative 
-                    : new URI("file:" + finalRelative.toString()));
+        }
+        String finalRelative = getAbsoluteUrlStr(baseUriStr, uriStr);
+        if (finalRelative != null) {
+            if (finalRelative.startsWith("file:")) {
+                File targetFile = new File(new URI(finalRelative));
                 if (!targetFile.exists()) {
-                    tryClasspath(finalRelative.toString().substring(5));
+                    tryClasspath(finalRelative.substring(5));
                     return;
-                }
-                URI target;
-                if (targetFile.exists()) {
-                    target = targetFile.toURI();
                 } else {
-                    target = finalRelative;
-                }
-                if (target.isAbsolute()) {
-                    uri = target;                
-                    is = target.toURL().openStream();
+                    uri = targetFile.toURI();
+                    is = targetFile.toURI().toURL().openStream();
                 }
+            } else {
+                tryClasspath(finalRelative);
             }
-        } catch (URISyntaxException ue) {
-            // move on
         }
     }
-    
+
     private void tryFileSystem(String baseUriStr, String uriStr) throws IOException, MalformedURLException {
         try {
             URI relative;
@@ -191,7 +151,7 @@
             } else {
                 relative = new URI(uriStr);
             }
-            
+
             if (relative.isAbsolute()) {
                 uri = relative;
                 is = relative.toURL().openStream();
@@ -208,7 +168,7 @@
                 } else {
                     base = new URI(baseUriStr);
                 }
-                
+
                 base = base.resolve(relative);
                 if (base.isAbsolute()) {
                     is = base.toURL().openStream();
@@ -235,22 +195,7 @@
         }
     }
 
-    private void tryJarState(String baseStr, String uriStr) throws IOException {
-        int i = baseStr.indexOf('!');
-        if (i == -1) {
-            tryFileSystemState(baseStr, uriStr);
-            return;
-        }
-        baseStr = baseStr.substring(i + 1);
-        URI u = getAbsoluteFileStr(baseStr.startsWith("file:") ? baseStr : "file:" + baseStr, uriStr);
-        // remove the prefix "file:"
-        tryClasspath(u.toString().substring(5));
-        if (is != null) {
-            return;
-        }        
-        tryFileSystemState("", uriStr);
-    }
-    
+
     private void tryJar(String baseStr, String uriStr) throws IOException {
         int i = baseStr.indexOf('!');
         if (i == -1) {
@@ -266,10 +211,10 @@
         } catch (URISyntaxException e) {
             // do nothing
         }
-        
+
         tryFileSystem("", uriStr);
     }
-    
+
     private void tryJar(String uriStr) throws IOException {
         int i = uriStr.indexOf('!');
         if (i == -1) {
@@ -278,7 +223,7 @@
         uriStr = uriStr.substring(i + 1);
         tryClasspath(uriStr);
     }
-    
+
     private void tryClasspath(String uriStr) throws IOException {
         if (uriStr.startsWith("classpath:")) {
             uriStr = uriStr.substring(10);
@@ -288,7 +233,17 @@
             tryRemote(uriStr);
         } else {
             try {
-                uri = new URI(url.toString());
+                lastImportUrl = url.toString();
+                if (lastImportUrl.startsWith("jar:")) {
+                    int i = lastImportUrl.indexOf("!");
+                    if (i != -1) {
+                        uri = new URI(url.toString().substring(i + 1));
+                    } else {
+                        uri = new URI(url.toString().substring(4));
+                    }
+                } else {
+                    uri = new URI(url.toString());
+                }
             } catch (URISyntaxException e) {
                 // How would this occurr??
             }
@@ -327,8 +282,12 @@
     public File getFile() {
         return file;
     }
-    
+
     public boolean isResolved() {
         return is != null;
+    }
+
+    public String getLastImportUrl() {
+        return lastImportUrl;
     }
 }

Modified: incubator/cxf/trunk/rt/core/src/test/resources/org/apache/cxf/wsdl11/s1/s3/schema3.xsd
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/test/resources/org/apache/cxf/wsdl11/s1/s3/schema3.xsd?view=diff&rev=472849&r1=472848&r2=472849
==============================================================================
--- incubator/cxf/trunk/rt/core/src/test/resources/org/apache/cxf/wsdl11/s1/s3/schema3.xsd (original)
+++ incubator/cxf/trunk/rt/core/src/test/resources/org/apache/cxf/wsdl11/s1/s3/schema3.xsd Thu Nov  9 02:38:09 2006
@@ -19,6 +19,8 @@
 -->
 <xs:schema version="1.0" targetNamespace="http://apache.org/hello_world_soap_http/types/s3" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
+  <xs:import namespace="http://www.w3.org/2005/08/addressing" schemaLocation="/schemas/wsdl/ws-addr.xsd"/>
+
   <xs:element name="greetMe">
     <xs:complexType>
       <xs:sequence>