1 23 24 package org.apache.slide.common; 25 26 import java.util.StringTokenizer ; 27 28 33 public final class UriPath { 34 35 private String [] tokens; 36 37 private UriPath() { 38 } 39 40 private UriPath(String [] tokens) { 41 this.tokens = tokens; 42 } 43 44 public UriPath(String uri) { 45 StringTokenizer t = new StringTokenizer ( uri, "/" ); 46 this.tokens = new String [t.countTokens()]; 47 for (int i = 0; t.hasMoreTokens(); i++) { 48 tokens[i] = t.nextToken(); 49 } 50 } 51 52 public String [] tokens() { 53 return tokens; 54 } 55 56 public String lastSegment() { 57 return tokens.length > 0 58 ? tokens[ tokens.length - 1 ] 59 : null; 60 } 61 62 public UriPath parent() { 63 if (this.tokens.length == 0) { 64 return null; 65 } 66 return subUriPath(0, tokens.length - 1); 67 } 68 69 public UriPath child( String segment ) { 70 String [] ctokens = new String [tokens.length + 1]; 71 for (int i = 0; i < tokens.length; i++) { 72 ctokens[i] = tokens[i]; 73 } 74 ctokens[tokens.length] = segment; 75 return new UriPath(ctokens); 76 } 77 78 public UriPath subUriPath(int start, int end) { 79 UriPath result = new UriPath(); 80 result.tokens = new String [end - start]; 81 System.arraycopy(tokens, start, result.tokens, 0, result.tokens.length); 82 return result; 83 } 84 85 public boolean equals(Object o) { 86 boolean result = false; 87 if (o instanceof UriPath) { 88 UriPath other = (UriPath)o; 89 if (other.tokens.length == this.tokens.length) { 90 result = true; 91 for (int i = 0; i < this.tokens.length; i++) { 92 if (!other.tokens[i].equals(this.tokens[i])) { 93 result = false; 94 break; 95 } 96 } 97 } 98 } 99 return result; 100 } 101 102 public int hashCode() { 103 if (tokens.length > 0) { 104 return tokens[tokens.length - 1].hashCode(); 105 } 106 else { 107 return 0; 108 } 109 } 110 111 public String toString() { 112 if (tokens.length > 0) { 113 StringBuffer b = new StringBuffer (); 114 for (int i = 0; i < tokens.length; i++) { 115 b.append("/").append(tokens[i]); 116 } 117 return b.toString(); 118 } 119 else { 120 return "/"; 121 } 122 } 123 } 124 | Popular Tags |