/** * @param delegate delegate tree * @param rootPath root path for the subtree * @param fullPath true if the root path should not be removed when accessing the delegate */ public SubPathTree(Tree<T> delegate, String rootPath, boolean fullPath) { this(delegate, PathUtil.asPath(rootPath), fullPath); }
/** * Append one path to another * * @param prefix prefix * @param subpath sub path * * @return sub path appended to the prefix */ public static Path appendPath(Path prefix, String subpath) { return asPath(appendPath(prefix.getPath(), subpath)); }
/** * List all treeHandlers as directories which have the given path as a parent * @param path path * @return */ private Set<Resource<T>> listStackDirectory(Path path) { HashSet<Resource<T>> merge = new HashSet<Resource<T>>(); if (treeHandlerList.size() > 0) { for (SelectiveTree<T> treeHandler : treeHandlerList) { if (PathUtil.hasRoot(treeHandler.getSubPath(), path) && !PathUtil.equals( treeHandler.getSubPath(), path )) { String relativePath = PathUtil.removePrefix(path.getPath(), treeHandler.getSubPath().getPath()); String[] components = PathUtil.componentsFromPathString(relativePath); Path subpath = PathUtil.appendPath(path, components[0]); merge.add(new ResourceBase<T>(subpath, null, true)); } } } return merge; }
public static Path parentPath(Path path) { return asPath(parentPathString(path.getPath())); }
/** * Append a converter plugin to the tree builder * * @param builder builder * @param pluginType converter plugin type * @param path path * @param selector metadata selector * @param config plugin config data * * @return builder */ private TreeBuilder<ResourceMeta> buildConverterPlugin(TreeBuilder<ResourceMeta> builder, String pluginType, String path, String selector, Map<String, String> config) { StorageConverterPlugin converterPlugin = loadPlugin( pluginType, config, storageConverterPluginProviderService ); //convert tree under the subpath if specified, AND matching the selector if specified return builder.convert( new StorageConverterPluginAdapter(pluginType,converterPlugin), null != path ? PathUtil.asPath(path.trim()) : null, null != selector ? PathUtil.<ResourceMeta>resourceSelector(selector) : null ); }
private boolean deleteRes(DirRes<T> dirRes) { boolean removed = false; if (!dirRes.dir) { Resource<T> res = dirRes.res; removed = index.remove(res.getPath()) != null; Path parentPath = PathUtil.parentPath(res.getPath()); if (!PathUtil.isRoot(parentPath) && null != index.get(parentPath)) { DirRes<T> parentRes = index.get(parentPath); parentRes.dirList.remove(res.getPath()); //remove parent dir if empty dirRes = parentRes; } else { return removed; } } while (null != dirRes && dirRes.dir && dirRes.dirList.size() < 1) { index.remove(dirRes.getPath()); Path parentPath = PathUtil.parentPath(dirRes.getPath()); if (parentPath != null && !PathUtil.isRoot(parentPath)) { DirRes<T> parentRes = index.get(parentPath); parentRes.dirList.remove(dirRes.getPath()); dirRes = parentRes; } else { dirRes = null; } } return removed; }
@Override public boolean matchesPath(Path path) { return path.equals(rootPath) || PathUtil.hasRoot(path, rootPath); } };
private boolean isLocalRoot(Path path) { return PathUtil.isRoot(PathUtil.removePrefix(rootPath.getPath(), path.getPath())); }
/** * convert internal path to external * * @param intpath * * @return */ private String translatePathExternal(String intpath) { if (fullPath) { return intpath; } else { return PathUtil.appendPath(rootPath.getPath(), intpath); } }
public static String pathName(String path) { String[] split = componentsFromPathString(path); if (split.length > 0) { return split[split.length - 1]; } return null; }
/** * @param path parent path * @param tree tree with a subpath * @return true if the subpath is directly under the path */ public static boolean hasParentPath(Path path, SubPath tree) { return path.equals(PathUtil.parentPath(tree.getSubPath())); }
/** * Listen to events on selective resources of the tree * * @param listener listener * @param resourceSelector resource selector * @return builder */ private TreeBuilder<T> listen(Listener<T> listener, String resourceSelector) { return TreeBuilder.<T>builder(new ListenerTree<T>(build(), listener, PathUtil.<T>resourceSelector(resourceSelector))); }
public static boolean matchesPath(Path path, SelectiveTree<?> tree) { return path.equals(tree.getSubPath()) || PathUtil.hasRoot(path, tree.getSubPath()); }
/** * @param path path * @return path components */ public static String[] componentsFromPath(final Path path) { return componentsFromPathString(path.getPath()); }
public MemoryTree() { index.put(PathUtil.asPath(""), root); }
/** * Configures storage plugins with the builder * * @param builder builder * @param index current prop index * @param configProps configuration properties */ private void configureStoragePlugin(TreeBuilder<ResourceMeta> builder, int index, Map<String, String> configProps) { String pref1 = getStorageConfigPrefix() + SEP + index; String pluginType = configProps.get(pref1 + SEP + TYPE); String path = configProps.get(pref1 + SEP + PATH); boolean removePathPrefix = Boolean.parseBoolean(configProps.get(pref1 + SEP + REMOVE_PATH_PREFIX)); Map<String, String> config = subPropertyMap(pref1 + SEP + CONFIG + SEP, configProps); config = expandConfig(config); Tree<ResourceMeta> resourceMetaTree = loadPlugin( pluginType, config, storagePluginProviderService ); if (index == 1 && PathUtil.isRoot(path)) { logger.debug("New base Storage[" + index + "]:" + path + " " + pluginType + ", config: " + config); builder.base(resourceMetaTree); } else { logger.debug("Subtree Storage[" + index + "]:" + path + " " + pluginType + ", config: " + config); builder.subTree(PathUtil.asPath(path.trim()), resourceMetaTree, !removePathPrefix); } }
private DirRes<T> createParentPaths(Path path) { String[] split = path.getPath().split("/"); DirRes<T> current = root; Path currentPath = PathUtil.asPath(""); for (int i = 0; i < split.length - 1; i++) { currentPath = PathUtil.appendPath(currentPath, split[i]); if (!index.containsKey(currentPath)) { DirRes<T> newDir = new DirRes<T>(currentPath); newDir.dir = true; current.dirList.put(currentPath, newDir); index.put(currentPath, newDir); current = newDir; } else { current = index.get(currentPath); } } return current; }
/** * @return true if the given path starts with the given root * * @param path test path * @param root root * */ public static boolean hasRoot(Path path, Path root) { return hasRoot(path.getPath(), root.getPath()); }