1 22 package org.jboss.virtual.plugins.vfs.helpers; 23 24 import java.util.StringTokenizer ; 25 26 32 public class PathTokenizer 33 { 34 37 private PathTokenizer() 38 { 39 } 40 41 48 public static String [] getTokens(String path) 49 { 50 if (path == null) 51 throw new IllegalArgumentException ("Null path"); 52 53 StringTokenizer tokenizer = new StringTokenizer (path, "/"); 54 int count = tokenizer.countTokens(); 55 if (count == 0) 56 return null; 57 58 String [] tokens = new String [count]; 59 int i = 0; 60 while (tokenizer.hasMoreTokens()) 61 { 62 String token = tokenizer.nextToken(); 63 64 if (token.equals("")) 65 throw new IllegalArgumentException ("A path element is empty: " + path); 66 if (token.equals(".") || token.equals("..")) 67 throw new IllegalArgumentException ("Reverse paths are not allowed (containing a . or ..), use getParent(): " + path); 68 69 tokens[i++] = token; 70 } 71 return tokens; 72 } 73 74 82 public static String getRemainingPath(String [] tokens, int i) 83 { 84 if (tokens == null) 85 throw new IllegalArgumentException ("Null tokens"); 86 if (i < 0 || i >= tokens.length) 87 throw new IllegalArgumentException ("i is not in the range of tokens: 0" + (tokens.length-1)); 88 89 if (i == tokens.length-1) 90 return tokens[tokens.length-1]; 91 92 StringBuilder buffer = new StringBuilder (); 93 for (; i < tokens.length-1; ++i) 94 { 95 buffer.append(tokens[i]); 96 buffer.append("/"); 97 } 98 buffer.append(tokens[tokens.length-1]); 99 return buffer.toString(); 100 } 101 } 102 | Popular Tags |