Class Metadata

java.lang.Object
org.apache.tika.metadata.Metadata
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
XMPMetadata

public class Metadata extends Object implements Serializable
A multi-valued metadata container.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new, empty metadata.
    Constructs a new, empty metadata with the specified write limiter.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(String name, String value)
    Add a metadata name/value mapping.
    protected void
    add(String name, String[] newValues)
    Add a metadata name/value mapping.
    void
    add(KeyPrefix prefix, String name, String value)
    Appends a value under a document- or tool-derived name, keyed as prefix + name.
    void
    add(KeyPrefix prefix, String name, Instant value)
    add(KeyPrefix, String, String) for a source-typed date value (e.g. an OLE VT_DATE / OOXML vt:filetime custom property): the value is stored in Tika's canonical ISO-8601 form, identical to how curated date Property constants serialize.
    void
    add(Property property, int value)
    Adds the integer value of the identified metadata property.
    void
    add(Property property, String value)
    Add a metadata property/value mapping.
    void
    add(Property property, Calendar date)
    Adds the date value of the identified metadata property.
    void
    addTrusted(String name, String value)
    Trusted add, bypassing the reserved-key guard: writes reach reserved Tika-native (tk:) keys directly.
    boolean
     
    get(String name)
    Get the value associated to a metadata name.
    get(Property property)
    Returns the value (if any) of the identified metadata property.
    getBoolean(Property property)
    Returns the value of the identified Boolean based metadata property.
    getDate(Property property)
    Returns the value of the identified Date based metadata property.
    getDouble(Property property)
    Returns the value of the identified Double based metadata property (REAL or RATIONAL value type, mirroring set(Property, double)).
    getInt(Property property)
    Returns the value of the identified Integer based metadata property.
    int[]
    Gets the array of ints of the identified "seq" integer metadata property.
    getLong(Property property)
    Returns the value of the identified Long based metadata property (INTEGER or REAL value type, mirroring set(Property, long)).
    long[]
    Gets the array of longs of the identified "seq" metadata property.
    Get the values associated to a metadata name.
    getValues(Property property)
    Get the values associated to a metadata name.
    int
     
    boolean
    Returns true if named value is multivalued.
    boolean
    Returns true if named value is multivalued.
    Returns an array of the names contained in the metadata.
    static Metadata
    Creates a new Metadata instance configured from the ParseContext.
    void
    Copies every key from other into this Metadata: for each name in other, this Metadata's values for that name are replaced wholesale with other's values, in order (multi-values preserved); names absent from other are left untouched.
    void
    reconstruct(String name, String value, boolean append)
    Trusted write for clone/merge/deserialize; reserved keys go via their Property.
    void
    remove(String name)
    Remove a metadata and all its associated values.
    void
    remove(Property property)
    Remove a metadata property and all its associated values.
    void
    set(String name, String value)
    Set metadata name/value.
    protected void
    set(String name, String[] values)
     
    void
    set(Property property, boolean value)
    Sets the integer value of the identified metadata property.
    void
    set(Property property, double value)
    Sets the real or rational value of the identified metadata property.
    void
    set(Property property, int value)
    Sets the integer value of the identified metadata property.
    void
    set(Property property, long value)
    Sets the long value of the identified metadata property.
    void
    set(Property property, String value)
    Sets the value of the identified metadata property.
    void
    set(Property property, String[] values)
    Sets the values of the identified metadata property.
    void
    set(Property property, Calendar date)
    Sets the date value of the identified metadata property.
    void
    set(Property property, Date date)
    Sets the date value of the identified metadata property.
    void
    setTrusted(String name, String value)
    Trusted set, bypassing the reserved-key guard: writes reach reserved Tika-native (tk:) keys directly.
    int
    Returns the number of metadata names in this metadata.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Metadata

      public Metadata()
      Constructs a new, empty metadata.
    • Metadata

      public Metadata(MetadataWriteLimiter writeLimiter)
      Constructs a new, empty metadata with the specified write limiter. The limiter will be applied to all subsequent writes.
      Parameters:
      writeLimiter - the limiter to apply to metadata writes, or null for no limits
      Since:
      Apache Tika 4.0.0
  • Method Details

    • newInstance

      public static Metadata newInstance(ParseContext context)
      Creates a new Metadata instance configured from the ParseContext.

      If a MetadataWriteLimiterFactory is present in the context, the returned Metadata will have a write limiter that enforces those limits. Otherwise, returns a plain Metadata object with no limits.

      Parsers should use this method instead of new Metadata() when creating metadata for embedded documents, to ensure limits are applied at creation time.

      Parameters:
      context - the ParseContext (may be null)
      Returns:
      a new Metadata instance configured from the context
      Since:
      Apache Tika 4.0.0
    • isMultiValued

      public boolean isMultiValued(Property property)
      Returns true if named value is multivalued.
      Parameters:
      property - metadata property
      Returns:
      true is named value is multivalued, false if single value or null
    • isMultiValued

      public boolean isMultiValued(String name)
      Returns true if named value is multivalued.
      Parameters:
      name - name of metadata
      Returns:
      true is named value is multivalued, false if single value or null
    • names

      public String[] names()
      Returns an array of the names contained in the metadata.
      Returns:
      Metadata names
    • get

      public String get(String name)
      Get the value associated to a metadata name. If many values are associated to the specified name, then the first one is returned.
      Parameters:
      name - of the metadata.
      Returns:
      the value associated to the specified metadata name.
    • get

      public String get(Property property)
      Returns the value (if any) of the identified metadata property. If many values are associated to the specified property, then the first one is returned.
      Parameters:
      property - property definition
      Returns:
      property value, or null if the property is not set
      Since:
      Apache Tika 0.7
    • getInt

      public Integer getInt(Property property)
      Returns the value of the identified Integer based metadata property. If many values are associated to the specified property, then the first one is returned.
      Parameters:
      property - simple integer property definition
      Returns:
      property value as a Integer, or null if the property is not set, or not a valid Integer
      Since:
      Apache Tika 0.8
    • getDate

      public Date getDate(Property property)
      Returns the value of the identified Date based metadata property. If many values are associated to the specified property, then the first one is returned.
      Parameters:
      property - simple date property definition
      Returns:
      property value as a Date, or null if the property is not set, or not a valid Date
      Since:
      Apache Tika 0.8
    • getLong

      public Long getLong(Property property)
      Returns the value of the identified Long based metadata property (INTEGER or REAL value type, mirroring set(Property, long)). If many values are associated to the specified property, then the first one is returned.
      Parameters:
      property - simple integer or real property definition
      Returns:
      property value as a Long, or null if the property is not set, or not a valid Long
      Since:
      Apache Tika 4.0.0
    • getBoolean

      public Boolean getBoolean(Property property)
      Returns the value of the identified Boolean based metadata property. If many values are associated to the specified property, then the first one is returned.
      Parameters:
      property - simple boolean property definition
      Returns:
      property value as a Boolean, or null if the property is not set, or not a valid Boolean
      Since:
      Apache Tika 4.0.0
    • getDouble

      public Double getDouble(Property property)
      Returns the value of the identified Double based metadata property (REAL or RATIONAL value type, mirroring set(Property, double)). If many values are associated to the specified property, then the first one is returned.
      Parameters:
      property - simple real or rational property definition
      Returns:
      property value as a Double, or null if the property is not set, or not a valid Double
      Since:
      Apache Tika 4.0.0
    • getValues

      public String[] getValues(Property property)
      Get the values associated to a metadata name.
      Parameters:
      property - of the metadata.
      Returns:
      the values associated to a metadata name.
    • getValues

      public String[] getValues(String name)
      Get the values associated to a metadata name.
      Parameters:
      name - of the metadata.
      Returns:
      the values associated to a metadata name.
    • add

      public void add(String name, String value)
      Add a metadata name/value mapping. Add the specified value to the list of values associated to the specified metadata name.

      This route rejects reserved Tika-native (tk:) keys. It is for names the caller owns. Document-controlled names -- custom OOXML/MSG properties, NetCDF and GRIB attribute names -- must not come through here, because a crafted file could then assert Tika's own computed keys by naming one; route those through add(KeyPrefix, String, String), which namespaces them and applies skip-and-WARN bounds instead of throwing.

      If you are writing a key Tika owns, use its Property overload, or addTrusted(java.lang.String, java.lang.String) when only the name is available.

      Parameters:
      name - the metadata name.
      value - the metadata value.
      Throws:
      IllegalArgumentException - if name is a reserved Tika-native (tk:) key; use its Property or addTrusted(java.lang.String, java.lang.String).
      See Also:
    • addTrusted

      public void addTrusted(String name, String value)
      Trusted add, bypassing the reserved-key guard: writes reach reserved Tika-native (tk:) keys directly. For internal/known-trusted writers (metadata filters, clone/merge/deserialize, emit-time enrichment) that legitimately need to assert a reserved key by name rather than by its Property.
    • add

      public void add(KeyPrefix prefix, String name, String value)
      Appends a value under a document- or tool-derived name, keyed as prefix + name. This is the write route for names Tika does not control (custom document properties, format-specific attribute names, tool output): append-only (a repeated name accumulates values, losslessly transcribing the source), with a built-in safety net for hostile or malformed input — a null/blank name, a name longer than MAX_PREFIX_ROUTE_NAME_LENGTH, or a new name arriving after MAX_PREFIX_ROUTE_NAMES distinct prefix-route names is skipped with a WARN, never an exception. Known, bounded vocabularies belong in curated Property constants instead; deployment-tunable limits belong in a StandardMetadataLimiter, which applies on top of this route.
      Parameters:
      prefix - the declared prefix for this source (never from document text)
      name - the source-derived name; skipped if null, blank, or over-length
      value - the value; a null value is skipped
      Throws:
      NullPointerException - if prefix is null
      Since:
      Apache Tika 4.0.0
    • add

      public void add(KeyPrefix prefix, String name, Instant value)
      add(KeyPrefix, String, String) for a source-typed date value (e.g. an OLE VT_DATE / OOXML vt:filetime custom property): the value is stored in Tika's canonical ISO-8601 form, identical to how curated date Property constants serialize. The date-typing travels with the value, not the key. An Instant too extreme to represent is skipped with a WARN. If the source's date field fails to parse in the first place, keep the raw text via the String overload instead.
      Since:
      Apache Tika 4.0.0
    • reconstruct

      public void reconstruct(String name, String value, boolean append)
      Trusted write for clone/merge/deserialize; reserved keys go via their Property.
      Parameters:
      append - add rather than set
      See Also:
    • putAll

      public void putAll(Metadata other)
      Copies every key from other into this Metadata: for each name in other, this Metadata's values for that name are replaced wholesale with other's values, in order (multi-values preserved); names absent from other are left untouched. Each value is written via reconstruct(String, String, boolean), so reserved tk: keys copy through their trusted route rather than the String-guarded route.

      Use this instead of a manual for (String n : src.names()) dest.set(n, src.get(n)) copy loop, which collapses multi-valued keys and throws on tk:-prefixed keys.

      Parameters:
      other - the Metadata to copy from; other == this is a no-op
      Throws:
      NullPointerException - if other is null
      Since:
      Apache Tika 4.0.0
    • add

      protected void add(String name, String[] newValues)
      Add a metadata name/value mapping. Add the specified value to the list of values associated to the specified metadata name.
      Parameters:
      name - the metadata name.
      newValues - the metadata values
    • add

      public void add(Property property, String value)
      Add a metadata property/value mapping. Add the specified value to the list of values associated to the specified metadata property.
      Parameters:
      property - the metadata property.
      value - the metadata value.
    • set

      public void set(String name, String value)
      Set metadata name/value. Associate the specified value to the specified metadata name. If some previous values were associated to this name, they are removed. If the given value is null, then the metadata entry is removed.

      This route rejects reserved Tika-native (tk:) keys -- see add(String, String) for why, and use the Property overload or setTrusted(java.lang.String, java.lang.String) for keys Tika owns.

      Parameters:
      name - the metadata name.
      value - the metadata value, or null
      Throws:
      IllegalArgumentException - if name is a reserved Tika-native (tk:) key and value is non-null; use its Property or setTrusted(java.lang.String, java.lang.String). A null value (removal) is always allowed, matching remove(String).
      See Also:
    • setTrusted

      public void setTrusted(String name, String value)
      Trusted set, bypassing the reserved-key guard: writes reach reserved Tika-native (tk:) keys directly. See addTrusted(java.lang.String, java.lang.String).
    • set

      protected void set(String name, String[] values)
    • set

      public void set(Property property, String value)
      Sets the value of the identified metadata property.
      Parameters:
      property - property definition
      value - property value
      Since:
      Apache Tika 0.7
    • set

      public void set(Property property, String[] values)
      Sets the values of the identified metadata property.
      Parameters:
      property - property definition
      values - property values
      Since:
      Apache Tika 1.2
    • set

      public void set(Property property, int value)
      Sets the integer value of the identified metadata property.
      Parameters:
      property - simple integer property definition
      value - property value
      Since:
      Apache Tika 0.8
    • set

      public void set(Property property, long value)
      Sets the long value of the identified metadata property.
      Parameters:
      property - simple integer or real property definition
      value - property value
      Since:
      Apache Tika 0.8
    • set

      public void set(Property property, boolean value)
      Sets the integer value of the identified metadata property.
      Parameters:
      property - simple integer property definition
      value - property value
      Since:
      Apache Tika 2.1.1
    • add

      public void add(Property property, int value)
      Adds the integer value of the identified metadata property.
      Parameters:
      property - seq integer property definition
      value - property value
      Since:
      Apache Tika 1.21
    • getIntValues

      public int[] getIntValues(Property property)
      Gets the array of ints of the identified "seq" integer metadata property.
      Parameters:
      property - seq integer property definition
      Returns:
      array of ints
      Since:
      Apache Tika 1.21
    • getLongValues

      public long[] getLongValues(Property property)
      Gets the array of longs of the identified "seq" metadata property. INTEGER or REAL valued (mirroring getLong(org.apache.tika.metadata.Property)).
      Parameters:
      property - seq integer or real property definition
      Returns:
      array of longs
      Since:
      Apache Tika 1.21
    • set

      public void set(Property property, double value)
      Sets the real or rational value of the identified metadata property.
      Parameters:
      property - simple real or simple rational property definition
      value - property value
      Since:
      Apache Tika 0.8
    • set

      public void set(Property property, Date date)
      Sets the date value of the identified metadata property.
      Parameters:
      property - simple integer property definition
      date - property value
      Since:
      Apache Tika 0.8
    • set

      public void set(Property property, Calendar date)
      Sets the date value of the identified metadata property.
      Parameters:
      property - simple integer property definition
      date - property value
      Since:
      Apache Tika 0.8
    • add

      public void add(Property property, Calendar date)
      Adds the date value of the identified metadata property.
      Parameters:
      property - simple calendar property definition
      date - property value
      Since:
      Apache Tika 2.5.0
    • remove

      public void remove(String name)
      Remove a metadata and all its associated values.
      Parameters:
      name - metadata name to remove
    • remove

      public void remove(Property property)
      Remove a metadata property and all its associated values.
      Parameters:
      property - metadata property to remove
    • size

      public int size()
      Returns the number of metadata names in this metadata.
      Returns:
      number of metadata names
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object