1 17 package org.alfresco.service.cmr.repository; 18 19 import java.io.Serializable ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.security.permissions.AccessDeniedException; 25 import org.alfresco.service.namespace.NamespacePrefixResolver; 26 import org.alfresco.service.namespace.QName; 27 import org.alfresco.util.ISO9075; 28 29 54 public final class Path implements Iterable <Path.Element>, Serializable 55 { 56 private static final long serialVersionUID = 3905520514524328247L; 57 private LinkedList <Element> elements; 58 59 public Path() 60 { 61 elements = new LinkedList <Element>(); 63 } 64 65 68 public Iterator <Path.Element> iterator() 69 { 70 return elements.iterator(); 71 } 72 73 80 public Path prepend(Path.Element pathElement) 81 { 82 elements.addFirst(pathElement); 83 return this; 84 } 85 86 92 public Path prepend(Path path) 93 { 94 elements.addAll(0, path.elements); 95 return this; 96 } 97 98 104 public Path append(Path.Element pathElement) 105 { 106 elements.addLast(pathElement); 107 return this; 108 } 109 110 116 public Path append(Path path) 117 { 118 elements.addAll(path.elements); 119 return this; 120 } 121 122 125 public Element first() 126 { 127 return elements.getFirst(); 128 } 129 130 133 public Element last() 134 { 135 return elements.getLast(); 136 } 137 138 public int size() 139 { 140 return elements.size(); 141 } 142 143 public Element get(int n) 144 { 145 return elements.get(n); 146 } 147 148 151 public String toString() 152 { 153 StringBuilder sb = new StringBuilder (128); 154 for (Element element : elements) 155 { 156 if((sb.length() > 1) || ((sb.length() == 1) && (sb.charAt(0) != '/'))) 157 { 158 sb.append("/"); 159 } 160 sb.append(element.getElementString()); 161 } 162 return sb.toString(); 163 } 164 165 168 public String toPrefixString(NamespacePrefixResolver resolver) 169 { 170 StringBuilder sb = new StringBuilder (128); 171 for (Element element : elements) 172 { 173 if((sb.length() > 1) || ((sb.length() == 1) && (sb.charAt(0) != '/'))) 174 { 175 sb.append("/"); 176 } 177 sb.append(element.getPrefixedString(resolver)); 178 } 179 return sb.toString(); 180 } 181 182 188 public String toDisplayPath(NodeService nodeService) 189 { 190 StringBuilder buf = new StringBuilder (64); 191 192 for (int i=0; i<elements.size()-1; i++) 193 { 194 String elementString = null; 195 Element element = elements.get(i); 196 if (element instanceof ChildAssocElement) 197 { 198 ChildAssociationRef elementRef = ((ChildAssocElement)element).getRef(); 199 if (elementRef.getParentRef() != null) 200 { 201 Serializable nameProp = null; 202 try 203 { 204 nameProp = nodeService.getProperty(elementRef.getChildRef(), ContentModel.PROP_NAME); 205 } 206 catch (AccessDeniedException err) 207 { 208 } 210 if (nameProp != null) 211 { 212 elementString = nameProp.toString(); 214 } 215 else 216 { 217 elementString = elementRef.getQName().getLocalName(); 219 } 220 } 221 } 222 else 223 { 224 elementString = element.getElementString(); 225 } 226 227 if (elementString != null) 228 { 229 buf.append("/"); 230 buf.append(elementString); 231 } 232 } 233 234 return buf.toString(); 235 } 236 237 243 public Path subPath(int depth) 244 { 245 return subPath(0, depth); 246 } 247 248 254 public Path subPath(int start, int end) 255 { 256 if (start < 0 || start > (elements.size() -1)) 257 { 258 throw new IndexOutOfBoundsException ("Start index " + start + " must be between 0 and " + (elements.size() -1)); 259 } 260 if (end < 0 || end > (elements.size() -1)) 261 { 262 throw new IndexOutOfBoundsException ("End index " + end + " must be between 0 and " + (elements.size() -1)); 263 } 264 if (end < start) 265 { 266 throw new IndexOutOfBoundsException ("End index " + end + " cannot be before start index " + start); 267 } 268 Path subPath = new Path(); 269 for (int i = start; i <= end; i++) 270 { 271 subPath.append(this.get(i)); 272 } 273 return subPath; 274 } 275 276 279 public boolean equals(Object o) 280 { 281 if(o == this) 282 { 283 return true; 284 } 285 if(!(o instanceof Path)) 286 { 287 return false; 288 } 289 Path other = (Path)o; 290 return this.elements.equals(other.elements); 291 } 292 293 296 public int hashCode() 297 { 298 return elements.hashCode(); 299 } 300 301 306 public abstract static class Element implements Serializable 307 { 308 311 public abstract String getElementString(); 312 313 317 public String getPrefixedString(NamespacePrefixResolver resolver) 318 { 319 return getElementString(); 320 } 321 322 325 public String toString() 326 { 327 return getElementString(); 328 } 329 } 330 331 335 public static class ChildAssocElement extends Element 336 { 337 private static final long serialVersionUID = 3689352104636790840L; 338 339 private ChildAssociationRef ref; 340 341 344 public ChildAssocElement(ChildAssociationRef ref) 345 { 346 this.ref = ref; 347 } 348 349 @Override 350 public String getElementString() 351 { 352 return createElementString(null); 353 } 354 355 @Override 356 public String getPrefixedString(NamespacePrefixResolver resolver) 357 { 358 return createElementString(resolver); 359 } 360 361 public ChildAssociationRef getRef() 362 { 363 return ref; 364 } 365 366 @Override 367 public boolean equals(Object o) 368 { 369 if(o == this) 370 { 371 return true; 372 } 373 if(!(o instanceof ChildAssocElement)) 374 { 375 return false; 376 } 377 ChildAssocElement other = (ChildAssocElement)o; 378 return this.ref.equals(other.ref); 379 } 380 381 @Override 382 public int hashCode() 383 { 384 return ref.hashCode(); 385 } 386 387 private String createElementString(NamespacePrefixResolver resolver) 388 { 389 StringBuilder sb = new StringBuilder (32); 390 if (ref.getParentRef() == null) 391 { 392 sb.append("/"); 393 } 394 else 395 { 396 sb.append(resolver == null ? ISO9075.getXPathName(ref.getQName()) : ISO9075.getXPathName(ref.getQName(), resolver)); 398 } 399 if (ref.getNthSibling() > -1) 400 { 401 sb.append("[").append(ref.getNthSibling()).append("]"); 402 } 403 return sb.toString(); 404 } 405 } 406 407 411 public static class AttributeElement extends Element 412 { 413 private static final long serialVersionUID = 3256727281668863544L; 414 415 private QName attribute; 416 private int position = -1; 417 418 421 public AttributeElement(QName attribute) 422 { 423 this.attribute = attribute; 424 } 425 426 public AttributeElement(QName attribute, int position) 427 { 428 this(attribute); 429 this.position = position; 430 } 431 432 @Override 433 public String getElementString() 434 { 435 return createElementString(null); 436 } 437 438 @Override 439 public String getPrefixedString(NamespacePrefixResolver resolver) 440 { 441 return createElementString(resolver); 442 } 443 444 private String createElementString(NamespacePrefixResolver resolver) 445 { 446 StringBuilder sb = new StringBuilder (32); 447 sb.append("@").append(resolver == null ? ISO9075.getXPathName(attribute) : ISO9075.getXPathName(attribute, resolver)); 448 449 if (position > -1) 450 { 451 sb.append("[").append(position).append("]"); 452 } 453 return sb.toString(); 454 } 455 456 public QName getQName() 457 { 458 return attribute; 459 } 460 461 public int position() 462 { 463 return position; 464 } 465 466 public boolean equals(Object o) 467 { 468 if(o == this) 469 { 470 return true; 471 } 472 if(!(o instanceof AttributeElement)) 473 { 474 return false; 475 } 476 AttributeElement other = (AttributeElement)o; 477 return this.getQName().equals(other.getQName()) && (this.position() == other.position()); 478 } 479 480 public int hashCode() 481 { 482 return getQName().hashCode()*32 + position(); 483 } 484 485 } 486 487 490 public static class DescendentOrSelfElement extends Element 491 { 492 private static final long serialVersionUID = 3258410616875005237L; 493 494 public String getElementString() 495 { 496 return "descendant-or-self::node()"; 497 } 498 499 public boolean equals(Object o) 500 { 501 if(o == this) 502 { 503 return true; 504 } 505 if(!(o instanceof DescendentOrSelfElement)) 506 { 507 return false; 508 } 509 return true; 510 } 511 512 public int hashCode() 513 { 514 return "descendant-or-self::node()".hashCode(); 515 } 516 517 } 518 519 522 public static class SelfElement extends Element 523 { 524 private static final long serialVersionUID = 3834311739151300406L; 525 526 public String getElementString() 527 { 528 return "."; 529 } 530 531 public boolean equals(Object o) 532 { 533 if(o == this) 534 { 535 return true; 536 } 537 if(!(o instanceof SelfElement)) 538 { 539 return false; 540 } 541 return true; 542 } 543 544 public int hashCode() 545 { 546 return ".".hashCode(); 547 } 548 } 549 550 553 public static class ParentElement extends Element 554 { 555 private static final long serialVersionUID = 3689915080477456179L; 556 557 public String getElementString() 558 { 559 return ".."; 560 } 561 562 public boolean equals(Object o) 563 { 564 if(o == this) 565 { 566 return true; 567 } 568 if(!(o instanceof ParentElement)) 569 { 570 return false; 571 } 572 return true; 573 } 574 575 public int hashCode() 576 { 577 return "..".hashCode(); 578 } 579 } 580 } 581 | Popular Tags |