[jboss-cvs] JBossAS SVN: r66716 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/vfs/helpers and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Nov 4 09:37:24 EST 2007
Author: alesj
Date: 2007-11-04 09:37:23 -0500 (Sun, 04 Nov 2007)
New Revision: 66716
Modified:
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/helpers/PathTokenizer.java
projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VirtualFileUnitTestCase.java
Log:
Reverse path on simple find path.
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java 2007-11-04 10:53:31 UTC (rev 66715)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java 2007-11-04 14:37:23 UTC (rev 66716)
@@ -315,10 +315,12 @@
if (path.length() == 0)
return this;
+ // check for reverse .. path
+ String appliedPath = PathTokenizer.applyReversePaths(path);
List<VirtualFileHandler> children = getChildren(false);
for (VirtualFileHandler child : children)
{
- if (child.getName().equals(path))
+ if (child.getName().equals(appliedPath))
return child;
}
throw new IOException("Child not found " + path + " for " + this);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/helpers/PathTokenizer.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/helpers/PathTokenizer.java 2007-11-04 10:53:31 UTC (rev 66715)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/helpers/PathTokenizer.java 2007-11-04 14:37:23 UTC (rev 66716)
@@ -22,6 +22,7 @@
package org.jboss.virtual.plugins.vfs.helpers;
import java.util.StringTokenizer;
+import java.io.IOException;
/**
* PathTokenizer.
@@ -40,8 +41,37 @@
private PathTokenizer()
{
}
-
+
/**
+ * Get the remaining path from some tokens
+ *
+ * @param tokens the tokens
+ * @param i the current location
+ * @param end the end index
+ * @return the remaining path
+ * @throws IllegalArgumentException for null tokens or i is out of range
+ */
+ protected static String getRemainingPath(String[] tokens, int i, int end)
+ {
+ if (tokens == null)
+ throw new IllegalArgumentException("Null tokens");
+ if (i < 0 || i >= end)
+ throw new IllegalArgumentException("i is not in the range of tokens: 0-" + (end-1));
+
+ if (i == end-1)
+ return tokens[end-1];
+
+ StringBuilder buffer = new StringBuilder();
+ for (; i < end-1; ++i)
+ {
+ buffer.append(tokens[i]);
+ buffer.append("/");
+ }
+ buffer.append(tokens[end-1]);
+ return buffer.toString();
+ }
+
+ /**
* Get the tokens
*
* @param path the path
@@ -86,20 +116,35 @@
{
if (tokens == null)
throw new IllegalArgumentException("Null tokens");
- if (i < 0 || i >= tokens.length)
- throw new IllegalArgumentException("i is not in the range of tokens: 0-" + (tokens.length-1));
-
- if (i == tokens.length-1)
- return tokens[tokens.length-1];
-
- StringBuilder buffer = new StringBuilder();
- for (; i < tokens.length-1; ++i)
+
+ return getRemainingPath(tokens, i, tokens.length);
+ }
+
+ /**
+ * Apply any .. paths in the path param.
+ *
+ * @param path the path
+ * @return simple path, containing no .. paths
+ * @throws IOException if reverse path goes over the top path
+ */
+ public static String applyReversePaths(String path) throws IOException
+ {
+ String[] tokens = getTokens(path);
+ if (tokens == null)
+ return null;
+
+ int i = 0;
+ for(int j = 0; j < tokens.length; j++)
{
- buffer.append(tokens[i]);
- buffer.append("/");
+ if (isReverseToken(tokens[j]))
+ i--;
+ else
+ tokens[i++] = tokens[j];
+
+ if (i < 0)
+ throw new IOException("Using reverse path on top path: " + path);
}
- buffer.append(tokens[tokens.length-1]);
- return buffer.toString();
+ return getRemainingPath(tokens, 0, i);
}
/**
Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VirtualFileUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VirtualFileUnitTestCase.java 2007-11-04 10:53:31 UTC (rev 66715)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VirtualFileUnitTestCase.java 2007-11-04 14:37:23 UTC (rev 66716)
@@ -1223,6 +1223,36 @@
assertFindChild(root, "folder3/child3", child3);
}
+ public void testFindSimpleReverse() throws Exception
+ {
+ MockVFSContext context = registerSimpleVFSContextWithChildrenAndNonLeafs();
+ VirtualFile child1 = getChildHandler(context, "folder1/child1").getVirtualFile();
+ VirtualFile child2 = getChildHandler(context, "folder2/child2").getVirtualFile();
+ VirtualFile child3 = getChildHandler(context, "folder3/child3").getVirtualFile();
+
+ VirtualFile root = VFS.getRoot(context.getRootURI());
+
+ assertFindChild(root, "", root);
+ assertFindChild(root, "folder2/../folder1/child1", child1);
+ assertFindChild(root, "folder3/child1/../../folder2/child2", child2);
+ try
+ {
+ assertFindChild(root, "../folder3/child3", child3);
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IOException.class, e);
+ }
+ try
+ {
+ assertFindChild(root, "folder2/../../folder3/child3", child3);
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IOException.class, e);
+ }
+ }
+
public void testFindChildSubChildren() throws Exception
{
MockVFSContext context = registerStructuredVFSContextWithSubChildren();
More information about the jboss-cvs-commits
mailing list