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 2015/04/07 11:57:33 UTC

svn commit: r1671782 - in /sling/trunk/contrib/extensions/i18n/src: main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java

Author: cziegeler
Date: Tue Apr  7 09:57:33 2015
New Revision: 1671782

URL: http://svn.apache.org/r1671782
Log:
SLING-4579 : Regression introduced by SLING-4512 : JCR API used in i18n implementation

Modified:
    sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
    sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java

Modified: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java?rev=1671782&r1=1671781&r2=1671782&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java (original)
+++ sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java Tue Apr  7 09:57:33 2015
@@ -33,10 +33,6 @@ import java.util.Map;
 import java.util.ResourceBundle;
 import java.util.Set;
 
-import javax.jcr.Node;
-import javax.jcr.RepositoryException;
-import javax.jcr.util.TraversingItemVisitor;
-
 import org.apache.jackrabbit.commons.json.JsonHandler;
 import org.apache.jackrabbit.commons.json.JsonParser;
 import org.apache.sling.api.resource.Resource;
@@ -284,30 +280,28 @@ public class JcrResourceBundle extends R
         }
     }
 
-    private void loadSlingMessageDictionary(Resource dictionaryResource, final Map<String, Object> targetDictionary) {
-        log.info("Loading sling:Message dictionary: {}", dictionaryResource.getPath());
-
-        TraversingItemVisitor.Default visitor = new TraversingItemVisitor.Default() {
-            @Override
-            protected void entering(Node node, int level) throws RepositoryException {
-                if (node.isNodeType(NT_MESSAGE) && node.hasProperty(PROP_VALUE)) {
-                    String key;
-                    if (node.hasProperty(PROP_KEY)) {
-                        key = node.getProperty(PROP_KEY).getString();
-                    } else {
-                        key = node.getName();
-                    }
-                    String value = node.getProperty(PROP_VALUE).getString();
-                    targetDictionary.put(key, value);
-                }
+    /**
+     * Depth-first traversal of a resource tree
+     */
+    private void scanForSlingMessages(final Resource rsrc, final Map<String, Object> targetDictionary) {
+        final ValueMap vm = rsrc.adaptTo(ValueMap.class);
+        if ( vm != null ) {
+            final String value = vm.get(PROP_VALUE, String.class);
+            if ( value != null ) {
+                final String key = vm.get(PROP_KEY, rsrc.getName());
+                targetDictionary.put(key, value);
             }
-        };
-        try {
-            Node node = dictionaryResource.adaptTo(Node.class);
-            visitor.visit(node);
-        } catch (RepositoryException e) {
-            log.error("Could not read sling:Message dictionary: " + dictionaryResource.getPath(), e);
         }
+
+        for(final Resource c : rsrc.getChildren()) {
+            scanForSlingMessages(c, targetDictionary);
+        }
+    }
+
+    private void loadSlingMessageDictionary(final Resource dictionaryResource, final Map<String, Object> targetDictionary) {
+        log.info("Loading sling:Message dictionary: {}", dictionaryResource.getPath());
+
+        this.scanForSlingMessages(dictionaryResource, targetDictionary);
     }
 
     private Set<String> loadPotentialLanguageRoots(ResourceResolver resourceResolver, Locale locale, String baseName) {
@@ -316,9 +310,8 @@ public class JcrResourceBundle extends R
         final String localeRFC4646String = toRFC4646String(locale);
         final String localeRFC4646StringLower = localeRFC4646String.toLowerCase();
 
-        Set<String> paths = new LinkedHashSet<String>();
-        @SuppressWarnings("deprecation")
-        Iterator<Resource> bundles = resourceResolver.findResources(QUERY_LANGUAGE_ROOTS, "xpath");
+        final Set<String> paths = new LinkedHashSet<String>();
+        final Iterator<Resource> bundles = resourceResolver.findResources(QUERY_LANGUAGE_ROOTS, "xpath");
         while (bundles.hasNext()) {
             Resource bundle = bundles.next();
             ValueMap properties = bundle.adaptTo(ValueMap.class);

Modified: sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java?rev=1671782&r1=1671781&r2=1671782&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java (original)
+++ sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java Tue Apr  7 09:57:33 2015
@@ -89,7 +89,7 @@ public class JcrResourceBundleTest exten
                         @Override
                         public Resource next() {
                             final Node node = nodes.nextNode();
-                            return new TestResource(node);
+                            return new TestResource(resolver, node);
                         }
 
                         @Override
@@ -113,10 +113,8 @@ public class JcrResourceBundleTest exten
             @Override
             public Resource getResource(String path) {
                 try {
-                    Node n = getSession().getNode(path);
-                    if (n != null) {
-                        return new TestResource(n);
-                    }
+                    final Node n = getSession().getNode(path);
+                    return new TestResource(resolver, n);
 
                 } catch (NamingException ne) {
                      //ignore
@@ -132,9 +130,34 @@ public class JcrResourceBundleTest exten
             }
 
             @Override
-            public Iterator<Resource> listChildren(Resource parent) {
-                // TODO Auto-generated method stub
-                return null;
+            public Iterator<Resource> listChildren(final Resource parent) {
+                try {
+                    final Node n = getSession().getNode(parent.getPath());
+                    final NodeIterator nodes = n.getNodes();
+                    return new Iterator<Resource>() {
+                        @Override
+                        public boolean hasNext() {
+                            return nodes.hasNext();
+                        }
+
+                        @Override
+                        public Resource next() {
+                            final Node node = nodes.nextNode();
+                            return new TestResource(resolver, node);
+                        }
+
+                        @Override
+                        public void remove() {
+                            throw new UnsupportedOperationException("remove");
+                        }
+                    };
+                } catch ( final RepositoryException re) {
+                    // ignore
+                    return null;
+                } catch ( final NamingException e) {
+                    // ignore
+                    return null;
+                }
             }
 
             @Override
@@ -626,10 +649,13 @@ public class JcrResourceBundleTest exten
 
     private class TestResource extends AbstractResource {
 
-        private Node node = null;
-        public TestResource(Node xnode) {
-            super();
-            node = xnode;
+        private final Node node;
+
+        private final ResourceResolver resolver;
+
+        public TestResource(final ResourceResolver resolver, final Node xnode) {
+            this.node = xnode;
+            this.resolver = resolver;
         }
 
         @Override
@@ -646,8 +672,7 @@ public class JcrResourceBundleTest exten
 
         @Override
         public ResourceResolver getResourceResolver() {
-            // TODO Auto-generated method stub
-            return null;
+            return this.resolver;
         }
 
         @Override
@@ -676,6 +701,12 @@ public class JcrResourceBundleTest exten
                     if ( node.hasProperty(JcrResourceBundle.PROP_BASENAME) ) {
                         props.put(JcrResourceBundle.PROP_BASENAME, node.getProperty(JcrResourceBundle.PROP_BASENAME).getString());
                     }
+                    if ( node.hasProperty(JcrResourceBundle.PROP_KEY) ) {
+                        props.put(JcrResourceBundle.PROP_KEY, node.getProperty(JcrResourceBundle.PROP_KEY).getString());
+                    }
+                    if ( node.hasProperty(JcrResourceBundle.PROP_VALUE) ) {
+                        props.put(JcrResourceBundle.PROP_VALUE, node.getProperty(JcrResourceBundle.PROP_VALUE).getString());
+                    }
                     return (AdapterType)new ValueMapDecorator(props);
                 } catch ( final RepositoryException re ) {
                     throw new RuntimeException(re);