velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r689660 - in /velocity/tools/trunk/src/main/java/org/apache/velocity/tools: ConversionUtils.java config/FileFactoryConfiguration.java generic/XmlTool.java
Date Wed, 27 Aug 2008 23:24:23 GMT
Author: nbubna
Date: Wed Aug 27 16:24:22 2008
New Revision: 689660

URL: http://svn.apache.org/viewvc?rev=689660&view=rev
Log:
move string->url conversion to ConversionUtils

Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ConversionUtils.java
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/FileFactoryConfiguration.java
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/XmlTool.java

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ConversionUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ConversionUtils.java?rev=689660&r1=689659&r2=689660&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ConversionUtils.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ConversionUtils.java Wed
Aug 27 16:24:22 2008
@@ -19,6 +19,8 @@
  * under the License.
  */
 
+import java.io.File;
+import java.net.URL;
 import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
@@ -588,4 +590,63 @@
             return null;
         }
     }
+
+    /**
+     * Converts a string to a {@link URL}.  It will first try to
+     * treat the string as a File name, then a classpath resource,
+     * then finally as a literal URL.  If none of these work, then
+     * this will return {@code null}.
+     *
+     * @param value - the string to parse
+     * @return the {@link URL} form of the string or {@code null}
+     * @see File
+     * @see ClassUtils#getResource(String,Object)
+     * @see URL
+     */
+    public static URL toURL(String name)
+    {
+        return toURL(name, ConversionUtils.class);
+    }
+
+    /**
+     * Converts a string to a {@link URL}.  It will first try to
+     * treat the string as a File name, then a classpath resource,
+     * then finally as a literal URL.  If none of these work, then
+     * this will return {@code null}.
+     *
+     * @param value - the string to parse
+     * @param caller - the object or Class seeking the url
+     * @return the {@link URL} form of the string or {@code null}
+     * @see File
+     * @see ClassUtils#getResource(String,Object)
+     * @see URL
+     */
+    public static URL toURL(String name, Object caller)
+    {
+        try
+        {
+            File file = new File(name);
+            if (file.exists())
+            {
+                return file.toURI().toURL();
+            }
+        }
+        catch (Exception e) {}
+        try
+        {
+            URL url = ClassUtils.getResource(name, caller);
+            if (url != null)
+            {
+                return url;
+            }
+        }
+        catch (Exception e) {}
+        try
+        {
+            return new URL(name);
+        }
+        catch (Exception e) {}
+        return null;
+    }
+
 }

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/FileFactoryConfiguration.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/FileFactoryConfiguration.java?rev=689660&r1=689659&r2=689660&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/FileFactoryConfiguration.java
(original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/config/FileFactoryConfiguration.java
Wed Aug 27 16:24:22 2008
@@ -19,14 +19,12 @@
  * under the License.
  */
 
-import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
-import java.net.MalformedURLException;
 import java.net.URL;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.runtime.log.Log;
-import org.apache.velocity.tools.ClassUtils;
+import org.apache.velocity.tools.ConversionUtils;
 
 /**
  * Provides support for reading a configuration file from a specified path,
@@ -108,39 +106,7 @@
 
     protected URL findURL(String path)
     {
-        // first, try the file system
-        File file = new File(path);
-        if (file.exists())
-        {
-            try
-            {
-                return file.toURI().toURL();
-            }
-            catch (MalformedURLException mue)
-            {
-                // this doesn't seem like it should happen if the file exists
-                // but i may be wrong, in which case we should change this to
-                // just log a debug message
-                throw new IllegalStateException("Could not convert existing file path \""+path+"\"
to URL", mue);
-            }
-        }
-        
-        // then search the classpath
-        URL url = ClassUtils.getResource(path, this);
-        if (url != null)
-        {
-            return url;
-        }
-
-        // finally, just try directly turning it into a URL
-        try
-        {
-            return new URL(path);
-        }
-        catch (MalformedURLException mue)
-        {
-            return null;
-        }
+        return ConversionUtils.toURL(path, this);
     }
 
     protected void read(URL url, boolean required, Log log)

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/XmlTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/XmlTool.java?rev=689660&r1=689659&r2=689660&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/XmlTool.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/XmlTool.java Wed
Aug 27 16:24:22 2008
@@ -19,7 +19,6 @@
  * under the License.
  */
 
-import java.io.File;
 import java.io.StringWriter;
 import java.net.URL;
 import java.util.ArrayList;
@@ -36,7 +35,6 @@
 import org.dom4j.io.XMLWriter;
 import org.dom4j.io.SAXReader;
 import org.apache.velocity.runtime.log.Log;
-import org.apache.velocity.tools.ClassUtils;
 import org.apache.velocity.tools.ConversionUtils;
 import org.apache.velocity.tools.ToolContext;
 import org.apache.velocity.tools.config.DefaultKey;
@@ -114,6 +112,10 @@
             {
                 read(file);
             }
+            catch (IllegalArgumentException iae)
+            {
+                throw iae;
+            }
             catch (Exception e)
             {
                 throw new RuntimeException("Could not read XML file at: "+file, e);
@@ -134,23 +136,6 @@
         this.nodes.add(node);
     }
 
-    //FIXME: dupe of FileFactoryConfiguration; move to one place
-    private URL getURL(String name) throws Exception
-    {
-        //TODO: grab the VelocityEngine so we can read files from there?
-        File file = new File(name);
-        if (file.exists())
-        {
-            return file.toURI().toURL();
-        }
-        URL url = ClassUtils.getResource(name, this);
-        if (url != null)
-        {
-            return url;
-        }
-        return new URL(name);
-    }
-
     private void log(Object o, Throwable t)
     {
         if (LOG != null)
@@ -164,7 +149,12 @@
      */
     protected void read(String file) throws Exception
     {
-        read(getURL(file));
+        URL url = ConversionUtils.toURL(file, this);
+        if (url == null)
+        {
+            throw new IllegalArgumentException("Could not find file, classpath resource or
standard URL for '"+file+"'.");
+        }
+        read(url);
     }
 
     /**



Mime
View raw message