<changes>
<release version="3.8-beta3" date="2011-??-??">
+ <action dev="poi-developers" type="add">Improve HSMF MAPIMessage access to the HTML and RTF versions of the message body (where available)</action>
<action dev="poi-developers" type="add">Add new method to HSMF of MAPIMessage.has7BitEncodingStrings() to make it easier to decide when encoding guessing is needed</action>
<action dev="poi-developers" type="fix">OutlookTextExtractor now requests 7 bit encoding guessing</action>
<action dev="poi-developers" type="add">Improve HSMF encoding guessing for 7 bit fields in MAPIMessage</action>
import java.util.regex.Pattern;
import org.apache.poi.POIDocument;
+import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
import org.apache.poi.hsmf.datatypes.AttachmentChunks.AttachmentChunksSorter;
+import org.apache.poi.hsmf.datatypes.ByteChunk;
import org.apache.poi.hsmf.datatypes.Chunk;
import org.apache.poi.hsmf.datatypes.ChunkGroup;
import org.apache.poi.hsmf.datatypes.Chunks;
+import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.hsmf.datatypes.NameIdChunks;
import org.apache.poi.hsmf.datatypes.RecipientChunks;
import org.apache.poi.hsmf.datatypes.Types;
* @throws ChunkNotFoundException
*/
public String getHmtlBody() throws ChunkNotFoundException {
- return getStringFromChunk(mainChunks.htmlBodyChunk);
+ if(mainChunks.htmlBodyChunkBinary != null) {
+ return mainChunks.htmlBodyChunkBinary.getAs7bitString();
+ }
+ return getStringFromChunk(mainChunks.htmlBodyChunkString);
+ }
+
+ /**
+ * Gets the RTF Rich Message body of this Outlook Message, if this email
+ * contains a RTF (rich) version.
+ * @return The string representation of the 'RTF' version of the body, if available.
+ * @throws ChunkNotFoundException
+ */
+ public String getRtfBody() throws ChunkNotFoundException {
+ ByteChunk chunk = mainChunks.rtfBodyChunk;
+ if(chunk == null) {
+ if(returnNullOnMissingChunk) {
+ return null;
+ } else {
+ throw new ChunkNotFoundException();
+ }
+ }
+
+ try {
+ MAPIRtfAttribute rtf = new MAPIRtfAttribute(
+ MAPIProperty.RTF_COMPRESSED, Types.BINARY, chunk.getValue()
+ );
+ return rtf.getDataString();
+ } catch(IOException e) {
+ throw new RuntimeException("Shouldn't happen", e);
+ }
}
/**
/** BODY Chunk, for plain/text messages */
public StringChunk textBodyChunk;
/** BODY Html Chunk, for html messages */
- public StringChunk htmlBodyChunk;
+ public StringChunk htmlBodyChunkString;
+ public ByteChunk htmlBodyChunkBinary;
+ /** BODY Rtf Chunk, for Rtf (Rich) messages */
+ public ByteChunk rtfBodyChunk;
/** Subject link chunk, in plain/text */
public StringChunk subjectChunk;
/** Value that is in the TO field (not actually the addresses as they are stored in recip directory nodes */
else if(chunk.getChunkId() == MAPIProperty.BODY.id) {
textBodyChunk = (StringChunk)chunk;
}
- else if(chunk.getChunkId() == MAPIProperty.BODY_HTML.id &&
- chunk instanceof StringChunk) {
- htmlBodyChunk = (StringChunk)chunk;
+ else if(chunk.getChunkId() == MAPIProperty.BODY_HTML.id) {
+ if(chunk instanceof StringChunk) {
+ htmlBodyChunkString = (StringChunk)chunk;
+ }
+ if(chunk instanceof ByteChunk) {
+ htmlBodyChunkBinary = (ByteChunk)chunk;
+ }
+ }
+ else if(chunk.getChunkId() == MAPIProperty.RTF_COMPRESSED.id) {
+ rtfBodyChunk = (ByteChunk)chunk;
}
// And add to the main list
TestCase.assertEquals("IN-SPIRE servers going down for a bit, back up around 8am", obtained);
}
-
/**
* Check if we can read the subject line of the blank message, we expect ""
*
String obtained = mapiMessage.getMessageClass();
TestCase.assertEquals("IPM.Note", obtained);
}
-
-
-
+
+ /**
+ * Ensure we can get the HTML and RTF versions
+ */
+ public void testReadBodyContents() throws Exception {
+ String html = mapiMessage.getHmtlBody();
+ String rtf = mapiMessage.getRtfBody();
+ assertNotNull(html);
+ assertNotNull(rtf);
+
+ assertTrue("Wrong text:\n" + html, html.startsWith("<!DOCTYPE"));
+ assertTrue("Wrong text:\n" + rtf, rtf.startsWith("{\\rtf1"));
+ }
}