Browse Source

SonarCube fix - make members private

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1773907 13f79535-47bb-0310-9956-ffa450edef68
pull/44/head
Andreas Beeker 7 years ago
parent
commit
269dd9e0af

+ 62
- 9
src/scratchpad/src/org/apache/poi/hsmf/datatypes/AttachmentChunks.java View File

@@ -38,15 +38,15 @@ import org.apache.poi.util.POILogger;
* attachment.
*/
public class AttachmentChunks implements ChunkGroup {
private static POILogger logger = POILogFactory.getLogger(AttachmentChunks.class);
private static final POILogger LOG = POILogFactory.getLogger(AttachmentChunks.class);
public static final String PREFIX = "__attach_version1.0_#";

public ByteChunk attachData;
public StringChunk attachExtension;
public StringChunk attachFileName;
public StringChunk attachLongFileName;
public StringChunk attachMimeTag;
public DirectoryChunk attachmentDirectory;
private ByteChunk attachData;
private StringChunk attachExtension;
private StringChunk attachFileName;
private StringChunk attachLongFileName;
private StringChunk attachMimeTag;
private DirectoryChunk attachmentDirectory;

/**
* This is in WMF Format. You'll probably want to pass it to Apache Batik to
@@ -99,6 +99,7 @@ public class AttachmentChunks implements ChunkGroup {
return allChunks.toArray(new Chunk[allChunks.size()]);
}

@Override
public Chunk[] getChunks() {
return getAll();
}
@@ -107,9 +108,59 @@ public class AttachmentChunks implements ChunkGroup {
return poifsName;
}

/**
* @return the ATTACH_DATA chunk
*/
public ByteChunk getAttachData() {
return attachData;
}

/**
* @return the attachment extension
*/
public StringChunk getAttachExtension() {
return attachExtension;
}

/**
* @return the attachment (short) filename
*/
public StringChunk getAttachFileName() {
return attachFileName;
}

/**
* @return the attachment (long) filename
*/
public StringChunk getAttachLongFileName() {
return attachLongFileName;
}

/**
* @return the attachment mimetag
*/
public StringChunk getAttachMimeTag() {
return attachMimeTag;
}

/**
* @return the attachment directory
*/
public DirectoryChunk getAttachmentDirectory() {
return attachmentDirectory;
}

/**
* @return the attachment preview bytes
*/
public ByteChunk getAttachRenderingWMF() {
return attachRenderingWMF;
}

/**
* Called by the parser whenever a chunk is found.
*/
@Override
public void record(Chunk chunk) {
// TODO: add further members for other properties like:
// - ATTACH_ADDITIONAL_INFO
@@ -127,7 +178,7 @@ public class AttachmentChunks implements ChunkGroup {
} else if (chunk instanceof DirectoryChunk) {
attachmentDirectory = (DirectoryChunk) chunk;
} else {
logger.log(POILogger.ERROR, "Unexpected data chunk of type " + chunk);
LOG.log(POILogger.ERROR, "Unexpected data chunk of type " + chunk);
}
} else if (chunkId == ATTACH_EXTENSION.id) {
attachExtension = (StringChunk) chunk;
@@ -148,6 +199,7 @@ public class AttachmentChunks implements ChunkGroup {
/**
* Used to flag that all the chunks of the attachment have now been located.
*/
@Override
public void chunksComplete() {
// Currently, we don't need to do anything special once
// all the chunks have been located
@@ -157,7 +209,8 @@ public class AttachmentChunks implements ChunkGroup {
* Orders by the attachment number.
*/
public static class AttachmentChunksSorter
implements Comparator<AttachmentChunks>, Serializable {
implements Comparator<AttachmentChunks>, Serializable {
@Override
public int compare(AttachmentChunks a, AttachmentChunks b) {
return a.poifsName.compareTo(b.poifsName);
}

+ 7
- 2
src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java View File

@@ -46,10 +46,12 @@ public class ByteChunk extends Chunk {
super(chunkId, type);
}

@Override
public void readValue(InputStream value) throws IOException {
this.value = IOUtils.toByteArray(value);
}

@Override
public void writeValue(OutputStream out) throws IOException {
out.write(value);
}
@@ -65,6 +67,7 @@ public class ByteChunk extends Chunk {
/**
* Returns the data in a debug-friendly string format
*/
@Override
public String toString() {
return toDebugFriendlyString(value);
}
@@ -74,8 +77,9 @@ public class ByteChunk extends Chunk {
* array, and the start of a longer one.
*/
protected static String toDebugFriendlyString(byte[] value) {
if (value == null)
if (value == null) {
return "(Null Byte Array)";
}

StringBuffer text = new StringBuffer();
text.append("Bytes len=").append(value.length);
@@ -86,8 +90,9 @@ public class ByteChunk extends Chunk {
limit = 12;
}
for (int i = 0; i < limit; i++) {
if (i > 0)
if (i > 0) {
text.append(',');
}
text.append(value[i]);
}
if (value.length > 16) {

+ 5
- 4
src/scratchpad/src/org/apache/poi/hsmf/datatypes/Chunk.java View File

@@ -27,9 +27,9 @@ import org.apache.poi.hsmf.datatypes.Types.MAPIType;
public abstract class Chunk {
public static final String DEFAULT_NAME_PREFIX = "__substg1.0_";

protected int chunkId;
protected MAPIType type;
protected String namePrefix;
private int chunkId;
private MAPIType type;
private String namePrefix;

protected Chunk(String namePrefix, int chunkId, MAPIType type) {
this.namePrefix = namePrefix;
@@ -63,8 +63,9 @@ public abstract class Chunk {
String type = this.type.asFileEnding();

String chunkId = Integer.toHexString(this.chunkId);
while (chunkId.length() < 4)
while (chunkId.length() < 4) {
chunkId = "0" + chunkId;
}

return this.namePrefix
+ chunkId.toUpperCase(Locale.ROOT)

+ 94
- 20
src/scratchpad/src/org/apache/poi/hsmf/datatypes/Chunks.java View File

@@ -37,7 +37,7 @@ import org.apache.poi.util.POILogger;
* TODO Deprecate the public Chunks in favour of Property Lookups
*/
public final class Chunks implements ChunkGroupWithProperties {
private static POILogger logger = POILogFactory.getLogger(Chunks.class);
private static final POILogger LOG = POILogFactory.getLogger(Chunks.class);

/**
* Holds all the chunks that were found, indexed by their MAPIProperty.
@@ -47,60 +47,64 @@ public final class Chunks implements ChunkGroupWithProperties {
private Map<MAPIProperty, List<Chunk>> allChunks = new HashMap<MAPIProperty, List<Chunk>>();

/** Type of message that the MSG represents (ie. IPM.Note) */
public StringChunk messageClass;
private StringChunk messageClass;
/** BODY Chunk, for plain/text messages */
public StringChunk textBodyChunk;
private StringChunk textBodyChunk;
/** BODY Html Chunk, for html messages */
public StringChunk htmlBodyChunkString;
public ByteChunk htmlBodyChunkBinary;
private StringChunk htmlBodyChunkString;
private ByteChunk htmlBodyChunkBinary;
/** BODY Rtf Chunk, for Rtf (Rich) messages */
public ByteChunk rtfBodyChunk;
private ByteChunk rtfBodyChunk;
/** Subject link chunk, in plain/text */
public StringChunk subjectChunk;
private StringChunk subjectChunk;
/**
* Value that is in the TO field (not actually the addresses as they are
* stored in recip directory nodes
*/
public StringChunk displayToChunk;
private StringChunk displayToChunk;
/** Value that is in the FROM field */
public StringChunk displayFromChunk;
private StringChunk displayFromChunk;
/** value that shows in the CC field */
public StringChunk displayCCChunk;
private StringChunk displayCCChunk;
/** Value that shows in the BCC field */
public StringChunk displayBCCChunk;
private StringChunk displayBCCChunk;
/** Sort of like the subject line, but without the RE: and FWD: parts. */
public StringChunk conversationTopic;
private StringChunk conversationTopic;
/** Type of server that the message originated from (SMTP, etc). */
public StringChunk sentByServerType;
private StringChunk sentByServerType;
/** The email headers */
public StringChunk messageHeaders;
private StringChunk messageHeaders;
/** TODO */
public MessageSubmissionChunk submissionChunk;
private MessageSubmissionChunk submissionChunk;
/** TODO */
public StringChunk emailFromChunk;
private StringChunk emailFromChunk;
/** The message ID */
public StringChunk messageId;
private StringChunk messageId;
/** The message properties */
private MessagePropertiesChunk messageProperties;

@Override
public Map<MAPIProperty, List<PropertyValue>> getProperties() {
if (messageProperties != null) {
return messageProperties.getProperties();
} else
} else {
return Collections.emptyMap();
}
}

public Map<MAPIProperty, PropertyValue> getRawProperties() {
if (messageProperties != null) {
return messageProperties.getRawProperties();
} else
} else {
return Collections.emptyMap();
}
}

public Map<MAPIProperty, List<Chunk>> getAll() {
return allChunks;
}

@Override
public Chunk[] getChunks() {
ArrayList<Chunk> chunks = new ArrayList<Chunk>(allChunks.size());
for (List<Chunk> c : allChunks.values()) {
@@ -109,9 +113,78 @@ public final class Chunks implements ChunkGroupWithProperties {
return chunks.toArray(new Chunk[chunks.size()]);
}

public StringChunk getMessageClass() {
return messageClass;
}

public StringChunk getTextBodyChunk() {
return textBodyChunk;
}

public StringChunk getHtmlBodyChunkString() {
return htmlBodyChunkString;
}

public ByteChunk getHtmlBodyChunkBinary() {
return htmlBodyChunkBinary;
}

public ByteChunk getRtfBodyChunk() {
return rtfBodyChunk;
}

public StringChunk getSubjectChunk() {
return subjectChunk;
}

public StringChunk getDisplayToChunk() {
return displayToChunk;
}

public StringChunk getDisplayFromChunk() {
return displayFromChunk;
}

public StringChunk getDisplayCCChunk() {
return displayCCChunk;
}

public StringChunk getDisplayBCCChunk() {
return displayBCCChunk;
}

public StringChunk getConversationTopic() {
return conversationTopic;
}

public StringChunk getSentByServerType() {
return sentByServerType;
}

public StringChunk getMessageHeaders() {
return messageHeaders;
}

public MessageSubmissionChunk getSubmissionChunk() {
return submissionChunk;
}

public StringChunk getEmailFromChunk() {
return emailFromChunk;
}

public StringChunk getMessageId() {
return messageId;
}

public MessagePropertiesChunk getMessageProperties() {
return messageProperties;
}

/**
* Called by the parser whenever a chunk is found.
*/
@Override
public void record(Chunk chunk) {
// Work out what MAPIProperty this corresponds to
MAPIProperty prop = MAPIProperty.get(chunk.getChunkId());
@@ -172,11 +245,12 @@ public final class Chunks implements ChunkGroupWithProperties {
allChunks.get(prop).add(chunk);
}

@Override
public void chunksComplete() {
if (messageProperties != null) {
messageProperties.matchVariableSizedPropertiesToChunks();
} else {
logger.log(POILogger.WARN,
LOG.log(POILogger.WARN,
"Message didn't contain a root list of properties!");
}
}

+ 1
- 0
src/scratchpad/src/org/apache/poi/hsmf/datatypes/MAPIProperty.java View File

@@ -1071,6 +1071,7 @@ public class MAPIProperty {
return str + usualType.asFileEnding();
}

@Override
public String toString() {
StringBuffer str = new StringBuffer();
str.append(name);

+ 4
- 3
src/scratchpad/src/org/apache/poi/hsmf/datatypes/MessageSubmissionChunk.java View File

@@ -36,8 +36,7 @@ import org.apache.poi.util.POILogger;
* used if you want to cancel a message or similar
*/
public class MessageSubmissionChunk extends Chunk {
private static POILogger logger = POILogFactory
.getLogger(MessageSubmissionChunk.class);
private static final POILogger LOG = POILogFactory.getLogger(MessageSubmissionChunk.class);
private String rawId;
private Calendar date;

@@ -59,6 +58,7 @@ public class MessageSubmissionChunk extends Chunk {
super(chunkId, type);
}

@Override
public void readValue(InputStream value) throws IOException {
// Stored in the file as us-ascii
byte[] data = IOUtils.toByteArray(value);
@@ -103,7 +103,7 @@ public class MessageSubmissionChunk extends Chunk {
date.set(Calendar.SECOND, Integer.parseInt(m.group(6)));
date.clear(Calendar.MILLISECOND);
} else {
logger.log(POILogger.WARN,
LOG.log(POILogger.WARN,
"Warning - unable to make sense of date "
+ dateS);
}
@@ -112,6 +112,7 @@ public class MessageSubmissionChunk extends Chunk {
}
}

@Override
public void writeValue(OutputStream out) throws IOException {
byte[] data = rawId.getBytes(Charset.forName("ASCII"));
out.write(data);

+ 3
- 0
src/scratchpad/src/org/apache/poi/hsmf/datatypes/NameIdChunks.java View File

@@ -33,6 +33,7 @@ public final class NameIdChunks implements ChunkGroup {
return allChunks.toArray(new Chunk[allChunks.size()]);
}

@Override
public Chunk[] getChunks() {
return getAll();
}
@@ -40,6 +41,7 @@ public final class NameIdChunks implements ChunkGroup {
/**
* Called by the parser whenever a chunk is found.
*/
@Override
public void record(Chunk chunk) {
allChunks.add(chunk);
}
@@ -47,6 +49,7 @@ public final class NameIdChunks implements ChunkGroup {
/**
* Used to flag that all the chunks of the NameID have now been located.
*/
@Override
public void chunksComplete() {
// Currently, we don't need to do anything special once
// all the chunks have been located

+ 1
- 1
src/scratchpad/src/org/apache/poi/hsmf/datatypes/PropertiesChunk.java View File

@@ -137,7 +137,7 @@ public abstract class PropertiesChunk extends Chunk {
// TODO Is this the right way?
Map<Integer, Chunk> chunks = new HashMap<Integer, Chunk>();
for (Chunk chunk : parentGroup.getChunks()) {
chunks.put(chunk.chunkId, chunk);
chunks.put(chunk.getChunkId(), chunk);
}

// Loop over our values, looking for chunk based ones

+ 12
- 1
src/scratchpad/src/org/apache/poi/hsmf/datatypes/PropertyValue.java View File

@@ -60,10 +60,12 @@ public class PropertyValue {
this.data = value;
}

@Override
public String toString() {
Object v = getValue();
if (v == null)
if (v == null) {
return "(No value available)";
}

if (v instanceof byte[]) {
return ByteChunk.toDebugFriendlyString((byte[]) v);
@@ -79,6 +81,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Void getValue() {
return null;
}
@@ -90,6 +93,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Boolean getValue() {
short val = LittleEndian.getShort(data);
return val > 0;
@@ -111,6 +115,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Short getValue() {
return LittleEndian.getShort(data);
}
@@ -128,6 +133,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Integer getValue() {
return LittleEndian.getInt(data);
}
@@ -146,6 +152,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Long getValue() {
return LittleEndian.getLong(data);
}
@@ -164,6 +171,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Float getValue() {
return LittleEndian.getFloat(data);
}
@@ -181,6 +189,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Double getValue() {
return LittleEndian.getDouble(data);
}
@@ -204,6 +213,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public BigInteger getValue() {
long unshifted = LittleEndian.getLong(data);
return BigInteger.valueOf(unshifted).divide(SHIFT);
@@ -229,6 +239,7 @@ public class PropertyValue {
super(property, flags, data);
}

@Override
public Calendar getValue() {
long time = LittleEndian.getLong(data);
time = (time / 10 / 1000) - OFFSET;

+ 14
- 6
src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java View File

@@ -33,7 +33,7 @@ import org.apache.poi.util.POILogger;
* If a message has multiple recipients, there will be several of these.
*/
public final class RecipientChunks implements ChunkGroupWithProperties {
private static POILogger logger = POILogFactory.getLogger(RecipientChunks.class);
private static final POILogger LOG = POILogFactory.getLogger(RecipientChunks.class);

public static final String PREFIX = "__recip_version1.0_#";

@@ -88,7 +88,7 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
try {
recipientNumber = Integer.parseInt(number, 16);
} catch (NumberFormatException e) {
logger.log(POILogger.ERROR,
LOG.log(POILogger.ERROR,
"Invalid recipient number in name " + name);
}
}
@@ -161,17 +161,20 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
/** Holds all the chunks that were found. */
private List<Chunk> allChunks = new ArrayList<Chunk>();

@Override
public Map<MAPIProperty, List<PropertyValue>> getProperties() {
if (recipientProperties != null) {
return recipientProperties.getProperties();
} else
} else {
return Collections.emptyMap();
}
}

public Chunk[] getAll() {
return allChunks.toArray(new Chunk[allChunks.size()]);
}

@Override
public Chunk[] getChunks() {
return getAll();
}
@@ -179,6 +182,7 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
/**
* Called by the parser whenever a chunk is found.
*/
@Override
public void record(Chunk chunk) {
if (chunk.getChunkId() == RECIPIENT_SEARCH.id) {
// TODO - parse
@@ -201,11 +205,12 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
allChunks.add(chunk);
}

@Override
public void chunksComplete() {
if (recipientProperties != null) {
recipientProperties.matchVariableSizedPropertiesToChunks();
} else {
logger.log(POILogger.WARN, "Recipeints Chunk didn't contain a list of properties!");
LOG.log(POILogger.WARN, "Recipeints Chunk didn't contain a list of properties!");
}
}

@@ -214,11 +219,14 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
*/
public static class RecipientChunksSorter
implements Comparator<RecipientChunks>, Serializable {
@Override
public int compare(RecipientChunks a, RecipientChunks b) {
if (a.recipientNumber < b.recipientNumber)
if (a.recipientNumber < b.recipientNumber) {
return -1;
if (a.recipientNumber > b.recipientNumber)
}
if (a.recipientNumber > b.recipientNumber) {
return +1;
}
return 0;
}
}

+ 10
- 7
src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java View File

@@ -66,11 +66,12 @@ public class StringChunk extends Chunk {
this.encoding7Bit = encoding;

// Re-read the String if we're a 7 bit one
if (type == Types.ASCII_STRING) {
if (getType() == Types.ASCII_STRING) {
parseString();
}
}

@Override
public void readValue(InputStream value) throws IOException {
rawValue = IOUtils.toByteArray(value);
parseString();
@@ -78,29 +79,30 @@ public class StringChunk extends Chunk {

private void parseString() {
String tmpValue;
if (type == Types.ASCII_STRING) {
if (getType() == Types.ASCII_STRING) {
tmpValue = parseAs7BitData(rawValue, encoding7Bit);
} else if (type == Types.UNICODE_STRING) {
} else if (getType() == Types.UNICODE_STRING) {
tmpValue = StringUtil.getFromUnicodeLE(rawValue);
} else {
throw new IllegalArgumentException("Invalid type " + type + " for String Chunk");
throw new IllegalArgumentException("Invalid type " + getType() + " for String Chunk");
}

// Clean up
this.value = tmpValue.replace("\0", "");
}

@Override
public void writeValue(OutputStream out) throws IOException {
out.write(rawValue);
}

private void storeString() {
if (type == Types.ASCII_STRING) {
if (getType() == Types.ASCII_STRING) {
rawValue = value.getBytes(Charset.forName(encoding7Bit));
} else if (type == Types.UNICODE_STRING) {
} else if (getType() == Types.UNICODE_STRING) {
rawValue = StringUtil.getToUnicodeLE(value);
} else {
throw new IllegalArgumentException("Invalid type " + type + " for String Chunk");
throw new IllegalArgumentException("Invalid type " + getType() + " for String Chunk");
}
}

@@ -120,6 +122,7 @@ public class StringChunk extends Chunk {
storeString();
}

@Override
public String toString() {
return this.value;
}

+ 1
- 0
src/scratchpad/src/org/apache/poi/hsmf/datatypes/Types.java View File

@@ -130,6 +130,7 @@ public final class Types {
return name;
}

@Override
public String toString() {
return id + " / 0x" + asFileEnding() + " - " + name + " @ "
+ length;

+ 27
- 56
src/scratchpad/src/org/apache/poi/hwpf/QuickTest.java View File

@@ -25,61 +25,32 @@ import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;

public final class QuickTest
{
public QuickTest()
{
}

public static void main(String[] args) throws IOException
{
HWPFDocument doc = new HWPFDocument (new FileInputStream (args[0]));
Range r = doc.getRange();

System.out.println("Example you supplied:");
System.out.println("---------------------");
for (int x = 0; x < r.numSections(); x++)
{
Section s = r.getSection(x);
for (int y = 0; y < s.numParagraphs(); y++)
{
Paragraph p = s.getParagraph(y);
for (int z = 0; z < p.numCharacterRuns(); z++)
{
//character run
CharacterRun run = p.getCharacterRun(z);
//character run text
String text = run.text();
// show us the text
System.out.print(text);
}
// use a new line at the paragraph break
System.out.println();
public final class QuickTest {
public QuickTest() {
}

public static void main(String[] args) throws IOException {
HWPFDocument doc = new HWPFDocument(new FileInputStream(args[0]));
Range r = doc.getRange();

System.out.println("Example you supplied:");
System.out.println("---------------------");
for (int x = 0; x < r.numSections(); x++) {
Section s = r.getSection(x);
for (int y = 0; y < s.numParagraphs(); y++) {
Paragraph p = s.getParagraph(y);
for (int z = 0; z < p.numCharacterRuns(); z++) {
// character run
CharacterRun run = p.getCharacterRun(z);
// character run text
String text = run.text();
// show us the text
System.out.print(text);
}
// use a new line at the paragraph break
System.out.println();
}
}
}


// System.out.println("\n\nExample using new method:");
// System.out.println("-------------------------");
// for (int x = 0; x < r.numSections(); x++)
// {
// Section s = r.getSection(x);
// for (int y = 0; y < s.numParagraphs(); y++)
// {
// Paragraph p = s.getParagraph(y);
// for (int z = 0; z < p.numCharacterRuns(); z++)
// {
// //character run
// CharacterRun run = p.getCharacterRun(z);
// //** get character run/paragraph common text **
// String text = run.commonText(p);
// // show us the text
// System.out.print(text);
// }
// // use a new line at the paragraph break
// System.out.println();
// }
// }
}

doc.close();
}
}

+ 28
- 28
src/scratchpad/testcases/org/apache/poi/hsmf/TestFileWithAttachmentsRead.java View File

@@ -69,11 +69,11 @@ public class TestFileWithAttachmentsRead extends TestCase {

// Basic checks
for (AttachmentChunks attachment : attachments) {
assertTrue(attachment.attachFileName.getValue().length() > 0);
assertTrue(attachment.attachLongFileName.getValue().length() > 0);
assertTrue(attachment.attachExtension.getValue().length() > 0);
if(attachment.attachMimeTag != null) {
assertTrue(attachment.attachMimeTag.getValue().length() > 0);
assertTrue(attachment.getAttachFileName().getValue().length() > 0);
assertTrue(attachment.getAttachLongFileName().getValue().length() > 0);
assertTrue(attachment.getAttachExtension().getValue().length() > 0);
if(attachment.getAttachMimeTag() != null) {
assertTrue(attachment.getAttachMimeTag().getValue().length() > 0);
}
}

@@ -81,18 +81,18 @@ public class TestFileWithAttachmentsRead extends TestCase {

// Now check in detail
attachment = twoSimpleAttachments.getAttachmentFiles()[0];
assertEquals("TEST-U~1.DOC", attachment.attachFileName.toString());
assertEquals("test-unicode.doc", attachment.attachLongFileName.toString());
assertEquals(".doc", attachment.attachExtension.getValue());
assertEquals(null, attachment.attachMimeTag);
assertEquals(24064, attachment.attachData.getValue().length);
assertEquals("TEST-U~1.DOC", attachment.getAttachFileName().toString());
assertEquals("test-unicode.doc", attachment.getAttachLongFileName().toString());
assertEquals(".doc", attachment.getAttachExtension().getValue());
assertEquals(null, attachment.getAttachMimeTag());
assertEquals(24064, attachment.getAttachData().getValue().length);

attachment = twoSimpleAttachments.getAttachmentFiles()[1];
assertEquals("pj1.txt", attachment.attachFileName.toString());
assertEquals("pj1.txt", attachment.attachLongFileName.toString());
assertEquals(".txt", attachment.attachExtension.getValue());
assertEquals(null, attachment.attachMimeTag);
assertEquals(89, attachment.attachData.getValue().length);
assertEquals("pj1.txt", attachment.getAttachFileName().toString());
assertEquals("pj1.txt", attachment.getAttachLongFileName().toString());
assertEquals(".txt", attachment.getAttachExtension().getValue());
assertEquals(null, attachment.getAttachMimeTag());
assertEquals(89, attachment.getAttachData().getValue().length);
}
/**
@@ -106,24 +106,24 @@ public class TestFileWithAttachmentsRead extends TestCase {

// Second is a PDF
attachment = pdfMsgAttachments.getAttachmentFiles()[1];
assertEquals("smbprn~1.pdf", attachment.attachFileName.toString());
assertEquals("smbprn.00009008.KdcPjl.pdf", attachment.attachLongFileName.toString());
assertEquals(".pdf", attachment.attachExtension.getValue());
assertEquals(null, attachment.attachMimeTag);
assertEquals(null, attachment.attachmentDirectory);
assertEquals(13539, attachment.attachData.getValue().length);
assertEquals("smbprn~1.pdf", attachment.getAttachFileName().toString());
assertEquals("smbprn.00009008.KdcPjl.pdf", attachment.getAttachLongFileName().toString());
assertEquals(".pdf", attachment.getAttachExtension().getValue());
assertEquals(null, attachment.getAttachMimeTag());
assertEquals(null, attachment.getAttachmentDirectory());
assertEquals(13539, attachment.getAttachData().getValue().length);
// First in a nested message
attachment = pdfMsgAttachments.getAttachmentFiles()[0];
assertEquals("Test Attachment", attachment.attachFileName.toString());
assertEquals(null, attachment.attachLongFileName);
assertEquals(null, attachment.attachExtension);
assertEquals(null, attachment.attachMimeTag);
assertEquals(null, attachment.attachData);
assertNotNull(attachment.attachmentDirectory);
assertEquals("Test Attachment", attachment.getAttachFileName().toString());
assertEquals(null, attachment.getAttachLongFileName());
assertEquals(null, attachment.getAttachExtension());
assertEquals(null, attachment.getAttachMimeTag());
assertEquals(null, attachment.getAttachData());
assertNotNull(attachment.getAttachmentDirectory());
// Check we can see some bits of it
MAPIMessage nested = attachment.attachmentDirectory.getAsEmbededMessage();
MAPIMessage nested = attachment.getAttachmentDirectory().getAsEmbededMessage();
assertEquals(1, nested.getRecipientNamesList().length);
assertEquals("Nick Booth", nested.getRecipientNames());
assertEquals("Test Attachment", nested.getConversationTopic());

+ 39
- 44
src/scratchpad/testcases/org/apache/poi/hwpf/HWPFTestCase.java View File

@@ -21,48 +21,43 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import junit.framework.TestCase;

public abstract class HWPFTestCase extends TestCase {
protected HWPFDocFixture _hWPFDocFixture;

protected HWPFTestCase() {
}

@Override
protected void setUp() throws Exception {
super.setUp();
/** @todo verify the constructors */
_hWPFDocFixture = new HWPFDocFixture(this, getTestFile());

_hWPFDocFixture.setUp();
}

protected String getTestFile()
{
return HWPFDocFixture.DEFAULT_TEST_FILE;
}

@Override
protected void tearDown() throws Exception {
if (_hWPFDocFixture != null) {
_hWPFDocFixture.tearDown();
}

_hWPFDocFixture = null;
super.tearDown();
}

public HWPFDocument writeOutAndRead(HWPFDocument doc) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HWPFDocument newDoc;
try {
doc.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
newDoc = new HWPFDocument(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
return newDoc;
}
import org.junit.After;
import org.junit.Before;

public abstract class HWPFTestCase {
protected HWPFDocFixture _hWPFDocFixture;

@Before
public void setUp() throws Exception {
/** @todo verify the constructors */
_hWPFDocFixture = new HWPFDocFixture(this, getTestFile());

_hWPFDocFixture.setUp();
}

protected String getTestFile() {
return HWPFDocFixture.DEFAULT_TEST_FILE;
}

@After
public void tearDown() throws Exception {
if (_hWPFDocFixture != null) {
_hWPFDocFixture.tearDown();
}

_hWPFDocFixture = null;
}

public HWPFDocument writeOutAndRead(HWPFDocument doc) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HWPFDocument newDoc;
try {
doc.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
newDoc = new HWPFDocument(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
return newDoc;
}
}

+ 32
- 47
src/scratchpad/testcases/org/apache/poi/hwpf/TestFieldsTables.java View File

@@ -19,86 +19,71 @@

package org.apache.poi.hwpf;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;

import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.model.FieldsTables;
import org.apache.poi.hwpf.model.FileInformationBlock;
import org.apache.poi.hwpf.model.PlexOfField;
import org.junit.Test;

/**
* Test case for the fields tables, this test is based on the test-fields.doc
* file instead of the usual test.doc.
*
* @author Cedric Bosdonnat <cbosdonnat@novell.com>
*
*/
public class TestFieldsTables extends HWPFTestCase
{
public class TestFieldsTables extends HWPFTestCase {

private static final String EXPECTED[] = {
"[19, 43) - FLD - 0x13; 0x1f\n" + "[43, 54) - FLD - 0x14; 0xff\n"
+ "[54, 59) - FLD - 0x15; 0x81\n",

"[19, 43) - FLD - 0x13; 0x1f\n" + "[43, 54) - FLD - 0x14; 0xff\n"
+ "[54, 59) - FLD - 0x15; 0x81\n",

"[31, 59) - FLD - 0x13; 0x45\n" + "[59, 61) - FLD - 0x14; 0xff\n"
+ "[61, 66) - FLD - 0x15; 0x80\n",

"[23, 49) - FLD - 0x13; 0x11\n" + "[49, 64) - FLD - 0x14; 0xff\n"
+ "[64, 69) - FLD - 0x15; 0x80\n",
"[31, 59) - FLD - 0x13; 0x45\n" + "[59, 61) - FLD - 0x14; 0xff\n"
+ "[61, 66) - FLD - 0x15; 0x80\n",

"[18, 42) - FLD - 0x13; 0x21\n" + "[42, 44) - FLD - 0x14; 0xff\n"
+ "[44, 47) - FLD - 0x15; 0x81\n"
+ "[47, 75) - FLD - 0x13; 0x1d\n"
+ "[75, 85) - FLD - 0x14; 0xff\n"
+ "[85, 91) - FLD - 0x15; 0x81\n",
"[23, 49) - FLD - 0x13; 0x11\n" + "[49, 64) - FLD - 0x14; 0xff\n"
+ "[64, 69) - FLD - 0x15; 0x80\n",

"[30, 54) - FLD - 0x13; 0x20\n" + "[54, 62) - FLD - 0x14; 0xff\n"
+ "[62, 68) - FLD - 0x15; 0x81\n",
"[18, 42) - FLD - 0x13; 0x21\n" + "[42, 44) - FLD - 0x14; 0xff\n"
+ "[44, 47) - FLD - 0x15; 0x81\n"
+ "[47, 75) - FLD - 0x13; 0x1d\n"
+ "[75, 85) - FLD - 0x14; 0xff\n"
+ "[85, 91) - FLD - 0x15; 0x81\n",

"[1, 31) - FLD - 0x13; 0x15\n" + "[31, 51) - FLD - 0x14; 0xff\n"
+ "[51, 541) - FLD - 0x15; 0x81\n",
"[30, 54) - FLD - 0x13; 0x20\n" + "[54, 62) - FLD - 0x14; 0xff\n"
+ "[62, 68) - FLD - 0x15; 0x81\n",

"[19, 47) - FLD - 0x13; 0x19\n" + "[47, 49) - FLD - 0x14; 0xff\n"
+ "[49, 55) - FLD - 0x15; 0x81\n"
"[1, 31) - FLD - 0x13; 0x15\n" + "[31, 51) - FLD - 0x14; 0xff\n"
+ "[51, 541) - FLD - 0x15; 0x81\n",

"[19, 47) - FLD - 0x13; 0x19\n" + "[47, 49) - FLD - 0x14; 0xff\n"
+ "[49, 55) - FLD - 0x15; 0x81\n"
};

public TestFieldsTables()
{
}

@Override
protected String getTestFile()
{
protected String getTestFile() {
return "test-fields.doc";
}

public void testReadFields()
{
@Test
public void testReadFields() {
FileInformationBlock fib = _hWPFDocFixture._fib;
byte[] tableStream = _hWPFDocFixture._tableStream;

FieldsTables fieldsTables = new FieldsTables( tableStream, fib );

for ( int i = 0; i < FieldsDocumentPart.values().length; i++ )
{
FieldsDocumentPart part = FieldsDocumentPart.values()[i];
FieldsTables fieldsTables = new FieldsTables(tableStream, fib);

ArrayList<PlexOfField> fieldsPlexes = fieldsTables
.getFieldsPLCF( part );
String result = dumpPlexes( fieldsPlexes );
assertEquals( EXPECTED[i], result );
int i = 0;
for (FieldsDocumentPart part : FieldsDocumentPart.values()) {
String result = dumpPlexes(fieldsTables.getFieldsPLCF(part));
assertEquals(EXPECTED[i++], result);
}
}

private String dumpPlexes( ArrayList<PlexOfField> fieldsPlexes )
{
private String dumpPlexes(ArrayList<PlexOfField> fieldsPlexes) {
StringBuilder dump = new StringBuilder();
for ( int i = 0; i < fieldsPlexes.size(); i++ )
{
final PlexOfField flds = fieldsPlexes.get( i );
dump.append( flds.toString() + "\n" );
for (PlexOfField flds : fieldsPlexes) {
dump.append(flds.toString() + "\n");
}
return dump.toString();
}

+ 36
- 38
src/scratchpad/testcases/org/apache/poi/hwpf/model/TestListTables.java View File

@@ -17,43 +17,41 @@

package org.apache.poi.hwpf.model;

import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.model.io.*;
public final class TestListTables
extends HWPFTestCase
{
public TestListTables()
{
}
public void testReadWrite()
throws Exception
{
FileInformationBlock fib = _hWPFDocFixture._fib;
byte[] tableStream = _hWPFDocFixture._tableStream;
int listOffset = fib.getFcPlfLst();
int lfoOffset = fib.getFcPlfLfo();
if (listOffset != 0 && fib.getLcbPlfLst() != 0)
{
ListTables listTables = new ListTables (tableStream, fib.getFcPlfLst(),
fib.getFcPlfLfo (), fib.getLcbPlfLfo());
HWPFFileSystem fileSys = new HWPFFileSystem ();
HWPFOutputStream tableOut = fileSys.getStream ("1Table");
listTables.writeListDataTo (fib, tableOut);
listTables.writeListOverridesTo( fib, tableOut);
ListTables newTables = new ListTables (tableOut.toByteArray (), fib.getFcPlfLst(),
fib.getFcPlfLfo (), fib.getLcbPlfLfo());
assertEquals(listTables, newTables);
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFTestCase;
import org.apache.poi.hwpf.model.io.HWPFFileSystem;
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
import org.junit.Test;
public final class TestListTables extends HWPFTestCase {
@Test
public void testReadWrite() throws IOException {
FileInformationBlock fib = _hWPFDocFixture._fib;
byte[] tableStream = _hWPFDocFixture._tableStream;
int listOffset = fib.getFcPlfLst();
int lfoOffset = fib.getFcPlfLfo();
int bLfoOffset = fib.getLcbPlfLfo();
if (listOffset != 0 && bLfoOffset != 0) {
// TODO: this is actually never executed ...
ListTables listTables = new ListTables(tableStream, listOffset, lfoOffset, bLfoOffset);
HWPFFileSystem fileSys = new HWPFFileSystem();
HWPFOutputStream tableOut = fileSys.getStream("1Table");
listTables.writeListDataTo(fib, tableOut);
listTables.writeListOverridesTo(fib, tableOut);
ListTables newTables = new ListTables(tableOut.toByteArray(),
fib.getFcPlfLst(), fib.getFcPlfLfo(), fib.getLcbPlfLfo());
assertEquals(listTables, newTables);
}
}
}

}

+ 49
- 46
src/scratchpad/testcases/org/apache/poi/hwpf/model/TestSavedByTable.java View File

@@ -17,64 +17,67 @@

package org.apache.poi.hwpf.model;

import java.io.*;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.junit.Test;

/**
* Unit test for {@link SavedByTable} and {@link SavedByEntry}.
*
* @author Daniel Noll
*/
public final class TestSavedByTable
extends TestCase
{
public final class TestSavedByTable {

/** The expected entries in the test document. */
private final List expected = Arrays.asList(new Object[] {
new SavedByEntry("cic22", "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
new SavedByEntry("cic22", "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
new SavedByEntry("cic22", "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
new SavedByEntry("JPratt", "C:\\TEMP\\Iraq - security.doc"),
new SavedByEntry("JPratt", "A:\\Iraq - security.doc"),
new SavedByEntry("ablackshaw", "C:\\ABlackshaw\\Iraq - security.doc"),
new SavedByEntry("ablackshaw", "C:\\ABlackshaw\\A;Iraq - security.doc"),
new SavedByEntry("ablackshaw", "A:\\Iraq - security.doc"),
new SavedByEntry("MKhan", "C:\\TEMP\\Iraq - security.doc"),
new SavedByEntry("MKhan", "C:\\WINNT\\Profiles\\mkhan\\Desktop\\Iraq.doc")
});
/** The expected entries in the test document. */
private final List<SavedByEntry> expected = Arrays.asList(
new SavedByEntry("cic22", "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
new SavedByEntry("cic22", "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
new SavedByEntry("cic22", "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
new SavedByEntry("JPratt", "C:\\TEMP\\Iraq - security.doc"),
new SavedByEntry("JPratt", "A:\\Iraq - security.doc"),
new SavedByEntry("ablackshaw", "C:\\ABlackshaw\\Iraq - security.doc"),
new SavedByEntry("ablackshaw", "C:\\ABlackshaw\\A;Iraq - security.doc"),
new SavedByEntry("ablackshaw", "A:\\Iraq - security.doc"),
new SavedByEntry("MKhan", "C:\\TEMP\\Iraq - security.doc"),
new SavedByEntry("MKhan", "C:\\WINNT\\Profiles\\mkhan\\Desktop\\Iraq.doc")
);

/**
* Tests reading in the entries, comparing them against the expected entries.
* Then tests writing the document out and reading the entries yet again.
*
* @throws Exception if an unexpected error occurs.
*/
public void testReadWrite()
throws Exception
{
// This document is widely available on the internet as "blair.doc".
// I tried stripping the content and saving the document but my version
// of Word (from Office XP) strips this table out.
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("saved-by-table.doc");
/**
* Tests reading in the entries, comparing them against the expected
* entries. Then tests writing the document out and reading the entries yet
* again.
*
* @throws Exception if an unexpected error occurs.
*/
@Test
public void testReadWrite() throws IOException {
// This document is widely available on the internet as "blair.doc".
// I tried stripping the content and saving the document but my version
// of Word (from Office XP) strips this table out.
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("saved-by-table.doc");

// Check what we just read.
assertEquals("List of saved-by entries was not as expected",
expected, doc.getSavedByTable().getEntries());
// Check what we just read.
assertEquals("List of saved-by entries was not as expected", expected,
doc.getSavedByTable().getEntries());

// Now write the entire document out, and read it back in...
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
doc.write(byteStream);
InputStream copyStream = new ByteArrayInputStream(byteStream.toByteArray());
HWPFDocument copy = new HWPFDocument(copyStream);
// Now write the entire document out, and read it back in...
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
doc.write(byteStream);
InputStream copyStream = new ByteArrayInputStream(byteStream.toByteArray());
doc.close();
HWPFDocument copy = new HWPFDocument(copyStream);

// And check again.
assertEquals("List of saved-by entries was incorrect after writing",
expected, copy.getSavedByTable().getEntries());
}
// And check again.
assertEquals("List of saved-by entries was incorrect after writing",
expected, copy.getSavedByTable().getEntries());
copy.close();
}
}

Loading…
Cancel
Save