Author: nick
Date: Wed May 16 23:15:09 2012
New Revision: 1339418

URL: http://svn.apache.org/viewvc?rev=1339418&view=rev
Log:
TIKA-926 Patch from Ray Gauss to allow set(Property,String[]) and 
add(Property,String), to mirror the string key based methods but with type 
safety

Modified:
    tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java
    tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Property.java
    
tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java

Modified: 
tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java?rev=1339418&r1=1339417&r2=1339418&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java 
(original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java 
Wed May 16 23:15:09 2012
@@ -290,6 +290,13 @@ public class Metadata implements Creativ
         }
         return values;
     }
+    
+    private String[] appendedValues(String[] values, final String value) {
+        String[] newValues = new String[values.length + 1];
+        System.arraycopy(values, 0, newValues, 0, values.length);
+        newValues[newValues.length - 1] = value;
+        return newValues;
+    }
 
     /**
      * Add a metadata name/value mapping. Add the specified value to the list 
of
@@ -305,10 +312,29 @@ public class Metadata implements Creativ
         if (values == null) {
             set(name, value);
         } else {
-            String[] newValues = new String[values.length + 1];
-            System.arraycopy(values, 0, newValues, 0, values.length);
-            newValues[newValues.length - 1] = value;
-            metadata.put(name, newValues);
+            metadata.put(name, appendedValues(values, value));
+        }
+    }
+    
+    /**
+     * Add a metadata property/value mapping. Add the specified value to the 
list of
+     * values associated to the specified metadata property.
+     * 
+     * @param property
+     *          the metadata property.
+     * @param value
+     *          the metadata value.
+     */
+    public void add(final Property property, final String value) {
+        String[] values = metadata.get(property.getName());
+        if (values == null) {
+            set(property.getName(), value);
+        } else {
+             if (property.isMultiValuePermitted()) {
+                 set(property, appendedValues(values, value));
+             } else {
+                 throw new PropertyTypeException(property.getPropertyType());
+             }
         }
     }
 
@@ -364,6 +390,29 @@ public class Metadata implements Creativ
             set(property.getName(), value);
         }
     }
+    
+    /**
+     * Sets the values of the identified metadata property.
+     *
+     * @since Apache Tika 1.2
+     * @param property property definition
+     * @param values    property values
+     */
+    public void set(Property property, String[] values) {
+        if (property == null) {
+            throw new NullPointerException("property must not be null");
+        }
+        if (property.getPropertyType() == PropertyType.COMPOSITE) {
+            set(property.getPrimaryProperty(), values);
+            if (property.getSecondaryExtractProperties() != null) {
+                for (Property secondaryExtractProperty : 
property.getSecondaryExtractProperties()) {
+                    set(secondaryExtractProperty, values);
+                }
+            }
+        } else {
+            metadata.put(property.getName(), values);
+        }
+    }
 
     /**
      * Sets the integer value of the identified metadata property.

Modified: 
tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Property.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Property.java?rev=1339418&r1=1339417&r2=1339418&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Property.java 
(original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Property.java 
Wed May 16 23:15:09 2012
@@ -136,6 +136,20 @@ public final class Property implements C
     public boolean isExternal() {
         return !internal;
     }
+    
+    /**
+     * Is the PropertyType one which accepts multiple values?
+     */
+    public boolean isMultiValuePermitted() {
+        if (propertyType == PropertyType.BAG || propertyType == 
PropertyType.SEQ ||
+            propertyType == PropertyType.ALT) {
+           return true;
+        } else if (propertyType == PropertyType.COMPOSITE) {
+           // Base it on the primary property's behaviour
+           return primaryProperty.isMultiValuePermitted();
+        }
+        return false;
+    }
 
     public PropertyType getPropertyType() {
         return propertyType;

Modified: 
tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java?rev=1339418&r1=1339417&r2=1339418&view=diff
==============================================================================
--- 
tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java 
(original)
+++ 
tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java 
Wed May 16 23:15:09 2012
@@ -72,6 +72,14 @@ public class TestMetadata extends TestCa
         assertEquals("value1", values[0]);
         assertEquals("value2", values[1]);
         assertEquals("value1", values[2]);
+        
+        Property nonMultiValued = Property.internalText("nonMultiValued");
+        meta.add(nonMultiValued, "value1");
+        try {
+            meta.add(nonMultiValued, "value2");
+            fail("add should fail on the second call of a non-multi valued 
item");
+        } catch (PropertyTypeException e) {
+        }
     }
 
     /** Test for the <code>set(String, String)</code> method. */


Reply via email to