jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r912647 - in /jackrabbit/trunk/jackrabbit-jcr-server/src: main/java/org/apache/jackrabbit/webdav/jcr/JcrValueType.java test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
Date Mon, 22 Feb 2010 16:41:38 GMT
Author: angela
Date: Mon Feb 22 16:41:38 2010
New Revision: 912647

URL: http://svn.apache.org/viewvc?rev=912647&view=rev
Log:
JCR-2507 : JcrValueType#typeFromContentType throws IllegalArgumentException for type weak-ref
and uri

Added:
    jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JcrValueType.java

Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JcrValueType.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JcrValueType.java?rev=912647&r1=912646&r2=912647&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JcrValueType.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JcrValueType.java
Mon Feb 22 16:41:38 2010
@@ -20,6 +20,8 @@
 import org.slf4j.LoggerFactory;
 
 import javax.jcr.PropertyType;
+import java.util.HashMap;
+import java.util.Map;
 
 /** <code>JcrValueType</code>... */
 public final class JcrValueType {
@@ -35,33 +37,58 @@
      *
      * resulting in the following types:
      * <pre>
-     * jcr-value/string
-     * jcr-value/boolean
-     * jcr-value/long
-     * jcr-value/double
-     * jcr-value/date
      * jcr-value/binary
+     * jcr-value/boolean
      * jcr-value/date
+     * jcr-value/decimal
+     * jcr-value/double
+     * jcr-value/long
      * jcr-value/name
      * jcr-value/path
+     * jcr-value/reference
+     * jcr-value/string
+     * jcr-value/undefined
+     * jcr-value/uri
+     * jcr-value/weakreference
      * </pre>
      */
     private static final String VALUE_CONTENT_TYPE_FRAGMENT = "jcr-value/";
 
+    /**
+     * Hardcoded lookup from content type as created by {@link #contentTypeFromType(int)}.
+     * Reason: As of JCR 2.0 there is no trivial rule to obtain the the TYPENAME
+     * constant from a lower-cased string: WeakReference uses camel-case and URI
+     * is all upper case...
+     */
+    private static final Map<String, Integer> TYPE_LOOKUP = new HashMap<String,Integer>();
+    static {
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.BINARY), PropertyType.BINARY);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.BOOLEAN), PropertyType.BOOLEAN);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.DATE), PropertyType.DATE);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.DECIMAL), PropertyType.DECIMAL);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.DOUBLE), PropertyType.DOUBLE);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.LONG), PropertyType.LONG);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.NAME), PropertyType.NAME);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.PATH), PropertyType.PATH);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.REFERENCE), PropertyType.REFERENCE);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.STRING), PropertyType.STRING);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.UNDEFINED), PropertyType.UNDEFINED);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.URI), PropertyType.URI);
+        TYPE_LOOKUP.put(contentTypeFromType(PropertyType.WEAKREFERENCE), PropertyType.WEAKREFERENCE);
+    }
+
     public static String contentTypeFromType(int propertyType) {
         return VALUE_CONTENT_TYPE_FRAGMENT + PropertyType.nameFromValue(propertyType).toLowerCase();
     }
 
     public static int typeFromContentType(String contentType) {
-        if (contentType != null && contentType.startsWith(VALUE_CONTENT_TYPE_FRAGMENT))
{
-            // no need to create value/values property. instead
-            // prop-value can be retrieved directly:
-            int pos = contentType.indexOf('/');
-            int pos2 = contentType.indexOf(';');
-
-            String typename = contentType.substring(pos+1, pos+2).toUpperCase() + contentType.substring(pos+2,
(pos2 > -1) ? pos2 : contentType.length());
-            return PropertyType.valueFromName(typename);
+        if (contentType != null && TYPE_LOOKUP.containsKey(contentType)) {
+            return TYPE_LOOKUP.get(contentType);
         }
+
+        // some invalid content type argument that does not match any of the
+        // strings created by contentTypeFromType(int propertyType)
+        log.warn("Cannot extract property type from content type " + contentType + " ->
Fallback is UNDEFINED.");
         return PropertyType.UNDEFINED;
     }
 }
\ No newline at end of file

Added: jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java?rev=912647&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
Mon Feb 22 16:41:38 2010
@@ -0,0 +1,42 @@
+/*
+ * 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.jackrabbit.webdav.jcr;
+
+import junit.framework.TestCase;
+
+import javax.jcr.PropertyType;
+
+/**
+ * <code>JcrValueTypeTest</code>...
+ */
+public class JcrValueTypeTest extends TestCase {
+
+    public void testTypeFromContentType() {
+        for (int i = PropertyType.UNDEFINED; i <= PropertyType.DECIMAL; i++) {
+            String ct = JcrValueType.contentTypeFromType(i);
+            assertEquals(i, JcrValueType.typeFromContentType(ct));
+        }
+    }
+
+    public void testTypeFromInvalidContentType() {
+        String[] invalids = new String[] {null, "", "jcr-value/invalid", "invalid/as-well"};
+
+        for (String invalid : invalids) {
+            assertEquals(PropertyType.UNDEFINED, JcrValueType.typeFromContentType(invalid));
+        }
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/JcrValueTypeTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message