cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t..@apache.org
Subject svn commit: r472861 - in /incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/resource: ExtendedURIResolver.java URIResolver.java
Date Thu, 09 Nov 2006 11:14:42 GMT
Author: tli
Date: Thu Nov  9 03:14:36 2006
New Revision: 472861

URL: http://svn.apache.org/viewvc?view=rev&rev=472861
Log:
revert and investigate for error

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

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=472861&r1=472860&r2=472861
==============================================================================
--- 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 03:14:36 2006
@@ -21,19 +21,74 @@
 
 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 URIResolver currentResolver;
+    private Stack<ResolverInfo> stack = new Stack<ResolverInfo>();
+    private org.apache.cxf.resource.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 {
-            currentResolver = new URIResolver();
+            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.resolveStateful(baseUri, schemaLocation, getClass());
             if (currentResolver.isResolved()) {
                 if (currentResolver.getURI() != null && currentResolver.getURI().isAbsolute())
{
@@ -50,12 +105,14 @@
                 return source;
             }
 
-        } catch (Exception e) {
+        } catch (IOException e) {
+            // move on...
+        } catch (URISyntaxException use) {            
             // move on...
         }
         return new InputSource(schemaLocation);
     }
-
+    
     public void close() {
         try {
             while (!resourceOpened.isEmpty()) {
@@ -66,9 +123,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=472861&r1=472860&r2=472861
==============================================================================
--- 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 03:14:36 2006
@@ -27,6 +27,7 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.Stack;
 
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
 
@@ -45,8 +46,23 @@
     private URI uri;
     private InputStream is;
     private Class calling;
+    
+    private Stack<Location> history = new Stack<Location>();
 
-    private String lastImportUrl;
+    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;
+        }
+    }
 
     public URIResolver() throws IOException {
     }
@@ -58,7 +74,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();
 
@@ -73,73 +89,97 @@
         }
     }
 
-    private String getAbsoluteUrlStr(String baseUriStr, String uriStr) throws MalformedURLException
{
+    
+    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
{
         URI relative;
         URI base;
-        try {
-            relative = new URI(uriStr);
+        try {            
+            File uriFile = new File(uriStr);
+            uriFile = new File(uriFile.getAbsolutePath());   
+            if (uriFile.exists()) {
+                relative = uriFile.toURI();
+            } else {
+                relative = new URI(uriStr);
+            }            
             if (relative.isAbsolute()) {
-                return new URI(uriStr).toString();
+                return new URI(uriStr);
             } 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 prefix + base.resolve(relative).toString();
+                    return base.resolve(relative);
                 } else {
+                    Location location = null;
                     // assume that the outmost element of history is parent
-                    if (lastImportUrl != null) {
-                        URI result = new URI(lastImportUrl).resolve(relative);
-                        return prefix + result.toString();
+                    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;
                     } else {
                         return null;
                     }
                 }
-            }
+            }            
         } catch (URISyntaxException e) {
             return null;
         }
         return null;
     }
-
-    public void resolveStateful(String baseUriStr, String uriStr, Class callingCls) 
-        throws IOException, MalformedURLException, URISyntaxException {
-
-        this.calling = (callingCls != null) ? callingCls : getClass();
-
+    
+    private void tryFileSystemState(String baseUriStr, String uriStr) 
+        throws IOException, MalformedURLException {
         if (baseUriStr == null && uriStr == null) {
             return;
-        }
-        String finalRelative = getAbsoluteUrlStr(baseUriStr, uriStr);
-        if (finalRelative != null) {
-            if (finalRelative.startsWith("file:")) {
-                File targetFile = new File(new URI(finalRelative));
+        }                
+        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()));
                 if (!targetFile.exists()) {
-                    tryClasspath(finalRelative.substring(5));
+                    tryClasspath(finalRelative.toString().substring(5));
                     return;
+                }
+                URI target;
+                if (targetFile.exists()) {
+                    target = targetFile.toURI();
                 } else {
-                    uri = targetFile.toURI();
-                    is = targetFile.toURI().toURL().openStream();
+                    target = finalRelative;
+                }
+                if (target.isAbsolute()) {
+                    uri = target;                
+                    is = target.toURL().openStream();
                 }
-            } else {
-                tryClasspath(finalRelative);
             }
+        } catch (URISyntaxException ue) {
+            // move on
         }
     }
-
+    
     private void tryFileSystem(String baseUriStr, String uriStr) throws IOException, MalformedURLException
{
         try {
             URI relative;
@@ -151,7 +191,7 @@
             } else {
                 relative = new URI(uriStr);
             }
-
+            
             if (relative.isAbsolute()) {
                 uri = relative;
                 is = relative.toURL().openStream();
@@ -168,7 +208,7 @@
                 } else {
                     base = new URI(baseUriStr);
                 }
-
+                
                 base = base.resolve(relative);
                 if (base.isAbsolute()) {
                     is = base.toURL().openStream();
@@ -195,7 +235,22 @@
         }
     }
 
-
+    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) {
@@ -211,10 +266,10 @@
         } catch (URISyntaxException e) {
             // do nothing
         }
-
+        
         tryFileSystem("", uriStr);
     }
-
+    
     private void tryJar(String uriStr) throws IOException {
         int i = uriStr.indexOf('!');
         if (i == -1) {
@@ -223,7 +278,7 @@
         uriStr = uriStr.substring(i + 1);
         tryClasspath(uriStr);
     }
-
+    
     private void tryClasspath(String uriStr) throws IOException {
         if (uriStr.startsWith("classpath:")) {
             uriStr = uriStr.substring(10);
@@ -233,17 +288,7 @@
             tryRemote(uriStr);
         } else {
             try {
-                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());
-                }
+                uri = new URI(url.toString());
             } catch (URISyntaxException e) {
                 // How would this occurr??
             }
@@ -282,12 +327,8 @@
     public File getFile() {
         return file;
     }
-
+    
     public boolean isResolved() {
         return is != null;
-    }
-
-    public String getLastImportUrl() {
-        return lastImportUrl;
     }
 }



Mime
View raw message