From ddc737457b57b0188b441af5c6c1b717b05002d2 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 13 Jan 2011 13:53:02 +0000 Subject: [PATCH] Start on TNEF RTF attribute decompression, but not quite finished yet git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1058555 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hmef/CompressedRTF.java | 5 +- .../org/apache/poi/hmef/MAPIAttribute.java | 14 +++++- .../org/apache/poi/hmef/MAPIRtfAttribute.java | 49 +++++++++++++++++++ .../apache/poi/hmef/MAPIStringAttribute.java | 4 ++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/scratchpad/src/org/apache/poi/hmef/MAPIRtfAttribute.java diff --git a/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java b/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java index c2bb38eefd..aa3b41c650 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java +++ b/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java @@ -31,10 +31,13 @@ import org.apache.poi.util.LittleEndian; * Within a {@link HMEFMessage}, the content is often * stored in as RTF, but LZW compressed. This class * handles decompressing it for you. + * + * Note - this doesn't quite decompress the data correctly, + * more work and unit testing is required... */ public final class CompressedRTF extends LZWDecompresser { public static final byte[] COMPRESSED_SIGNATURE = - new byte[] { (byte)'L', (byte)'Z', (byte)'F', (byte)'U' }; + new byte[] { (byte)'L', (byte)'Z', (byte)'F', (byte)'u' }; public static final byte[] UNCOMPRESSED_SIGNATURE = new byte[] { (byte)'M', (byte)'E', (byte)'L', (byte)'A' }; public static final int COMPRESSED_SIGNATURE_INT = diff --git a/src/scratchpad/src/org/apache/poi/hmef/MAPIAttribute.java b/src/scratchpad/src/org/apache/poi/hmef/MAPIAttribute.java index 3014f48d2d..d8922213b1 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/MAPIAttribute.java +++ b/src/scratchpad/src/org/apache/poi/hmef/MAPIAttribute.java @@ -62,7 +62,17 @@ public class MAPIAttribute { } public String toString() { - return property.toString() + " " + HexDump.toHex(data); + String hex; + if(data.length <= 16) { + hex = HexDump.toHex(data); + } else { + byte[] d = new byte[16]; + System.arraycopy(data, 0, d, 0, 16); + hex = HexDump.toHex(d); + hex = hex.substring(0, hex.length()-1) + ", ....]"; + } + + return property.toString() + " " + hex; } /** @@ -146,6 +156,8 @@ public class MAPIAttribute { MAPIAttribute attr; if(type == Types.UNICODE_STRING || type == Types.ASCII_STRING) { attr = new MAPIStringAttribute(prop, type, data); + } else if(id == MAPIProperty.RTF_COMPRESSED.id) { + attr = new MAPIRtfAttribute(prop, type, data); } else { attr = new MAPIAttribute(prop, type, data); } diff --git a/src/scratchpad/src/org/apache/poi/hmef/MAPIRtfAttribute.java b/src/scratchpad/src/org/apache/poi/hmef/MAPIRtfAttribute.java new file mode 100644 index 0000000000..567c2b6dd3 --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hmef/MAPIRtfAttribute.java @@ -0,0 +1,49 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hmef; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.apache.poi.hsmf.datatypes.MAPIProperty; +import org.apache.poi.util.StringUtil; + +/** + * A pure-MAPI attribute holding RTF (compressed or not), which applies + * to a {@link HMEFMessage} or one of its {@link Attachment}s. + */ +public final class MAPIRtfAttribute extends MAPIAttribute { + private final String data; + + public MAPIRtfAttribute(MAPIProperty property, int type, byte[] data) throws IOException { + super(property, type, data); + + CompressedRTF rtf = new CompressedRTF(); + byte[] decomp = rtf.decompress(new ByteArrayInputStream(data)); + + this.data = StringUtil.getFromCompressedUnicode(decomp, 0, decomp.length); + } + + public String getDataString() { + return data; + } + + public String toString() { + return getProperty().toString() + " " + data; + } +} diff --git a/src/scratchpad/src/org/apache/poi/hmef/MAPIStringAttribute.java b/src/scratchpad/src/org/apache/poi/hmef/MAPIStringAttribute.java index 570923641e..6597cb95ee 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/MAPIStringAttribute.java +++ b/src/scratchpad/src/org/apache/poi/hmef/MAPIStringAttribute.java @@ -65,6 +65,10 @@ public final class MAPIStringAttribute extends MAPIAttribute { this.data = tmpData; } + public String getDataString() { + return data; + } + public String toString() { return getProperty().toString() + " " + data; } -- 2.39.5