This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.resourcemerger-1.0.0
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcemerger.git

commit 6c958101e21e58de72b7030e0fc3210e935a1dd4
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Thu Jan 30 08:11:18 2014 +0000

    SLING-2986 : Reimplement listChildren and make MergedResource immutable
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/resourcemerger@1562716
 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/resourcemerger/impl/MergedResource.java  |  7 --
 .../impl/MergedResourceProvider.java               | 98 +++++++---------------
 2 files changed, 28 insertions(+), 77 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java 
b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
index 4fc8d7a..df1da1a 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
@@ -90,13 +90,6 @@ public class MergedResource extends AbstractResource {
     /**
      * {@inheritDoc}
      */
-    public void addMappedResource(final String path) {
-        mappedResources.add(path);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     public Iterable<String> getMappedResources() {
         return mappedResources;
     }
diff --git 
a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
 
b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
index d0c3083..53d0243 100644
--- 
a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
+++ 
b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
@@ -19,7 +19,6 @@
 package org.apache.sling.resourcemerger.impl;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 
@@ -54,7 +53,6 @@ public class MergedResourceProvider implements 
ResourceProvider {
      * {@inheritDoc}
      */
     public Resource getResource(final ResourceResolver resolver, final String 
path) {
-
         if (resolver.getSearchPath() != null) {
             final String relativePath = getRelativePath(path);
 
@@ -77,7 +75,7 @@ public class MergedResourceProvider implements 
ResourceProvider {
                             final String[] childrenToHideArray = 
parentProps.get(MergedResourceConstants.PN_HIDE_CHILDREN, String[].class);
                             if ( childrenToHideArray != null ) {
                                 for(final String name : childrenToHideArray ) {
-                                    if ( name.equals(baseRes.getName()) ) {
+                                    if ( name.equals(baseRes.getName()) || 
name.equals("*") ) {
                                         hidden = true;
                                         break;
                                     }
@@ -108,85 +106,45 @@ public class MergedResourceProvider implements 
ResourceProvider {
      * {@inheritDoc}
      */
     public Iterator<Resource> listChildren(final Resource resource) {
-        if (resource instanceof MergedResource) {
-            final MergedResource mergedResource = (MergedResource) resource;
-            final ResourceResolver resolver = 
mergedResource.getResourceResolver();
-            final List<Resource> children = new ArrayList<Resource>();
-
-            for (final String mappedResourcePath : 
mergedResource.getMappedResources()) {
-                final Resource mappedResource = 
resolver.getResource(mappedResourcePath);
+        final ResourceResolver resolver = resource.getResourceResolver();
 
-                // Check if the resource exists
-                if (mappedResource == null) {
-                    continue;
-                }
-
-                // Check if some previously defined children have to be ignored
-                final ValueMap mappedResourceProps = 
mappedResource.adaptTo(ValueMap.class);
-                final String[] childrenToHideArray = 
mappedResourceProps.get(MergedResourceConstants.PN_HIDE_CHILDREN, 
String[].class);
-                if ( childrenToHideArray != null ) {
-                    final List<String> childrenToHide = 
Arrays.asList(childrenToHideArray);
-                    if ( childrenToHide.contains("*") ) {
-                        // Clear current children list
-                        children.clear();
-                    } else {
-                        // Go through current children in order to hide them 
individually
-                        final Iterator<Resource> it = children.iterator();
-                        while (it.hasNext()) {
-                            if (childrenToHide.contains(it.next().getName())) {
-                                it.remove();
-                            }
-                        }
-                    }
-                }
+        if (resolver.getSearchPath() != null) {
+            final String relativePath = getRelativePath(resource.getPath());
 
-                // Browse children of current physical resource
-                for (final Resource child : mappedResource.getChildren()) {
-                    final String childRelativePath = 
mergedResource.getRelativePath() + "/" + child.getName();
-
-                    if 
(ResourceUtil.getValueMap(child).get(MergedResourceConstants.PN_HIDE_RESOURCE, 
Boolean.FALSE)) {
-                        // Child resource has to be hidden
-                        children.remove(new MergedResource(resolver, 
mergeRootPath, childRelativePath));
-
-                    } else {
-                        // Check if the child resource already exists in the 
children list
-                        MergedResource mergedResChild = new 
MergedResource(resolver, mergeRootPath, childRelativePath);
-                        int mergedResChildIndex = -1;
-                        if (children.contains(mergedResChild)) {
-                            // Get current index of the merged resource's child
-                            mergedResChildIndex = 
children.indexOf(mergedResChild);
-                            mergedResChild = (MergedResource) 
children.get(mergedResChildIndex);
+            if ( relativePath != null ) {
+                final List<String> names = new ArrayList<String>();
+                // Loop over provided base paths
+                for (final String basePath : resolver.getSearchPath()) {
+                    final Resource baseResource = 
resolver.getResource(basePath + "/" + relativePath);
+                    if ( baseResource != null ) {
+                        final String rsrcName = baseResource.getName();
+                        if ( !names.contains(rsrcName) ) {
+                            names.add(rsrcName);
                         }
-                        // Add a new mapped resource to the merged resource
-                        mergedResChild.addMappedResource(child.getPath());
-                        boolean mergedResChildExists = mergedResChildIndex > 
-1;
-
                         // Check if children need reordering
                         int orderBeforeIndex = -1;
-                        String orderBefore = 
ResourceUtil.getValueMap(child).get(MergedResourceConstants.PN_ORDER_BEFORE, 
String.class);
-                        if (orderBefore != null && 
!orderBefore.equals(mergedResChild.getName())) {
-                            // Get a dummy merged resource just to know the 
index of that merged resource
-                            MergedResource orderBeforeRes = new 
MergedResource(resolver, mergeRootPath, mergedResource.getRelativePath() + "/" 
+ orderBefore);
-                            if (children.contains(orderBeforeRes)) {
-                                orderBeforeIndex = 
children.indexOf(orderBeforeRes);
-                            }
+                        final ValueMap vm = 
ResourceUtil.getValueMap(baseResource);
+                        final String orderBefore = 
vm.get(MergedResourceConstants.PN_ORDER_BEFORE, String.class);
+                        if (orderBefore != null && 
!orderBefore.equals(rsrcName)) {
+                            // search entry
+                            orderBeforeIndex = names.indexOf(orderBefore);
                         }
 
                         if (orderBeforeIndex > -1) {
-                            // Add merged resource's child at the right 
position
-                            children.add(orderBeforeIndex, mergedResChild);
-                            if (mergedResChildExists) {
-                                children.remove(mergedResChildIndex > 
orderBeforeIndex ? ++mergedResChildIndex : mergedResChildIndex);
-                            }
-                        } else if (!mergedResChildExists) {
-                            // Only add the merged resource's child if it did 
not exist yet
-                            children.add(mergedResChild);
+                            names.add(orderBeforeIndex, rsrcName);
+                            names.remove(names.size() - 1);
                         }
                     }
                 }
+                final List<Resource> children = new ArrayList<Resource>();
+                for(final String name : names) {
+                    final Resource mergedResource = this.getResource(resolver, 
resource.getPath() + "/" + name);
+                    if ( mergedResource != null ) {
+                        children.add(mergedResource);
+                    }
+                }
+                return children.iterator();
             }
-
-            return children.iterator();
         }
 
         // Return null for resources that aren't a MergedResource

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Reply via email to