Commits

Anonymous committed 7b3d168

Add a URLUtil class with tests, and use it in ClassFinder

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1930e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 5f7d7bb

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork2/util/URLUtil.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.util;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import java.net.URL;
+import java.net.MalformedURLException;
+
+/**
+ * Helper class to extract file paths from different urls
+ */
+public class URLUtil {
+    public static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:)(file:)?(.*)!/(.*)");
+    private static final int JAR_FILE_PATH = 3;
+
+    /**
+     * Convert URLs to URLs with "jar" protocol
+     * @param url URL to convert to a jar url
+     * @return a URL to a jar, or null if the URL parameter is a regular file
+     */
+    public static URL getJarURL(URL url) {
+        String fileName = url.toExternalForm();
+        Matcher jarMatcher = JAR_PATTERN.matcher(fileName);
+        try {
+            if (jarMatcher.matches()) {
+                String path = jarMatcher.group(JAR_FILE_PATH);
+                return new URL("jar:file:" + path + "!/");
+            } else {
+                //it is not a jar or zip file
+                return null;
+            }
+        } catch (MalformedURLException e) {
+            //can this ever happen?
+            return null;
+        }
+    }
+}

File src/java/com/opensymphony/xwork2/util/finder/ClassFinder.java

 
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import com.opensymphony.xwork2.util.URLUtil;
 import org.objectweb.asm.AnnotationVisitor;
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.FieldVisitor;
     }
 
     private List<String> jar(URL location) throws IOException {
-        String jarPath = location.getFile();
-        if (jarPath.contains("!")){
-            jarPath = jarPath.substring(0, jarPath.indexOf("!"));
-        }
-        URL url = new URL(jarPath);
+        URL url = URLUtil.getJarURL(location);
         InputStream in = url.openStream();
         try {
             JarInputStream jarStream = new JarInputStream(in);

File src/test/com/opensymphony/xwork2/util/URLUtilTest.java

+package com.opensymphony.xwork2.util;
+
+import junit.framework.TestCase;
+
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.URLStreamHandlerFactory;
+import java.net.URLStreamHandler;
+import java.net.URLConnection;
+import java.io.IOException;
+
+public class URLUtilTest extends TestCase {
+
+    public void testSimpleFile() throws MalformedURLException {
+        URL url = new URL("file:c:/somefile.txt");
+        URL outputURL = URLUtil.getJarURL(url);
+
+        assertNull(outputURL);
+    }
+
+    public void testJarFile() throws MalformedURLException {
+        URL url = new URL("jar:file:/c:/somefile.jar!/");
+        URL outputURL = URLUtil.getJarURL(url);
+
+        assertNotNull(outputURL);
+        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+
+        url = new URL("jar:file:/c:/somefile.jar!/somestuf/bla/bla");
+        outputURL = URLUtil.getJarURL(url);
+        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+
+        url = new URL("jar:file:c:/somefile.jar!/somestuf/bla/bla");
+        outputURL = URLUtil.getJarURL(url);
+        assertEquals("jar:file:c:/somefile.jar!/", outputURL.toExternalForm());
+    }
+
+    public void testZipFile() throws MalformedURLException {
+        URL url = new URL("zip:/c:/somefile.zip!/");
+        URL outputURL = URLUtil.getJarURL(url);
+
+        assertNotNull(outputURL);
+        assertEquals("jar:file:/c:/somefile.zip!/", outputURL.toExternalForm());
+
+        url = new URL("zip:/c:/somefile.zip!/somestuf/bla/bla");
+        outputURL = URLUtil.getJarURL(url);
+        assertEquals("jar:file:/c:/somefile.zip!/", outputURL.toExternalForm());
+
+        url = new URL("zip:c:/somefile.zip!/somestuf/bla/bla");
+        outputURL = URLUtil.getJarURL(url);
+        assertEquals("jar:file:c:/somefile.zip!/", outputURL.toExternalForm());
+    }
+
+    public void testWSJarFile() throws MalformedURLException {
+        URL url = new URL("wsjar:file:/c:/somefile.jar!/");
+        URL outputURL = URLUtil.getJarURL(url);
+
+        assertNotNull(outputURL);
+        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+
+        url = new URL("wsjar:file:/c:/somefile.jar!/somestuf/bla/bla");
+        outputURL = URLUtil.getJarURL(url);
+        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+
+        url = new URL("wsjar:file:c:/somefile.jar!/somestuf/bla/bla");
+        outputURL = URLUtil.getJarURL(url);
+        assertEquals("jar:file:c:/somefile.jar!/", outputURL.toExternalForm());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        try {
+            URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
+                public URLStreamHandler createURLStreamHandler(String protocol) {
+                    return new URLStreamHandler() {
+                        protected URLConnection openConnection(URL u) throws IOException {
+                            return null;  //To change body of implemented methods use File | Settings | File Templates.
+                        }
+                    };
+                }
+            });
+        } catch (Throwable e) {
+            //the factory cant be set multiple times..just ignore exception no biggie
+        }
+    }
+
+
+}