You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/02/29 17:44:16 UTC

svn commit: r632383 - in /incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource: ServletResourceIterator.java ServletResourceProvider.java

Author: fmeschbe
Date: Fri Feb 29 08:44:10 2008
New Revision: 632383

URL: http://svn.apache.org/viewvc?rev=632383&view=rev
Log:
SLING-262 Fully implement the ResourceProvider.listChildren() methods in the
ServletResourceProvider by means of a new ServletResourceIterator class

Added:
    incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceIterator.java
Modified:
    incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceProvider.java

Added: incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceIterator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceIterator.java?rev=632383&view=auto
==============================================================================
--- incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceIterator.java (added)
+++ incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceIterator.java Fri Feb 29 08:44:10 2008
@@ -0,0 +1,101 @@
+/*
+ * 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.servlet.resolver.resource;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.SyntheticResource;
+
+class ServletResourceIterator implements Iterator<Resource> {
+
+    private final ServletResourceProvider provider;
+
+    private final Resource parentResource;
+
+    private Iterator<String> pathIter;
+
+    private String parentPath;
+
+    private Resource next;
+
+    private Set<String> visited;
+
+    ServletResourceIterator(ServletResourceProvider provider, Resource parent) {
+        this.provider = provider;
+        this.parentResource = parent;
+
+        pathIter = provider.getServletPathIterator();
+        parentPath = parent.getPath();
+        if (!parentPath.endsWith("/")) {
+            parentPath = parentPath.concat("/");
+        }
+        visited = new HashSet<String>();
+        next = seek();
+    }
+
+    public boolean hasNext() {
+        return next != null;
+    }
+
+    public Resource next() {
+        if (!hasNext()) {
+            throw new NoSuchElementException();
+        }
+
+        Resource result = next;
+        next = seek();
+        return result;
+    }
+
+    public void remove() {
+        throw new UnsupportedOperationException("remove");
+    }
+
+    private Resource seek() {
+        while (pathIter.hasNext()) {
+            String path = pathIter.next();
+            if (path.startsWith(parentPath)) {
+                int nextSlash = path.indexOf('/', parentPath.length());
+                if (nextSlash < 0) {
+                    return new ServletResource(
+                        parentResource.getResourceResolver(),
+                        provider.getServlet(), path);
+                }
+
+                path = path.substring(0, nextSlash);
+                if (!visited.contains(path)) {
+                    visited.add(path);
+                    if (parentResource.getResourceResolver().getResource(path) == null) {
+                        return new SyntheticResource(
+                            parentResource.getResourceResolver(), path,
+                            ResourceProvider.RESOURCE_TYPE_SYNTHETIC);
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+
+}

Modified: incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceProvider.java?rev=632383&r1=632382&r2=632383&view=diff
==============================================================================
--- incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceProvider.java (original)
+++ incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/resource/ServletResourceProvider.java Fri Feb 29 08:44:10 2008
@@ -157,55 +157,20 @@
     }
 
     public Iterator<Resource> listChildren(final Resource parent) {
-        return new Iterator<Resource>() {
-
-            private Iterator<String> pathIter;
-
-            private String parentPath;
-
-            private Resource next;
-
-            {
-                pathIter = resourcePaths.iterator();
-                parentPath = parent.getPath() + "/";
-                next = seek();
-            }
-
-            public boolean hasNext() {
-                return next != null;
-            }
-
-            public Resource next() {
-                if (!hasNext()) {
-                    throw new NoSuchElementException();
-                }
-
-                Resource result = next;
-                next = seek();
-                return result;
-            }
-
-            public void remove() {
-                throw new UnsupportedOperationException("remove");
-            }
-
-            private Resource seek() {
-                while (pathIter.hasNext()) {
-                    String path = pathIter.next();
-                    if (path.startsWith(parentPath)
-                        && path.indexOf('/', parentPath.length()) < 0) {
-                        return new ServletResource(
-                            parent.getResourceResolver(), servlet, path);
-                    }
-                }
-
-                return null;
-            }
-
-        };
+        return new ServletResourceIterator(this, parent);
     }
 
+    Servlet getServlet() {
+        return servlet;
+    }
+    
+    Iterator<String> getServletPathIterator() {
+        return resourcePaths.iterator();
+    }
+    
     public String[] getSerlvetPaths() {
         return resourcePaths.toArray(new String[resourcePaths.size()]);
     }
+    
+    
 }