001 /* 002 * JBoss, Home of Professional Open Source. 003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors 004 * as indicated by the @author tags. See the copyright.txt file in the 005 * distribution for a full listing of individual contributors. 006 * 007 * This is free software; you can redistribute it and/or modify it 008 * under the terms of the GNU Lesser General Public License as 009 * published by the Free Software Foundation; either version 2.1 of 010 * the License, or (at your option) any later version. 011 * 012 * This software is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this software; if not, write to the Free 019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 021 */ 022 package org.jboss.dna.graph.properties; 023 024 import java.io.Serializable; 025 import java.util.Iterator; 026 import java.util.List; 027 import net.jcip.annotations.Immutable; 028 import org.jboss.dna.common.text.Jsr283Encoder; 029 import org.jboss.dna.common.text.NoOpEncoder; 030 import org.jboss.dna.common.text.TextDecoder; 031 import org.jboss.dna.common.text.TextEncoder; 032 import org.jboss.dna.common.text.UrlEncoder; 033 import org.jboss.dna.graph.properties.basic.BasicName; 034 import org.jboss.dna.graph.properties.basic.BasicPathSegment; 035 036 /** 037 * An object representation of a node path within a repository. 038 * <p> 039 * A path consists of zero or more segments that can contain any characters, although the string representation may require some 040 * characters to be encoded. For example, if a path contains a segment with a forward slash, then this forward slash must be 041 * escaped when writing the whole path to a string (since a forward slash is used as the {@link #DELIMITER delimiter} between 042 * segments). 043 * </p> 044 * <p> 045 * Because of this encoding and decoding issue, there is no standard representation of a path as a string. Instead, this class 046 * uses {@link TextEncoder text encoders} to escape certain characters when writing to a string or unescaping the string 047 * representation. These encoders and used only with individual segments, and therefore are not used to encode the 048 * {@link #DELIMITER delimiter}. Three standard encoders are provided, although others can certainly be used: 049 * <ul> 050 * <li>{@link #JSR283_ENCODER Jsr283Encoder} - an encoder and decoder that is compliant with <a 051 * href="http://jcp.org/en/jsr/detail?id=283">JSR-283</a> by converting the reserved characters (namely '*', '/', ':', '[', ']' 052 * and '|') to their unicode equivalent.</td> 053 * </li> 054 * <li>{@link #URL_ENCODER UrlEncoder} - an encoder and decoder that is useful for converting text to be used within a URL, as 055 * defined by Section 2.3 of <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>. This encoder does encode many characters 056 * (including '`', '@', '#', '$', '^', '&', '{', '[', '}', ']', '|', ':', ';', '\', '"', '<', ',', '>', '?', '/', and ' '), while 057 * others are not encoded (including '-', '_', '.', '!', '~', '*', '\', ''', '(', and ')'). Note that only the '*' character is 058 * the only character reserved by JSR-283 that is not encoded by the URL encoder.</li> 059 * <li>{@link #NO_OP_ENCODER NoOpEncoder} - an {@link TextEncoder encoder} implementation that does nothing.</li> 060 * </ul> 061 * </p> 062 * <p> 063 * This class simplifies working with paths and using a <code>Path</code> is often more efficient that processing and 064 * manipulating the equivalent <code>String</code>. This class can easily {@link #iterator() iterate} over the segments, return 065 * the {@link #size() number of segments}, {@link #compareTo(Path) compare} with other paths, {@link #resolve(Path) resolve} 066 * relative paths, return the {@link #getParent() ancestor (or parent)}, determine whether one path is an 067 * {@link #isAncestorOf(Path) ancestor} or {@link #isDecendantOf(Path) decendent} of another path, and 068 * {@link #getCommonAncestor(Path) finding a common ancestor}. 069 * </p> 070 * 071 * @author Randall Hauch 072 * @author John Verhaeg 073 */ 074 @Immutable 075 public interface Path extends Comparable<Path>, Iterable<Path.Segment>, Serializable { 076 077 /** 078 * The text encoder that does nothing. 079 */ 080 public static final TextEncoder NO_OP_ENCODER = new NoOpEncoder(); 081 082 /** 083 * The text encoder that encodes according to JSR-283. 084 */ 085 public static final TextEncoder JSR283_ENCODER = new Jsr283Encoder(); 086 087 /** 088 * The text encoder that encodes text according to the rules of <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>. 089 */ 090 public static final TextEncoder URL_ENCODER = new UrlEncoder().setSlashEncoded(true); 091 092 /** 093 * The text decoder that does nothing. 094 */ 095 public static final TextDecoder NO_OP_DECODER = new NoOpEncoder(); 096 097 /** 098 * The text decoder that decodes according to JSR-283. 099 */ 100 public static final TextDecoder JSR283_DECODER = new Jsr283Encoder(); 101 102 /** 103 * The text decoder that decodes text according to the rules of <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>. 104 */ 105 public static final TextDecoder URL_DECODER = new UrlEncoder().setSlashEncoded(true); 106 107 /** 108 * The default text encoder to be used when none is otherwise specified. This is currently the {@link #JSR283_ENCODER JSR-283 109 * encoder}. 110 */ 111 public static final TextEncoder DEFAULT_ENCODER = JSR283_ENCODER; 112 113 /** 114 * The default text decoder to be used when none is otherwise specified. This is currently the {@link #JSR283_ENCODER JSR-283 115 * encoder}. 116 */ 117 public static final TextDecoder DEFAULT_DECODER = JSR283_DECODER; 118 119 /** 120 * The delimiter character used to separate segments within a path. 121 */ 122 public static final char DELIMITER = '/'; 123 124 /** 125 * String form of the delimiter used to separate segments within a path. 126 */ 127 public static final String DELIMITER_STR = new String(new char[] {DELIMITER}); 128 129 /** 130 * String representation of the segment that references a parent. 131 */ 132 public static final String PARENT = ".."; 133 134 /** 135 * String representation of the segment that references the same segment. 136 */ 137 public static final String SELF = "."; 138 139 /** 140 * The index that will be returned for a {@link Segment} that {@link Segment#hasIndex() has no index}. 141 */ 142 public static final int NO_INDEX = -1; 143 144 /** 145 * Representation of the segments that occur within a path. 146 * 147 * @author Randall Hauch 148 */ 149 @Immutable 150 public static interface Segment extends Cloneable, Comparable<Segment>, Serializable { 151 152 /** 153 * Get the name component of this segment. 154 * 155 * @return the segment's name 156 */ 157 public Name getName(); 158 159 /** 160 * Get the index for this segment, which will be {@link Path#NO_INDEX 0} if this segment has no specific index. 161 * 162 * @return the index 163 */ 164 public int getIndex(); 165 166 /** 167 * Return whether this segment has an index. 168 * 169 * @return true if this segment has an index, or false otherwise. 170 */ 171 public boolean hasIndex(); 172 173 /** 174 * Return whether this segment is a self-reference. 175 * 176 * @return true if the segment is a self-reference, or false otherwise. 177 */ 178 public boolean isSelfReference(); 179 180 /** 181 * Return whether this segment is a reference to a parent. 182 * 183 * @return true if the segment is a parent-reference, or false otherwise. 184 */ 185 public boolean isParentReference(); 186 187 /** 188 * Get the raw string form of the segment using the {@link Path#NO_OP_ENCODER no-op encoder}. This is equivalent to 189 * calling <code>getString(Path.NO_OP_ENCODER)</code>. 190 * 191 * @return the un-encoded string 192 * @see #getString(TextEncoder) 193 */ 194 public String getUnencodedString(); 195 196 /** 197 * Get the string form of the segment. The {@link #DEFAULT_ENCODER default encoder} is used to encode characters in each 198 * of the path segments. 199 * 200 * @return the encoded string 201 * @see #getString(TextEncoder) 202 */ 203 public String getString(); 204 205 /** 206 * Get the encoded string form of the segment, using the supplied encoder to encode characters in each of the path 207 * segments. 208 * 209 * @param encoder the encoder to use, or null if the {@link #DEFAULT_ENCODER default encoder} should be used 210 * @return the encoded string 211 * @see #getString() 212 */ 213 public String getString( TextEncoder encoder ); 214 215 /** 216 * Get the string form of the segment, using the supplied namespace registry to convert the name's namespace URI to a 217 * prefix. The {@link #DEFAULT_ENCODER default encoder} is used to encode characters in each of the path segments. 218 * 219 * @param namespaceRegistry the namespace registry that should be used to obtain the prefix for the 220 * {@link Name#getNamespaceUri() namespace URI} in the segment's {@link #getName() name} 221 * @return the encoded string 222 * @throws IllegalArgumentException if the namespace registry is null 223 * @see #getString(NamespaceRegistry,TextEncoder) 224 */ 225 public String getString( NamespaceRegistry namespaceRegistry ); 226 227 /** 228 * Get the encoded string form of the segment, using the supplied namespace registry to convert the name's namespace URI 229 * to a prefix and the supplied encoder to encode characters in each of the path segments. 230 * 231 * @param namespaceRegistry the namespace registry that should be used to obtain the prefix for the 232 * {@link Name#getNamespaceUri() namespace URI} in the segment's {@link #getName() name} 233 * @param encoder the encoder to use, or null if the {@link #DEFAULT_ENCODER default encoder} should be used 234 * @return the encoded string 235 * @throws IllegalArgumentException if the namespace registry is null 236 * @see #getString(NamespaceRegistry) 237 */ 238 public String getString( NamespaceRegistry namespaceRegistry, 239 TextEncoder encoder ); 240 } 241 242 /** 243 * Singleton instance of the name referencing a self, provided as a convenience. 244 */ 245 public static final Name SELF_NAME = new BasicName(null, SELF); 246 247 /** 248 * Singleton instance of the name referencing a parent, provided as a convenience. 249 */ 250 public static final Name PARENT_NAME = new BasicName(null, PARENT); 251 252 /** 253 * Singleton instance of the path segment referencing a parent, provided as a convenience. 254 */ 255 public static final Path.Segment SELF_SEGMENT = new BasicPathSegment(SELF_NAME); 256 257 /** 258 * Singleton instance of the path segment referencing a parent, provided as a convenience. 259 */ 260 public static final Path.Segment PARENT_SEGMENT = new BasicPathSegment(PARENT_NAME); 261 262 /** 263 * Return the number of segments in this path. 264 * 265 * @return the number of path segments 266 */ 267 public int size(); 268 269 /** 270 * Return whether this path represents the root path. 271 * 272 * @return true if this path is the root path, or false otherwise 273 */ 274 public boolean isRoot(); 275 276 /** 277 * Determine whether this path represents the same as the supplied path. This is equivalent to calling <code> 278 * this.compareTo(other) == 0 </code>. 279 * 280 * @param other the other path to compare with this path; may be null 281 * @return true if the paths are equivalent, or false otherwise 282 */ 283 public boolean isSameAs( Path other ); 284 285 /** 286 * Determine whether this path is the {@link #isSameAs(Path) same as} to or a {@link #isAncestorOf(Path) ancestor of} the 287 * supplied path. This method is equivalent to (but may be more efficient than) calling <code>isSame(other) || 288 * isAncestor(other)</code>, and is a convenience method that is identical to calling <code>other.isAtOrBelow(this)</code>. 289 * 290 * @param other the other path to compare with this path; may be null 291 * @return true if the paths are equivalent or if this path is considered an ancestor of the other path, or false otherwise 292 */ 293 public boolean isAtOrAbove( Path other ); 294 295 /** 296 * Determine whether this path is the {@link #isSameAs(Path) same as} to or a {@link #isDecendantOf(Path) decendant of} the 297 * supplied path. This method is equivalent to (but may be more efficient than) calling <code>isSame(other) || 298 * isAncestor(other)</code>. 299 * 300 * @param other the other path to compare with this path; may be null 301 * @return true if the paths are equivalent or if this path is considered a decendant of the other path, or false otherwise 302 */ 303 public boolean isAtOrBelow( Path other ); 304 305 /** 306 * Determine whether this path is an ancestor of the supplied path. A path is considered an ancestor of another path if the 307 * the ancestor path appears in its entirety at the beginning of the decendant path, and where the decendant path contains at 308 * least one additional segment. 309 * 310 * @param decendant the path that may be the decendant; may be null 311 * @return true if this path is an ancestor of the supplied path, or false otherwise 312 */ 313 public boolean isAncestorOf( Path decendant ); 314 315 /** 316 * Determine whether this path is an decendant of the supplied path. A path is considered a decendant of another path if the 317 * the decendant path starts exactly with the entire ancestor path but contains at least one additional segment. 318 * 319 * @param ancestor the path that may be the ancestor; may be null 320 * @return true if this path is an decendant of the supplied path, or false otherwise 321 */ 322 public boolean isDecendantOf( Path ancestor ); 323 324 /** 325 * Return whether this path is an absolute path. A path is either relative or {@link #isAbsolute() absolute}. An absolute path 326 * starts with a "/". 327 * 328 * @return true if the path is absolute, or false otherwise 329 */ 330 public boolean isAbsolute(); 331 332 /** 333 * Return whether this path is normalized and contains no "." segments and as few ".." segments as possible. For example, the 334 * path "../a" is normalized, while "/a/b/c/../d" is not normalized. 335 * 336 * @return true if this path is normalized, or false otherwise 337 */ 338 public boolean isNormalized(); 339 340 /** 341 * Get a normalized path with as many ".." segments and all "." resolved. 342 * 343 * @return the normalized path, or this object if this path is already normalized 344 * @throws InvalidPathException if the normalized form would result in a path with negative length (e.g., "/a/../../..") 345 */ 346 public Path getNormalizedPath(); 347 348 /** 349 * Get the canonical form of this path. A canonical path has is {@link #isAbsolute() absolute} and {@link #isNormalized()}. 350 * 351 * @return the canonical path, or this object if it is already in its canonical form 352 * @throws InvalidPathException if the path is not absolute and cannot be canonicalized 353 */ 354 public Path getCanonicalPath(); 355 356 /** 357 * Get a relative path from the supplied path to this path. 358 * 359 * @param startingPath the path specifying the starting point for the new relative path; may not be null 360 * @return the relative path 361 * @throws IllegalArgumentException if the supplied path is null 362 * @throws PathNotFoundException if both this path and the supplied path are not absolute 363 */ 364 public Path relativeTo( Path startingPath ); 365 366 /** 367 * Get the absolute path by resolving the supplied relative (non-absolute) path against this absolute path. 368 * 369 * @param relativePath the relative path that is to be resolved against this path 370 * @return the absolute and normalized path resolved from this path and the supplied absolute path 371 * @throws IllegalArgumentException if the supplied path is null 372 * @throws InvalidPathException if the this path is not absolute or if the supplied path is not relative. 373 */ 374 public Path resolve( Path relativePath ); 375 376 /** 377 * Get the absolute path by resolving this relative (non-absolute) path against the supplied absolute path. 378 * 379 * @param absolutePath the absolute path to which this relative path should be resolve 380 * @return the absolute path resolved from this path and the supplied absolute path 381 * @throws IllegalArgumentException if the supplied path is null 382 * @throws InvalidPathException if the supplied path is not absolute or if this path is not relative. 383 */ 384 public Path resolveAgainst( Path absolutePath ); 385 386 /** 387 * Return the path to the parent, or this path if it is the {@link #isRoot() root}. This is an efficient operation that does 388 * not require copying any data. 389 * 390 * @return the parent path, or this path if it is already the root 391 */ 392 public Path getParent(); 393 394 /** 395 * Return the path to the ancestor of the supplied degree. An ancestor of degree <code>x</code> is the path that is <code>x 396 * </code> levels up along the path. For example, <code>degree = 0</code> returns this path, while <code>degree = 1</code> 397 * returns the parent of this path, <code>degree = 2</code> returns the grandparent of this path, and so on. Note that the 398 * result may be unexpected if this path is not {@link #isNormalized() normalized}, as a non-normalized path contains ".." and 399 * "." segments. 400 * 401 * @param degree 402 * @return the ancestor of the supplied degree 403 * @throws IllegalArgumentException if the degree is negative 404 * @throws InvalidPathException if the degree is greater than the {@link #size() length} of this path 405 */ 406 public Path getAncestor( int degree ); 407 408 /** 409 * Determine whether this path and the supplied path have the same immediate ancestor. In other words, this method determines 410 * whether the node represented by this path is a sibling of the node represented by the supplied path. 411 * 412 * @param that the other path 413 * @return true if this path and the supplied path have the same immediate ancestor. 414 * @throws IllegalArgumentException if the supplied path is null 415 */ 416 public boolean hasSameAncestor( Path that ); 417 418 /** 419 * Find the lowest common ancestor of this path and the supplied path. 420 * 421 * @param that the other path 422 * @return the lowest common ancestor, which may be the root path if there is no other. 423 * @throws IllegalArgumentException if the supplied path is null 424 */ 425 public Path getCommonAncestor( Path that ); 426 427 /** 428 * Get the last segment in this path. 429 * 430 * @return the last segment, or null if the path is empty 431 */ 432 public Segment getLastSegment(); 433 434 /** 435 * Get the segment at the supplied index. 436 * 437 * @param index the index 438 * @return the segment 439 * @throws IndexOutOfBoundsException if the index is out of bounds 440 */ 441 public Segment getSegment( int index ); 442 443 /** 444 * Return a new path consisting of the segments starting at <code>beginIndex</code> index (inclusive). This is equivalent to 445 * calling <code>path.subpath(beginIndex,path.size()-1)</code>. 446 * 447 * @param beginIndex the beginning index, inclusive. 448 * @return the specified subpath 449 * @exception IndexOutOfBoundsException if the <code>beginIndex</code> is negative or larger than the length of this <code> 450 * Path</code> object 451 */ 452 public Path subpath( int beginIndex ); 453 454 /** 455 * Return a new path consisting of the segments between the <code>beginIndex</code> index (inclusive) and the <code>endIndex 456 * </code> index (exclusive). 457 * 458 * @param beginIndex the beginning index, inclusive. 459 * @param endIndex the ending index, exclusive. 460 * @return the specified subpath 461 * @exception IndexOutOfBoundsException if the <code>beginIndex</code> is negative, or <code>endIndex</code> is larger than 462 * the length of this <code>Path</code> object, or <code>beginIndex</code> is larger than <code>endIndex</code>. 463 */ 464 public Path subpath( int beginIndex, 465 int endIndex ); 466 467 /** 468 * {@inheritDoc} 469 */ 470 public Iterator<Segment> iterator(); 471 472 /** 473 * Obtain a copy of the segments in this path. None of the segments are encoded. 474 * 475 * @return the array of segments as a copy 476 */ 477 public Segment[] getSegmentsArray(); 478 479 /** 480 * Get an unmodifiable list of the path segments. 481 * 482 * @return the unmodifiable list of path segments; never null 483 */ 484 public List<Segment> getSegmentsList(); 485 486 /** 487 * Get the string form of the path. The {@link #DEFAULT_ENCODER default encoder} is used to encode characters in each of the 488 * path segments. 489 * 490 * @return the encoded string 491 * @see #getString(TextEncoder) 492 */ 493 public String getString(); 494 495 /** 496 * Get the encoded string form of the path, using the supplied encoder to encode characters in each of the path segments. 497 * 498 * @param encoder the encoder to use, or null if the {@link #DEFAULT_ENCODER default encoder} should be used 499 * @return the encoded string 500 * @see #getString() 501 */ 502 public String getString( TextEncoder encoder ); 503 504 /** 505 * Get the string form of the path, using the supplied namespace registry to convert the names' namespace URIs to prefixes. 506 * The {@link #DEFAULT_ENCODER default encoder} is used to encode characters in each of the path segments. 507 * 508 * @param namespaceRegistry the namespace registry that should be used to obtain the prefix for the 509 * {@link Name#getNamespaceUri() namespace URIs} in the segment {@link Segment#getName() names} 510 * @return the encoded string 511 * @throws IllegalArgumentException if the namespace registry is null 512 * @see #getString(NamespaceRegistry,TextEncoder) 513 */ 514 public String getString( NamespaceRegistry namespaceRegistry ); 515 516 /** 517 * Get the encoded string form of the path, using the supplied namespace registry to convert the names' namespace URIs to 518 * prefixes and the supplied encoder to encode characters in each of the path segments. 519 * 520 * @param namespaceRegistry the namespace registry that should be used to obtain the prefix for the 521 * {@link Name#getNamespaceUri() namespace URIs} in the segment {@link Segment#getName() names} 522 * @param encoder the encoder to use, or null if the {@link #DEFAULT_ENCODER default encoder} should be used 523 * @return the encoded string 524 * @throws IllegalArgumentException if the namespace registry is null 525 * @see #getString(NamespaceRegistry) 526 */ 527 public String getString( NamespaceRegistry namespaceRegistry, 528 TextEncoder encoder ); 529 530 }