Improve HSMF MAPIMessage access to the HTML and RTF versions of the message body (where available)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1087782 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-04-01 16:01:29 +00:00
parent 002d0a5995
commit 9f40e3df71
4 changed files with 61 additions and 9 deletions

View File

@ -34,6 +34,7 @@
<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>

View File

@ -29,11 +29,14 @@ import java.util.regex.Matcher;
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;
@ -184,7 +187,36 @@ public class MAPIMessage extends POIDocument {
* @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);
}
}
/**

View File

@ -38,7 +38,10 @@ public final class Chunks implements ChunkGroup {
/** 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 */
@ -119,9 +122,16 @@ public final class Chunks implements ChunkGroup {
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

View File

@ -120,7 +120,6 @@ private MAPIMessage mapiMessage;
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 ""
*
@ -130,7 +129,17 @@ private MAPIMessage mapiMessage;
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"));
}
}