mirror of https://github.com/apache/poi.git
Fix some chunk types, fix the directory descent, fix the Msg2txt example, and start on fixing core tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@897167 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8b89688ddb
commit
4903ecfca1
|
@ -17,16 +17,14 @@
|
||||||
|
|
||||||
package org.apache.poi.hsmf.examples;
|
package org.apache.poi.hsmf.examples;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.poi.hsmf.MAPIMessage;
|
import org.apache.poi.hsmf.MAPIMessage;
|
||||||
|
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
|
||||||
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
|
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,7 +33,6 @@ import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
|
||||||
* attachments.
|
* attachments.
|
||||||
*
|
*
|
||||||
* @author Bruno Girin
|
* @author Bruno Girin
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Msg2txt {
|
public class Msg2txt {
|
||||||
|
|
||||||
|
@ -105,17 +102,13 @@ public class Msg2txt {
|
||||||
} catch (ChunkNotFoundException e) {
|
} catch (ChunkNotFoundException e) {
|
||||||
System.err.println("No message body");
|
System.err.println("No message body");
|
||||||
}
|
}
|
||||||
Map attachmentMap = msg.getAttachmentFiles();
|
|
||||||
if(attachmentMap.size() > 0) {
|
AttachmentChunks[] attachments = msg.getAttachmentFiles();
|
||||||
|
if(attachments.length > 0) {
|
||||||
File d = new File(attDirName);
|
File d = new File(attDirName);
|
||||||
if(d.mkdir()) {
|
if(d.mkdir()) {
|
||||||
for(
|
for(AttachmentChunks attachment : attachments) {
|
||||||
Iterator ii = attachmentMap.entrySet().iterator();
|
processAttachment(attachment, d);
|
||||||
ii.hasNext();
|
|
||||||
) {
|
|
||||||
Map.Entry entry = (Map.Entry)ii.next();
|
|
||||||
processAttachment(d, entry.getKey().toString(),
|
|
||||||
(ByteArrayInputStream)entry.getValue());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Can't create directory "+attDirName);
|
System.err.println("Can't create directory "+attDirName);
|
||||||
|
@ -131,33 +124,26 @@ public class Msg2txt {
|
||||||
/**
|
/**
|
||||||
* Processes a single attachment: reads it from the Outlook MSG file and
|
* Processes a single attachment: reads it from the Outlook MSG file and
|
||||||
* writes it to disk as an individual file.
|
* writes it to disk as an individual file.
|
||||||
*
|
*
|
||||||
|
* @param attachment the chunk group describing the attachment
|
||||||
* @param dir the directory in which to write the attachment file
|
* @param dir the directory in which to write the attachment file
|
||||||
* @param fileName the name of the attachment file
|
|
||||||
* @param fileIn the input stream that contains the attachment's data
|
|
||||||
* @throws IOException when any of the file operations fails
|
* @throws IOException when any of the file operations fails
|
||||||
*/
|
*/
|
||||||
public void processAttachment(File dir, String fileName,
|
public void processAttachment(AttachmentChunks attachment,
|
||||||
ByteArrayInputStream fileIn) throws IOException {
|
File dir) throws IOException {
|
||||||
|
String fileName = attachment.attachFileName.toString();
|
||||||
|
if(attachment.attachLongFileName != null) {
|
||||||
|
fileName = attachment.attachLongFileName.toString();
|
||||||
|
}
|
||||||
|
|
||||||
File f = new File(dir, fileName);
|
File f = new File(dir, fileName);
|
||||||
OutputStream fileOut = null;
|
OutputStream fileOut = null;
|
||||||
try {
|
try {
|
||||||
fileOut = new FileOutputStream(f);
|
fileOut = new FileOutputStream(f);
|
||||||
byte[] buffer = new byte[2048];
|
fileOut.write(attachment.attachData.getValue());
|
||||||
int bNum = fileIn.read(buffer);
|
|
||||||
while(bNum > 0) {
|
|
||||||
fileOut.write(buffer);
|
|
||||||
bNum = fileIn.read(buffer);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
if(fileOut != null) {
|
||||||
if(fileIn != null) {
|
fileOut.close();
|
||||||
fileIn.close();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if(fileOut != null) {
|
|
||||||
fileOut.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ public final class Chunks implements ChunkGroup {
|
||||||
/** Type of server that the message originated from (SMTP, etc). */
|
/** Type of server that the message originated from (SMTP, etc). */
|
||||||
public StringChunk sentByServerType;
|
public StringChunk sentByServerType;
|
||||||
/** TODO */
|
/** TODO */
|
||||||
public StringChunk dateChunk;
|
public ByteChunk dateChunk;
|
||||||
/** TODO */
|
/** TODO */
|
||||||
public StringChunk emailFromChunk;
|
public StringChunk emailFromChunk;
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ public final class Chunks implements ChunkGroup {
|
||||||
subjectChunk = (StringChunk)chunk;
|
subjectChunk = (StringChunk)chunk;
|
||||||
break;
|
break;
|
||||||
case DATE:
|
case DATE:
|
||||||
dateChunk = (StringChunk)chunk;
|
dateChunk = (ByteChunk)chunk;
|
||||||
break;
|
break;
|
||||||
case CONVERSATION_TOPIC:
|
case CONVERSATION_TOPIC:
|
||||||
conversationTopic = (StringChunk)chunk;
|
conversationTopic = (StringChunk)chunk;
|
||||||
|
|
|
@ -32,7 +32,7 @@ public final class RecipientChunks implements ChunkGroup {
|
||||||
public static final int RECIPIENT_EMAIL = 0x39FE;
|
public static final int RECIPIENT_EMAIL = 0x39FE;
|
||||||
|
|
||||||
/** TODO */
|
/** TODO */
|
||||||
public StringChunk recipientSearchChunk;
|
public ByteChunk recipientSearchChunk;
|
||||||
/** TODO */
|
/** TODO */
|
||||||
public StringChunk recipientEmailChunk;
|
public StringChunk recipientEmailChunk;
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public final class RecipientChunks implements ChunkGroup {
|
||||||
public void record(Chunk chunk) {
|
public void record(Chunk chunk) {
|
||||||
switch(chunk.getChunkId()) {
|
switch(chunk.getChunkId()) {
|
||||||
case RECIPIENT_SEARCH:
|
case RECIPIENT_SEARCH:
|
||||||
recipientSearchChunk = (StringChunk)chunk;
|
recipientSearchChunk = (ByteChunk)chunk;
|
||||||
break;
|
break;
|
||||||
case RECIPIENT_EMAIL:
|
case RECIPIENT_EMAIL:
|
||||||
recipientEmailChunk = (StringChunk)chunk;
|
recipientEmailChunk = (StringChunk)chunk;
|
||||||
|
|
|
@ -55,7 +55,7 @@ public class StringChunk extends Chunk {
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case Types.ASCII_STRING:
|
case Types.ASCII_STRING:
|
||||||
try {
|
try {
|
||||||
tmpValue = new String(data, "UTF-16LE");
|
tmpValue = new String(data, "CP1252");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new RuntimeException("Core encoding not found, JVM broken?", e);
|
throw new RuntimeException("Core encoding not found, JVM broken?", e);
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ public class StringChunk extends Chunk {
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case Types.ASCII_STRING:
|
case Types.ASCII_STRING:
|
||||||
try {
|
try {
|
||||||
data = value.getBytes("UTF-16LE");
|
data = value.getBytes("CP1252");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new RuntimeException("Core encoding not found, JVM broken?", e);
|
throw new RuntimeException("Core encoding not found, JVM broken?", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ public final class POIFSChunkParser {
|
||||||
// there doesn't seem to be any use of that in Outlook
|
// there doesn't seem to be any use of that in Outlook
|
||||||
for(Entry entry : node) {
|
for(Entry entry : node) {
|
||||||
if(entry instanceof DirectoryNode) {
|
if(entry instanceof DirectoryNode) {
|
||||||
DirectoryNode dir = (DirectoryNode)node;
|
DirectoryNode dir = (DirectoryNode)entry;
|
||||||
ChunkGroup group = null;
|
ChunkGroup group = null;
|
||||||
|
|
||||||
// Do we know what to do with it?
|
// Do we know what to do with it?
|
||||||
|
@ -66,7 +66,7 @@ public final class POIFSChunkParser {
|
||||||
group = new NameIdChunks();
|
group = new NameIdChunks();
|
||||||
}
|
}
|
||||||
if(dir.getName().startsWith(RecipientChunks.PREFIX)) {
|
if(dir.getName().startsWith(RecipientChunks.PREFIX)) {
|
||||||
group = new NameIdChunks();
|
group = new RecipientChunks();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(group != null) {
|
if(group != null) {
|
||||||
|
|
|
@ -18,8 +18,16 @@
|
||||||
package org.apache.poi.hsmf.parsers;
|
package org.apache.poi.hsmf.parsers;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.poi.hsmf.MAPIMessage;
|
||||||
|
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
|
||||||
|
import org.apache.poi.hsmf.datatypes.ChunkGroup;
|
||||||
|
import org.apache.poi.hsmf.datatypes.Chunks;
|
||||||
|
import org.apache.poi.hsmf.datatypes.NameIdChunks;
|
||||||
|
import org.apache.poi.hsmf.datatypes.RecipientChunks;
|
||||||
|
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
|
||||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
import org.apache.poi.POIDataSamples;
|
import org.apache.poi.POIDataSamples;
|
||||||
|
|
||||||
|
@ -44,13 +52,83 @@ public final class TestPOIFSChunkParser extends TestCase {
|
||||||
new FileInputStream(samples.getFile("attachment_test_msg.msg"))
|
new FileInputStream(samples.getFile("attachment_test_msg.msg"))
|
||||||
);
|
);
|
||||||
POIFSFileSystem without = new POIFSFileSystem(
|
POIFSFileSystem without = new POIFSFileSystem(
|
||||||
new FileInputStream(samples.getFile("simple_test_msg.msg"))
|
new FileInputStream(samples.getFile("quick.msg"))
|
||||||
);
|
);
|
||||||
|
AttachmentChunks attachment;
|
||||||
|
|
||||||
// Check details on the one with
|
|
||||||
|
// Check raw details on the one with
|
||||||
// One with, from the top
|
with.getRoot().getEntry("__attach_version1.0_#00000000");
|
||||||
|
with.getRoot().getEntry("__attach_version1.0_#00000001");
|
||||||
|
POIFSChunkParser.parse(with.getRoot());
|
||||||
|
|
||||||
|
ChunkGroup[] groups = POIFSChunkParser.parse(with.getRoot());
|
||||||
|
assertEquals(5, groups.length);
|
||||||
|
assertTrue(groups[0] instanceof Chunks);
|
||||||
|
assertTrue(groups[1] instanceof RecipientChunks);
|
||||||
|
assertTrue(groups[2] instanceof AttachmentChunks);
|
||||||
|
assertTrue(groups[3] instanceof AttachmentChunks);
|
||||||
|
assertTrue(groups[4] instanceof NameIdChunks);
|
||||||
|
|
||||||
|
attachment = (AttachmentChunks)groups[2];
|
||||||
|
assertEquals("TEST-U~1.DOC", attachment.attachFileName.toString());
|
||||||
|
assertEquals("test-unicode.doc", attachment.attachLongFileName.toString());
|
||||||
|
assertEquals(24064, attachment.attachData.getValue().length);
|
||||||
|
|
||||||
|
attachment = (AttachmentChunks)groups[3];
|
||||||
|
assertEquals("pj1.txt", attachment.attachFileName.toString());
|
||||||
|
assertEquals("pj1.txt", attachment.attachLongFileName.toString());
|
||||||
|
assertEquals(89, attachment.attachData.getValue().length);
|
||||||
|
|
||||||
|
|
||||||
|
// Check raw details on one without
|
||||||
|
try {
|
||||||
|
without.getRoot().getEntry("__attach_version1.0_#00000000");
|
||||||
|
fail();
|
||||||
|
} catch(FileNotFoundException e) {}
|
||||||
|
try {
|
||||||
|
without.getRoot().getEntry("__attach_version1.0_#00000001");
|
||||||
|
fail();
|
||||||
|
} catch(FileNotFoundException e) {}
|
||||||
|
|
||||||
|
|
||||||
|
// One with, from the top
|
||||||
|
MAPIMessage msgWith = new MAPIMessage(with);
|
||||||
|
assertEquals(2, msgWith.getAttachmentFiles().length);
|
||||||
|
|
||||||
|
attachment = msgWith.getAttachmentFiles()[0];
|
||||||
|
assertEquals("TEST-U~1.DOC", attachment.attachFileName.toString());
|
||||||
|
assertEquals("test-unicode.doc", attachment.attachLongFileName.toString());
|
||||||
|
assertEquals(24064, attachment.attachData.getValue().length);
|
||||||
|
|
||||||
|
attachment = msgWith.getAttachmentFiles()[1];
|
||||||
|
assertEquals("pj1.txt", attachment.attachFileName.toString());
|
||||||
|
assertEquals("pj1.txt", attachment.attachLongFileName.toString());
|
||||||
|
assertEquals(89, attachment.attachData.getValue().length);
|
||||||
|
|
||||||
|
// Plus check core details are there
|
||||||
|
try {
|
||||||
|
assertEquals("'nicolas1.23456@free.fr'", msgWith.getDisplayTo());
|
||||||
|
assertEquals("Nicolas1 23456", msgWith.getDisplayFrom());
|
||||||
|
assertEquals("test pi\u00e8ce jointe 1", msgWith.getSubject());
|
||||||
|
} catch(ChunkNotFoundException e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// One without, from the top
|
// One without, from the top
|
||||||
|
MAPIMessage msgWithout = new MAPIMessage(without);
|
||||||
|
|
||||||
|
// No attachments
|
||||||
|
assertEquals(0, msgWithout.getAttachmentFiles().length);
|
||||||
|
|
||||||
|
// But has core details
|
||||||
|
try {
|
||||||
|
assertEquals("Kevin Roast", msgWithout.getDisplayTo());
|
||||||
|
assertEquals("Kevin Roast", msgWithout.getDisplayFrom());
|
||||||
|
assertEquals("Test the content transformer", msgWithout.getSubject());
|
||||||
|
} catch(ChunkNotFoundException e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue