1 16 package org.apache.cocoon.forms.formmodel.tree; 17 18 import java.util.StringTokenizer ; 19 20 25 public class TreePath { 26 27 public static final TreePath ROOT_PATH = new TreePath(); 28 32 private TreePath parentPath; 33 34 35 private String key; 36 37 38 private String cachedToString; 39 40 44 private TreePath() { 45 this.key = ""; 46 this.cachedToString = "/"; 47 } 48 49 53 public TreePath(String key) { 54 if (key == null || key.length() == 0) { 55 throw new IllegalArgumentException ("key must be non empty."); 56 } 57 58 if (key.indexOf('/') != -1) { 59 throw new IllegalArgumentException ("key cannot contain a '/'"); 60 } 61 this.key = key; 62 parentPath = ROOT_PATH; 63 } 64 65 69 public TreePath(TreePath parent, String key) { 70 this(key); 71 if (parent == null) { 72 throw new IllegalArgumentException ("Parent path must be non null."); 73 } 74 this.parentPath = parent; 75 } 76 77 84 public Object [] getObjectPath(TreeModel model) { 85 int i = getPathCount(); 86 Object [] result = new Object [i--]; 87 88 for (TreePath path = this; path != null; path = path.parentPath) { 89 result[i--] = path.getLastPathObject(model); 90 } 91 return result; 92 } 93 94 101 public Object getLastPathObject(TreeModel model) { 102 Object parent; 103 if (this.parentPath == ROOT_PATH) { 104 parent = model.getRoot(); 105 } else { 106 parent = this.parentPath.getLastPathObject(model); 107 } 108 return model.getChild(parent, this.key); 109 } 110 111 116 public int getPathCount() { 117 int result = 0; 118 for (TreePath path = this; path != null; path = path.parentPath) { 119 result++; 120 } 121 return result; 122 } 123 124 155 public boolean equals(Object obj) { 156 if (obj == this) { 157 return true; 158 } 159 if (!(obj instanceof TreePath)) { 160 return false; 161 } 162 163 TreePath otherPath = (TreePath)obj; 164 165 if (getPathCount() != otherPath.getPathCount()) { 166 return false; 167 } 168 169 TreePath path = this; 170 do { 171 if (otherPath == null || !path.key.equals(otherPath.key)) { 172 return false; 173 } 174 path = path.parentPath; 175 otherPath = otherPath.parentPath; 176 } while(path != null); 177 178 return true; 179 } 180 181 public int hashCode() { 182 return key.hashCode(); 184 } 185 186 197 public boolean isDescendant(TreePath aTreePath) { 198 if (aTreePath == this) 199 return true; 200 201 if (aTreePath != null) { 202 int pathLength = getPathCount(); 203 int oPathLength = aTreePath.getPathCount(); 204 205 if (oPathLength < pathLength) 206 return false; 208 while (oPathLength-- > pathLength) 209 aTreePath = aTreePath.getParentPath(); 210 return equals(aTreePath); 211 } 212 return false; 213 } 214 215 228 232 public TreePath getParentPath() { 233 return parentPath; 234 } 235 236 239 public String getLastKey() { 240 return this.key; 241 } 242 243 248 public String toString() { 249 if (this.cachedToString == null) { 250 StringBuffer buf = new StringBuffer (); 251 appendTo(buf); 252 this.cachedToString = buf.toString(); 253 } 254 return this.cachedToString; 255 } 256 257 258 private void appendTo(StringBuffer buf) { 259 if (this.parentPath != ROOT_PATH) { 260 this.parentPath.appendTo(buf); 261 } 262 buf.append('/'); 263 buf.append(this.key); 264 } 265 266 273 public static TreePath valueOf(String s) { 274 if (s == null || s.length() == 0) { 276 throw new IllegalArgumentException ("Invalid empty string"); 277 } 278 StringTokenizer stok = new StringTokenizer (s, "/"); 279 TreePath current = ROOT_PATH; 280 while (stok.hasMoreTokens()) { 281 String tok = stok.nextToken(); 282 current = current == null ? new TreePath(tok) : new TreePath(current, tok); 283 } 284 285 return current; 286 } 287 288 public Object getObject(TreeModel model) { 289 return this.parentPath == null ? 290 model.getRoot() : 291 model.getChild(this.parentPath.getObject(model), this.key); 292 } 293 } 294 | Popular Tags |