FileDocCategorySizeDatePackage
CustomProperties.javaAPI DocApache Poi 3.0.112233Mon Jan 01 12:39:34 GMT 2007org.apache.poi.hpsf

CustomProperties

public class CustomProperties extends HashMap

Maintains the instances of {@link CustomProperty} that belong to a {@link DocumentSummaryInformation}. The class maintains the names of the custom properties in a dictionary. It implements the {@link Map} interface and by this provides a simplified view on custom properties: A property's name is the key that maps to a typed value. This implementation hides property IDs from the developer and regards the property names as keys to typed values.

While this class provides a simple API to custom properties, it ignores the fact that not names, but IDs are the real keys to properties. Under the hood this class maintains a 1:1 relationship between IDs and names. Therefore you should not use this class to process property sets with several IDs mapping to the same name or with properties without a name: the result will contain only a subset of the original properties. If you really need to deal such property sets, use HPSF's low-level access methods.

An application can call the {@link #isPure} method to check whether a property set parsed by {@link CustomProperties} is still pure (i.e. unmodified) or whether one or more properties have been dropped.

This class is not thread-safe; concurrent access to instances of this class must be syncronized.

author
Rainer Klute <klute@rainer-klute.de>
since
2006-02-09
version
$Id$

Fields Summary
private Map
dictionaryIDToName

Maps property IDs to property names.

private Map
dictionaryNameToID

Maps property names to property IDs.

private boolean
isPure

Tells whether this object is pure or not.

Constructors Summary
Methods Summary
public java.lang.Objectget(java.lang.String name)

Gets a named value from the custom properties.

param
name the name of the value to get
return
the value or null if a value with the specified name is not found in the custom properties.

        final Long id = (Long) dictionaryNameToID.get(name);
        final CustomProperty cp = (CustomProperty) super.get(id);
        return cp != null ? cp.getValue() : null;
    
public intgetCodepage()

Gets the codepage.

return
the codepage or -1 if the codepage is undefined.

        int codepage = -1;
        for (final Iterator i = this.values().iterator(); codepage == -1 && i.hasNext();)
        {
            final CustomProperty cp = (CustomProperty) i.next();
            if (cp.getID() == PropertyIDMap.PID_CODEPAGE)
                codepage = ((Integer) cp.getValue()).intValue();
        }
        return codepage;
    
java.util.MapgetDictionary()

Gets the dictionary which contains IDs and names of the named custom properties.

return
the dictionary.

        return dictionaryIDToName;
    
public booleanisPure()

Tells whether this {@link CustomProperties} instance is pure or one or more properties of the underlying low-level property set has been dropped.

return
true if the {@link CustomProperties} is pure, else false.

        return isPure;
    
public java.lang.Objectput(java.lang.Object name, java.lang.Object customProperty)

Puts a {@link CustomProperty} into this map. It is assumed that the {@link CustomProperty} already has a valid ID. Otherwise use {@link #put(CustomProperty)}.




                                
             
    
        final CustomProperty cp = (CustomProperty) customProperty;
        if (name == null)
        {
            /* Ignoring a property without a name. */
            isPure = false;
            return null;
        }
        if (!(name instanceof String))
            throw new ClassCastException("The name of a custom property must " +
                    "be a java.lang.String, but it is a " +
                    name.getClass().getName());
        if (!(name.equals(cp.getName())))
            throw new IllegalArgumentException("Parameter \"name\" (" + name +
                    ") and custom property's name (" + cp.getName() +
                    ") do not match.");

        /* Register name and ID in the dictionary. Mapping in both directions is possible. If there is already a  */
        final Long idKey = new Long(cp.getID());
        final Object oldID = dictionaryNameToID.get(name);
        dictionaryIDToName.remove(oldID);
        dictionaryNameToID.put(name, idKey);
        dictionaryIDToName.put(idKey, name);

        /* Put the custom property into this map. */
        final Object oldCp = super.remove(oldID);
        super.put(idKey, cp);
        return oldCp;
    
public java.lang.Objectput(java.lang.String name, java.util.Date value)

Adds a named date property.

param
name The property's name.
param
value The property's value.
return
the property that was stored under the specified name before, or null if there was no such property before.

        final MutableProperty p = new MutableProperty();
        p.setID(-1);
        p.setType(Variant.VT_FILETIME);
        p.setValue(value);
        final CustomProperty cp = new CustomProperty(p, name);
        return put(cp);
    
private java.lang.Objectput(org.apache.poi.hpsf.CustomProperty customProperty)

Puts a {@link CustomProperty} that has not yet a valid ID into this map. The method will allocate a suitable ID for the custom property:

  • If there is already a property with the same name, take the ID of that property.

  • Otherwise find the highest ID and use its value plus one.

param
customProperty
return
If the was already a property with the same name, the
throws
ClassCastException

        final String name = customProperty.getName();

        /* Check whether a property with this name is in the map already. */
        final Long oldId = (Long) dictionaryNameToID.get(name);
        if (oldId != null)
            customProperty.setID(oldId.longValue());
        else
        {
            long max = 1;
            for (final Iterator i = dictionaryIDToName.keySet().iterator(); i.hasNext();)
            {
                final long id = ((Long) i.next()).longValue();
                if (id > max)
                    max = id;
            }
            customProperty.setID(max + 1);
        }
        return this.put(name, customProperty);
    
public java.lang.Objectput(java.lang.String name, java.lang.String value)

Adds a named string property.

param
name The property's name.
param
value The property's value.
return
the property that was stored under the specified name before, or null if there was no such property before.

        final MutableProperty p = new MutableProperty();
        p.setID(-1);
        p.setType(Variant.VT_LPWSTR);
        p.setValue(value);
        final CustomProperty cp = new CustomProperty(p, name);
        return put(cp);
    
public java.lang.Objectput(java.lang.String name, java.lang.Long value)

Adds a named long property.

param
name The property's name.
param
value The property's value.
return
the property that was stored under the specified name before, or null if there was no such property before.

        final MutableProperty p = new MutableProperty();
        p.setID(-1);
        p.setType(Variant.VT_I8);
        p.setValue(value);
        final CustomProperty cp = new CustomProperty(p, name);
        return put(cp);
    
public java.lang.Objectput(java.lang.String name, java.lang.Double value)

Adds a named double property.

param
name The property's name.
param
value The property's value.
return
the property that was stored under the specified name before, or null if there was no such property before.

        final MutableProperty p = new MutableProperty();
        p.setID(-1);
        p.setType(Variant.VT_R8);
        p.setValue(value);
        final CustomProperty cp = new CustomProperty(p, name);
        return put(cp);
    
public java.lang.Objectput(java.lang.String name, java.lang.Integer value)

Adds a named integer property.

param
name The property's name.
param
value The property's value.
return
the property that was stored under the specified name before, or null if there was no such property before.

        final MutableProperty p = new MutableProperty();
        p.setID(-1);
        p.setType(Variant.VT_I4);
        p.setValue(value);
        final CustomProperty cp = new CustomProperty(p, name);
        return put(cp);
    
public java.lang.Objectput(java.lang.String name, java.lang.Boolean value)

Adds a named boolean property.

param
name The property's name.
param
value The property's value.
return
the property that was stored under the specified name before, or null if there was no such property before.

        final MutableProperty p = new MutableProperty();
        p.setID(-1);
        p.setType(Variant.VT_BOOL);
        p.setValue(value);
        final CustomProperty cp = new CustomProperty(p, name);
        return put(cp);
    
public java.lang.Objectremove(java.lang.String name)

Removes a custom property.

param
name The name of the custom property to remove
return
The removed property or null if the specified property was not found.
see
java.util.HashSet#remove(java.lang.Object)

        final Long id = (Long) dictionaryNameToID.get(name);
        if (id == null)
            return null;
        dictionaryIDToName.remove(id);
        dictionaryNameToID.remove(name);
        return super.remove(id);
    
public voidsetCodepage(int codepage)

Sets the codepage.

param
codepage the codepage

        final MutableProperty p = new MutableProperty();
        p.setID(PropertyIDMap.PID_CODEPAGE);
        p.setType(Variant.VT_I2);
        p.setValue(new Integer(codepage));
        put(new CustomProperty(p));
    
public voidsetPure(boolean isPure)

Sets the purity of the custom property set.

param
isPure the purity

        this.isPure = isPure;