public void addResource(PathAddress relativeAddress, Resource toAdd) {
Resource model = root;
final Iterator<PathElement> i = relativeAddress.iterator();
while (i.hasNext()) {
final PathElement element = i.next();
if (element.isMultiTarget()) {
throw MESSAGES.cannotWriteTo("*");
}
if (!i.hasNext()) {
if (model.hasChild(element)) {
throw MESSAGES.duplicateResourceAddress(relativeAddress);
} else {
model.registerChild(element, toAdd);
model = toAdd;
}
} else {
model = model.getChild(element);
if (model == null) {
PathAddress ancestor = PathAddress.EMPTY_ADDRESS;
for (PathElement pe : relativeAddress) {
ancestor = ancestor.append(pe);
if (element.equals(pe)) {
break;
}
}
throw MESSAGES.resourceNotFound(ancestor, relativeAddress);
}