From 68a076bf26303c563ce582da36b89e7779d4910a Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 13 Apr 2007 15:51:41 +0000 Subject: [PATCH] Adding PathUtils.toURL() methods. git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@528536 13f79535-47bb-0310-9956-ffa450edef68 --- archiva-base/archiva-common/pom.xml | 29 -------------- .../maven/archiva/common/utils/PathUtil.java | 32 +++++++++++++++ .../archiva/common/utils/PathUtilTest.java | 40 ++++++++++++++++++- 3 files changed, 71 insertions(+), 30 deletions(-) diff --git a/archiva-base/archiva-common/pom.xml b/archiva-base/archiva-common/pom.xml index c5f3ff269..1dfc08037 100644 --- a/archiva-base/archiva-common/pom.xml +++ b/archiva-base/archiva-common/pom.xml @@ -50,38 +50,9 @@ - org.codehaus.plexus plexus-maven-plugin - diff --git a/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/PathUtil.java b/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/PathUtil.java index 25df4254a..2116815a7 100644 --- a/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/PathUtil.java +++ b/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/PathUtil.java @@ -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() ); diff --git a/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/PathUtilTest.java b/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/PathUtilTest.java index 58abbd2bf..a66951511 100644 --- a/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/PathUtilTest.java +++ b/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/PathUtilTest.java @@ -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 ) ); + } } -- 2.39.5