diff options
author | Nick Burch <nick@apache.org> | 2013-07-03 11:24:01 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2013-07-03 11:24:01 +0000 |
commit | b3608a4cfaf292d397033c566931c4fd1f8752d3 (patch) | |
tree | fb87f705ba816b79f057d0111d2615d7706c2468 /src/java | |
parent | 6d772a851e4f527f777fb551a95f630ef24043aa (diff) | |
download | poi-b3608a4cfaf292d397033c566931c4fd1f8752d3.tar.gz poi-b3608a4cfaf292d397033c566931c4fd1f8752d3.zip |
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
Diffstat (limited to 'src/java')
3 files changed, 50 insertions, 30 deletions
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; * <p>Convenience class representing a DocumentSummary Information stream in a * Microsoft Office document.</p> * - * @author Rainer Klute <a - * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a> - * @author Drew Varner (Drew.Varner closeTo sc.edu) - * @author robert_flaherty@hyperion.com * @see SummaryInformation */ public class DocumentSummaryInformation extends SpecialPropertySet { - /** * <p>The document name a document summary information stream * usually has in a POIFS filesystem.</p> @@ -67,8 +62,7 @@ public class DocumentSummaryInformation extends SpecialPropertySet ("Not a " + getClass().getName()); } - - + /** * <p>Returns the category (or <code>null</code>).</p> * @@ -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 } - /** * <p>Returns <code>true</code> if the custom links are dirty.</p> <p> * @@ -565,7 +558,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } - /** * <p>Gets the custom properties.</p> * @@ -629,8 +621,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } } - - /** * <p>Creates section 2 if it is not already present.</p> * @@ -645,8 +635,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } } - - /** * <p>Removes the custom properties.</p> */ @@ -659,7 +647,6 @@ public class DocumentSummaryInformation extends SpecialPropertySet } - /** * <p>Throws an {@link UnsupportedOperationException} with a message text * telling which functionality is not yet implemented.</p> 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; /** * <p>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<Section> 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; * <p>Convenience class representing a Summary Information stream in a * Microsoft Office document.</p> * - * @author Rainer Klute <a - * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a> * @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); } |