kaz 02/03/17 20:38:49
Modified: src/java/org/apache/maven DVSLPathTool.java
src/test/org/apache/maven DVSLPathToolTest.java
Log:
- Added a new public method to DVSLPathTool that accepts a filename (the
other method accepts both a base directory and filename).
- Updated the unit tests to test this method as well.
Revision Changes Path
1.2 +62 -4
jakarta-turbine-maven/src/java/org/apache/maven/DVSLPathTool.java
Index: DVSLPathTool.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/DVSLPathTool.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DVSLPathTool.java 17 Mar 2002 05:20:54 -0000 1.1
+++ DVSLPathTool.java 18 Mar 2002 04:38:49 -0000 1.2
@@ -63,7 +63,7 @@
* 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 $
+ * @version $Id: DVSLPathTool.java,v 1.2 2002/03/18 04:38:49 kaz Exp $
*/
public class DVSLPathTool
{
@@ -103,7 +103,7 @@
/*
* 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.
+ * base directory and filename.
*/
String separator = determineSeparator(filename);
basedir = Strings.chompLast(basedir, separator);
@@ -115,7 +115,65 @@
* filename is then used to determine the relative path.
*/
String relativeFilename = filename.substring(basedir.length());
- if (relativeFilename.length() == 0)
+
+ return determineRelativePath(relativeFilename, separator);
+ }
+
+ /**
+ * Determines the relative path of a filename. 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 argument 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 filename The filename to be parsed.
+ * @return The relative path of the filename. This value is not
+ * terminated with a forward slash. A zero-length string is
+ * returned if: <code>filename</code> is null or zero-length.
+ */
+ public static final String getRelativePath(String filename)
+ {
+ if (filename == null || filename.length() == 0)
+ {
+ return "";
+ }
+
+ /*
+ * Normalize the argument. First, determine the file separator
+ * that is being used, then strip that off the end of the
+ * filename. Then, if the filename doesn't begin with a
+ * separator, add one.
+ */
+ String separator = determineSeparator(filename);
+ filename = Strings.chompLast(filename, separator);
+ if (! filename.startsWith(separator))
+ {
+ filename = separator + filename;
+ }
+
+ return determineRelativePath(filename, separator);
+ }
+
+ /**
+ * Determines the relative path of a filename. For each separator
+ * within the filename (except the leading if present), append the
+ * "../" string to the return value.
+ *
+ * @param filename The filename to parse.
+ * @param separator The separator used within the filename.
+ * @return The relative path of the filename. This value is not
+ * terminated with a forward slash. A zero-length string is
+ * returned if: the filename is zero-length.
+ */
+ private static final String determineRelativePath(String filename,
+ String separator)
+ {
+ if (filename.length() == 0)
{
return "";
}
@@ -125,7 +183,7 @@
* leading slash. If the path has no slashes, then the filename
* is relative to the current directory.
*/
- int slashCount = Strings.count(relativeFilename, separator) - 1;
+ int slashCount = Strings.count(filename, separator) - 1;
if (slashCount <= 0)
{
return ".";
1.2 +95 -58
jakarta-turbine-maven/src/test/org/apache/maven/DVSLPathToolTest.java
Index: DVSLPathToolTest.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-maven/src/test/org/apache/maven/DVSLPathToolTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DVSLPathToolTest.java 17 Mar 2002 05:20:54 -0000 1.1
+++ DVSLPathToolTest.java 18 Mar 2002 04:38:49 -0000 1.2
@@ -62,7 +62,7 @@
* 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 $
+ * @version $Id: DVSLPathToolTest.java,v 1.2 2002/03/18 04:38:49 kaz Exp $
*/
public class DVSLPathToolTest extends TestCase
{
@@ -83,62 +83,77 @@
{
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/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/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", ".."),
+ "../../../../..", "../../../../../.."),
+ 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();
@@ -148,25 +163,47 @@
assertEquals(tests[i].toString(), tests[i].expected,
pt.getRelativePath(tests[i].basedir, tests[i].filename));
}
+
+ for (int i=0; i<tests.length; i++)
+ {
+ assertEquals(tests[i].toString(), tests[i].expectedNoBase,
+ pt.getRelativePath(tests[i].filename));
+ }
}
/**
- * Value object for a relative path test case.
+ * Value object for a relative path test case. This class is used
+ * when testing the <code>getRelativePath(basedir, filename)</code>
+ * and <code>getRelativePath(filename)</code> methods. The expected
+ * results for each method are different which is the reason that
+ * both expected values are stored as part of the class.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pete Kazmier</a>
- * @version $Id: DVSLPathToolTest.java,v 1.1 2002/03/17 05:20:54 kaz Exp $
+ * @version $Id: DVSLPathToolTest.java,v 1.2 2002/03/18 04:38:49 kaz Exp $
*/
private static class TestArgs
{
String basedir;
String filename;
String expected;
+ String expectedNoBase;
- TestArgs(String b, String f, String e)
+ /**
+ * Constructor.
+ *
+ * @param b The base directory.
+ * @param f The filename.
+ * @param e The expected result when calling
+ * <code>getRelativePath(basedir, filename)</code>.
+ * @param enb The expected result when calling
+ * <code>getRelativePath(filename)</code>.
+ */
+ TestArgs(String b, String f, String e, String enb)
{
basedir = b;
filename = f;
expected = e;
+ expectedNoBase = enb;
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>