incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1519583 - in /sling/trunk: bundles/api/src/main/java/org/apache/sling/api/resource/ contrib/extensions/collection/ contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/
Date Tue, 03 Sep 2013 06:57:27 GMT
Author: cziegeler
Date: Tue Sep  3 06:57:26 2013
New Revision: 1519583

URL: http://svn.apache.org/r1519583
Log:
SLING-2854 : Move the ResourceCollectionUtil.createUniqueChildName method to ResourceUtils

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
    sling/trunk/contrib/extensions/collection/pom.xml
    sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=1519583&r1=1519582&r2=1519583&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
(original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
Tue Sep  3 06:57:26 2013
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.api.resource;
 
+import java.text.MessageFormat;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -608,4 +609,39 @@ public class ResourceUtil {
         }
         return rsrc;
     }
+
+    /**
+     * Create a unique name for a child of the <code>parent</code>.
+     * Creates a unique name and test if child already exists.
+     * If child resource with the same name exists, iterate until a unique one is found.
+     *
+     * @param parent The parent resource
+     * @param name   The name of the child resource
+     * @return a unique non-existing name for child resource for a given <code>parent</code>
+     *
+     * @throws {@link PersistenceException} if it can not find unique name for child resource.
+     * @throws {@link NullPointerException} if <code>parent</code> is null
+     * @since 2.5.0
+     */
+    public static String createUniqueChildName(final Resource parent, final String name)
+    throws PersistenceException {
+        if (parent.getChild(name) != null) {
+            // leaf node already exists, create new unique name
+            String childNodeName = null;
+            int i = 0;
+            do {
+                childNodeName = name + String.valueOf(i);
+                //just so that it does not run into an infinite loop
+                // this should not happen though :)
+                if (i == Integer.MAX_VALUE) {
+                    String message = MessageFormat.format("can not find a unique name {0}
for {1}", name, parent.getPath());
+                    throw new PersistenceException(message);
+                }
+                i++;
+            } while (parent.getChild(childNodeName) != null);
+
+            return childNodeName;
+        }
+        return name;
+    }
 }

Modified: sling/trunk/contrib/extensions/collection/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/collection/pom.xml?rev=1519583&r1=1519582&r2=1519583&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/collection/pom.xml (original)
+++ sling/trunk/contrib/extensions/collection/pom.xml Tue Sep  3 06:57:26 2013
@@ -83,7 +83,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.4.0</version>
+            <version>2.4.3-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         

Modified: sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java?rev=1519583&r1=1519582&r2=1519583&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
(original)
+++ sling/trunk/contrib/extensions/collection/src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java
Tue Sep  3 06:57:26 2013
@@ -38,7 +38,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import org.apache.sling.resource.collection.ResourceCollection;
-import org.apache.sling.resource.collection.impl.util.ResourceCollectionUtil;
 
 /**
  * Implements <code>ResourceCollection</code>
@@ -108,7 +107,7 @@ public class ResourceCollectionImpl impl
             properties.put(ResourceCollectionConstants.REF_PROPERTY, res.getPath());
             resolver.create(
                 membersResource,
-                ResourceCollectionUtil.createUniqueChildName(membersResource,
+                ResourceUtil.createUniqueChildName(membersResource,
                     res.getName()), properties);
             log.debug("added member to resource {} to collection {}",
                 new String[] { res.getPath(), resource.getPath() });
@@ -133,7 +132,7 @@ public class ResourceCollectionImpl impl
         	properties.put(ResourceCollectionConstants.REF_PROPERTY, res.getPath());
             resolver.create(
                 membersResource,
-                ResourceCollectionUtil.createUniqueChildName(membersResource,
+                ResourceUtil.createUniqueChildName(membersResource,
                     res.getName()), properties);
             log.debug("added member to resource {} to collection {}",
                 new String[] { res.getPath(), resource.getPath() });



Mime
View raw message