2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
/* ====================================================================
|
|
|
|
Copyright 2002-2004 Apache Software Foundation
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
package org.apache.poi.hpsf;
|
|
|
|
|
2003-12-02 12:46:01 -05:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2003-08-23 11:12:22 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2003-08-02 15:02:28 -04:00
|
|
|
import java.util.Map;
|
2003-08-23 11:12:22 -04:00
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
|
|
|
|
import org.apache.poi.hpsf.wellknown.SectionIDMap;
|
2003-08-23 11:12:22 -04:00
|
|
|
import org.apache.poi.util.LittleEndian;
|
2002-02-13 23:00:59 -05:00
|
|
|
|
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Represents a section in a {@link PropertySet}.</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2003-08-30 05:13:53 -04:00
|
|
|
* @author Rainer Klute <a
|
|
|
|
* href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
|
2002-07-17 12:23:22 -04:00
|
|
|
* @author Drew Varner (Drew.Varner allUpIn sc.edu)
|
|
|
|
* @version $Id$
|
|
|
|
* @since 2002-02-09
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public class Section
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
|
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Maps property IDs to section-private PID strings. These
|
|
|
|
* strings can be found in the property with ID 0.</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
|
|
|
protected Map dictionary;
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/**
|
|
|
|
* <p>The section's format ID, {@link #getFormatID}.</p>
|
|
|
|
*/
|
2003-02-22 09:27:16 -05:00
|
|
|
protected ClassID formatID;
|
2002-02-13 23:00:59 -05:00
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the format ID. The format ID is the "type" of the
|
2003-01-29 13:01:18 -05:00
|
|
|
* section. For example, if the format ID of the first {@link
|
|
|
|
* Section} contains the bytes specified by
|
|
|
|
* <code>org.apache.poi.hpsf.wellknown.SectionIDMap.SUMMARY_INFORMATION_ID</code>
|
2003-09-18 14:56:35 -04:00
|
|
|
* the section (and thus the property set) is a SummaryInformation.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The format ID
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public ClassID getFormatID()
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
return formatID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/**
|
|
|
|
* @see #getOffset
|
|
|
|
*/
|
2003-02-22 09:27:16 -05:00
|
|
|
protected long offset;
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
|
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the offset of the section in the stream.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The offset of the section in the stream.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public long getOffset()
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/**
|
|
|
|
* @see #getSize
|
|
|
|
*/
|
2003-02-22 09:27:16 -05:00
|
|
|
protected int size;
|
2002-02-13 23:00:59 -05:00
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the section's size in bytes.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The section's size in bytes.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public int getSize()
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/**
|
2003-09-04 16:15:24 -04:00
|
|
|
* FIXME (2): Get rid of this! The property count is implicitly available as
|
|
|
|
* the length of the "properties" array.
|
|
|
|
*
|
2003-08-02 15:02:28 -04:00
|
|
|
* @see #getPropertyCount
|
|
|
|
*/
|
2003-09-04 16:15:24 -04:00
|
|
|
protected int propertyCount;
|
2002-02-13 23:00:59 -05:00
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the number of properties in this section.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The number of properties in this section.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public int getPropertyCount()
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
return propertyCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/**
|
|
|
|
* @see #getProperties
|
|
|
|
*/
|
2003-09-04 16:15:24 -04:00
|
|
|
protected Property[] properties;
|
2002-02-13 23:00:59 -05:00
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns this section's properties.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return This section's properties.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public Property[] getProperties()
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-22 09:27:16 -05:00
|
|
|
/**
|
|
|
|
* <p>Creates an empty and uninitialized {@link Section}.
|
|
|
|
*/
|
|
|
|
protected Section()
|
2003-08-02 15:02:28 -04:00
|
|
|
{ }
|
2003-02-22 09:27:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Creates a {@link Section} instance from a byte array.</p>
|
2002-02-13 23:00:59 -05:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @param src Contains the complete property set stream.
|
|
|
|
* @param offset The position in the stream that points to the
|
|
|
|
* section's format ID.
|
2003-12-02 12:46:01 -05:00
|
|
|
*
|
|
|
|
* @exception UnsupportedEncodingException if the section's codepage is not
|
|
|
|
* supported.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-08-02 15:02:28 -04:00
|
|
|
public Section(final byte[] src, final int offset)
|
2003-12-02 12:46:01 -05:00
|
|
|
throws UnsupportedEncodingException
|
2002-07-17 12:23:22 -04:00
|
|
|
{
|
2003-08-02 15:02:28 -04:00
|
|
|
int o1 = offset;
|
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
/*
|
2002-12-10 01:15:20 -05:00
|
|
|
* Read the format ID.
|
2002-05-11 10:47:24 -04:00
|
|
|
*/
|
2003-08-02 15:02:28 -04:00
|
|
|
formatID = new ClassID(src, o1);
|
|
|
|
o1 += ClassID.LENGTH;
|
2002-02-13 23:00:59 -05:00
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
/*
|
2002-12-10 01:15:20 -05:00
|
|
|
* Read the offset from the stream's start and positions to
|
|
|
|
* the section header.
|
2002-05-11 10:47:24 -04:00
|
|
|
*/
|
2003-08-02 15:02:28 -04:00
|
|
|
this.offset = LittleEndian.getUInt(src, o1);
|
|
|
|
o1 = (int) this.offset;
|
2002-05-11 10:47:24 -04:00
|
|
|
|
|
|
|
/*
|
2002-12-10 01:15:20 -05:00
|
|
|
* Read the section length.
|
2002-05-11 10:47:24 -04:00
|
|
|
*/
|
2003-08-02 15:02:28 -04:00
|
|
|
size = (int) LittleEndian.getUInt(src, o1);
|
|
|
|
o1 += LittleEndian.INT_SIZE;
|
2002-05-11 10:47:24 -04:00
|
|
|
|
|
|
|
/*
|
2002-12-10 01:15:20 -05:00
|
|
|
* Read the number of properties.
|
2002-05-11 10:47:24 -04:00
|
|
|
*/
|
2003-08-02 15:02:28 -04:00
|
|
|
propertyCount = (int) LittleEndian.getUInt(src, o1);
|
|
|
|
o1 += LittleEndian.INT_SIZE;
|
2002-05-11 10:47:24 -04:00
|
|
|
|
|
|
|
/*
|
2002-12-10 01:15:20 -05:00
|
|
|
* Read the properties. The offset is positioned at the first
|
2003-08-23 11:12:22 -04:00
|
|
|
* entry of the property list. There are two problems:
|
|
|
|
*
|
|
|
|
* 1. For each property we have to find out its length. In the
|
|
|
|
* property list we find each property's ID and its offset relative
|
|
|
|
* to the section's beginning. Unfortunately the properties in the
|
|
|
|
* property list need not to be in ascending order, so it is not
|
|
|
|
* possible to calculate the length as
|
|
|
|
* (offset of property(i+1) - offset of property(i)). Before we can
|
|
|
|
* that we first have to sort the property list by ascending offsets.
|
|
|
|
*
|
|
|
|
* 2. We have to read the property with ID 1 before we read other
|
|
|
|
* properties, at least before other properties containing strings.
|
|
|
|
* The reason is that property 1 specifies the codepage. If it is
|
|
|
|
* 1200, all strings are in Unicode. In other words: Before we can
|
|
|
|
* read any strings we have to know whether they are in Unicode or
|
|
|
|
* not. Unfortunately property 1 is not guaranteed to be the first in
|
|
|
|
* a section.
|
2003-08-02 15:02:28 -04:00
|
|
|
*
|
2003-08-23 11:12:22 -04:00
|
|
|
* The algorithm below reads the properties in two passes: The first
|
|
|
|
* one looks for property ID 1 and extracts the codepage number. The
|
|
|
|
* seconds pass reads the other properties.
|
2002-05-11 10:47:24 -04:00
|
|
|
*/
|
2002-02-13 23:00:59 -05:00
|
|
|
properties = new Property[propertyCount];
|
2003-08-23 11:12:22 -04:00
|
|
|
|
|
|
|
/* Pass 1: Read the property list. */
|
2003-08-02 15:02:28 -04:00
|
|
|
int pass1Offset = o1;
|
2003-08-23 11:12:22 -04:00
|
|
|
List propertyList = new ArrayList(propertyCount);
|
|
|
|
PropertyListEntry ple;
|
2002-12-10 01:15:20 -05:00
|
|
|
for (int i = 0; i < properties.length; i++)
|
2003-08-02 15:02:28 -04:00
|
|
|
{
|
2003-08-23 11:12:22 -04:00
|
|
|
ple = new PropertyListEntry();
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/* Read the property ID. */
|
2003-08-23 11:12:22 -04:00
|
|
|
ple.id = (int) LittleEndian.getUInt(src, pass1Offset);
|
2002-12-10 01:15:20 -05:00
|
|
|
pass1Offset += LittleEndian.INT_SIZE;
|
|
|
|
|
|
|
|
/* Offset from the section's start. */
|
2003-08-23 11:12:22 -04:00
|
|
|
ple.offset = (int) LittleEndian.getUInt(src, pass1Offset);
|
2002-12-10 01:15:20 -05:00
|
|
|
pass1Offset += LittleEndian.INT_SIZE;
|
|
|
|
|
2003-08-23 11:12:22 -04:00
|
|
|
/* Add the entry to the property list. */
|
|
|
|
propertyList.add(ple);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the property list by ascending offsets: */
|
|
|
|
Collections.sort(propertyList);
|
2003-08-02 15:02:28 -04:00
|
|
|
|
2003-08-23 11:12:22 -04:00
|
|
|
/* Calculate the properties' lengths. */
|
|
|
|
for (int i = 0; i < propertyCount - 1; i++)
|
|
|
|
{
|
|
|
|
final PropertyListEntry ple1 =
|
|
|
|
(PropertyListEntry) propertyList.get(i);
|
|
|
|
final PropertyListEntry ple2 =
|
|
|
|
(PropertyListEntry) propertyList.get(i + 1);
|
|
|
|
ple1.length = ple2.offset - ple1.offset;
|
|
|
|
}
|
|
|
|
if (propertyCount > 0)
|
|
|
|
{
|
|
|
|
ple = (PropertyListEntry) propertyList.get(propertyCount - 1);
|
|
|
|
ple.length = size - ple.offset;
|
|
|
|
}
|
2003-08-02 15:02:28 -04:00
|
|
|
|
2003-08-23 11:12:22 -04:00
|
|
|
/* Look for the codepage. */
|
|
|
|
int codepage = -1;
|
|
|
|
for (final Iterator i = propertyList.iterator();
|
|
|
|
codepage == -1 && i.hasNext();)
|
|
|
|
{
|
|
|
|
ple = (PropertyListEntry) i.next();
|
|
|
|
|
|
|
|
/* Read the codepage if the property ID is 1. */
|
|
|
|
if (ple.id == PropertyIDMap.PID_CODEPAGE)
|
|
|
|
{
|
2003-08-02 15:02:28 -04:00
|
|
|
/* Read the property's value type. It must be
|
|
|
|
* VT_I2. */
|
2003-08-23 11:12:22 -04:00
|
|
|
int o = (int) (this.offset + ple.offset);
|
2003-08-02 15:02:28 -04:00
|
|
|
final long type = LittleEndian.getUInt(src, o);
|
|
|
|
o += LittleEndian.INT_SIZE;
|
|
|
|
|
|
|
|
if (type != Variant.VT_I2)
|
|
|
|
throw new HPSFRuntimeException
|
|
|
|
("Value type of property ID 1 is not VT_I2 but " +
|
|
|
|
type + ".");
|
2002-12-10 01:15:20 -05:00
|
|
|
|
|
|
|
/* Read the codepage number. */
|
|
|
|
codepage = LittleEndian.getUShort(src, o);
|
2003-08-02 15:02:28 -04:00
|
|
|
}
|
|
|
|
}
|
2002-12-10 01:15:20 -05:00
|
|
|
|
2003-08-23 11:12:22 -04:00
|
|
|
/* Pass 2: Read all properties - including the codepage property,
|
|
|
|
* if available. */
|
|
|
|
int i1 = 0;
|
|
|
|
for (final Iterator i = propertyList.iterator(); i.hasNext();)
|
2003-08-02 15:02:28 -04:00
|
|
|
{
|
2003-08-23 11:12:22 -04:00
|
|
|
ple = (PropertyListEntry) i.next();
|
|
|
|
properties[i1++] = new Property(ple.id, src,
|
|
|
|
this.offset + ple.offset,
|
|
|
|
ple.length, codepage);
|
2002-02-13 23:00:59 -05:00
|
|
|
}
|
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
/*
|
2002-12-10 01:15:20 -05:00
|
|
|
* Extract the dictionary (if available).
|
2002-05-11 10:47:24 -04:00
|
|
|
*/
|
2002-02-13 23:00:59 -05:00
|
|
|
dictionary = (Map) getProperty(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-23 11:12:22 -04:00
|
|
|
/**
|
|
|
|
* <p>Represents an entry in the property list and holds a property's ID and
|
|
|
|
* its offset from the section's beginning.</p>
|
|
|
|
*/
|
|
|
|
class PropertyListEntry implements Comparable
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
int offset;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>Compares this {@link PropertyListEntry} with another one by their
|
|
|
|
* offsets. A {@link PropertyListEntry} is "smaller" than another one if
|
|
|
|
* its offset from the section's begin is smaller.</p>
|
|
|
|
*
|
|
|
|
* @see Comparable#compareTo(java.lang.Object)
|
|
|
|
*/
|
|
|
|
public int compareTo(final Object o)
|
|
|
|
{
|
|
|
|
if (!(o instanceof PropertyListEntry))
|
|
|
|
throw new ClassCastException(o.toString());
|
|
|
|
final int otherOffset = ((PropertyListEntry) o).offset;
|
|
|
|
if (offset < otherOffset)
|
|
|
|
return -1;
|
|
|
|
else if (offset == otherOffset)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the value of the property with the specified ID. If
|
|
|
|
* the property is not available, <code>null</code> is returned
|
|
|
|
* and a subsequent call to {@link #wasNull} will return
|
|
|
|
* <code>true</code>.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @param id The property's ID
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The property's value
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-09-04 16:15:24 -04:00
|
|
|
public Object getProperty(final long id)
|
2002-07-17 12:23:22 -04:00
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
wasNull = false;
|
2002-07-17 12:23:22 -04:00
|
|
|
for (int i = 0; i < properties.length; i++)
|
|
|
|
if (id == properties[i].getID())
|
2002-02-13 23:00:59 -05:00
|
|
|
return properties[i].getValue();
|
|
|
|
wasNull = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the value of the numeric property with the specified
|
|
|
|
* ID. If the property is not available, 0 is returned. A
|
|
|
|
* subsequent call to {@link #wasNull} will return
|
|
|
|
* <code>true</code> to let the caller distinguish that case from
|
|
|
|
* a real property value of 0.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @param id The property's ID
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The property's value
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-09-04 16:15:24 -04:00
|
|
|
protected int getPropertyIntValue(final long id)
|
2002-07-17 12:23:22 -04:00
|
|
|
{
|
2003-09-13 08:31:28 -04:00
|
|
|
final Long i;
|
|
|
|
final Object o = getProperty(id);
|
|
|
|
if (o == null)
|
2002-02-13 23:00:59 -05:00
|
|
|
return 0;
|
2003-09-13 08:31:28 -04:00
|
|
|
if (!(o instanceof Long))
|
|
|
|
throw new HPSFRuntimeException
|
|
|
|
("This property is not an integer type, but " +
|
|
|
|
o.getClass().getName() + ".");
|
|
|
|
i = (Long) o;
|
|
|
|
return i.intValue();
|
2002-02-13 23:00:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-05-03 03:29:09 -04:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the value of the boolean property with the specified
|
|
|
|
* ID. If the property is not available, <code>false</code> is
|
|
|
|
* returned. A subsequent call to {@link #wasNull} will return
|
|
|
|
* <code>true</code> to let the caller distinguish that case from
|
|
|
|
* a real property value of <code>false</code>.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @param id The property's ID
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The property's value
|
2002-05-03 03:29:09 -04:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
protected boolean getPropertyBooleanValue(final int id)
|
|
|
|
{
|
2002-05-03 03:29:09 -04:00
|
|
|
final Boolean b = (Boolean) getProperty(id);
|
2002-07-17 12:23:22 -04:00
|
|
|
if (b != null)
|
2002-05-03 03:29:09 -04:00
|
|
|
return b.booleanValue();
|
2002-07-17 12:23:22 -04:00
|
|
|
else
|
2002-05-03 03:29:09 -04:00
|
|
|
return false;
|
2002-05-11 10:47:24 -04:00
|
|
|
}
|
2002-05-03 03:29:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-02 15:02:28 -04:00
|
|
|
/**
|
|
|
|
* <p>This member is <code>true</code> if the last call to {@link
|
|
|
|
* #getPropertyIntValue} or {@link #getProperty} tried to access a
|
|
|
|
* property that was not available, else <code>false</code>.</p>
|
|
|
|
*/
|
2002-02-13 23:00:59 -05:00
|
|
|
private boolean wasNull;
|
|
|
|
|
2002-05-11 10:47:24 -04:00
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Checks whether the property which the last call to {@link
|
|
|
|
* #getPropertyIntValue} or {@link #getProperty} tried to access
|
|
|
|
* was available or not. This information might be important for
|
|
|
|
* callers of {@link #getPropertyIntValue} since the latter
|
|
|
|
* returns 0 if the property does not exist. Using {@link
|
|
|
|
* #wasNull} the caller can distiguish this case from a property's
|
|
|
|
* real value of 0.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return <code>true</code> if the last call to {@link
|
|
|
|
* #getPropertyIntValue} or {@link #getProperty} tried to access a
|
|
|
|
* property that was not available, else <code>false</code>.
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2002-07-17 12:23:22 -04:00
|
|
|
public boolean wasNull()
|
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
return wasNull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2002-07-17 12:23:22 -04:00
|
|
|
* <p>Returns the PID string associated with a property ID. The ID
|
|
|
|
* is first looked up in the {@link Section}'s private
|
|
|
|
* dictionary. If it is not found there, the method calls {@link
|
|
|
|
* SectionIDMap#getPIDString}.</p>
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @param pid The property ID
|
2002-05-11 10:47:24 -04:00
|
|
|
*
|
2002-07-17 12:23:22 -04:00
|
|
|
* @return The property ID's string value
|
2002-02-13 23:00:59 -05:00
|
|
|
*/
|
2003-09-01 14:53:28 -04:00
|
|
|
public String getPIDString(final long pid)
|
2002-07-17 12:23:22 -04:00
|
|
|
{
|
2002-02-13 23:00:59 -05:00
|
|
|
String s = null;
|
2002-07-17 12:23:22 -04:00
|
|
|
if (dictionary != null)
|
2003-09-01 14:53:28 -04:00
|
|
|
s = (String) dictionary.get(new Long(pid));
|
2002-07-17 12:23:22 -04:00
|
|
|
if (s == null)
|
2002-05-11 10:47:24 -04:00
|
|
|
s = SectionIDMap.getPIDString(getFormatID().getBytes(), pid);
|
2002-07-17 12:23:22 -04:00
|
|
|
if (s == null)
|
2002-02-13 23:00:59 -05:00
|
|
|
s = SectionIDMap.UNDEFINED;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2003-08-03 16:16:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>Checks whether this section is equal to another object.</p>
|
|
|
|
*
|
2003-08-30 05:13:53 -04:00
|
|
|
* @param o The object to compare this section with
|
2003-08-03 16:16:46 -04:00
|
|
|
* @return <code>true</code> if the objects are equal, <code>false</code> if
|
|
|
|
* not
|
|
|
|
*/
|
|
|
|
public boolean equals(final Object o)
|
|
|
|
{
|
|
|
|
if (o == null || !(o instanceof Section))
|
|
|
|
return false;
|
|
|
|
final Section s = (Section) o;
|
|
|
|
if (!s.getFormatID().equals(getFormatID()))
|
|
|
|
return false;
|
|
|
|
if (s.getPropertyCount() != getPropertyCount())
|
|
|
|
return false;
|
2003-09-20 11:43:08 -04:00
|
|
|
|
|
|
|
/* Compare all properties except 0 and 1 as they must be handled
|
|
|
|
* specially. */
|
|
|
|
Property[] pa1 = new Property[getProperties().length];
|
|
|
|
Property[] pa2 = new Property[s.getProperties().length];
|
|
|
|
System.arraycopy(getProperties(), 0, pa1, 0, pa1.length);
|
|
|
|
System.arraycopy(s.getProperties(), 0, pa2, 0, pa2.length);
|
|
|
|
|
|
|
|
/* Extract properties 0 and 1 and remove them from the copy of the
|
|
|
|
* arrays. */
|
|
|
|
Property p10 = null;
|
|
|
|
Property p20 = null;
|
|
|
|
for (int i = 0; i < pa1.length; i++)
|
|
|
|
{
|
|
|
|
final long id = pa1[i].getID();
|
|
|
|
if (id == 0)
|
|
|
|
{
|
|
|
|
p10 = pa1[i];
|
|
|
|
pa1 = remove(pa1, i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (id == 1)
|
|
|
|
{
|
2003-10-23 16:44:24 -04:00
|
|
|
// p11 = pa1[i];
|
2003-09-20 11:43:08 -04:00
|
|
|
pa1 = remove(pa1, i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < pa2.length; i++)
|
|
|
|
{
|
|
|
|
final long id = pa2[i].getID();
|
|
|
|
if (id == 0)
|
|
|
|
{
|
|
|
|
p20 = pa2[i];
|
|
|
|
pa2 = remove(pa2, i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (id == 1)
|
|
|
|
{
|
2003-10-23 16:44:24 -04:00
|
|
|
// p21 = pa2[i];
|
2003-09-20 11:43:08 -04:00
|
|
|
pa2 = remove(pa2, i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean dictionaryEqual = true;
|
|
|
|
if (p10 != null && p20 != null)
|
|
|
|
dictionaryEqual = p10.getValue().equals(p20.getValue());
|
|
|
|
else if (p10 != null || p20 != null)
|
|
|
|
dictionaryEqual = false;
|
|
|
|
if (!dictionaryEqual)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return Util.equals(pa1, pa2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>Removes a field from a property array. The resulting array is
|
|
|
|
* compactified and returned.</p>
|
|
|
|
*/
|
|
|
|
private Property[] remove(final Property[] pa, final int i)
|
|
|
|
{
|
|
|
|
final Property[] h = new Property[pa.length - 1];
|
|
|
|
if (i > 0)
|
|
|
|
System.arraycopy(pa, 0, h, 0, i);
|
|
|
|
System.arraycopy(pa, i + 1, h, i, h.length - i);
|
|
|
|
return h;
|
2003-08-03 16:16:46 -04:00
|
|
|
}
|
|
|
|
|
2003-08-30 05:13:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Object#hashCode()
|
|
|
|
*/
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
long hashCode = 0;
|
|
|
|
hashCode += getFormatID().hashCode();
|
|
|
|
final Property[] pa = getProperties();
|
|
|
|
for (int i = 0; i < pa.length; i++)
|
|
|
|
hashCode += pa[i].hashCode();
|
|
|
|
final int returnHashCode = (int) (hashCode & 0x0ffffffffL);
|
|
|
|
return returnHashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Object#toString()
|
|
|
|
*/
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
final StringBuffer b = new StringBuffer();
|
|
|
|
final Property[] pa = getProperties();
|
|
|
|
b.append(getClass().getName());
|
|
|
|
b.append('[');
|
|
|
|
b.append("formatID: ");
|
|
|
|
b.append(getFormatID());
|
|
|
|
b.append(", offset: ");
|
|
|
|
b.append(getOffset());
|
|
|
|
b.append(", propertyCount: ");
|
|
|
|
b.append(getPropertyCount());
|
|
|
|
b.append(", size: ");
|
|
|
|
b.append(getSize());
|
|
|
|
b.append(", properties: [\n");
|
|
|
|
for (int i = 0; i < pa.length; i++)
|
|
|
|
{
|
|
|
|
b.append(pa[i].toString());
|
|
|
|
b.append(",\n");
|
|
|
|
}
|
|
|
|
b.append(']');
|
|
|
|
b.append(']');
|
|
|
|
return b.toString();
|
|
|
|
}
|
|
|
|
|
2003-09-18 14:56:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>Gets the section's dictionary. A dictionary allows an application to
|
|
|
|
* use human-readable property names instead of numeric property IDs. It
|
|
|
|
* contains mappings from property IDs to their associated string
|
|
|
|
* values. The dictionary is stored as the property with ID 0. The codepage
|
|
|
|
* for the strings in the dictionary is defined by property with ID 1.</p>
|
|
|
|
*
|
|
|
|
* @return the dictionary or <code>null</code> if the section does not have
|
|
|
|
* a dictionary.
|
|
|
|
*/
|
|
|
|
public Map getDictionary()
|
|
|
|
{
|
|
|
|
return dictionary;
|
|
|
|
}
|
|
|
|
|
2003-12-02 12:46:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>Gets the section's codepage, if any.</p>
|
|
|
|
*
|
|
|
|
* @return The section's codepage if one is defined, else -1.
|
|
|
|
*/
|
|
|
|
public int getCodepage()
|
|
|
|
{
|
|
|
|
final Integer codepage =
|
|
|
|
(Integer) getProperty(PropertyIDMap.PID_CODEPAGE);
|
|
|
|
return codepage != null ? codepage.intValue() : -1;
|
|
|
|
}
|
|
|
|
|
2002-02-13 23:00:59 -05:00
|
|
|
}
|