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
This commit is contained in:
Joakim Erdfelt 2007-04-13 15:51:41 +00:00
parent 2d359dfb0e
commit 68a076bf26
3 changed files with 71 additions and 30 deletions

View File

@ -50,38 +50,9 @@
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<!--
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
-->
<plugin> <plugin>
<groupId>org.codehaus.plexus</groupId> <groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-maven-plugin</artifactId> <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> </plugin>
</plugins> </plugins>
</build> </build>

View File

@ -19,7 +19,10 @@
* under the License. * under the License.
*/ */
import org.apache.commons.lang.StringUtils;
import java.io.File; import java.io.File;
import java.net.MalformedURLException;
/** /**
* PathUtil - simple utility methods for path manipulation. * PathUtil - simple utility methods for path manipulation.
@ -29,6 +32,35 @@
*/ */
public class PathUtil 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 ) public static String getRelative( String basedir, File file )
{ {
return getRelative( basedir, file.getAbsolutePath() ); return getRelative( basedir, file.getAbsolutePath() );

View File

@ -19,7 +19,9 @@
* under the License. * 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; import junit.framework.TestCase;
@ -37,4 +39,40 @@ public void testToRelative()
assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository", assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
"/home/user/foo/repository/path/to/resource.xml" ) ); "/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 ) );
}
} }