jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r396265 - in /jackrabbit/trunk/jackrabbit/src: main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java test/java/org/apache/jackrabbit/core/TestAll.java
Date Sun, 23 Apr 2006 12:39:37 GMT
Author: jukka
Date: Sun Apr 23 05:39:35 2006
New Revision: 396265

URL: http://svn.apache.org/viewcvs?rev=396265&view=rev
Log:
JCR-409: Added NamespaceRegistryImpl.safeRegisterNamespace() to simpilify namespace registration.

Added:
    jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
    jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/TestAll.java

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewcvs/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java?rev=396265&r1=396264&r2=396265&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
Sun Apr 23 05:39:35 2006
@@ -207,6 +207,49 @@
         return "_pre" + (prefixToURI.size() + 1);
     }
 
+    /**
+     * Registers a namespace using the given prefix hint. Does nothing
+     * if the namespace is already registered. If the given prefix hint
+     * is not yet registered as a prefix, then it is used as the prefix
+     * of the registered namespace. Otherwise a unique prefix is generated
+     * based on the given hint.
+     *
+     * @param prefixHint the prefix hint
+     * @param uri the namespace URI
+     * @throws RepositoryException if an unexpected error occurs
+     * @see #registerNamespace(String, String)
+     */
+    public void safeRegisterNamespace(String prefixHint, String uri)
+            throws NamespaceException, RepositoryException {
+        try {
+            // Check if the namespace is already registered
+            getPrefix(uri);
+            // ... it is, so do nothing.
+        } catch (NamespaceException e1) {
+            // ... it is not, try to find a unique prefix.
+
+            // First, check and replace troublesome prefix hints.
+            if (prefixHint.toLowerCase().startsWith(QName.NS_XML_PREFIX)
+                    || !XMLChar.isValidNCName(prefixHint)) {
+                prefixHint = "_pre";
+            }
+
+            // Then, find an appropriate prefix based on the hint
+            String prefix = prefixHint;
+            try {
+                for (int suffix = 2; true; suffix++) {
+                    // Is this prefix already registered?
+                    getURI(prefix);
+                    // ... it is, generate a new prefix and try again.
+                    prefix = prefixHint + suffix;
+                }
+            } catch (NamespaceException e2) {
+                // ... it is not, register the namespace with this prefix.
+                registerNamespace(prefix, uri);
+            }
+        }
+    }
+
     //----------------------------------------------------< NamespaceRegistry >
     /**
      * {@inheritDoc}

Added: jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java
URL: http://svn.apache.org/viewcvs/jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java?rev=396265&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java
(added)
+++ jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java
Sun Apr 23 05:39:35 2006
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import javax.jcr.NamespaceException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.name.QName;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for the {@link NamespaceRegistryImpl} class. See also the
+ * {@link org.apache.jackrabbit.test.api.NamespaceRegistryTest} and
+ * {@link org.apache.jackrabbit.test.api.NamespaceRegistryReadMethodsTest}
+ * test cases in the JCR TCK test suite.
+ */
+public class NamespaceRegistryImplTest extends TestCase {
+
+    private static final String PREFIX = "test_safe_register_namespace";
+
+    private static final String URI1 = "test-safe-register-namespace1";
+
+    private static final String URI2 = "test-safe-register-namespace2";
+
+    private static final String URI3 = "test-safe-register-namespace3";
+
+    private Session session;
+
+    private NamespaceRegistryImpl registry;
+
+    /**
+     * Removes the registered test namespaces.
+     *
+     * @throws RepositoryException on repository errors
+     */
+    private void clean() throws RepositoryException {
+        try {
+            registry.unregisterNamespace(registry.getPrefix(URI1));
+        } catch (NamespaceException e) {
+            // URI1 not yet registered
+        }
+        try {
+            registry.unregisterNamespace(registry.getPrefix(URI2));
+        } catch (NamespaceException e) {
+            // URI2 not yet registered
+        }
+        try {
+            registry.unregisterNamespace(registry.getPrefix(URI3));
+        } catch (NamespaceException e) {
+            // URI3 not yet registered
+        }
+    }
+
+    protected void setUp() throws RepositoryException {
+        session = TestRepository.getInstance().login();
+        registry = (NamespaceRegistryImpl)
+            session.getWorkspace().getNamespaceRegistry();
+        clean();
+    }
+
+    protected void tearDown() throws RepositoryException {
+        clean();
+        session.logout();
+    }
+
+    public void testSafeRegisterNamespace() throws RepositoryException {
+        try {
+            registry.safeRegisterNamespace(PREFIX, URI1);
+        } catch (NamespaceException e) {
+            fail("safeRegisterNamespace() fails to register a namespace");
+        }
+        try {
+            registry.safeRegisterNamespace(PREFIX, URI2);
+        } catch (NamespaceException e) {
+            fail("safeRegisterNamespace() fails to generate a unique prefix");
+        }
+        try {
+            registry.safeRegisterNamespace(PREFIX, URI1);
+        } catch (NamespaceException e) {
+            fail("safeRegisterNamespace() fails to reregister a namespace");
+        }
+        try {
+            registry.safeRegisterNamespace(QName.NS_XML_PREFIX, URI3);
+        } catch (NamespaceException e) {
+            fail("safeRegisterNamespace() fails to handle a reserved prefix");
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/NamespaceRegistryImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/TestAll.java
URL: http://svn.apache.org/viewcvs/jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/TestAll.java?rev=396265&r1=396264&r2=396265&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/TestAll.java (original)
+++ jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/core/TestAll.java Sun
Apr 23 05:39:35 2006
@@ -37,6 +37,7 @@
         //suite.addTestSuite(ConcurrencyTest.class);
         //suite.addTestSuite(ConcurrentSaveTest.class);
         //suite.addTestSuite(LockTest.class);
+        suite.addTestSuite(NamespaceRegistryImplTest.class);
         suite.addTestSuite(TransientRepositoryTest.class);
         suite.addTestSuite(XATest.class);
 



Mime
View raw message