Author: amitgupt
Date: Tue Mar 31 11:48:25 2015
New Revision: 1670310

URL: http://svn.apache.org/r1670310
Log:
SLING-4512 - Traversal Warnings in OAK while creating i18n JcrResourceBundle

Modified:
    
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.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=1670310&r1=1670309&r2=1670310&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 Mar 31 11:48:25 2015
@@ -32,15 +32,15 @@ import java.util.Locale;
 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.jackrabbit.util.ISO9075;
-import org.apache.sling.api.SlingException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -49,7 +49,7 @@ public class JcrResourceBundle extends R
 
     private static final Logger log = 
LoggerFactory.getLogger(JcrResourceBundle.class);
 
-    static final String JCR_PATH = "jcr:path";
+    static final String NT_MESSAGE = "sling:Message";
 
     static final String PROP_KEY = "sling:key";
 
@@ -59,14 +59,6 @@ public class JcrResourceBundle extends R
 
     static final String PROP_LANGUAGE = "jcr:language";
 
-    /**
-     * java.util.Formatter pattern to build the XPath query to search for
-     * messages in a given root path (%s argument).
-     *
-     * @see #loadFully(ResourceResolver, Set, Set)
-     */
-    private static final String QUERY_MESSAGES_FORMAT = 
"/jcr:root%s//element(*,sling:Message)";
-
     static final String QUERY_LANGUAGE_ROOTS = 
"//element(*,mix:language)[@jcr:language]";
 
     private final Map<String, Object> resources;
@@ -202,7 +194,7 @@ public class JcrResourceBundle extends R
             if (dictionaryResource.getName().endsWith(".json")) {
                 loadJsonDictionary(dictionaryResource, dictionary);
             } else {
-                loadSlingMessageDictionary(resolver, root, dictionary);
+                loadSlingMessageDictionary(dictionaryResource, dictionary);
             }
 
             if (!dictionary.isEmpty()) {
@@ -293,34 +285,29 @@ public class JcrResourceBundle extends R
         }
     }
 
-    private void loadSlingMessageDictionary(ResourceResolver resourceResolver, 
String path, Map<String, Object> targetDictionary) {
-        // run query for sling:Message nodes
-        String dictQuery = String.format(QUERY_MESSAGES_FORMAT, 
ISO9075.encodePath(path));
-
-        log.info("Loading sling:Message dictionary: {}", path);
-        log.info("Executing query {}", dictQuery);
+    private void loadSlingMessageDictionary(Resource dictionaryResource, final 
Map<String, Object> targetDictionary) {
+        log.info("Loading sling:Message dictionary: {}", 
dictionaryResource.getPath());
 
-        try {
-            // do an XPath query because this won't go away soon and still
-            // (2011/04/04) is the fastest query language ...
-            Iterator<Map<String, Object>> queryResult = 
resourceResolver.queryResources(dictQuery, "xpath");
-
-            while (queryResult.hasNext()) {
-                final Map<String, Object> row = queryResult.next();
-                if (row.containsKey(PROP_VALUE)) {
-                    final String jcrPath = (String) row.get(JCR_PATH);
-                    String key = (String) row.get(PROP_KEY);
-
-                    if (key == null) {
-                        key = ResourceUtil.getName(jcrPath);
+        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();
                     }
-
-                    targetDictionary.put(key, row.get(PROP_VALUE));
+                    String value = node.getProperty(PROP_VALUE).getString();
+                    targetDictionary.put(key, value);
                 }
             }
-
-        } catch (final SlingException se) {
-            log.error("Exception during resource query " + dictQuery, se);
+        };
+        try {
+            Node node = dictionaryResource.adaptTo(Node.class);
+            visitor.visit(node);
+        } catch (RepositoryException e) {
+            log.error("Could not read sling:Message dictionary: " + 
dictionaryResource.getPath(), e);
         }
     }
 


Reply via email to