incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r688126 - /incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Date Fri, 22 Aug 2008 17:16:23 GMT
Author: cziegeler
Date: Fri Aug 22 10:16:22 2008
New Revision: 688126

URL: http://svn.apache.org/viewvc?rev=688126&view=rev
Log:
#0000 - Add method to create repository paths.

Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java?rev=688126&r1=688125&r2=688126&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
(original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Fri Aug 22 10:16:22 2008
@@ -20,6 +20,7 @@
 
 import java.io.InputStream;
 import java.util.Calendar;
+import java.util.StringTokenizer;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
@@ -240,4 +241,57 @@
 
         return resourceSuperType;
     }
+
+    /**
+     * Creates or gets the {@link javax.jcr.Node Node} at the given Path.
+     * In case it has to create the Node all non-existent intermediate path-elements
+     * will be create with the given intermediate node type and the returned node
+     * will be created with the given nodeType
+     *
+     * @param path to create
+     * @param intermediateNodeType to use for creation of intermediate nodes
+     * @param nodeType to use for creation of the final node
+     * @param session to use
+     * @param autoSave Should save be called when a new node is created?
+     * @return the Node at path
+     * @throws RepositoryException in case of exception accessing the Repository
+     */
+    public static Node createPath(String path,
+                                  String intermediateNodeType,
+                                  String nodeType,
+                                  Session session,
+                                  boolean autoSave)
+    throws RepositoryException {
+        if (path == null || path.length() == 0 || "/".equals(path)) {
+            return session.getRootNode();
+        } else if (!session.itemExists(path)) {
+            Node node = session.getRootNode();
+            path = path.substring(1);
+            int pos = path.lastIndexOf('/');
+            if ( pos != -1 ) {
+                final StringTokenizer st = new StringTokenizer(path.substring(0, pos), "/");
+                while ( st.hasMoreTokens() ) {
+                    final String token = st.nextToken();
+                    if ( !node.hasNode(token) ) {
+                        try {
+                            node.addNode(token, intermediateNodeType);
+                            if ( autoSave ) node.save();
+                        } catch (RepositoryException re) {
+                            // we ignore this as this folder might be created from a different
task
+                            node.refresh(false);
+                        }
+                    }
+                    node = node.getNode(token);
+                }
+                path = path.substring(pos + 1);
+            }
+            if ( !node.hasNode(path) ) {
+                node.addNode(path, nodeType);
+                if ( autoSave ) node.save();
+            }
+            return node.getNode(path);
+        } else {
+            return (Node) session.getItem(path);
+        }
+    }
 }



Mime
View raw message