You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2012/05/16 23:42:49 UTC

svn commit: r1339380 - in /tika/trunk/tika-core/src: main/java/org/apache/tika/metadata/ test/java/org/apache/tika/metadata/

Author: nick
Date: Wed May 16 21:42:49 2012
New Revision: 1339380

URL: http://svn.apache.org/viewvc?rev=1339380&view=rev
Log:
TIKA-927 - Patch from Ray Gauss to support Composite Properties (useful for backwards compatibility, and mapping between application and core properties)

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/main/java/org/apache/tika/metadata/PropertyTypeException.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=1339380&r1=1339379&r2=1339380&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 21:42:49 2012
@@ -31,6 +31,8 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.TimeZone;
 
+import org.apache.tika.metadata.Property.PropertyType;
+
 /**
  * A multi-valued metadata container.
  */
@@ -348,7 +350,19 @@ public class Metadata implements Creativ
      * @param value    property value
      */
     public void set(Property property, String value) {
-        set(property.getName(), value);
+        if (property == null) {
+            throw new NullPointerException("property must not be null");
+        }
+        if (property.getPropertyType() == PropertyType.COMPOSITE) {
+            set(property.getPrimaryProperty(), value);
+            if (property.getSecondaryExtractProperties() != null) {
+                for (Property secondaryExtractProperty : property.getSecondaryExtractProperties()) {
+                    set(secondaryExtractProperty, value);
+                }
+            }
+        } else {
+            set(property.getName(), value);
+        }
     }
 
     /**

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=1339380&r1=1339379&r2=1339380&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 21:42:49 2012
@@ -37,12 +37,12 @@ import java.util.TreeSet;
 public final class Property implements Comparable<Property> {
 
     public static enum PropertyType {
-        SIMPLE, STRUCTURE, BAG, SEQ, ALT
+        SIMPLE, STRUCTURE, BAG, SEQ, ALT, COMPOSITE
     }
 
     public static enum ValueType {
         BOOLEAN, OPEN_CHOICE, CLOSED_CHOICE, DATE, INTEGER, LOCALE,
-        MIME_TYPE, PROPER_NAME, RATIONAL, REAL, TEXT, URI, URL, XPATH
+        MIME_TYPE, PROPER_NAME, RATIONAL, REAL, TEXT, URI, URL, XPATH, PROPERTY
     }
 
     private static final Map<String, Property> properties =
@@ -55,6 +55,10 @@ public final class Property implements C
     private final PropertyType propertyType;
 
     private final ValueType valueType;
+    
+    private final Property primaryProperty;
+    
+    private final Property[] secondaryExtractProperties;
 
     /**
      * The available choices for the open and closed choice value types.
@@ -63,7 +67,7 @@ public final class Property implements C
 
     private Property(
             String name, boolean internal, PropertyType propertyType,
-            ValueType valueType, String[] choices) {
+            ValueType valueType, String[] choices, Property primaryProperty, Property[] secondaryExtractProperties) {
         this.name = name;
         this.internal = internal;
         this.propertyType = propertyType;
@@ -74,11 +78,26 @@ public final class Property implements C
         } else {
             this.choices = null;
         }
-
-        synchronized (properties) {
-            properties.put(name, this);
+        
+        if (primaryProperty != null) {
+            this.primaryProperty = primaryProperty;
+            this.secondaryExtractProperties = secondaryExtractProperties;
+        } else {
+            this.primaryProperty = this;
+            this.secondaryExtractProperties = null;
+            
+            // Only store primary properties for lookup, not composites
+            synchronized (properties) {
+               properties.put(name, this);
+           }
         }
     }
+    
+    private Property(
+            String name, boolean internal, PropertyType propertyType,
+            ValueType valueType, String[] choices) {
+    	this(name, internal, propertyType, valueType, choices, null, null);
+    }
 
     private Property(
             String name, boolean internal,
@@ -95,7 +114,7 @@ public final class Property implements C
             PropertyType propertyType, ValueType valueType) {
         this(name, internal, propertyType, valueType, null);
     }
-
+    
     public String getName() {
         return name;
     }
@@ -126,7 +145,25 @@ public final class Property implements C
     public Set<String> getChoices() {
         return choices;
     }
+    
+    /**
+     * Gets the primary property for a composite property
+     * 
+     * @return the primary property
+     */
+    public Property getPrimaryProperty() {
+        return primaryProperty;
+    }
 
+    /**
+     * Gets the secondary properties for a composite property
+     * 
+     * @return the secondary properties
+     */
+    public Property[] getSecondaryExtractProperties() {
+		return secondaryExtractProperties;
+	}
+    
     public static SortedSet<Property> getProperties(String prefix) {
         SortedSet<Property> set = new TreeSet<Property>();
         String p = prefix + ":";
@@ -209,6 +246,41 @@ public final class Property implements C
     public static Property externalText(String name) {
         return new Property(name, false, ValueType.TEXT);
     }
+    
+    /**
+     * Constructs a new composite property from the given primary and array of secondary properties.
+     * <p>
+     * Note that name of the composite property is taken from its primary property, 
+     * and primary and secondary properties must not be composite properties themselves.
+     * 
+     * @param primaryProperty
+     * @param secondaryExtractProperties
+     * @return the composite property
+     */
+    public static Property composite(Property primaryProperty, Property[] secondaryExtractProperties) {
+        if (primaryProperty == null) {
+            throw new NullPointerException("primaryProperty must not be null");
+        }
+        if (primaryProperty.getPropertyType() == PropertyType.COMPOSITE) {
+            throw new PropertyTypeException(primaryProperty.getPropertyType());
+        }
+        if (secondaryExtractProperties != null) {
+            for (Property secondaryExtractProperty : secondaryExtractProperties) {
+                if (secondaryExtractProperty.getPropertyType() == PropertyType.COMPOSITE) {
+                    throw new PropertyTypeException(secondaryExtractProperty.getPropertyType());
+                }
+            }
+        }
+        String[] choices = null;
+        if (primaryProperty.getChoices() != null) {
+            choices = primaryProperty.getChoices().toArray(
+                    new String[primaryProperty.getChoices().size()]);
+        }
+        return new Property(primaryProperty.getName(),
+                primaryProperty.isInternal(), PropertyType.COMPOSITE,
+                ValueType.PROPERTY, choices, primaryProperty,
+                secondaryExtractProperties);
+    }
 
     //----------------------------------------------------------< Comparable >
 

Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/PropertyTypeException.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/PropertyTypeException.java?rev=1339380&r1=1339379&r2=1339380&view=diff
==============================================================================
--- tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/PropertyTypeException.java (original)
+++ tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/PropertyTypeException.java Wed May 16 21:42:49 2012
@@ -34,4 +34,9 @@ public final class PropertyTypeException
     public PropertyTypeException(ValueType expected, ValueType found) {
         super("Expected a property with a " + expected + " value, but received a " + found);
     }
+    public PropertyTypeException(PropertyType unsupportedPropertyType) {
+    	super((unsupportedPropertyType != PropertyType.COMPOSITE) ? 
+    			(unsupportedPropertyType + " is not supported") : 
+    			("Composite Properties must not include other Composite Properties as either Primary or Secondary"));
+    }
 }

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=1339380&r1=1339379&r2=1339380&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 21:42:49 2012
@@ -330,4 +330,14 @@ public class TestMetadata extends TestCa
         		"1970-01-01T00:00:01", meta.get(Metadata.DATE));
     }
     
+    public void testCompositeProperty() {
+    	Metadata meta = new Metadata();
+    	Property compositeProperty = Property.composite(
+            DublinCore.DESCRIPTION, new Property[] { Property.internalText(Metadata.DESCRIPTION)});
+    	String message = "composite description";
+    	meta.set(compositeProperty, message);
+    	assertEquals(message, meta.get(DublinCore.DESCRIPTION));
+    	assertEquals(message, meta.get(Metadata.DESCRIPTION));
+    }
+    
 }