JcrNamespaceRegistry implements org.modeshape.jcr.api.NamespaceRegistry { @Override public synchronized void registerNamespace( String prefix, String uri ) throws NamespaceException, RepositoryException { CheckArg.isNotNull(prefix, "prefix"); CheckArg.isNotNull(uri, "uri"); checkSession(); if (DEFAULT_NAMESPACE_PREFIX.equals(prefix) || DEFAULT_NAMESPACE_URI.equals(uri)) { throw new NamespaceException(JcrI18n.unableToChangeTheDefaultNamespace.text()); } if (prefix.toLowerCase().startsWith(XML_NAMESPACE_PREFIX)) { throw new NamespaceException(JcrI18n.unableToRegisterNamespaceUsingXmlPrefix.text(prefix, uri)); } if (!XmlCharacters.isValidNcName(prefix)) { throw new NamespaceException(JcrI18n.unableToRegisterNamespaceWithInvalidPrefix.text(prefix, uri)); } try { session.nameFactory().create(prefix); } catch (ValueFormatException e) { throw new NamespaceException(JcrI18n.unableToRegisterNamespaceWithInvalidPrefix.text(prefix, uri)); } String existingPrefixForURI = registry.getPrefixForNamespaceUri(uri, false); if (prefix.equals(existingPrefixForURI)) { return; } boolean global = false; switch (behavior) { case SESSION: String existingUriForPrefix = registry.getNamespaceForPrefix(prefix); if (existingUriForPrefix != null) { registry.unregister(existingUriForPrefix); } registry.unregister(uri); break; case WORKSPACE: global = true; 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)); } try { session.checkWorkspacePermission(session.workspaceName(), ModeShapePermissions.REGISTER_NAMESPACE); } catch (AccessControlException ace) { throw new AccessDeniedException(ace); } if (existingPrefixForURI != null) { checkURINotInUse(uri); } registry.unregister(uri); break; default: assert false; } try { registry.register(prefix, uri); session.signalNamespaceChanges(global); } catch (RuntimeException e) { throw new RepositoryException(e.getMessage(), e.getCause()); } } protected  JcrNamespaceRegistry( NamespaceRegistry workspaceRegistry,
                                    JcrSession session );   JcrNamespaceRegistry( Behavior behavior,
                          NamespaceRegistry localRegistry,
                          NamespaceRegistry workspaceRegistry,
                          JcrSession session ); @Override String getPrefix( String uri ); @Override String[] getPrefixes(); @Override String getURI( String prefix ); @Override String[] getURIs(); @Override boolean isRegisteredPrefix( String prefix ); @Override boolean isRegisteredUri( String uri ); @Override synchronized void registerNamespace( String prefix,
                                                String uri ); @Override synchronized void unregisterNamespace( String prefix ); @Override String registerNamespace( String uri ); @Override String toString();  }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowRegisteringNullPrefix() throws Exception { registry.registerNamespace("foo", null); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowRegisteringNullUri() throws Exception { registry.registerNamespace(null, "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringPrefixThatStartsWithLowercaseXml() throws Exception { registry.registerNamespace("xmlw", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringPrefixThatStartsWithUppercaseXml() throws Exception { registry.registerNamespace("XMLw", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringPrefixThatStartsWithMixedcaseXml() throws Exception { registry.registerNamespace("XmLw", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringEmptyPrefix() throws Exception { registry.registerNamespace("", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringEmptyUri() throws Exception { registry.registerNamespace("foo", ""); }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingJcrPrefix() throws Exception { registry.registerNamespace("jcr", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingNtPrefix() throws Exception { registry.registerNamespace("nt", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingMixPrefix() throws Exception { registry.registerNamespace("mix", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingSvPrefix() throws Exception { registry.registerNamespace("sv", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingXmlPrefix() throws Exception { registry.registerNamespace("xml", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingDnaPrefix() throws Exception { registry.registerNamespace("mode", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingJcrUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingNtUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingMixUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingSvUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingXmlUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingXmlnsUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringUsingModeUri() throws Exception { registry.registerNamespace("foo", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringPrefixThatIsNotValidXmlNCName() throws Exception { registry.registerNamespace("1foot&in<door", "http: }
@Test( expected = NamespaceException.class ) public void shouldNotAllowRegisteringNamespaceWithPrefixThatIsNotValidName() throws Exception { registry.registerNamespace("foo:", "http: }
@Test public void shouldRegisterNewPrefixWithNewUri() throws Exception { String prefix = "foo"; String uri = "http: assertThatNamespacePrefixIsNotRegistered(prefix); assertThatNamespaceUriIsNotRegistered(uri); registry.registerNamespace(prefix, uri); assertThatNamespaceIsRegistered(prefix, uri); }
@Test public void shouldRegisterRemoveExistingMappingWhenUsingNewPrefixWithPreviouslyUsedUri() throws Exception { String prefix1 = "foo1"; String prefix2 = "foo2"; String uri = "http: assertThatNamespacePrefixIsNotRegistered(prefix1); assertThatNamespacePrefixIsNotRegistered(prefix2); assertThatNamespaceUriIsNotRegistered(uri); registry.registerNamespace(prefix1, uri); assertThatNamespaceIsRegistered(prefix1, uri); registry.registerNamespace(prefix2, uri); assertThatNamespaceIsRegistered(prefix2, uri); assertThatNamespacePrefixIsNotRegistered(prefix1); }
@Test public void shouldRegisterOverwriteExistingMappingWhenUsingPreviouslyUsedPrefixWithNewUri() throws Exception { String prefix = "foo1"; String uri1 = "http: String uri2 = "http: assertThatNamespacePrefixIsNotRegistered(prefix); assertThatNamespaceUriIsNotRegistered(uri1); assertThatNamespaceUriIsNotRegistered(uri2); registry.registerNamespace(prefix, uri1); assertThatNamespaceIsRegistered(prefix, uri1); registry.registerNamespace(prefix, uri2); assertThatNamespaceIsRegistered(prefix, uri2); assertThatNamespaceUriIsNotRegistered(uri1); }
@Test @FixFor( "MODE-2142" ) public void shouldNotAllowChangingThePrefixOfUsedNamespaces() throws Exception { String prefix = "admb"; String uri = "http: NodeTypeManager nodeTypeManager = session.getWorkspace().getNodeTypeManager(); registry.registerNamespace(prefix, uri); assertThatNamespaceIsRegistered(prefix, uri); NodeTypeTemplate nodeTypeTemplate = nodeTypeManager.createNodeTypeTemplate(); String nodeTypeName = prefix + ":test"; nodeTypeTemplate.setName(nodeTypeName); nodeTypeManager.registerNodeType(nodeTypeTemplate, false); try { registry.registerNamespace("newPrefix", uri); fail("Should not allow changing the prefix of a namespace used by a node type"); } catch (NamespaceException e) { } nodeTypeManager.unregisterNodeType(nodeTypeName); registry.registerNamespace("newPrefix", uri); assertThatNamespacePrefixIsNotRegistered(prefix); assertThatNamespaceIsRegistered("newPrefix", uri); }
@FixFor("MODE-2278") @Test(expected = NamespaceException.class) public void shouldNotAllowColonInPrefix() throws Exception { registry.registerNamespace("invalid", "http: registry.registerNamespace("invalid:prefix", "http: }