/*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
}
+ /*
+ * PATH UTILITIES
+ */
+
/** Make sure that: starts with '/', do not end with '/', do not have '//' */
public static String normalizePath(String path) {
List<String> tokens = tokenize(path);
return path.toString();
}
+ /**
+ * Creates a path from a UUID (e.g. 6ebda899-217d-4bf1-abe4-2839085c8f3c =>
+ * 6ebda899-217d/4bf1/abe4/2839085c8f3c/). '/' at the end, not the beginning
+ */
+ public static String uuidAsPath(String uuid) {
+ StringBuffer path = new StringBuffer(uuid.length());
+ String[] tokens = uuid.split("-");
+ for (int i = 0; i < tokens.length; i++) {
+ path.append(tokens[i]);
+ if (i != 0)
+ path.append('/');
+ }
+ return path.toString();
+ }
+
/**
* The provided data as a path ('/' at the end, not the beginning)
*
throw new ArgeoException("Path " + path + " cannot end with '/'");
int index = path.lastIndexOf('/');
if (index < 0)
- throw new ArgeoException("Cannot find last path element for "
- + path);
+ return path;
return path.substring(index + 1);
}
return mkdirs(session, path, type, null, false);
}
+ /**
+ * Create sub nodes relative to a parent node
+ *
+ * @param nodeType
+ * the type of the leaf node
+ */
+ public static Node mkdirs(Node parentNode, String relativePath,
+ String nodeType) {
+ return mkdirs(parentNode, relativePath, nodeType, null);
+ }
+
+ /**
+ * Create sub nodes relative to a parent node
+ *
+ * @param nodeType
+ * the type of the leaf node
+ */
+ public static Node mkdirs(Node parentNode, String relativePath,
+ String nodeType, String intermediaryNodeType) {
+ List<String> tokens = tokenize(relativePath);
+ Node currParent = parentNode;
+ try {
+ for (int i = 0; i < tokens.size(); i++) {
+ String name = tokens.get(i);
+ if (currParent.hasNode(name)) {
+ currParent = currParent.getNode(name);
+ } else {
+ if (i != (tokens.size() - 1)) {// intermediary
+ currParent = currParent.addNode(name,
+ intermediaryNodeType);
+ } else {// leaf
+ currParent = currParent.addNode(name, nodeType);
+ }
+ }
+ }
+ return currParent;
+ } catch (RepositoryException e) {
+ throw new ArgeoException("Cannot mkdirs relative path "
+ + relativePath + " from " + parentNode, e);
+ }
+ }
+
/**
* Synchronized and save is performed, to avoid race conditions in
* initializers leading to duplicate nodes.
}
}
- /** Update lastModified recursively until this parent. */
+ /**
+ * Update lastModified recursively until this parent.
+ *
+ * @param node
+ * the node
+ * @param untilPath
+ * the base path, null is equivalent to "/"
+ */
public static void updateLastModifiedAndParents(Node node, String untilPath) {
try {
- if (!node.getPath().startsWith(untilPath))
+ if (untilPath != null && !node.getPath().startsWith(untilPath))
throw new ArgeoException(node + " is not under " + untilPath);
updateLastModified(node);
- if (!node.getPath().equals(untilPath))
- updateLastModifiedAndParents(node.getParent(), untilPath);
+ if (untilPath == null) {
+ if (!node.getPath().equals("/"))
+ updateLastModifiedAndParents(node.getParent(), untilPath);
+ } else {
+ if (!node.getPath().equals(untilPath))
+ updateLastModifiedAndParents(node.getParent(), untilPath);
+ }
} catch (RepositoryException e) {
throw new ArgeoException("Cannot update lastModified from " + node
+ " until " + untilPath, e);
* files
* @return how many files were copied
*/
+ @SuppressWarnings("resource")
public static Long copyFiles(Node fromNode, Node toNode, Boolean recursive,
ArgeoMonitor monitor) {
long count = 0l;