Element getElement(PseudoPath path) { Element currentElement = fileTree; for (int i = 0; i < path.getNameCount(); i++) { currentElement = getChildByName(currentElement, path.getName(i)); if (currentElement == null) return null; } return currentElement; }
public synchronized void addFile(PseudoPath path) { if (path.equals(new PseudoPath())) { throw new IllegalArgumentException("Adding empty path does not allowed."); } PseudoPath parent = path.getParent(); Element parentElement = getElement(parent); if (parentElement == null) { throw new IllegalArgumentException("Parent directory does not exist."); } else { Element childElement = new Element(FileType.FILE.getName()); String filename = path.getName(path.getNameCount() - 1); childElement.addAttribute(new Attribute("name", filename)); parentElement.appendChild(childElement); } }
public synchronized void deletePath(PseudoPath path) { if (path.getNameCount() == 0) { throw new IllegalArgumentException("Empty path can not be deleted."); } Element toBeDeleted = getElement(path); if (toBeDeleted != null) { ParentNode parent = toBeDeleted.getParent(); parent.removeChild(toBeDeleted); } }
public synchronized void addToDirectory(PseudoPath dir, FSImage child) { if (!getType(dir).equals(FileType.DIR.getName())) { throw new IllegalArgumentException("Can add files only to directories."); } Element parent = getElement(dir); Element newChild = new Element(child.fileTree); String nameOfChildRoot = child.fileTree.getAttributeValue("name"); PseudoPath pathToChild = dir.resolve(nameOfChildRoot); Element oldChild = getElement(pathToChild); if (oldChild != null) { parent.replaceChild(oldChild, newChild); } else { parent.appendChild(newChild); } }