incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r618414 - in /incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader: JsonReader.java Loader.java XJsonReader.java
Date Mon, 04 Feb 2008 20:15:33 GMT
Author: fmeschbe
Date: Mon Feb  4 12:15:32 2008
New Revision: 618414

URL: http://svn.apache.org/viewvc?rev=618414&view=rev
Log:
SLING-194 Add support for JSON format written by JsonWriter (XJsonReader)

   Work in Progress, see issue for details

Added:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/XJsonReader.java
Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/JsonReader.java
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/Loader.java

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/JsonReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/JsonReader.java?rev=618414&r1=618413&r2=618414&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/JsonReader.java
(original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/JsonReader.java
Mon Feb  4 12:15:32 2008
@@ -52,7 +52,7 @@
         }
     }
 
-    private Node createNode(String name, JSONObject nodeDescriptor) throws JSONException
{
+    protected Node createNode(String name, JSONObject nodeDescriptor) throws JSONException
{
         Node node = new Node();
         node.setName(name);
 
@@ -100,7 +100,7 @@
         return node;
     }
 
-    private Property createProperty(String name, Object propDescriptorObject) throws JSONException
{
+    protected Property createProperty(String name, Object propDescriptorObject) throws JSONException
{
         if (propDescriptorObject == null) {
             return null;
         }
@@ -155,7 +155,7 @@
         return property;
     }
 
-    private String getType(Object object) {
+    protected String getType(Object object) {
         if (object instanceof Double || object instanceof Float) {
             return PropertyType.TYPENAME_DOUBLE;
         } else if (object instanceof Number) {

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/Loader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/Loader.java?rev=618414&r1=618413&r2=618414&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/Loader.java
(original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/Loader.java
Mon Feb  4 12:15:32 2008
@@ -57,6 +57,7 @@
 
     public static final String EXT_XML = ".xml";
     public static final String EXT_JSON = ".json";
+    public static final String EXT_XJSON = ".xjson";
 
     // default content type for createFile()
     private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
@@ -67,6 +68,7 @@
     private ContentLoaderService jcrContentHelper;
     private XmlReader xmlReader;
     private JsonReader jsonReader;
+    private XJsonReader xjsonReader;
     private Map<String, List<String>> delayedReferences;
 
     // bundles whose registration failed and should be retried
@@ -81,6 +83,7 @@
     public void dispose() {
         this.xmlReader = null;
         this.jsonReader = null;
+        this.xjsonReader = null;
         this.delayedReferences = null;
         if (this.delayedBundles != null) {
             this.delayedBundles.clear();
@@ -192,6 +195,9 @@
                 if (nodeDescriptor == null) {
                     nodeDescriptor = bundle.getEntry(base + EXT_JSON);
                 }
+                if (nodeDescriptor == null) {
+                    nodeDescriptor = bundle.getEntry(base + EXT_XJSON);
+                }
 
                 // if we have a descriptor, which has not been processed yet,
                 // otherwise call crateFolder, which creates an nt:folder or
@@ -217,7 +223,9 @@
                 }
 
                 // install if it is a descriptor
-                if (entry.endsWith(EXT_XML) || entry.endsWith(EXT_JSON)) {
+                if (entry.endsWith(EXT_XML)
+                        || entry.endsWith(EXT_JSON)
+                        || entry.endsWith(EXT_XJSON)) {
                     if (this.createNode(parent, this.getName(entry), file) != null) {
                         ignoreEntry.add(file);
                         continue;
@@ -251,6 +259,8 @@
                 nodeReader = this.getXmlReader();
             } else if (nodeXML.getPath().toLowerCase().endsWith(".json")) {
                 nodeReader = this.getJsonReader();
+            } else if (nodeXML.getPath().toLowerCase().endsWith(".xjson")) {
+                nodeReader = this.getXJsonReader();
             } else {
                 // cannot find out the type
                 return null;
@@ -500,6 +510,13 @@
             this.jsonReader = new JsonReader();
         }
         return this.jsonReader;
+    }
+
+    private XJsonReader getXJsonReader() {
+        if (this.xjsonReader == null) {
+            this.xjsonReader = new XJsonReader();
+        }
+        return this.xjsonReader;
     }
 
 }

Added: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/XJsonReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/XJsonReader.java?rev=618414&view=auto
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/XJsonReader.java
(added)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/XJsonReader.java
Mon Feb  4 12:15:32 2008
@@ -0,0 +1,110 @@
+/*
+ * 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.jcr.resource.internal.loader;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
+
+public class XJsonReader extends JsonReader {
+
+    private static final Set<String> ignoredNames = new HashSet<String>();
+    static {
+        ignoredNames.add("jcr:primaryType");
+        ignoredNames.add("jcr:mixinTypes");
+        ignoredNames.add("jcr:uuid");
+        ignoredNames.add("jcr:baseVersion");
+        ignoredNames.add("jcr:predecessors");
+        ignoredNames.add("jcr:successors");
+        ignoredNames.add("jcr:checkedOut");
+        ignoredNames.add("jcr:created");
+    }
+
+    protected Node createNode(String name, JSONObject obj) throws JSONException {
+        Node node = new Node();
+        node.setName(name);
+
+        Object primaryType = obj.opt("jcr:primaryType");
+        if (primaryType != null) {
+            node.setPrimaryNodeType(String.valueOf(primaryType));
+        }
+
+        Object mixinsObject = obj.opt("jcr:mixinTyes");
+        if (mixinsObject instanceof JSONArray) {
+            JSONArray mixins = (JSONArray) mixinsObject;
+            for (int i = 0; i < mixins.length(); i++) {
+                node.addMixinNodeType(mixins.getString(i));
+            }
+        }
+
+        // add properties and nodes
+        JSONArray names = obj.names();
+        for (int i = 0; i < names.length(); i++) {
+            String n = names.getString(i);
+            // skip well known objects
+            if (!ignoredNames.contains(n)) {
+                Object o = obj.get(n);
+                if (o instanceof JSONObject) {
+                    Node child = this.createNode(n, (JSONObject) o);
+                    node.addChild(child);
+                } else if (o instanceof JSONArray) {
+                    Property prop = createProperty(n, o);
+                    node.addProperty(prop);
+                } else {
+                    Property prop = createProperty(n, o);
+                    node.addProperty(prop);
+                }
+            }
+        }
+        return node;
+    }
+
+    protected Property createProperty(String name, Object value)
+            throws JSONException {
+        Property property = new Property();
+        property.setName(name);
+
+        // assume simple value
+        if (value instanceof JSONArray) {
+            // multivalue
+            JSONArray array = (JSONArray) value;
+            if (array.length() > 0) {
+                for (int i = 0; i < array.length(); i++) {
+                    property.addValue(array.get(i));
+                }
+                value = array.opt(0);
+            } else {
+                property.addValue(null);
+                value = null;
+            }
+
+        } else {
+            // single value
+            property.setValue(String.valueOf(value));
+        }
+        // set type
+        property.setType(getType(value));
+
+        return property;
+    }
+
+}
\ No newline at end of file



Mime
View raw message