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); } }