kaz 02/03/16 21:20:54
Added: src/java/org/apache/maven DVSLPathTool.java
src/test/org/apache/maven DVSLPathToolTest.java
Log:
This DVSL tool provides functionality similiar to Anakia's $relativePath
context variable. DVSL currently does not provide any facilities to
obtain the relative path of a file with respece to the basedir specified
as part of the Ant task. This tool provides the getRelativePath()
(based on the same method from Anakia).
Geir is making some changes to DVSL to enable this functionality to be
used. Specifically, he is adding the name of the file that is currently
being processed by the DVSL Ant task into the context. It was decided
that $relativePath was too specific to add into the DVSLTask so we opted
to build this tool and make a more generic change to DVSLTask.
Also included is the unit test for this class.
Revision Changes Path
1.1
jakarta-turbine-maven/src/java/org/apache/maven/DVSLPathTool.java
Index: DVSLPathTool.java
===================================================================
package org.apache.maven;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.StringTokenizer;
import org.apache.commons.lang.Strings;
/**
* Path tool for use with the DVSL toolbox. This class contains static
* methods to assist in determining path-related information such as
* relative paths.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pete Kazmier</a>
* @version $Id: DVSLPathTool.java,v 1.1 2002/03/17 05:20:54 kaz Exp $
*/
public class DVSLPathTool
{
/**
* Determines the relative path of a filename from a base directory.
* This method is useful in building relative links within pages of
* a web site. It provides similar functionality to Anakia's
* <code>$relativePath</code> context variable. The arguments to
* this method may contain either forward or backward slashes as
* file separators. The relative path returned is formed using
* forward slashes as it is expected this path is to be used as a
* link in a web page (again mimicking Anakia's behavior).
* <p/>
* This method is thread-safe.
*
* @param basedir The base directory.
* @param filename The filename that is relative to the base
* directory.
* @return The relative path of the filename from the base
* directory. This value is not terminated with a forward slash.
* A zero-length string is returned if: the filename is not relative to
* the base directory, <code>basedir</code> is null or zero-length,
* or <code>filename</code> is null or zero-length.
*/
public static final String getRelativePath(String basedir, String filename)
{
/*
* Verify the arguments and make sure the filename is relative
* to the base directory.
*/
if (basedir == null || basedir.length() == 0 || filename == null ||
filename.length() == 0 || !filename.startsWith(basedir))
{
return "";
}
/*
* Normalize the arguments. First, determine the file separator
* that is being used, then strip that off the end of both the
* base directory and filename if present.
*/
String separator = determineSeparator(filename);
basedir = Strings.chompLast(basedir, separator);
filename = Strings.chompLast(filename, separator);
/*
* Remove the base directory from the filename to end up with a
* relative filename (relative to the base directory). This
* filename is then used to determine the relative path.
*/
String relativeFilename = filename.substring(basedir.length());
if (relativeFilename.length() == 0)
{
return "";
}
/*
* Count the slashes in the relative filename, but exclude the
* leading slash. If the path has no slashes, then the filename
* is relative to the current directory.
*/
int slashCount = Strings.count(relativeFilename, separator) - 1;
if (slashCount <= 0)
{
return ".";
}
/*
* The relative filename contains one or more slashes indicating
* that the file is within one or more directories. Thus, each
* slash represents a "../" in the relative path.
*/
StringBuffer sb = new StringBuffer();
for (int i = 0; i < slashCount ; i++)
{
sb.append ("../");
}
/*
* Finally, return the relative path but strip the trailing
* slash to mimic Anakia's behavior.
*/
return Strings.chop(sb.toString());
}
/**
* Helper method to determine the file separator (forward or
* backward slash) used in a filename. The slash that occurs more
* often is returned as the separator.
*
* @param filename The filename parsed to determine the file
* separator.
* @return The file separator used within <code>filename</code>.
* This value is either a forward or backward slash.
*/
private static final String determineSeparator(String filename)
{
int forwardCount = Strings.count(filename, "/");
int backwardCount = Strings.count(filename, "\\");
return forwardCount >= backwardCount ? "/" : "\\";
}
}
1.1
jakarta-turbine-maven/src/test/org/apache/maven/DVSLPathToolTest.java
Index: DVSLPathToolTest.java
===================================================================
package org.apache.maven;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for <code>DVSLPathTool</code>.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pete Kazmier</a>
* @version $Id: DVSLPathToolTest.java,v 1.1 2002/03/17 05:20:54 kaz Exp $
*/
public class DVSLPathToolTest extends TestCase
{
public DVSLPathToolTest(String testName)
{
super(testName);
}
public static Test suite()
{
return new TestSuite(DVSLPathToolTest.class);
}
/**
* Tests <code>DVSLPathTool.getRelativePath</code>.
*/
public void testGetRelativePath()
{
TestArgs[] tests =
{
new TestArgs("", "/home/www/html", ""),
new TestArgs("/", "/home/www/html", "../.."),
new TestArgs("", "/home/www/html/", ""),
new TestArgs("/", "/home/www/html/", "../.."),
new TestArgs("", "home/www/html", ""),
new TestArgs("/", "home/www/html", ""),
new TestArgs("", "home/www/html/", ""),
new TestArgs("/", "home/www/html/", ""),
new TestArgs("/", "/home", "."),
new TestArgs("/usr/local", null, ""),
new TestArgs(null, "/usr/local", ""),
new TestArgs(null, null, ""),
new TestArgs("/home/kaz", "/home/kaz", ""),
new TestArgs("/home/kaz", "/home/kaz/", ""),
new TestArgs("/home/kaz/", "/home/kaz", ""),
new TestArgs("/home/kaz/", "/home/kaz/", ""),
new TestArgs("/home/kaz", "/home/kaz/file.html", "."),
new TestArgs("/home/kaz/", "/home/kaz/file.html", "."),
new TestArgs("/home/kaz", "/home/kaz/howto/jeff.html", ".."),
new TestArgs("/home/", "/home/kaz/howto/images/", "../.."),
new TestArgs("/home/", "/home/kaz/howto/jeff.html", "../.."),
new TestArgs("/home/", "/home/kaz/howto/u/y/z/jeff.html",
"../../../../.."),
new TestArgs("/home/boston", "/usr/local/where/am/i", ""),
new TestArgs("home/boston", "/usr/local/where/am/i", ""),
new TestArgs("home/boston", "home/boston/where/am/i","../.."),
new TestArgs("", "\\home\\www\\html", ""),
new TestArgs("\\", "\\home\\www\\html", "../.."),
new TestArgs("", "\\home\\www\\html\\", ""),
new TestArgs("\\", "\\home\\www\\html\\", "../.."),
new TestArgs("", "home\\www\\html", ""),
new TestArgs("\\", "home\\www\\html", ""),
new TestArgs("", "home\\www\\html\\", ""),
new TestArgs("\\", "home\\www\\html\\", ""),
new TestArgs("\\", "\\home", "."),
new TestArgs("\\usr\\local", null, ""),
new TestArgs(null, "\\usr\\local", ""),
new TestArgs(null, null, ""),
new TestArgs("\\home\\kaz", "\\home\\kaz", ""),
new TestArgs("\\home\\kaz", "\\home\\kaz\\", ""),
new TestArgs("\\home\\kaz\\", "\\home\\kaz", ""),
new TestArgs("\\home\\kaz\\", "\\home\\kaz\\", ""),
new TestArgs("\\home\\kaz", "\\home\\kaz\\file.html", "."),
new TestArgs("\\home\\kaz\\", "\\home\\kaz\\file.html", "."),
new TestArgs("\\home\\kaz", "\\home\\kaz\\howto\\jeff.html", ".."),
new TestArgs("\\home\\", "\\home\\kaz\\howto\\images\\", "../.."),
new TestArgs("\\home\\", "\\home\\kaz\\howto\\jeff.html", "../.."),
new TestArgs("\\home\\", "\\home\\kaz\\howto\\u\\y\\z\\jeff.html",
"../../../../.."),
new TestArgs("\\home\\boston", "\\usr\\local\\where\\am\\i", ""),
new TestArgs("home\\boston", "\\usr\\local\\where\\am\\i", ""),
new TestArgs("home\\boston", "home\\boston\\where\\am\\i","../.."),
new TestArgs("/x/y/z/", "/x/y/z/a\\b\\c", "."),
new TestArgs("/x/y/z/", "\\x\\y\\z\\a\\b\\c", ""),
new TestArgs("\\x\\y\\z", "\\x\\y\\z\\a/b\\c", ".."),
new TestArgs("\\x\\y\\z/", "\\x\\y\\z/\\a/b\\c", ".."),
};
DVSLPathTool pt = new DVSLPathTool();
for (int i=0; i<tests.length; i++)
{
assertEquals(tests[i].toString(), tests[i].expected,
pt.getRelativePath(tests[i].basedir, tests[i].filename));
}
}
/**
* Value object for a relative path test case.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pete Kazmier</a>
* @version $Id: DVSLPathToolTest.java,v 1.1 2002/03/17 05:20:54 kaz Exp $
*/
private static class TestArgs
{
String basedir;
String filename;
String expected;
TestArgs(String b, String f, String e)
{
basedir = b;
filename = f;
expected = e;
}
/**
* Provide detailed information regarding the test case which
* can be used as part of the JUnit error message in the event
* of a unit test failure.
*
* @return A string describing the test parameters.
*/
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("Basedir: ");
sb.append(basedir);
sb.append(" Filename: ");
sb.append(filename);
return sb.toString();
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>