FileDocCategorySizeDatePackage
DirectoryProperty.javaAPI DocApache Poi 3.0.18954Mon Jan 01 12:39:36 GMT 2007org.apache.poi.poifs.property

DirectoryProperty

public class DirectoryProperty extends Property implements Parent
Directory property
author
Marc Johnson (mjohnson at apache dot org)

Fields Summary
private List
_children
private Set
_children_names
Constructors Summary
public DirectoryProperty(String name)
Default constructor

param
name the name of the directory

        super();
        _children       = new ArrayList();
        _children_names = new HashSet();
        setName(name);
        setSize(0);
        setPropertyType(PropertyConstants.DIRECTORY_TYPE);
        setStartBlock(0);
        setNodeColor(_NODE_BLACK);   // simplification
    
protected DirectoryProperty(int index, byte[] array, int offset)
reader constructor

param
index index number
param
array byte data
param
offset offset into byte data

        super(index, array, offset);
        _children       = new ArrayList();
        _children_names = new HashSet();
    
Methods Summary
public voidaddChild(org.apache.poi.poifs.property.Property property)
Add a new child to the collection of children

param
property the new child to be added; must not be null
exception
IOException if we already have a child with the same name

        String name = property.getName();

        if (_children_names.contains(name))
        {
            throw new IOException("Duplicate name \"" + name + "\"");
        }
        _children_names.add(name);
        _children.add(property);
    
public booleanchangeName(org.apache.poi.poifs.property.Property property, java.lang.String newName)
Change a Property's name

param
property the Property whose name is being changed
param
newName the new name for the Property
return
true if the name change could be made, else false

        boolean result;
        String  oldName = property.getName();

        property.setName(newName);
        String cleanNewName = property.getName();

        if (_children_names.contains(cleanNewName))
        {

            // revert the change
            property.setName(oldName);
            result = false;
        }
        else
        {
            _children_names.add(cleanNewName);
            _children_names.remove(oldName);
            result = true;
        }
        return result;
    
public booleandeleteChild(org.apache.poi.poifs.property.Property property)
Delete a Property

param
property the Property being deleted
return
true if the Property could be deleted, else false

        boolean result = _children.remove(property);

        if (result)
        {
            _children_names.remove(property.getName());
        }
        return result;
    
public java.util.IteratorgetChildren()
Get an iterator over the children of this Parent; all elements are instances of Property.

return
Iterator of children; may refer to an empty collection

        return _children.iterator();
    
public booleanisDirectory()

return
true if a directory type Property

        return true;
    
protected voidpreWrite()
Perform whatever activities need to be performed prior to writing

        if (_children.size() > 0)
        {
            Property[] children =
                ( Property [] ) _children.toArray(new Property[ 0 ]);

            Arrays.sort(children, new PropertyComparator());
            int midpoint = children.length / 2;

            setChildProperty(children[ midpoint ].getIndex());
            children[ 0 ].setPreviousChild(null);
            children[ 0 ].setNextChild(null);
            for (int j = 1; j < midpoint; j++)
            {
                children[ j ].setPreviousChild(children[ j - 1 ]);
                children[ j ].setNextChild(null);
            }
            if (midpoint != 0)
            {
                children[ midpoint ]
                    .setPreviousChild(children[ midpoint - 1 ]);
            }
            if (midpoint != (children.length - 1))
            {
                children[ midpoint ].setNextChild(children[ midpoint + 1 ]);
                for (int j = midpoint + 1; j < children.length - 1; j++)
                {
                    children[ j ].setPreviousChild(null);
                    children[ j ].setNextChild(children[ j + 1 ]);
                }
                children[ children.length - 1 ].setPreviousChild(null);
                children[ children.length - 1 ].setNextChild(null);
            }
            else
            {
                children[ midpoint ].setNextChild(null);
            }
        }