You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2012/10/24 10:21:31 UTC

svn commit: r1401581 - /sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java

Author: cziegeler
Date: Wed Oct 24 08:21:31 2012
New Revision: 1401581

URL: http://svn.apache.org/viewvc?rev=1401581&view=rev
Log:
SLING-2195 : Add a resource visitor. Applied slightly modified patch from Victor Saar

Added:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java   (with props)

Added: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java?rev=1401581&view=auto
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java (added)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java Wed Oct 24 08:21:31 2012
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  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.sling.api.resource;
+
+import java.util.Iterator;
+
+/**
+ * The <code>AbstractResourceVisitor</code> helps in traversing a
+ * resource tree by decoupling the actual traversal code
+ * from application code. Concrete subclasses should implement
+ * the {@link ResourceVisitor#visit(Resource)} method.
+ * 
+ * @since 2.2
+ */
+public abstract class AbstractResourceVisitor {
+
+    /**
+     * Visit the given resource and all its descendants.
+     * @param res The resource
+     */
+    public void accept(final Resource res) {
+        if (res != null) {
+            this.visit(res);
+            this.traverseChildren(res.listChildren());
+        }
+    }
+
+    /**
+     * Visit the given resources.
+     * @param children The list of resources
+     */
+    protected void traverseChildren(final Iterator<Resource> children) {
+        while (children.hasNext()) {
+            final Resource child = children.next();
+
+            accept(child);
+        }
+    }
+
+    /**
+     * Implement this method to do actual work on the resources.
+     * @param res The resource
+     */
+    protected abstract void visit(final Resource res);
+}

Propchange: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/AbstractResourceVisitor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain