You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/04/10 11:11:17 UTC
svn commit: r646703 - in /incubator/sling/trunk/jcr/resource/src:
main/java/org/apache/sling/jcr/resource/internal/loader/
test/java/org/apache/sling/jcr/resource/internal/loader/
Author: fmeschbe
Date: Thu Apr 10 02:11:14 2008
New Revision: 646703
URL: http://svn.apache.org/viewvc?rev=646703&view=rev
Log:
SLING-266 - Drop oldstyle JSON support and replace by new JSON-export format
Removed:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/loader/XJsonReader.java
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/loader/XJsonReaderTest.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
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/loader/JsonReaderTest.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=646703&r1=646702&r2=646703&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 Thu Apr 10 02:11:14 2008
@@ -22,7 +22,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.Iterator;
+import java.util.HashSet;
+import java.util.Set;
import javax.jcr.PropertyType;
@@ -36,6 +37,18 @@
*/
class JsonReader implements NodeReader {
+ 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");
+ }
+
static final ImportProvider PROVIDER = new ImportProvider() {
private JsonReader jsonReader;
@@ -63,91 +76,56 @@
}
}
- protected Node createNode(String name, JSONObject nodeDescriptor) throws JSONException {
+ protected Node createNode(String name, JSONObject obj) throws JSONException {
Node node = new Node();
node.setName(name);
- Object primaryType = nodeDescriptor.opt("primaryNodeType");
+ Object primaryType = obj.opt("jcr:primaryType");
if (primaryType != null) {
node.setPrimaryNodeType(String.valueOf(primaryType));
}
- Object mixinsObject = nodeDescriptor.opt("mixinNodeTypes");
+ Object mixinsObject = obj.opt("jcr:mixinTypes");
if (mixinsObject instanceof JSONArray) {
JSONArray mixins = (JSONArray) mixinsObject;
- for (int i=0; i < mixins.length(); i++) {
+ for (int i = 0; i < mixins.length(); i++) {
node.addMixinNodeType(mixins.getString(i));
}
}
- Object propertiesObject = nodeDescriptor.opt("properties");
- if (propertiesObject instanceof JSONObject) {
- JSONObject properties = (JSONObject) propertiesObject;
- for (Iterator<String> pi=properties.keys(); pi.hasNext(); ) {
- String propName = pi.next();
- Property prop = this.createProperty(propName, properties.get(propName));
- node.addProperty(prop);
- }
- }
-
- Object nodesObject = nodeDescriptor.opt("nodes");
- if (nodesObject instanceof JSONArray) {
- JSONArray nodes = (JSONArray) nodesObject;
- for (int i=0; i < nodes.length(); i++) {
- Object entry = nodes.opt(i);
- if (entry instanceof JSONObject) {
- JSONObject nodeObject = (JSONObject) entry;
- String nodeName = nodeObject.optString("name", null);
- if (nodeName == null) {
- nodeName = "000000" + i;
- nodeName = nodeName.substring(nodeName.length()-6);
- }
- Node child = this.createNode(nodeName, nodeObject);
+ // add properties and nodes
+ JSONArray names = obj.names();
+ for (int i = 0; names != null && 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 propDescriptorObject) throws JSONException {
- if (propDescriptorObject == null) {
- return null;
- }
-
+ protected Property createProperty(String name, Object value)
+ throws JSONException {
Property property = new Property();
property.setName(name);
- Object value;
- String type;
- if (propDescriptorObject instanceof JSONObject) {
- JSONObject propDescriptor = (JSONObject) propDescriptorObject;
-
- value = propDescriptor.opt("value");
- if (value == null) {
- // check multivalue
- value = propDescriptor.opt("values");
- if (value == null) {
- // missing value, ignore property
- return null;
- }
- }
-
- Object typeObject = propDescriptor.opt("type");
- type = (typeObject != null) ? String.valueOf(typeObject) : null;
-
- } else {
- value = propDescriptorObject;
- type = null;
- }
-
// assume simple value
if (value instanceof JSONArray) {
// multivalue
JSONArray array = (JSONArray) value;
if (array.length() > 0) {
- for (int i=0; i < array.length(); i++) {
+ for (int i = 0; i < array.length(); i++) {
property.addValue(array.get(i));
}
value = array.opt(0);
@@ -160,8 +138,8 @@
// single value
property.setValue(String.valueOf(value));
}
-
- property.setType((type != null) ? type : this.getType(value));
+ // set type
+ property.setType(getType(value));
return property;
}
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=646703&r1=646702&r2=646703&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 Thu Apr 10 02:11:14 2008
@@ -90,7 +90,7 @@
importProviders = new LinkedHashMap<String, ImportProvider>();
importProviders.put(EXT_JCR_XML, null);
importProviders.put(EXT_JSON, JsonReader.PROVIDER);
- importProviders.put(EXT_XJSON, XJsonReader.PROVIDER);
+ importProviders.put(EXT_XJSON, JsonReader.PROVIDER);
importProviders.put(EXT_XML, XmlReader.PROVIDER);
}
Modified: incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/loader/JsonReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/loader/JsonReaderTest.java?rev=646703&r1=646702&r2=646703&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/loader/JsonReaderTest.java (original)
+++ incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/loader/JsonReaderTest.java Thu Apr 10 02:11:14 2008
@@ -62,11 +62,9 @@
}
public void testDefaultPrimaryNodeType() throws IOException {
- String name = "test";
- String json = "{ \"name\": \"" + name + "\" }";
+ String json = "{}";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
- assertEquals(name, node.getName());
assertNull(node.getPrimaryNodeType());
assertNull("No mixins expected", node.getMixinNodeTypes());
assertNull("No properties expected", node.getProperties());
@@ -74,11 +72,9 @@
}
public void testDefaultPrimaryNodeTypeWithSurroundWhitespace() throws IOException {
- String name = "test";
- String json = " { \"name\": \"" + name + "\" } ";
+ String json = " { } ";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
- assertEquals(name, node.getName());
assertNull(node.getPrimaryNodeType());
assertNull("No mixins expected", node.getMixinNodeTypes());
assertNull("No properties expected", node.getProperties());
@@ -86,11 +82,9 @@
}
public void testDefaultPrimaryNodeTypeWithoutEnclosingBraces() throws IOException {
- String name = "test";
- String json = "\"name\": \"" + name + "\"";
+ String json = "";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
- assertEquals(name, node.getName());
assertNull(node.getPrimaryNodeType());
assertNull("No mixins expected", node.getMixinNodeTypes());
assertNull("No properties expected", node.getProperties());
@@ -98,11 +92,9 @@
}
public void testDefaultPrimaryNodeTypeWithoutEnclosingBracesWithSurroundWhitespace() throws IOException {
- String name = "test";
- String json = " \"name\": \"" + name + "\" ";
+ String json = " ";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
- assertEquals(name, node.getName());
assertNull(node.getPrimaryNodeType());
assertNull("No mixins expected", node.getMixinNodeTypes());
assertNull("No properties expected", node.getProperties());
@@ -110,9 +102,8 @@
}
public void testExplicitePrimaryNodeType() throws IOException {
- String name = "test";
String type = "xyz:testType";
- String json = "{ \"name\": \"" + name + "\", \"primaryNodeType\": \"" + type + "\" }";
+ String json = "{ \"jcr:primaryType\": \"" + type + "\" }";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -121,7 +112,7 @@
public void testMixinNodeTypes1() throws JSONException, IOException {
Set<Object> mixins = this.toSet(new Object[]{ "xyz:mix1" });
- String json = "{ \"name\": \"test\", \"mixinNodeTypes\": " + this.toJsonArray(mixins) + "}";
+ String json = "{ \"jcr:mixinTypes\": " + this.toJsonArray(mixins) + "}";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -130,7 +121,7 @@
public void testMixinNodeTypes2() throws JSONException, IOException {
Set<Object> mixins = this.toSet(new Object[]{ "xyz:mix1", "abc:mix2" });
- String json = "{ \"name\": \"test\", \"mixinNodeTypes\": " + this.toJsonArray(mixins) + "}";
+ String json = "{ \"jcr:mixinTypes\": " + this.toJsonArray(mixins) + "}";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -139,7 +130,7 @@
public void testPropertiesNone() throws IOException, JSONException {
List<Property> properties = null;
- String json = "{ \"name\": \"test\", \"properties\": " + this.toJsonObject(properties) + "}";
+ String json = "{ \"properties\": " + this.toJsonObject(properties) + "}";
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -152,14 +143,14 @@
prop.setName("p1");
prop.setValue("v1");
properties.add(prop);
-
- String json = "{ \"name\": \"test\", \"properties\": " + this.toJsonObject(properties) + "}";
-
+
+ String json = this.toJsonObject(properties).toString();
+
Node node = this.parse(json);
assertNotNull("Expecting node", node);
assertEquals(new HashSet<Property>(properties), new HashSet<Property>(node.getProperties()));
}
-
+
public void testPropertiesTwoSingleValue() throws IOException, JSONException {
List<Property> properties = new ArrayList<Property>();
Property prop = new Property();
@@ -171,7 +162,7 @@
prop.setValue("v2");
properties.add(prop);
- String json = "{ \"name\": \"test\", \"properties\": " + this.toJsonObject(properties) + "}";
+ String json = this.toJsonObject(properties).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -185,7 +176,7 @@
prop.addValue("v1");
properties.add(prop);
- String json = "{ \"name\": \"test\", \"properties\": " + this.toJsonObject(properties) + "}";
+ String json = this.toJsonObject(properties).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -199,7 +190,7 @@
prop.addValue(null); // empty multivalue property
properties.add(prop);
- String json = "{ \"name\": \"test\", \"properties\": " + this.toJsonObject(properties) + "}";
+ String json = this.toJsonObject(properties).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -208,7 +199,7 @@
public void testChildrenNone() throws IOException, JSONException {
List<Node> nodes = null;
- String json = "{ \"name\": \"test\", \"nodes\": " + this.toJsonObject(nodes) + "}";
+ String json = this.toJsonObject(nodes).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -221,7 +212,7 @@
child.setName("p1");
nodes.add(child);
- String json = "{ \"name\": \"test\", \"nodes\": " + this.toJsonArray(nodes).toString() + "}";
+ String json = this.toJsonObject(nodes).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -235,7 +226,7 @@
child.addMixinNodeType("p1:mix");
nodes.add(child);
- String json = "{ \"name\": \"test\", \"nodes\": " + this.toJsonArray(nodes) + "}";
+ String json = this.toJsonObject(nodes).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -251,7 +242,7 @@
child.setName("p2");
nodes.add(child);
- String json = "{ \"name\": \"test\", \"nodes\": " + this.toJsonArray(nodes) + "}";
+ String json = this.toJsonObject(nodes).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -268,7 +259,7 @@
child.addProperty(prop);
nodes.add(child);
- String json = "{ \"name\": \"test\", \"nodes\": " + this.toJsonArray(nodes) + "}";
+ String json = this.toJsonObject(nodes).toString();
Node node = this.parse(json);
assertNotNull("Expecting node", node);
@@ -330,22 +321,24 @@
private JSONObject toJsonObject(Node node) throws JSONException {
JSONObject obj = new JSONObject();
- obj.putOpt("name", node.getName());
-
if (node.getPrimaryNodeType() != null) {
- obj.putOpt("primaryNodeType", node.getPrimaryNodeType());
+ obj.putOpt("jcr:primaryType", node.getPrimaryNodeType());
}
if (node.getMixinNodeTypes() != null) {
- obj.putOpt("mixinNodeTypes", this.toJsonArray(node.getMixinNodeTypes()));
+ obj.putOpt("jcr:mixinTypes", this.toJsonArray(node.getMixinNodeTypes()));
}
if (node.getProperties() != null) {
- obj.putOpt("properties", this.toJsonObject(node.getProperties()));
+ for (Property prop : node.getProperties()) {
+ obj.put(prop.getName(), toJsonObject(prop));
+ }
}
if (node.getChildren() != null) {
- obj.putOpt("nodes", this.toJsonArray(node.getChildren()));
+ for (Node child : node.getChildren()) {
+ obj.put(child.getName(), toJsonObject(child));
+ }
}
return obj;
@@ -355,13 +348,12 @@
if (!property.isMultiValue() && PropertyType.TYPENAME_STRING.equals(property.getType())) {
return this.toJsonObject(property.getValue());
}
- JSONObject obj = new JSONObject();
+ Object obj;
if (property.isMultiValue()) {
- obj.putOpt("value", this.toJsonArray(property.getValues()));
+ obj = this.toJsonArray(property.getValues());
} else {
- obj.putOpt("value", this.toJsonObject(property.getValue()));
+ obj = this.toJsonObject(property.getValue());
}
- obj.putOpt("type", property.getType());
return obj;
}