Author: nick
Date: Mon Jan 11 12:19:42 2010
New Revision: 897847

URL: http://svn.apache.org/viewvc?rev=897847&view=rev
Log:
Make it possible to return null on missing chunks, rather than the exception

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

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java?rev=897847&r1=897846&r2=897847&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java Mon Jan 
11 12:19:42 2010
@@ -50,6 +50,8 @@
        private NameIdChunks nameIdChunks;
        private RecipientChunks recipientChunks;
        private AttachmentChunks[] attachmentChunks;
+       
+       private boolean returnNullOnMissingChunk = false;
 
        /**
         * Constructor for creating new files.
@@ -125,7 +127,11 @@
         */
        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 @@
           if(mainChunks.submissionChunk != null) {
              return mainChunks.submissionChunk.getAcceptedAtTime();
           }
+          if(returnNullOnMissingChunk)
+             return null;
           throw new ChunkNotFoundException();
        }
 
@@ -268,4 +276,25 @@
    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;
+   }
+   
+   
 }

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java?rev=897847&r1=897846&r2=897847&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java 
(original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java Mon 
Jan 11 12:19:42 2010
@@ -22,6 +22,7 @@
 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 @@
       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
+      }
+       }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to