// The URI must already be registered ...
String existingPrefix = registry.getPrefixForNamespaceUri(uri, false);
if (existingPrefix == null) {
// Paragraph 1 ...
throw new NamespaceException(JcrI18n.unableToRemapUriNotRegisteredInNamespaceRegistry.text(prefix, uri));
}
if (existingPrefix.equals(prefix)) return; // Paragraph 2
// Is the prefix already used in a mapping ...
String existingUri = registry.getNamespaceForPrefix(prefix);
if (existingUri != null) {
// Is this existing mapping local, or is it in the global (workspace) registry?
String globalPrefix = workspaceRegistry.getPrefixForNamespaceUri(existingUri, false);
if (!prefix.equals(globalPrefix)) {
// Paragraph 3: The mapping is local to the session, so this local mapping should just be reverted ...
registry.unregister(existingUri);
}
// Paragraph 4: The mapping is global, so this is not allowed; the existing ...
String msg = JcrI18n.unableToRemapUriUsingPrefixUsedInNamespaceRegistry.text(prefix, uri, existingUri);
throw new NamespaceException(msg);
}
// Otherwise, the prefix is not already used in a mapping, so we can continue ...
break;
case JSR283_SESSION:
// --------------------------------------
// JSR-283 Session remapping behavior ...
// --------------------------------------
// Section 4.3.3 (of the Draft specification):
// "All local mappings already present in the Session that include either the specified prefix
// or the specified uri are removed and the new mapping is added."
String existingUriForPrefix = registry.getNamespaceForPrefix(prefix);
if (existingUriForPrefix != null) {
registry.unregister(existingUriForPrefix);
}
registry.unregister(uri);
break;
case WORKSPACE:
// --------------------------------------------------
// JSR-170 & JSR-283 Workspace namespace registry ...
// --------------------------------------------------
try {
session.checkPermission((Path)null, JcrSession.DNA_REGISTER_NAMESPACE_PERMISSION);
} catch (AccessControlException ace) {
throw new AccessDeniedException(ace);
}
// Check the zero-length prefix and zero-length URI ...
if (DEFAULT_NAMESPACE_PREFIX.equals(prefix) || DEFAULT_NAMESPACE_URI.equals(uri)) {
throw new NamespaceException(JcrI18n.unableToChangeTheDefaultNamespace.text());
}
// Check whether the prefix or URI are reserved (case-sensitive) ...
if (STANDARD_BUILT_IN_PREFIXES.contains(prefix)) {
throw new NamespaceException(JcrI18n.unableToRegisterReservedNamespacePrefix.text(prefix, uri));
}
if (STANDARD_BUILT_IN_URIS.contains(uri)) {
throw new NamespaceException(JcrI18n.unableToRegisterReservedNamespaceUri.text(prefix, uri));
}
break;
default:
assert false; // should never happen
}
// Check the zero-length prefix and zero-length URI ...
if (DEFAULT_NAMESPACE_PREFIX.equals(prefix) || DEFAULT_NAMESPACE_URI.equals(uri)) {
throw new NamespaceException(JcrI18n.unableToChangeTheDefaultNamespace.text());
}
// Check whether the prefix begins with 'xml' (in any case) ...
if (prefix.toLowerCase().startsWith(XML_NAMESPACE_PREFIX)) {
throw new NamespaceException(JcrI18n.unableToRegisterNamespaceUsingXmlPrefix.text(prefix, uri));
}
// The prefix must be a valid XML Namespace prefix (i.e., a valid NCName) ...
if (!XmlCharacters.isValidName(prefix)) {
throw new NamespaceException(JcrI18n.unableToRegisterNamespaceWithInvalidPrefix.text(prefix, uri));
}
// Now we're sure the prefix and URI are valid and okay for a custom mapping ...
try {
registry.register(prefix, uri);