]> source.dussan.org Git - poi.git/commitdiff
Make it possible to return null on missing chunks, rather than the exception
authorNick Burch <nick@apache.org>
Mon, 11 Jan 2010 12:19:42 +0000 (12:19 +0000)
committerNick Burch <nick@apache.org>
Mon, 11 Jan 2010 12:19:42 +0000 (12:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@897847 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java
src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java

index ea7335c5b17cf878e0be84943bd1d19bd8827b0c..911119b19e875a3e43b9a1632a15ac71a28a9ee7 100644 (file)
@@ -50,6 +50,8 @@ public class MAPIMessage extends POIDocument {
        private NameIdChunks nameIdChunks;
        private RecipientChunks recipientChunks;
        private AttachmentChunks[] attachmentChunks;
+       
+       private boolean returnNullOnMissingChunk = false;
 
        /**
         * Constructor for creating new files.
@@ -125,7 +127,11 @@ public class MAPIMessage extends POIDocument {
         */
        public String getStringFromChunk(StringChunk chunk) throws ChunkNotFoundException {
           if(chunk == null) {
-             throw new ChunkNotFoundException();
+             if(returnNullOnMissingChunk) {
+                return null;
+             } else {
+                throw new ChunkNotFoundException();
+             }
           }
           return chunk.getValue();
        }
@@ -230,6 +236,8 @@ public class MAPIMessage extends POIDocument {
           if(mainChunks.submissionChunk != null) {
              return mainChunks.submissionChunk.getAcceptedAtTime();
           }
+          if(returnNullOnMissingChunk)
+             return null;
           throw new ChunkNotFoundException();
        }
 
@@ -268,4 +276,25 @@ public class MAPIMessage extends POIDocument {
    public void write(OutputStream out) throws IOException {
       throw new UnsupportedOperationException("Writing isn't yet supported for HSMF, sorry");
    }
+
+
+   /**
+    * Will you get a null on a missing chunk, or a 
+    *  {@link ChunkNotFoundException} (default is the
+    *  exception).
+    */
+   public boolean isReturnNullOnMissingChunk() {
+      return returnNullOnMissingChunk;
+   }
+
+   /**
+    * Sets whether on asking for a missing chunk,
+    *  you get back null or a {@link ChunkNotFoundException}
+    *  (default is the exception). 
+    */
+   public void setReturnNullOnMissingChunk(boolean returnNullOnMissingChunk) {
+      this.returnNullOnMissingChunk = returnNullOnMissingChunk;
+   }
+   
+   
 }
index 793d9ade0af96077a24323ad2a71b0a8167bea03..008a4edba469f1770bc27b8f7755b9b61e10a0aa 100644 (file)
@@ -22,6 +22,7 @@ import java.io.IOException;
 import junit.framework.TestCase;
 
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
 
 /**
  * Tests to verify that we can perform basic opperations on 
@@ -74,4 +75,31 @@ public final class TestBasics extends TestCase {
       assertEquals(0, outlook30.getAttachmentFiles().length);
       assertEquals(2, attachments.getAttachmentFiles().length);
        }
+       
+       /**
+        * Test missing chunks
+        */
+       public void testMissingChunks() throws Exception {
+          assertEquals(false, attachments.isReturnNullOnMissingChunk());
+
+          try {
+             attachments.getMessageDate();
+             fail();
+          } catch(ChunkNotFoundException e) {
+             // Good
+          }
+          
+          attachments.setReturnNullOnMissingChunk(true);
+          
+          assertEquals(null, attachments.getMessageDate());
+          
+      attachments.setReturnNullOnMissingChunk(false);
+      
+      try {
+         attachments.getMessageDate();
+         fail();
+      } catch(ChunkNotFoundException e) {
+         // Good
+      }
+       }
 }