cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject cvs commit: cocoon-2.1/src/test/org/apache/cocoon/util/test NetUtilsTestCase.java
Date Thu, 29 Apr 2004 20:10:04 GMT
ugo         2004/04/29 13:10:04

  Modified:    src/java/org/apache/cocoon/util NetUtils.java
               src/test/org/apache/cocoon/util/test NetUtilsTestCase.java
  Log:
  New version of NetUtils.normalize that relies only on JDK core classes.
  
  Revision  Changes    Path
  1.14      +30 -39    cocoon-2.1/src/java/org/apache/cocoon/util/NetUtils.java
  
  Index: NetUtils.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/util/NetUtils.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- NetUtils.java	29 Apr 2004 00:20:53 -0000	1.13
  +++ NetUtils.java	29 Apr 2004 20:10:04 -0000	1.14
  @@ -19,15 +19,16 @@
   import java.io.IOException;
   import java.io.OutputStreamWriter;
   import java.io.UnsupportedEncodingException;
  -
   import java.util.BitSet;
   import java.util.Enumeration;
   import java.util.Iterator;
  +import java.util.LinkedList;
   import java.util.Map;
  +import java.util.StringTokenizer;
   
  -import org.apache.excalibur.source.SourceParameters;
   import org.apache.cocoon.environment.Request;
   import org.apache.commons.lang.StringUtils;
  +import org.apache.excalibur.source.SourceParameters;
   
   /**
    * A collection of <code>File</code>, <code>URL</code> and filename
  @@ -321,47 +322,37 @@
        * @return The normalized uri
        */
       public static String normalize(String uri) {
  -        String[] dirty = org.apache.cocoon.util.StringUtils.split(uri, "/");
  -        int length = dirty.length;
  -        String[] clean = new String[length];
  -
  -        boolean path;
  -        boolean finished;
  -        while (true) {
  -            path = false;
  -            finished = true;
  -            for (int i = 0, j = 0; (i < length) && (dirty[i] != null); i++)
{
  -                if (".".equals(dirty[i])) {
  -                    // ignore
  -                } else if ("..".equals(dirty[i])) {
  -                    clean[j++] = dirty[i];
  -                    if (path) finished = false;
  -                } else {
  -                    if ((i+1 < length) && ("..".equals(dirty[i+1]))) {
  -                        i++;
  -                    } else {
  -                        clean[j++] = dirty[i];
  -                        path = true;
  -                    }
  +        if ("".equals(uri)) {
  +            return uri;
  +        }
  +        boolean isAbs = (uri.charAt(0) == '/');
  +        boolean isDir = (uri.charAt(uri.length() - 1) == '/');
  +        StringTokenizer st = new StringTokenizer(uri, "/");
  +        LinkedList clean = new LinkedList();
  +        while (st.hasMoreTokens()) {
  +            String token = st.nextToken();
  +            if ("..".equals(token)) {
  +                if (! clean.isEmpty()) {
  +                    clean.removeLast();
                   }
  +            } else if (! ".".equals(token) && ! "".equals(token)) {
  +                clean.add(token);
               }
  -            if (finished) {
  -                break;
  -            } else {
  -                dirty = clean;
  -                clean = new String[length];
  +        }
  +        StringBuffer sb = new StringBuffer();
  +        if (isAbs) {
  +            sb.append('/');
  +        }
  +        for (Iterator it = clean.iterator() ; it.hasNext() ; ) {
  +            sb.append(it.next());
  +            if (it.hasNext()) {
  +                sb.append('/');
               }
           }
  -
  -        StringBuffer b = new StringBuffer(uri.length());
  -        
  -        for (int i = 0; (i < length) && (clean[i] != null); i++) {
  -            b.append(clean[i]);
  -            if ((i+1 < length) && (clean[i+1] != null)) {
  -                b.append("/");
  -            } 
  +        if (isDir && sb.length() > 0 && sb.charAt(sb.length() - 1) !=
'/') {
  +            sb.append('/');
           }
  -        return b.toString();
  +        return sb.toString();
       }
   
       /**
  
  
  
  1.8       +2 -2      cocoon-2.1/src/test/org/apache/cocoon/util/test/NetUtilsTestCase.java
  
  Index: NetUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/test/org/apache/cocoon/util/test/NetUtilsTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- NetUtilsTestCase.java	29 Apr 2004 00:21:50 -0000	1.7
  +++ NetUtilsTestCase.java	29 Apr 2004 20:10:04 -0000	1.8
  @@ -208,7 +208,7 @@
           Object[] test_values = {
                   new String[]{"", ""},
                   new String[]{"/", "/"},
  -                new String[]{"/../", "/../"},
  +                new String[]{"/../", "/"},
                   new String[]{"/foo/bar", "/foo/bar"},
                   new String[]{"/foo/bar/", "/foo/bar/"},
                   new String[]{"/foo/../bar", "/bar"},
  
  
  

Mime
View raw message