From: Nick Burch Date: Wed, 3 Jul 2013 11:24:01 +0000 (+0000) Subject: Fix bug #55191 - Avoid a ClassCastException if a HPSF string property isn't directly... X-Git-Tag: REL_3_10_BETA2~48 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b3608a4cfaf292d397033c566931c4fd1f8752d3;p=poi.git Fix bug #55191 - Avoid a ClassCastException if a HPSF string property isn't directly stored as a string git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1499326 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java b/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java index fb27487319..4a8303098a 100644 --- a/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java +++ b/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java @@ -28,15 +28,10 @@ import org.apache.poi.util.CodePageUtil; *

Convenience class representing a DocumentSummary Information stream in a * Microsoft Office document.

* - * @author Rainer Klute <klute@rainer-klute.de> - * @author Drew Varner (Drew.Varner closeTo sc.edu) - * @author robert_flaherty@hyperion.com * @see SummaryInformation */ public class DocumentSummaryInformation extends SpecialPropertySet { - /** *

The document name a document summary information stream * usually has in a POIFS filesystem.

@@ -67,8 +62,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet ("Not a " + getClass().getName()); } - - + /** *

Returns the category (or null).

* @@ -76,7 +70,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet */ public String getCategory() { - return (String) getProperty(PropertyIDMap.PID_CATEGORY); + return getPropertyStringValue(PropertyIDMap.PID_CATEGORY); } /** @@ -109,7 +103,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet */ public String getPresentationFormat() { - return (String) getProperty(PropertyIDMap.PID_PRESFORMAT); + return getPropertyStringValue(PropertyIDMap.PID_PRESFORMAT); } /** @@ -477,7 +471,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet */ public String getManager() { - return (String) getProperty(PropertyIDMap.PID_MANAGER); + return getPropertyStringValue(PropertyIDMap.PID_MANAGER); } /** @@ -509,7 +503,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet */ public String getCompany() { - return (String) getProperty(PropertyIDMap.PID_COMPANY); + return getPropertyStringValue(PropertyIDMap.PID_COMPANY); } /** @@ -533,7 +527,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } - /** *

Returns true if the custom links are dirty.

* @@ -565,7 +558,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } - /** *

Gets the custom properties.

* @@ -629,8 +621,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } } - - /** *

Creates section 2 if it is not already present.

* @@ -645,8 +635,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } } - - /** *

Removes the custom properties.

*/ @@ -659,7 +647,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } - /** *

Throws an {@link UnsupportedOperationException} with a message text * telling which functionality is not yet implemented.

diff --git a/src/java/org/apache/poi/hpsf/SpecialPropertySet.java b/src/java/org/apache/poi/hpsf/SpecialPropertySet.java index 2d04ef6d8e..c40641aa98 100644 --- a/src/java/org/apache/poi/hpsf/SpecialPropertySet.java +++ b/src/java/org/apache/poi/hpsf/SpecialPropertySet.java @@ -24,6 +24,7 @@ import java.util.List; import org.apache.poi.hpsf.wellknown.PropertyIDMap; import org.apache.poi.poifs.filesystem.DirectoryEntry; +import org.apache.poi.util.LittleEndian; /** *

Abstract superclass for the convenience classes {@link @@ -149,7 +150,7 @@ public abstract class SpecialPropertySet extends MutablePropertySet /** * @see PropertySet#getSections */ - public List getSections() + public List

getSections() { return delegate.getSections(); } @@ -324,6 +325,40 @@ public abstract class SpecialPropertySet extends MutablePropertySet } + + /** + * Fetches the property with the given ID, then does its + * best to return it as a String + * @return The property as a String, or null if unavailable + */ + protected String getPropertyStringValue(final int propertyId) { + Object o = getProperty(propertyId); + + // Normal cases + if (o == null) return null; + if (o instanceof String) return (String)o; + + // Do our best with some edge cases + if (o instanceof byte[]) { + byte[] b = (byte[])o; + if (b.length == 0) { + return ""; + } + if (b.length == 1) { + return Byte.toString(b[0]); + } + if (b.length == 2) { + return Integer.toString( LittleEndian.getUShort(b) ); + } + if (b.length == 4) { + return Long.toString( LittleEndian.getUInt(b) ); + } + // Maybe it's a string? who knows! + return new String(b); + } + return o.toString(); + } + /** * @see org.apache.poi.hpsf.PropertySet#hashCode() diff --git a/src/java/org/apache/poi/hpsf/SummaryInformation.java b/src/java/org/apache/poi/hpsf/SummaryInformation.java index 31eaa60574..d25aa58e27 100644 --- a/src/java/org/apache/poi/hpsf/SummaryInformation.java +++ b/src/java/org/apache/poi/hpsf/SummaryInformation.java @@ -25,8 +25,6 @@ import org.apache.poi.hpsf.wellknown.PropertyIDMap; *

Convenience class representing a Summary Information stream in a * Microsoft Office document.

* - * @author Rainer Klute <klute@rainer-klute.de> * @see DocumentSummaryInformation */ public final class SummaryInformation extends SpecialPropertySet { @@ -69,7 +67,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getTitle() { - return (String) getProperty(PropertyIDMap.PID_TITLE); + return getPropertyStringValue(PropertyIDMap.PID_TITLE); } @@ -105,7 +103,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getSubject() { - return (String) getProperty(PropertyIDMap.PID_SUBJECT); + return getPropertyStringValue(PropertyIDMap.PID_SUBJECT); } @@ -141,7 +139,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getAuthor() { - return (String) getProperty(PropertyIDMap.PID_AUTHOR); + return getPropertyStringValue(PropertyIDMap.PID_AUTHOR); } @@ -177,7 +175,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getKeywords() { - return (String) getProperty(PropertyIDMap.PID_KEYWORDS); + return getPropertyStringValue(PropertyIDMap.PID_KEYWORDS); } @@ -213,7 +211,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getComments() { - return (String) getProperty(PropertyIDMap.PID_COMMENTS); + return getPropertyStringValue(PropertyIDMap.PID_COMMENTS); } @@ -249,7 +247,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getTemplate() { - return (String) getProperty(PropertyIDMap.PID_TEMPLATE); + return getPropertyStringValue(PropertyIDMap.PID_TEMPLATE); } @@ -285,7 +283,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getLastAuthor() { - return (String) getProperty(PropertyIDMap.PID_LASTAUTHOR); + return getPropertyStringValue(PropertyIDMap.PID_LASTAUTHOR); } @@ -321,7 +319,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getRevNumber() { - return (String) getProperty(PropertyIDMap.PID_REVNUMBER); + return getPropertyStringValue(PropertyIDMap.PID_REVNUMBER); } @@ -668,7 +666,7 @@ public final class SummaryInformation extends SpecialPropertySet { */ public String getApplicationName() { - return (String) getProperty(PropertyIDMap.PID_APPNAME); + return getPropertyStringValue(PropertyIDMap.PID_APPNAME); }