jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1394421 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
Date Fri, 05 Oct 2012 09:32:33 GMT
Author: mduerig
Date: Fri Oct  5 09:32:33 2012
New Revision: 1394421

URL: http://svn.apache.org/viewvc?rev=1394421&view=rev
Log:
OAK-350: Unify PropertyState and CoreValue
- Replace CoreValue.getString with PropertyState.getValue(Type)

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java?rev=1394421&r1=1394420&r2=1394421&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
Fri Oct  5 09:32:33 2012
@@ -16,18 +16,29 @@
  */
 package org.apache.jackrabbit.oak.kernel;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 import javax.jcr.PropertyType;
 
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
-import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.jackrabbit.oak.api.Type.BINARIES;
+import static org.apache.jackrabbit.oak.api.Type.BOOLEANS;
+import static org.apache.jackrabbit.oak.api.Type.LONGS;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 
 class JsopDiff implements NodeStateDiff {
+    private static final Logger log = LoggerFactory.getLogger(JsopDiff.class);
 
     private final MicroKernel kernel;
 
@@ -120,34 +131,51 @@ class JsopDiff implements NodeStateDiff 
     private void toJson(PropertyState propertyState, JsopBuilder jsop) {
         if (propertyState.isArray()) {
             jsop.array();
-            for (CoreValue value : propertyState.getValues()) {
-                toJson(value, jsop);
-            }
+            toJsonValue(propertyState, jsop);
             jsop.endArray();
         } else {
-            toJson(propertyState.getValue(), jsop);
+            toJsonValue(propertyState, jsop);
         }
     }
 
-    private void toJson(CoreValue value, JsopBuilder jsop) {
-        int type = value.getType();
-        if (type == PropertyType.BOOLEAN) {
-            jsop.value(value.getBoolean());
-        } else if (type == PropertyType.LONG) {
-            jsop.value(value.getLong());
-        } else {
-            String string;
-            if (type == PropertyType.BINARY
-                    && !(value instanceof BinaryValue)) {
-                string = kernel.write(value.getNewStream());
-            } else {
-                string = value.getString();
-            }
-            if (type != PropertyType.STRING
-                    || CoreValueMapper.startsWithHint(string)) {
-                string = CoreValueMapper.getHintForType(type) + ':' + string;
-            }
-            jsop.value(string);
+    private void toJsonValue(PropertyState property, JsopBuilder jsop) {
+        int type = property.getType().tag();
+        switch (type) {
+            case PropertyType.BOOLEAN:
+                for (boolean value : property.getValue(BOOLEANS)) {
+                    jsop.value(value);
+                }
+                break;
+            case PropertyType.LONG:
+                for (long value : property.getValue(LONGS)) {
+                    jsop.value(value);
+                }
+                break;
+            case PropertyType.BINARY:
+                for (Blob value : property.getValue(BINARIES)) {
+                    InputStream is = value.getNewStream();
+                    String binId = CoreValueMapper.getHintForType(type) + ':' + kernel.write(is);
+                    close(is);
+                    jsop.value(binId);
+                }
+                break;
+            default:
+                for (String value : property.getValue(STRINGS)) {
+                    if (PropertyType.STRING != type || CoreValueMapper.startsWithHint(value))
{
+                        value = CoreValueMapper.getHintForType(type) + ':' + value;
+                    }
+                    jsop.value(value);
+                }
+                break;
+        }
+    }
+
+    private static void close(InputStream stream) {
+        try {
+            stream.close();
+        }
+        catch (IOException e) {
+            log.warn("Error closing stream", e);
         }
     }
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java?rev=1394421&r1=1394420&r2=1394421&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
(original)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
Fri Oct  5 09:32:33 2012
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
 
+import javax.jcr.PropertyType;
 import javax.servlet.http.HttpServletResponse;
 
 import com.fasterxml.jackson.core.JsonFactory;
@@ -28,18 +29,12 @@ import com.fasterxml.jackson.core.JsonGe
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.api.Type;
 import org.apache.tika.mime.MediaType;
 
 import static org.apache.jackrabbit.oak.api.Type.BINARIES;
-import static org.apache.jackrabbit.oak.api.Type.BINARY;
-import static org.apache.jackrabbit.oak.api.Type.BOOLEAN;
 import static org.apache.jackrabbit.oak.api.Type.BOOLEANS;
-import static org.apache.jackrabbit.oak.api.Type.DECIMAL;
 import static org.apache.jackrabbit.oak.api.Type.DECIMALS;
-import static org.apache.jackrabbit.oak.api.Type.DOUBLE;
 import static org.apache.jackrabbit.oak.api.Type.DOUBLES;
-import static org.apache.jackrabbit.oak.api.Type.LONG;
 import static org.apache.jackrabbit.oak.api.Type.LONGS;
 import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 
@@ -110,24 +105,24 @@ class JsonRepresentation implements Repr
     private static void renderValue(PropertyState property, JsonGenerator generator)
             throws IOException {
         // TODO: Type info?
-        Type<?> type = property.getType();
-        if (BOOLEAN.equals(type)) {
+        int type = property.getType().tag();
+        if (type == PropertyType.BOOLEAN) {
             for (boolean value : property.getValue(BOOLEANS)) {
                 generator.writeBoolean(value);
             }
-        } else if (DECIMAL.equals(type)) {
+        } else if (type == PropertyType.DECIMAL) {
             for (BigDecimal value : property.getValue(DECIMALS)) {
                 generator.writeNumber(value);
             }
-        } else if (DOUBLE.equals(type)) {
+        } else if (type == PropertyType.DOUBLE) {
             for (double value : property.getValue(DOUBLES)) {
                 generator.writeNumber(value);
             }
-        } else if (LONG.equals(type)) {
+        } else if (type == PropertyType.LONG) {
             for (long value : property.getValue(LONGS)) {
                 generator.writeNumber(value);
             }
-        } else if (BINARY.equals(type)) {
+        } else if (type == PropertyType.BINARY) {
             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
             for (Blob value : property.getValue(BINARIES)) {
                 InputStream stream = value.getNewStream();



Mime
View raw message