incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r673405 - in /incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal: ContentLoader.java JsonReader.java
Date Wed, 02 Jul 2008 14:36:50 GMT
Author: cziegeler
Date: Wed Jul  2 07:36:49 2008
New Revision: 673405

URL: http://svn.apache.org/viewvc?rev=673405&view=rev
Log:
SLING-553 Allow references and paths to be loaded from JSON through JsonReader. Applied patch
by Bryce Ewing.

Modified:
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/JsonReader.java

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java?rev=673405&r1=673404&r2=673405&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
Wed Jul  2 07:36:49 2008
@@ -52,6 +52,7 @@
 
     /** Delayed references during content loading for the reference property. */
     private final Map<String, List<String>> delayedReferences = new HashMap<String,
List<String>>();
+    private final Map<String, String[]> delayedMultipleReferences = new HashMap<String,
String[]>();
 
     private String defaultRootName;
 
@@ -231,7 +232,7 @@
         if ( propertyType == PropertyType.REFERENCE ) {
             // need to resolve the reference
             String propPath = node.getPath() + "/" + name;
-            String uuid = getUUID(node.getSession(), propPath, value);
+            String uuid = getUUID(node.getSession(), propPath, getAbsPath(node, value));
             if (uuid != null) {
                 node.setProperty(name, uuid, propertyType);
             }
@@ -262,7 +263,26 @@
             && !node.getProperty(name).isNew()) {
             return;
         }
-        node.setProperty(name, values, propertyType);
+        if ( propertyType == PropertyType.REFERENCE ) {
+            String propPath = node.getPath() + "/" + name;
+
+            boolean hasAll = true;
+            String[] uuids = new String[values.length];
+            String[] uuidOrPaths = new String[values.length];
+            for (int i = 0; i < values.length; i++) {
+                uuids[i] = getUUID(node.getSession(), propPath, getAbsPath(node, values[i]));
+                uuidOrPaths[i] = uuids[i] != null ? uuids[i] : getAbsPath(node, values[i]);
+                if (uuids[i] == null) hasAll = false;
+            }
+
+            node.setProperty(name, uuids, propertyType);
+
+            if (!hasAll) {
+                delayedMultipleReferences.put(propPath, uuidOrPaths);
+            }
+        } else {
+            node.setProperty(name, values, propertyType);
+        }
     }
 
     protected Value createValue(final ValueFactory factory, Object value) {
@@ -343,6 +363,21 @@
         resolveReferences(node);
     }
 
+    private String getAbsPath(Node node, String path) throws RepositoryException {
+        if (path.startsWith("/")) return path;
+
+        while (path.startsWith("../")) {
+            path = path.substring(3);
+            node = node.getParent();
+        }
+
+        while (path.startsWith("./")) {
+            path = path.substring(2);
+        }
+
+        return node.getPath() + "/" + path;
+    }
+
     private String getUUID(Session session, String propPath,
                           String referencePath)
     throws RepositoryException {
@@ -386,7 +421,32 @@
             String name = getName(property);
             Node parentNode = getParentNode(session, property);
             if (parentNode != null) {
-                parentNode.setProperty(name, uuid, PropertyType.REFERENCE);
+                if (parentNode.hasProperty(name) && parentNode.getProperty(name).getDefinition().isMultiple())
{
+                    boolean hasAll = true;
+                    String[] uuidOrPaths = delayedMultipleReferences.get(property);
+                    String[] uuids = new String[uuidOrPaths.length];
+                    for (int i = 0; i < uuidOrPaths.length; i++) {
+                        // is the reference still a path
+                        if (uuidOrPaths[i].startsWith("/")) {
+                            if (uuidOrPaths[i].equals(node.getPath())) {
+                                uuidOrPaths[i] = uuid;
+                                uuids[i] = uuid;
+                            } else {
+                                uuids[i] = null;
+                                hasAll = false;
+                            }
+                        } else {
+                            uuids[i] = uuidOrPaths[i];
+                        }
+                    }
+                    parentNode.setProperty(name, uuids, PropertyType.REFERENCE);
+
+                    if (hasAll) {
+                        delayedMultipleReferences.remove(property);
+                    }
+                } else {
+                    parentNode.setProperty(name, uuid, PropertyType.REFERENCE);
+                }
             }
         }
     }

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/JsonReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/JsonReader.java?rev=673405&r1=673404&r2=673405&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/JsonReader.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/JsonReader.java
Wed Jul  2 07:36:49 2008
@@ -38,6 +38,9 @@
  */
 class JsonReader implements ContentReader {
 
+    private static final String REFERENCE = "jcr:reference:";
+    private static final String PATH = "jcr:path:";
+
     private static final Set<String> ignoredNames = new HashSet<String>();
     static {
         ignoredNames.add("jcr:primaryType");
@@ -128,32 +131,41 @@
                 for (int i = 0; i < array.length(); i++) {
                     values[i] = array.get(i).toString();
                 }
-                final int propertyType = getType(values[0]);
-                contentCreator.createProperty(name, propertyType, values);
+                final int propertyType = getType(name, values[0]);
+                contentCreator.createProperty(getName(name), propertyType, values);
             } else {
-                contentCreator.createProperty(name, PropertyType.STRING, new String[0]);
+                contentCreator.createProperty(getName(name), PropertyType.STRING, new String[0]);
             }
 
         } else {
             // single value
-            final int propertyType = getType(value);
-            contentCreator.createProperty(name, propertyType, String.valueOf(value));
+            final int propertyType = getType(name, value);
+            contentCreator.createProperty(getName(name), propertyType, value.toString());
         }
     }
 
-    protected int getType(Object object) {
+    protected int getType(String name, Object object) {
         if (object instanceof Double || object instanceof Float) {
             return PropertyType.DOUBLE;
         } else if (object instanceof Number) {
             return PropertyType.LONG;
         } else if (object instanceof Boolean) {
             return PropertyType.BOOLEAN;
+        } else if (object instanceof String) {
+            if (name.startsWith(REFERENCE)) return PropertyType.REFERENCE;
+            if (name.startsWith(PATH)) return PropertyType.PATH;
         }
 
         // fall back to default
         return PropertyType.STRING;
     }
 
+    protected String getName(String name) {
+        if (name.startsWith(REFERENCE)) return name.substring(REFERENCE.length());
+        if (name.startsWith(PATH)) return name.substring(PATH.length());
+        return name;
+    }
+
     private String toString(InputStream ins) throws IOException {
         if (!ins.markSupported()) {
             ins = new BufferedInputStream(ins);



Mime
View raw message