You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2005/08/17 20:00:29 UTC

svn commit: r233221 - in /portals/jetspeed-2/trunk/commons/src: java/org/apache/jetspeed/util/Path.java test/ test/org/ test/org/apache/ test/org/apache/jetspeed/ test/org/apache/jetspeed/util/ test/org/apache/jetspeed/util/TestPathUtil.java

Author: weaver
Date: Wed Aug 17 11:00:26 2005
New Revision: 233221

URL: http://svn.apache.org/viewcvs?rev=233221&view=rev
Log:
This is a path utility i created for one of my other projects.  I based it off of the Eclipse SDK Path object.  It creates a nice, standard way to parse paths.

Added:
    portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/Path.java   (with props)
    portals/jetspeed-2/trunk/commons/src/test/
    portals/jetspeed-2/trunk/commons/src/test/org/
    portals/jetspeed-2/trunk/commons/src/test/org/apache/
    portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/
    portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/
    portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/TestPathUtil.java   (with props)

Added: portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/Path.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/Path.java?rev=233221&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/Path.java (added)
+++ portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/Path.java Wed Aug 17 11:00:26 2005
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2000-2001,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.util;
+
+import java.io.Serializable;
+import java.util.StringTokenizer;
+
+public class Path implements Serializable
+{
+    public static final String PATH_SEPERATOR = "/";
+
+    private final String path;
+
+    private final String[] segments;
+
+    private final String fileName;
+
+    private final String baseName;
+
+    private final String fileExtension;
+
+    private final String queryString;
+    
+    private final String pathOnly;
+
+    public Path(String path)
+    {
+        this.path = path;
+        String[] split = path.split("\\?");
+        pathOnly = split[0];
+        StringTokenizer t = new StringTokenizer(pathOnly, PATH_SEPERATOR);
+        segments = new String[t.countTokens()];
+
+        int i = 0;
+
+        while (t.hasMoreTokens())
+        {
+            segments[i] = t.nextToken();
+            i++;
+        }
+        fileName = segments[(segments.length - 1)];
+        int extIndex = fileName.lastIndexOf('.');
+        
+        if(extIndex > -1)
+        {
+            baseName = fileName.substring(0, extIndex);
+            fileExtension = fileName.substring(extIndex);
+        }
+        else
+        {
+            baseName =  fileName;
+            fileExtension = null;
+        }
+
+        if (split.length > 1)
+        {
+            queryString = split[1];
+        }
+        else
+        {
+            queryString = null;
+        }
+    }
+
+    public String getSegment(int i)
+    {
+        return segments[i];
+    }
+    
+    public Path getSubPath(int beginAtSegment)
+    {
+        StringBuffer newPathString = new StringBuffer();
+        for(int i=beginAtSegment; i<segments.length; i++)
+        {
+            newPathString.append("/").append(segments[i]);
+        }
+        
+        if(queryString != null)
+        {
+            newPathString.append("?").append(queryString);
+        }
+        
+        return new Path(newPathString.toString());
+    }
+
+    public String getBaseName()
+    {
+        return baseName;
+    }
+    
+
+    public String getFileExtension()
+    {
+        return fileExtension;
+    }
+    
+
+    public String getFileName()
+    {
+        return fileName;
+    }
+    
+
+    public String getQueryString()
+    {
+        return queryString;
+    }
+    
+    public int length()
+    {
+        return segments.length;
+    }    
+        
+    public String toString()
+    {
+        return path;
+    }
+    
+    public String pathOnly()
+    {
+        return pathOnly;
+    }
+
+    public boolean equals(Object obj)
+    {
+        if(obj instanceof Path)
+        {
+            return ((Path) obj).path.equals(this.path);
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public int hashCode()
+    {        
+        return (getClass().getName()+"::"+path).hashCode();
+    }
+    
+
+
+}

Propchange: portals/jetspeed-2/trunk/commons/src/java/org/apache/jetspeed/util/Path.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/TestPathUtil.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/TestPathUtil.java?rev=233221&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/TestPathUtil.java (added)
+++ portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/TestPathUtil.java Wed Aug 17 11:00:26 2005
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2000-2001,2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.util;
+
+import junit.framework.TestCase;
+
+public class TestPathUtil extends TestCase
+{
+    public void testPath()
+    {
+        Path path = new Path("/root/sub1/sub2/file.html?foo=bar&name=bob");
+        
+        Path path2 = new Path("/root/sub1/sub2/file.html?foo=bar&name=bob");
+        
+        assertEquals(path, path2);
+        
+        assertEquals(".html", path.getFileExtension());
+        assertEquals("foo=bar&name=bob", path.getQueryString());
+        assertEquals("file.html", path.getFileName());
+        assertEquals("file", path.getBaseName());
+        
+        assertEquals(4, path.length());
+        
+        assertEquals("root", path.getSegment(0));
+        assertEquals("sub1", path.getSegment(1));
+        assertEquals("sub2", path.getSegment(2));
+        assertEquals("file.html", path.getSegment(3));  
+        assertEquals("/root/sub1/sub2/file.html", path.pathOnly());
+        
+        assertEquals("/sub1/sub2/file.html?foo=bar&name=bob", path.getSubPath(1).toString());
+        
+        path = new Path("file.html");
+        assertEquals(".html", path.getFileExtension());
+        assertEquals("file.html", path.getFileName());
+        assertEquals("file", path.getBaseName());
+        
+        assertNull(path.getQueryString());
+        
+        assertEquals(1, path.length());
+        assertEquals("file.html", path.getSegment(0));  
+        
+        path = new Path("file");
+        
+        assertEquals("file", path.getBaseName());
+        
+        Path pathNoFile = new Path("/root/sub1/sub2");
+        assertEquals("root", pathNoFile.getSegment(0));
+        assertEquals("sub1", pathNoFile.getSegment(1));
+        assertEquals("sub2", pathNoFile.getSegment(2));
+        
+        assertEquals("/sub1/sub2", pathNoFile.getSubPath(1).toString());
+                
+    }
+}

Propchange: portals/jetspeed-2/trunk/commons/src/test/org/apache/jetspeed/util/TestPathUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org