public Path addFileExtension(String extension) { if (isRoot() || isEmpty() || hasTrailingSeparator()) { return this; } int len = segments.length; String[] newSegments = new String[len]; System.arraycopy(segments, 0, newSegments, 0, len - 1); newSegments[len - 1] = segments[len - 1] + '.' + extension; return new Path(newSegments, separators); }
protected static void copyTree(int rootIndex, File src, File dst, Path prefix, PathFilter filter) throws IOException { if (src.isFile()) { String relPath = src.getPath().substring(rootIndex); if (!filter.accept(new Path(relPath))) { return; } if (!prefix.isRoot()) { // remove prefix from path String path = dst.getPath(); String pff = prefix.toString(); int prefixIndex = path.lastIndexOf(pff); if (prefixIndex > 0) { path = path.substring(0, prefixIndex) + path.substring(prefixIndex + pff.length()); dst = new File(path.toString()); } } dst.getParentFile().mkdirs(); copyFile(src, dst); } else if (src.isDirectory()) { File[] files = src.listFiles(); for (File file : files) { copyTree(rootIndex, file, new File(dst, file.getName()), prefix, filter); } } }
public boolean isPrefixOf(Path anotherPath) { if (isEmpty() || (isRoot() && anotherPath.isAbsolute())) { return true; } int len = segments.length; if (len > anotherPath.segmentCount()) { return false; } for (int i = 0; i < len; i++) { if (!segments[i].equals(anotherPath.segment(i))) { return false; } } return true; }
public Path addTrailingSeparator() { if (hasTrailingSeparator() || isRoot()) { return this; } if (isEmpty()) { return new Path(segments, HAS_LEADING); } return new Path(segments, separators | HAS_TRAILING); }
@Factory(value = "currentPersonalWorkspacePath", scope = EVENT) public String getCurrentPersonalWorkspace() { if (currentPersonalWorkspacePath == null && Boolean.TRUE.equals(isUserWorkspace)) { // Do not compute path if not necessary Path path = navigationContext.getCurrentDocument().getPath(); String lastSegment = ""; while (!path.isRoot() || !path.isEmpty()) { if (UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT.equals(path.lastSegment())) { if (lastSegment.isEmpty()) { currentPersonalWorkspacePath = ""; } else { currentPersonalWorkspacePath = path.append(lastSegment).toString(); } return currentPersonalWorkspacePath; } else { lastSegment = path.lastSegment(); } path = path.removeLastSegments(1); } } return currentPersonalWorkspacePath; }
protected String findFarthestContainerPath(String documentPath) { Path containerPath = new Path(documentPath); String result; do { result = containerPath.toString(); containerPath = containerPath.removeLastSegments(1); } while (!containerPath.isRoot() && documentManager.exists(new PathRef(containerPath.toString()))); return result; }
public Path append(Path tail) { // optimize some easy cases if (tail == null || tail.segmentCount() == 0) { return this; } if (isEmpty()) { return tail.makeRelative(); } if (isRoot()) { return tail.makeAbsolute(); } // concatenate the two segment arrays int myLen = segments.length; int tailLen = tail.segmentCount(); String[] newSegments = new String[myLen + tailLen]; System.arraycopy(segments, 0, newSegments, 0, myLen); for (int i = 0; i < tailLen; i++) { newSegments[myLen + i] = tail.segment(i); } // use my leading separators and the tail's trailing separator Path result = new Path(newSegments, (separators & HAS_LEADING) | (tail.hasTrailingSeparator() ? HAS_TRAILING : 0)); String tailFirstSegment = newSegments[myLen]; if (tailFirstSegment.equals("..") || tailFirstSegment.equals(".")) { //$NON-NLS-1$ //$NON-NLS-2$ result.canonicalize(); } return result; }
@Override public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException { Path path = xdoc.getPath(); if (path.isEmpty() || path.isRoot()) { return null; // TODO avoid to import the root } path = root.append(path); // compute target path DocumentModel doc = createDocument(xdoc, path); DocumentLocation source = xdoc.getSourceLocation(); DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(), doc.getRepositoryName()); map.put(source.getDocRef(), doc.getRef()); return map; }