From 4cb27e50954e254cf5d27d1c4fa1a0a680af8a8e Mon Sep 17 00:00:00 2001
From: Nick Burch
Date: Wed, 13 Jan 2010 15:42:53 +0000
Subject: [PATCH] Remove some generics warnings from the HPSF custom properties
and sections parts
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@898804 13f79535-47bb-0310-9956-ffa450edef68
---
.../org/apache/poi/hpsf/CustomProperties.java | 83 ++++++++++++++-----
.../poi/hpsf/DocumentSummaryInformation.java | 10 +--
.../org/apache/poi/hpsf/MutableSection.java | 38 +++------
src/java/org/apache/poi/hpsf/Section.java | 20 ++---
.../extractor/HPSFPropertiesExtractor.java | 4 +-
.../poi/hpsf/basic/TestWriteWellKnown.java | 17 ++++
6 files changed, 110 insertions(+), 62 deletions(-)
diff --git a/src/java/org/apache/poi/hpsf/CustomProperties.java b/src/java/org/apache/poi/hpsf/CustomProperties.java
index a3242057b..83858eb73 100644
--- a/src/java/org/apache/poi/hpsf/CustomProperties.java
+++ b/src/java/org/apache/poi/hpsf/CustomProperties.java
@@ -49,21 +49,26 @@ import org.apache.poi.hpsf.wellknown.PropertyIDMap;
*
This class is not thread-safe; concurrent access to instances of this
* class must be synchronized.
*
+ *
While this class is roughly HashMap, that's the
+ * internal representation. To external calls, it should appear as
+ * HashMap mapping between Names and Custom Property Values.
+ *
* @author Rainer Klute <klute@rainer-klute.de>
*/
-public class CustomProperties extends HashMap
+@SuppressWarnings("serial")
+public class CustomProperties extends HashMap
*/
- private List preprops;
+ private List preprops;
@@ -74,7 +74,7 @@ public class MutableSection extends Section
dirty = true;
formatID = null;
offset = -1;
- preprops = new LinkedList();
+ preprops = new LinkedList();
}
@@ -145,7 +145,7 @@ public class MutableSection extends Section
public void setProperties(final Property[] properties)
{
this.properties = properties;
- preprops = new LinkedList();
+ preprops = new LinkedList();
for (int i = 0; i < properties.length; i++)
preprops.add(properties[i]);
dirty = true;
@@ -276,8 +276,8 @@ public class MutableSection extends Section
*/
public void removeProperty(final long id)
{
- for (final Iterator i = preprops.iterator(); i.hasNext();)
- if (((Property) i.next()).getID() == id)
+ for (final Iterator i = preprops.iterator(); i.hasNext();)
+ if (i.next().getID() == id)
{
i.remove();
break;
@@ -423,12 +423,10 @@ public class MutableSection extends Section
}
/* Sort the property list by their property IDs: */
- Collections.sort(preprops, new Comparator()
+ Collections.sort(preprops, new Comparator()
{
- public int compare(final Object o1, final Object o2)
+ public int compare(final Property p1, final Property p2)
{
- final Property p1 = (Property) o1;
- final Property p2 = (Property) o2;
if (p1.getID() < p2.getID())
return -1;
else if (p1.getID() == p2.getID())
@@ -440,7 +438,7 @@ public class MutableSection extends Section
/* Write the properties and the property list into their respective
* streams: */
- for (final ListIterator i = preprops.listIterator(); i.hasNext();)
+ for (final ListIterator i = preprops.listIterator(); i.hasNext();)
{
final MutableProperty p = (MutableProperty) i.next();
final long id = p.getID();
@@ -502,14 +500,14 @@ public class MutableSection extends Section
* @exception IOException if an I/O exception occurs.
*/
private static int writeDictionary(final OutputStream out,
- final Map dictionary, final int codepage)
+ final Map dictionary, final int codepage)
throws IOException
{
int length = TypeWriter.writeUIntToStream(out, dictionary.size());
- for (final Iterator i = dictionary.keySet().iterator(); i.hasNext();)
+ for (final Iterator i = dictionary.keySet().iterator(); i.hasNext();)
{
- final Long key = (Long) i.next();
- final String value = (String) dictionary.get(key);
+ final Long key = i.next();
+ final String value = dictionary.get(key);
if (codepage == Constants.CP_UNICODE)
{
@@ -617,21 +615,11 @@ public class MutableSection extends Section
*
* @see Section#getDictionary()
*/
- public void setDictionary(final Map dictionary)
+ public void setDictionary(final Map dictionary)
throws IllegalPropertySetDataException
{
if (dictionary != null)
{
- for (final Iterator i = dictionary.keySet().iterator();
- i.hasNext();)
- if (!(i.next() instanceof Long))
- throw new IllegalPropertySetDataException
- ("Dictionary keys must be of type Long.");
- for (final Iterator i = dictionary.values().iterator();
- i.hasNext();)
- if (!(i.next() instanceof String))
- throw new IllegalPropertySetDataException
- ("Dictionary values must be of type String.");
this.dictionary = dictionary;
/* Set the dictionary property (ID 0). Please note that the second
diff --git a/src/java/org/apache/poi/hpsf/Section.java b/src/java/org/apache/poi/hpsf/Section.java
index 917929a64..2c49f0223 100644
--- a/src/java/org/apache/poi/hpsf/Section.java
+++ b/src/java/org/apache/poi/hpsf/Section.java
@@ -42,7 +42,7 @@ public class Section
*
Maps property IDs to section-private PID strings. These
* strings can be found in the property with ID 0.
@@ -244,10 +244,10 @@ public class Section
/* Look for the codepage. */
int codepage = -1;
- for (final Iterator i = propertyList.iterator();
+ for (final Iterator i = propertyList.iterator();
codepage == -1 && i.hasNext();)
{
- ple = (PropertyListEntry) i.next();
+ ple = i.next();
/* Read the codepage if the property ID is 1. */
if (ple.id == PropertyIDMap.PID_CODEPAGE)
@@ -271,9 +271,9 @@ public class Section
/* Pass 2: Read all properties - including the codepage property,
* if available. */
int i1 = 0;
- for (final Iterator i = propertyList.iterator(); i.hasNext();)
+ for (final Iterator i = propertyList.iterator(); i.hasNext();)
{
- ple = (PropertyListEntry) i.next();
+ ple = i.next();
Property p = new Property(ple.id, src,
this.offset + ple.offset,
ple.length, codepage);
@@ -294,7 +294,7 @@ public class Section
*
Represents an entry in the property list and holds a property's ID and
* its offset from the section's beginning.
*/
- class PropertyListEntry implements Comparable
+ class PropertyListEntry implements Comparable
{
int id;
int offset;
@@ -307,11 +307,9 @@ public class Section
*
* @see Comparable#compareTo(java.lang.Object)
*/
- public int compareTo(final Object o)
+ public int compareTo(final PropertyListEntry o)
{
- if (!(o instanceof PropertyListEntry))
- throw new ClassCastException(o.toString());
- final int otherOffset = ((PropertyListEntry) o).offset;
+ final int otherOffset = o.offset;
if (offset < otherOffset)
return -1;
else if (offset == otherOffset)
@@ -630,7 +628,7 @@ public class Section
* @return the dictionary or null if the section does not have
* a dictionary.
*/
- public Map getDictionary()
+ public Map getDictionary()
{
return dictionary;
}
diff --git a/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java b/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java
index 450e8d3b4..14bb16887 100644
--- a/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java
+++ b/src/java/org/apache/poi/hpsf/extractor/HPSFPropertiesExtractor.java
@@ -57,9 +57,9 @@ public class HPSFPropertiesExtractor extends POITextExtractor {
// Now custom ones
CustomProperties cps = dsi == null ? null : dsi.getCustomProperties();
if(cps != null) {
- Iterator keys = cps.keySet().iterator();
+ Iterator keys = cps.nameSet().iterator();
while(keys.hasNext()) {
- String key = (String)keys.next();
+ String key = keys.next();
String val = getPropertyValueText( cps.get(key) );
text.append(key + " = " + val + "\n");
}
diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java b/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java
index ad6e1237c..8c95fdb7b 100644
--- a/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java
+++ b/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java
@@ -348,8 +348,25 @@ public class TestWriteWellKnown extends TestCase {
customProperties.put("min_Long", MIN_LONG);
customProperties.put("max_Double", MAX_DOUBLE);
customProperties.put("min_Double", MIN_DOUBLE);
+
+ // Check the keys went in
+ assertTrue(customProperties.containsKey("Schl\u00fcssel \u00e4"));
+ assertTrue(customProperties.containsKey("Boolean"));
+
+ // Check the values went in
+ assertEquals("Wert \u00e4", customProperties.get("Schl\u00fcssel \u00e4"));
+ assertEquals(Boolean.TRUE, customProperties.get("Boolean"));
+ assertTrue(customProperties.containsValue(Boolean.TRUE));
+ assertTrue(customProperties.containsValue("Wert \u00e4"));
+
+ // Check that things that aren't in aren't in
+ assertFalse(customProperties.containsKey("False Boolean"));
+ assertFalse(customProperties.containsValue(Boolean.FALSE));
+
+ // Save as our custom properties
dsi.setCustomProperties(customProperties);
+
/* Write the summary information stream and the document summary
* information stream to the POI filesystem. */
si.write(dir, siEntry.getName());