jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1135988 - /jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Values.java
Date Wed, 15 Jun 2011 10:26:44 GMT
Author: mduerig
Date: Wed Jun 15 10:26:43 2011
New Revision: 1135988

URL: http://svn.apache.org/viewvc?rev=1135988&view=rev
Log:
spi2microkernel prototype (WIP)
value types: string, double, boolean, long

Modified:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Values.java

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Values.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Values.java?rev=1135988&r1=1135987&r2=1135988&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Values.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/Values.java
Wed Jun 15 10:26:43 2011
@@ -26,7 +26,6 @@ import org.apache.jackrabbit.spi.commons
 
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
-import javax.jcr.UnsupportedRepositoryOperationException;
 import java.math.BigDecimal;
 import java.util.HashMap;
 import java.util.Map;
@@ -34,45 +33,89 @@ import java.util.Map;
 public final class Values {
     private static final QValueFactory FACTORY = QValueFactoryImpl.getInstance();
 
+    private static final Character[] TYPES = {
+        toHex(PropertyType.UNDEFINED),
+            toHex(PropertyType.STRING),
+            toHex(PropertyType.BINARY),
+            toHex(PropertyType.LONG),
+            toHex(PropertyType.DOUBLE),
+            toHex(PropertyType.DATE),
+            toHex(PropertyType.BOOLEAN),
+            toHex(PropertyType.NAME),
+            toHex(PropertyType.PATH),
+            toHex(PropertyType.REFERENCE),
+            toHex(PropertyType.WEAKREFERENCE),
+            toHex(PropertyType.URI),
+            toHex(PropertyType.DECIMAL),
+    };
+
     public static final QValue FALSE = create(false);
     public static final QValue TRUE = create(true);
 
-    private static final Map<Class<?>, ValueFactory> VALUE_FACTORIES = new HashMap<Class<?>,
ValueFactory>() {{
-        put(String.class, new ValueFactory() {
+    private static final Map<Object, ValueFactory> VALUE_FACTORIES = new HashMap<Object,
ValueFactory>() {{
+        ValueFactory valueFactory = new ValueFactory() {
             public QValue create(Object value) throws RepositoryException {
-                // todo refactor, clean up, implement value types
-                if ("F:NAN".equals(value)) {
-                    return FACTORY.create(Double.NaN);
-                }
-                else {
-                    return FACTORY.create((String) value, PropertyType.STRING);
-                }
+                return create((String) value);
             }
-        });
-        put(Boolean.class, new ValueFactory() {
+
+            public QValue create(String value) throws RepositoryException {
+                return FACTORY.create(value, PropertyType.STRING);
+            }
+        };
+        put(String.class, valueFactory);
+        put(TYPES[PropertyType.STRING], valueFactory);
+
+        valueFactory = new ValueFactory() {
             public QValue create(Object value) throws RepositoryException {
-                return FACTORY.create((Boolean) value);
+                return FACTORY.create((Long) value);
             }
-        });
-        put(Double.class, new ValueFactory() {
+
+            public QValue create(String value) throws RepositoryException {
+                return FACTORY.create(value, PropertyType.LONG);
+            }
+        };
+        put(Long.class, valueFactory);
+        put(TYPES[PropertyType.LONG], valueFactory);
+
+        valueFactory = new ValueFactory() {
             public QValue create(Object value) throws RepositoryException {
                 return FACTORY.create((Double) value);
             }
-        });
-        put(Long.class, new ValueFactory() {
+
+            public QValue create(String value) throws RepositoryException {
+                return FACTORY.create(value, PropertyType.DOUBLE);
+            }
+        };
+        put(Double.class, valueFactory);
+        put(TYPES[PropertyType.DOUBLE], valueFactory);
+
+        put(Boolean.class, new ValueFactory() {
             public QValue create(Object value) throws RepositoryException {
-                return FACTORY.create((Long) value);
+                return FACTORY.create((Boolean) value);
+            }
+
+            public QValue create(String value) throws RepositoryException {
+                return FACTORY.create(value, PropertyType.BOOLEAN);
             }
         });
+
         put(Integer.class, new ValueFactory() {
             public QValue create(Object value) throws RepositoryException {
                 return FACTORY.create((Integer) value);
             }
+
+            public QValue create(String value) throws RepositoryException {
+                return null; // todo implement create
+            }
         });
         put(BigDecimal.class, new ValueFactory() {
             public QValue create(Object value) throws RepositoryException {
                 return FACTORY.create((BigDecimal) value);
             }
+
+            public QValue create(String value) throws RepositoryException {
+                return null; // todo implement create   
+            }
         });
         // todo handle other value types
     }};
@@ -80,9 +123,24 @@ public final class Values {
     private Values() {}
 
     public static QValue create(Object value) throws RepositoryException {
+        // Type encoded value
+        if (value instanceof String) {
+            String val = (String) value;
+            if (val.length() >= 2 && val.charAt(1) == ':') {
+                ValueFactory valueFactory = VALUE_FACTORIES.get(val.charAt(0));
+                if (valueFactory == null) {
+                    throw new RepositoryException("Not supported value " + value);
+                }
+                else {
+                    return valueFactory.create(val.substring(2));
+                }
+            }
+        }
+
+        // Non type encoded value
         ValueFactory valueFactory = VALUE_FACTORIES.get(value.getClass());
         if (valueFactory == null) {
-            throw new UnsupportedRepositoryOperationException("Not supported value " + value);
+            throw new RepositoryException("Not supported value " + value);
         }
         else {
             return valueFactory.create(value);
@@ -90,29 +148,35 @@ public final class Values {
     }
 
     public static String create(QValue value) throws RepositoryException {
-        // todo implement value types
-        switch(value.getType()) {
-            case PropertyType.STRING:    // S: (String)
-                return JsonBuilder.quote(JsonBuilder.escape(value.getString()));
-
-            case PropertyType.DOUBLE:    // F: (Float)
-                if (Double.isNaN(value.getDouble())) {
-                    return "\"F:NAN\"";
-                }
-            case PropertyType.LONG:      // I: (Integer)
-            case PropertyType.BOOLEAN:   // L: (Logical)
-            case PropertyType.DECIMAL:   // D: (Decimal)
-                return value.getString();
+        int type = value.getType();
+        
+        switch(type) {
+            case PropertyType.STRING:
+                String s = value.getString();
+                return s.length() >= 2 && s.charAt(1) == ':'
+                    ? typeEncode(type, JsonBuilder.escape(s))
+                    : JsonBuilder.quote(JsonBuilder.escape(s));
+
+            case PropertyType.DOUBLE:
+                return Double.isNaN(value.getDouble())
+                    ? typeEncode(type, "NaN")
+                    : value.getString();
 
-            case PropertyType.DATE:      // C: (Calendar)
-            case PropertyType.BINARY:    // Y: (binarY)
-            case PropertyType.NAME:      // N: (Name)
-            case PropertyType.PATH:      // P: (Path)
-            case PropertyType.REFERENCE: // R: (Reference)
-            case PropertyType.WEAKREFERENCE:  // W: (Weakreference)
-            case PropertyType.URI:       // U: (Uri)
-            default:  // todo implement support for the other value types
-                throw new UnsupportedRepositoryOperationException("Cannot handle value of
type " +
+            case PropertyType.LONG:
+            case PropertyType.BOOLEAN:
+                return value.getString();
+            
+                // todo implement support for the other value types
+            case PropertyType.DECIMAL:
+            case PropertyType.DATE:
+            case PropertyType.BINARY:
+            case PropertyType.NAME:
+            case PropertyType.PATH:
+            case PropertyType.REFERENCE:
+            case PropertyType.WEAKREFERENCE:
+            case PropertyType.URI:       
+            default:
+                throw new RepositoryException("Cannot handle value of type " +
                         PropertyType.nameFromValue(value.getType()));
         }
     }
@@ -135,6 +199,16 @@ public final class Values {
 
     //------------------------------------------< private >---
 
+    private static Character toHex(int i) {
+        return i >= 0 && i <= 15
+            ? Integer.toHexString(i).charAt(0)
+            : 'X';
+    }
+
+    private static String typeEncode(int type, String value) {
+        return "\"" + TYPES[type] + ':' + value + '\"';
+    }
+    
     private static QValue create(boolean value) {
         try {
             return FACTORY.create(value);
@@ -147,5 +221,6 @@ public final class Values {
 
     private interface ValueFactory {
         QValue create(Object value) throws RepositoryException;
+        QValue create(String value) throws RepositoryException;
     }
 }



Mime
View raw message