/** * Constructor * * @param prefix absolute path to mount the cms uri space on */ public PrefixUriMapper(Path prefix) { if (!prefix.isAbsolute()) { throw new IllegalArgumentException("Prefix must be an absolute path"); } this.prefix = prefix; }
public Path toAbsolute() { if (!isAbsolute()) { return ROOT.append(this); } else { return this; } }
public String part(int index) { if (index < 0) { throw new IndexOutOfBoundsException(); } int start = isAbsolute() ? 1 : 0; for (int i = 0; i < index; i++) { start = path.indexOf(SEPARATOR, start); if (start < 0) { throw new IndexOutOfBoundsException(); } start++; } int end = path.indexOf(SEPARATOR, start); if (end < 0) { end = path.length(); } return path.substring(start, end); }
public Path append(Path relative) { if (relative == null) { throw new IllegalArgumentException("Argument 'relative' cannot be null"); } if (relative.isAbsolute()) { throw new IllegalArgumentException("Cannot append an absolute path"); } StringBuilder appended = new StringBuilder(path.length() + 1 + relative.path.length()); appended.append(path); if (!path.endsWith(SEPARATOR)) { appended.append("/"); } appended.append(relative.path); return new Path(appended.toString()); }
public boolean isDescendantOf(Path other) { if (other.isRoot()) { if (isRoot()) { return false; } else if (isAbsolute()) { return true; } else { return false; } } else { return path.startsWith(other.path + SEPARATOR) && path.length() > other.path.length(); } }
private Path doCanonical() { boolean absolute = isAbsolute(); int prepend = 0;
public Path subpath(int idx) { if (idx <= 0) { throw new IndexOutOfBoundsException(); } final int size = size(); final boolean abs = isAbsolute(); int chunks = (abs) ? size + 1 : size; if (idx > chunks) { throw new IndexOutOfBoundsException(); } if (idx == chunks) { // shortcut in case we want the entire path return this; } int iterations = ((abs) ? idx - 1 : idx); int end = 1; for (int i = 0; i < iterations; i++) { end = path.indexOf(SEPARATOR, end + 1); } if (end == 0) { return new Path("/"); } String newPath = path.substring(0, end); return new Path(newPath); }
public Path toRelative(Path ancestor) { if (isRoot()) { throw new IllegalStateException("Cannot make root path relative"); } if (!isDescendantOf(ancestor)) { throw new IllegalArgumentException("Cannot create relative path because this path: " + this + " is not descendant of ancestor argument: " + ancestor); } Path fragment = new Path(path.substring(ancestor.path.length()), false); if (fragment.isAbsolute()) { fragment = fragment.toRelative(new Path("/")); } return fragment; } }
public String toRealWebNodePath(String nodePath) { Path prefix = new Path(getSiteRootPath()); Path path = new Path(nodePath); if (path.isRoot()) { path = new Path("."); } else if (path.isAbsolute()) { path = path.toRelative(new Path("/")); } return prefix.append(path).toString(); }
public String fromRealWebNodePath(String nodePath) { Path prefix = new Path(getSiteRootPath()); Path path = new Path(nodePath); if (path.equals(prefix)) { path = new Path("/"); } else if (path.isDescendantOf(prefix)) { path = path.toRelative(prefix); } if (!path.isAbsolute()) { path = new Path("/").append(path); } return path.toString(); }
@SuppressWarnings("unchecked") public void validate(IValidatable validatable) { Object o = validatable.getValue(); if (o != null) { JcrNode node = nodeModel.getObject(); Path path = null; if (o instanceof Path) { path = (Path) o; } else { path = new Path(o.toString()); } if (!path.isAbsolute()) { Path parent = new Path(node.getPath()); if (!((BrixNode) node).isFolder()) parent = parent.parent(); path = parent.append(path); } else { path = new Path(SitePlugin.get().toRealWebNodePath(path.toString())); } if (node.getSession().itemExists(path.toString()) == false) { ValidationError error = new ValidationError(); error.setMessage("Node ${path} could not be found"); error.addKey("NodePathValidator"); error.getVariables().put("path", path.toString()); validatable.error(error); } } } }
public JcrNode nodeForPath(BrixNode baseNode, String path) { Path realPath = new Path(SitePlugin.get().toRealWebNodePath(path)); if (realPath.isAbsolute() == false) { Path base = new Path(baseNode.getPath()); if (!baseNode.isFolder()) { base = base.parent(); } realPath = base.append(realPath); } String strPath = realPath.toString(); if (baseNode.getSession().itemExists(strPath) == false) { return null; } else { return ((JcrNode) baseNode.getSession().getItem(strPath)); } }