You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2013/07/03 13:24:02 UTC

svn commit: r1499326 - in /poi/trunk/src/java/org/apache/poi/hpsf: DocumentSummaryInformation.java SpecialPropertySet.java SummaryInformation.java

Author: nick
Date: Wed Jul  3 11:24:01 2013
New Revision: 1499326

URL: http://svn.apache.org/r1499326
Log:
Fix bug #55191 - Avoid a ClassCastException if a HPSF string property isn't directly stored as a string

Modified:
    poi/trunk/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java
    poi/trunk/src/java/org/apache/poi/hpsf/SpecialPropertySet.java
    poi/trunk/src/java/org/apache/poi/hpsf/SummaryInformation.java

Modified: poi/trunk/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java?rev=1499326&r1=1499325&r2=1499326&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java (original)
+++ poi/trunk/src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java Wed Jul  3 11:24:01 2013
@@ -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">&lt;klute@rainer-klute.de&gt;</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 
                 ("Not a " + getClass().getName());
     }
 
-
-
+    
     /**
      * <p>Returns the category (or <code>null</code>).</p>
      *
@@ -76,7 +70,7 @@ public class DocumentSummaryInformation 
      */
     public String getCategory()
     {
-        return (String) getProperty(PropertyIDMap.PID_CATEGORY);
+        return getPropertyStringValue(PropertyIDMap.PID_CATEGORY);
     }
 
     /**
@@ -109,7 +103,7 @@ public class DocumentSummaryInformation 
      */
     public String getPresentationFormat()
     {
-        return (String) getProperty(PropertyIDMap.PID_PRESFORMAT);
+        return getPropertyStringValue(PropertyIDMap.PID_PRESFORMAT);
     }
 
     /**
@@ -477,7 +471,7 @@ public class DocumentSummaryInformation 
      */
     public String getManager()
     {
-        return (String) getProperty(PropertyIDMap.PID_MANAGER);
+        return getPropertyStringValue(PropertyIDMap.PID_MANAGER);
     }
 
     /**
@@ -509,7 +503,7 @@ public class DocumentSummaryInformation 
      */
     public String getCompany()
     {
-        return (String) getProperty(PropertyIDMap.PID_COMPANY);
+        return getPropertyStringValue(PropertyIDMap.PID_COMPANY);
     }
 
     /**
@@ -533,7 +527,6 @@ public class DocumentSummaryInformation 
     }
 
 
-
     /**
      * <p>Returns <code>true</code> if the custom links are dirty.</p> <p>
      *
@@ -565,7 +558,6 @@ public class DocumentSummaryInformation 
     }
 
 
-
     /**
      * <p>Gets the custom properties.</p>
      *
@@ -629,8 +621,6 @@ public class DocumentSummaryInformation 
         }
     }
 
-
-
     /**
      * <p>Creates section 2 if it is not already present.</p>
      *
@@ -645,8 +635,6 @@ public class DocumentSummaryInformation 
         }
     }
 
-
-
     /**
      * <p>Removes the custom properties.</p>
      */
@@ -659,7 +647,6 @@ public class DocumentSummaryInformation 
     }
 
 
-
     /**
      * <p>Throws an {@link UnsupportedOperationException} with a message text
      * telling which functionality is not yet implemented.</p>

Modified: poi/trunk/src/java/org/apache/poi/hpsf/SpecialPropertySet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hpsf/SpecialPropertySet.java?rev=1499326&r1=1499325&r2=1499326&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hpsf/SpecialPropertySet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hpsf/SpecialPropertySet.java Wed Jul  3 11:24:01 2013
@@ -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
     /**
      * @see PropertySet#getSections
      */
-    public List getSections()
+    public List<Section> getSections()
     {
         return delegate.getSections();
     }
@@ -324,6 +325,40 @@ public abstract class SpecialPropertySet
     }
 
 
+    
+    /**
+     * 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()

Modified: poi/trunk/src/java/org/apache/poi/hpsf/SummaryInformation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hpsf/SummaryInformation.java?rev=1499326&r1=1499325&r2=1499326&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hpsf/SummaryInformation.java (original)
+++ poi/trunk/src/java/org/apache/poi/hpsf/SummaryInformation.java Wed Jul  3 11:24:01 2013
@@ -25,8 +25,6 @@ import org.apache.poi.hpsf.wellknown.Pro
  * <p>Convenience class representing a Summary Information stream in a
  * Microsoft Office document.</p>
  *
- * @author Rainer Klute <a
- *         href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  * @see DocumentSummaryInformation
  */
 public final class SummaryInformation extends SpecialPropertySet {
@@ -69,7 +67,7 @@ public final class SummaryInformation ex
      */
     public String getTitle()
     {
-        return (String) getProperty(PropertyIDMap.PID_TITLE);
+        return getPropertyStringValue(PropertyIDMap.PID_TITLE);
     }
 
 
@@ -105,7 +103,7 @@ public final class SummaryInformation ex
      */
     public String getSubject()
     {
-        return (String) getProperty(PropertyIDMap.PID_SUBJECT);
+        return getPropertyStringValue(PropertyIDMap.PID_SUBJECT);
     }
 
 
@@ -141,7 +139,7 @@ public final class SummaryInformation ex
      */
     public String getAuthor()
     {
-        return (String) getProperty(PropertyIDMap.PID_AUTHOR);
+        return getPropertyStringValue(PropertyIDMap.PID_AUTHOR);
     }
 
 
@@ -177,7 +175,7 @@ public final class SummaryInformation ex
      */
     public String getKeywords()
     {
-        return (String) getProperty(PropertyIDMap.PID_KEYWORDS);
+        return getPropertyStringValue(PropertyIDMap.PID_KEYWORDS);
     }
 
 
@@ -213,7 +211,7 @@ public final class SummaryInformation ex
      */
     public String getComments()
     {
-        return (String) getProperty(PropertyIDMap.PID_COMMENTS);
+        return getPropertyStringValue(PropertyIDMap.PID_COMMENTS);
     }
 
 
@@ -249,7 +247,7 @@ public final class SummaryInformation ex
      */
     public String getTemplate()
     {
-        return (String) getProperty(PropertyIDMap.PID_TEMPLATE);
+        return getPropertyStringValue(PropertyIDMap.PID_TEMPLATE);
     }
 
 
@@ -285,7 +283,7 @@ public final class SummaryInformation ex
      */
     public String getLastAuthor()
     {
-        return (String) getProperty(PropertyIDMap.PID_LASTAUTHOR);
+        return getPropertyStringValue(PropertyIDMap.PID_LASTAUTHOR);
     }
 
 
@@ -321,7 +319,7 @@ public final class SummaryInformation ex
      */
     public String getRevNumber()
     {
-        return (String) getProperty(PropertyIDMap.PID_REVNUMBER);
+        return getPropertyStringValue(PropertyIDMap.PID_REVNUMBER);
     }
 
 
@@ -668,7 +666,7 @@ public final class SummaryInformation ex
      */
     public String getApplicationName()
     {
-        return (String) getProperty(PropertyIDMap.PID_APPNAME);
+        return getPropertyStringValue(PropertyIDMap.PID_APPNAME);
     }
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org