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

MutablePropertySet

public class MutablePropertySet extends PropertySet

Adds writing support to the {@link PropertySet} class.

Please be aware that this class' functionality will be merged into the {@link PropertySet} class at a later time, so the API will change.

author
Rainer Klute <klute@rainer-klute.de>
version
$Id: MutablePropertySet.java 489730 2006-12-22 19:18:16Z bayard $
since
2003-02-19

Fields Summary
private final int
OFFSET_HEADER

The length of the property set stream header.

Constructors Summary
public MutablePropertySet()

Constructs a MutablePropertySet instance. Its primary task is to initialize the immutable field with their proper values. It also sets fields that might change to reasonable defaults.

        /* Initialize the "byteOrder" field. */
        byteOrder = LittleEndian.getUShort(BYTE_ORDER_ASSERTION);

        /* Initialize the "format" field. */
        format = LittleEndian.getUShort(FORMAT_ASSERTION);

        /* Initialize "osVersion" field as if the property has been created on
         * a Win32 platform, whether this is the case or not. */
        osVersion = (OS_WIN32 << 16) | 0x0A04;

        /* Initailize the "classID" field. */
        classID = new ClassID();

        /* Initialize the sections. Since property set must have at least
         * one section it is added right here. */
        sections = new LinkedList();
        sections.add(new MutableSection());
    
public MutablePropertySet(PropertySet ps)

Constructs a MutablePropertySet by doing a deep copy of an existing PropertySet. All nested elements, i.e. Sections and Property instances, will be their mutable counterparts in the new MutablePropertySet.

param
ps The property set to copy

        byteOrder = ps.getByteOrder();
        format = ps.getFormat();
        osVersion = ps.getOSVersion();
        setClassID(ps.getClassID());
        clearSections();
        for (final Iterator i = ps.getSections().iterator(); i.hasNext();)
        {
            final MutableSection s = new MutableSection((Section) (i.next()));
            addSection(s);
        }
    
Methods Summary
public voidaddSection(org.apache.poi.hpsf.Section section)

Adds a section to this property set.

param
section The {@link Section} to add. It will be appended after any sections that are already present in the property set and thus become the last section.

        if (sections == null)
            sections = new LinkedList();
        sections.add(section);
    
public voidclearSections()

Removes all sections from this property set.

        sections = null;
    
public voidsetByteOrder(int byteOrder)

Sets the "byteOrder" property.

param
byteOrder the byteOrder value to set

  /* Section count */



                    
        
    
        this.byteOrder = byteOrder;
    
public voidsetClassID(org.apache.poi.hpsf.ClassID classID)

Sets the property set stream's low-level "class ID" field.

param
classID The property set stream's low-level "class ID" field.
see
PropertySet#getClassID()

        this.classID = classID;
    
public voidsetFormat(int format)

Sets the "format" property.

param
format the format value to set

        this.format = format;
    
public voidsetOSVersion(int osVersion)

Sets the "osVersion" property.

param
osVersion the osVersion value to set

        this.osVersion = osVersion;
    
public java.io.InputStreamtoInputStream()

Returns the contents of this property set stream as an input stream. The latter can be used for example to write the property set into a POIFS document. The input stream represents a snapshot of the property set. If the latter is modified while the input stream is still being read, the modifications will not be reflected in the input stream but in the {@link MutablePropertySet} only.

return
the contents of this property set stream
throws
WritingNotSupportedException if HPSF does not yet support writing of a property's variant type.
throws
IOException if an I/O exception occurs.

        final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
        write(psStream);
        psStream.close();
        final byte[] streamData = psStream.toByteArray();
        return new ByteArrayInputStream(streamData);
    
public voidwrite(org.apache.poi.poifs.filesystem.DirectoryEntry dir, java.lang.String name)

Writes a property set to a document in a POI filesystem directory.

param
dir The directory in the POI filesystem to write the document to.
param
name The document's name. If there is already a document with the same name in the directory the latter will be overwritten.
throws
WritingNotSupportedException
throws
IOException

        /* If there is already an entry with the same name, remove it. */
        try
        {
            final Entry e = dir.getEntry(name);
            e.delete();
        }
        catch (FileNotFoundException ex)
        {
            /* Entry not found, no need to remove it. */
        }
        /* Create the new entry. */
        dir.createDocument(name, toInputStream());
    
public voidwrite(java.io.OutputStream out)

Writes the property set to an output stream.

param
out the output stream to write the section to
exception
IOException if an error when writing to the output stream occurs
exception
WritingNotSupportedException if HPSF does not yet support writing a property's variant type.

        /* Write the number of sections in this property set stream. */
        final int nrSections = sections.size();
        int length = 0;

        /* Write the property set's header. */
        length += TypeWriter.writeToStream(out, (short) getByteOrder());
        length += TypeWriter.writeToStream(out, (short) getFormat());
        length += TypeWriter.writeToStream(out, getOSVersion());
        length += TypeWriter.writeToStream(out, getClassID());
        length += TypeWriter.writeToStream(out, nrSections);
        int offset = OFFSET_HEADER;

        /* Write the section list, i.e. the references to the sections. Each
         * entry in the section list consist of the section's class ID and the
         * section's offset relative to the beginning of the stream. */
        offset += nrSections * (ClassID.LENGTH + LittleEndian.INT_SIZE);
        final int sectionsBegin = offset;
        for (final ListIterator i = sections.listIterator(); i.hasNext();)
        {
            final MutableSection s = (MutableSection) i.next();
            final ClassID formatID = s.getFormatID();
            if (formatID == null)
                throw new NoFormatIDException();
            length += TypeWriter.writeToStream(out, s.getFormatID());
            length += TypeWriter.writeUIntToStream(out, offset);
            try
            {
                offset += s.getSize();
            }
            catch (HPSFRuntimeException ex)
            {
                final Throwable cause = ex.getReason();
                if (cause instanceof UnsupportedEncodingException)
                    throw new IllegalPropertySetDataException(cause);
                else
                    throw ex;
            }
        }

        /* Write the sections themselves. */
        offset = sectionsBegin;
        for (final ListIterator i = sections.listIterator(); i.hasNext();)
        {
            final MutableSection s = (MutableSection) i.next();
            offset += s.write(out);
        }