]> source.dussan.org Git - archiva.git/commitdiff
Adding PathUtils.toURL() methods.
authorJoakim Erdfelt <joakime@apache.org>
Fri, 13 Apr 2007 15:51:41 +0000 (15:51 +0000)
committerJoakim Erdfelt <joakime@apache.org>
Fri, 13 Apr 2007 15:51:41 +0000 (15:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@528536 13f79535-47bb-0310-9956-ffa450edef68

archiva-jpox-database-refactor/archiva-base/archiva-common/pom.xml
archiva-jpox-database-refactor/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/PathUtil.java
archiva-jpox-database-refactor/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/PathUtilTest.java

index c5f3ff269855caa5497a228086ca6fe0fa6ac1e8..1dfc08037beb51a55b48750312947ddc6b09b57e 100644 (file)
   </dependencies>
   <build>
     <plugins>
-      <!--
-      <plugin>
-        <artifactId>maven-jar-plugin</artifactId>
-        <executions>
-          <execution>
-            <id>test-jar</id>
-            <goals>
-              <goal>test-jar</goal>
-            </goals>
-          </execution>
-        </executions>
-      </plugin>
-       -->
       <plugin>
         <groupId>org.codehaus.plexus</groupId>
         <artifactId>plexus-maven-plugin</artifactId>
-        <!--
-        <executions>
-          <execution>
-            <id>merge</id>
-            <goals>
-              <goal>merge-descriptors</goal>
-            </goals>
-            <configuration>
-              <descriptors>
-                <descriptor>${basedir}/src/main/resources/META-INF/plexus/components.xml</descriptor>
-                <descriptor>${project.build.directory}/generated-resources/plexus/META-INF/plexus/components.xml</descriptor>
-              </descriptors>
-            </configuration>
-          </execution>
-        </executions>
-         -->
       </plugin>
     </plugins>
   </build>
index 25df4254a8255edcc87536049b22854bc01e59dc..2116815a7d8d14b90f05ec05a2c5aea24b7b54fe 100644 (file)
@@ -19,7 +19,10 @@ package org.apache.maven.archiva.common.utils;
  * under the License.
  */
 
+import org.apache.commons.lang.StringUtils;
+
 import java.io.File;
+import java.net.MalformedURLException;
 
 /**
  * PathUtil - simple utility methods for path manipulation. 
@@ -29,6 +32,35 @@ import java.io.File;
  */
 public class PathUtil
 {
+    public static String toUrl( String path )
+    {
+        // Is our work already done for us?
+        if ( path.startsWith( "file:/" ) )
+        {
+            return path;
+        }
+        
+        return toUrl( new File( path ) );
+    }
+
+    public static String toUrl( File file )
+    {
+        try
+        {
+            return file.toURL().toExternalForm();
+        }
+        catch ( MalformedURLException e )
+        {
+            String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' );
+            if ( pathCorrected.startsWith( "file:/" ) )
+            {
+                return pathCorrected;
+            }
+
+            return "file://" + pathCorrected;
+        }
+    }
+
     public static String getRelative( String basedir, File file )
     {
         return getRelative( basedir, file.getAbsolutePath() );
index 58abbd2bf627132b375867ee42f44e74970ed789..a66951511d030ee2df3198865759480732182ca3 100644 (file)
@@ -19,7 +19,9 @@ package org.apache.maven.archiva.common.utils;
  * under the License.
  */
 
-import org.apache.maven.archiva.common.utils.PathUtil;
+import org.apache.commons.lang.StringUtils;
+
+import java.io.File;
 
 import junit.framework.TestCase;
 
@@ -37,4 +39,40 @@ public class PathUtilTest
         assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
                                                                     "/home/user/foo/repository/path/to/resource.xml" ) );
     }
+
+    public void testToUrlRelativePath()
+    {
+        File workingDir = new File( "." );
+
+        String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
+
+        // Some JVM's retain the "." at the end of the path.  Drop it.
+        if ( workingDirname.endsWith( "/." ) )
+        {
+            workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
+        }
+
+        String path = "path/to/resource.xml";
+        String expectedPath = "file:" + workingDirname + "/" + path;
+
+        assertEquals( expectedPath, PathUtil.toUrl( path ) );
+    }
+
+    public void testToUrlUsingFileUrl()
+    {
+        File workingDir = new File( "." );
+
+        String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
+
+        // Some JVM's retain the "." at the end of the path.  Drop it.
+        if ( workingDirname.endsWith( "/." ) )
+        {
+            workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
+        }
+
+        String path = "path/to/resource.xml";
+        String expectedPath = "file:" + workingDirname + "/" + path;
+
+        assertEquals( expectedPath, PathUtil.toUrl( expectedPath ) );
+    }
 }