// Verify that the prefix is not already used ...
Name newPrefixName = nameForPrefix(newPrefix);
ChildReference ref = childRefs.getChild(newPrefixName);
if (ref != null) {
// There's an existing node with the same prefix/name ...
CachedNode existingNode = system.getNode(ref);
String existingUri = strings.create(existingNode.getProperty(ModeShapeLexicon.URI, system).getFirstValue());
if (newUri.equals(existingUri)) {
// The URI also matches, so nothing to do ...
continue;
}
// Otherwise, the prefix was bound to another URI, so this means we're taking an existing prefix already bound
// to one URI and assigning it to another URI. Per the JavaDoc for javax.jcr.Namespace#register(String,String)
// the old URI is to be unregistered -- meaning we should delete it ...
namespaces.removeChild(system, ref.getKey());
system.destroy(ref.getKey());
continue;
}
// Look for an existing namespace node that uses the same URI ...
NodeKey key = keyForNamespaceUri(newUri);
CachedNode existingNode = system.getNode(key);
if (existingNode != null) {
// Get the prefix for the existing namespace node ...
Segment segment = existingNode.getSegment(system);
String existingPrefix = prefixFor(segment);
if (GENERATED_NAMESPACE_NODE_NAME.equals(segment.getName()) || !existingPrefix.equals(newPrefix)) {
// The prefix but was not used elsewhere, so we know we can just change it ...
namespaces.renameChild(system, key, names.create(newPrefix));
removedPrefixes.add(existingPrefix);