From 4aedf8cb5e652577adb7ead1839613909b234deb Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 8 Jan 2010 13:49:09 +0000 Subject: [PATCH] Quick bit of refactoring to save parsing the type and id twice git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@897205 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hsmf/datatypes/ByteChunk.java | 7 ++-- .../org/apache/poi/hsmf/datatypes/Chunk.java | 20 ++++-------- .../datatypes/MessageSubmissionChunk.java | 4 +-- .../poi/hsmf/datatypes/StringChunk.java | 4 +-- .../poi/hsmf/parsers/POIFSChunkParser.java | 32 ++++++++++++------- .../poi/hsmf/datatypes/TestChunkData.java | 2 +- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java index 166a5feef4..06ff28cccc 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java @@ -36,10 +36,9 @@ public class ByteChunk extends Chunk { /** * Creates a Byte Chunk. */ - public ByteChunk(String entryName) { - super(entryName); - } - + public ByteChunk(String namePrefix, int chunkId, int type) { + super(namePrefix, chunkId, type); + } /** * Create a Byte Chunk, with the specified * type. diff --git a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/Chunk.java b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/Chunk.java index 9e6748ab11..7fcb252c96 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/Chunk.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/Chunk.java @@ -28,21 +28,13 @@ abstract public class Chunk { protected int type; protected String namePrefix; - protected Chunk(String entryName) { - int splitAt = entryName.lastIndexOf('_'); - if(splitAt == -1 || splitAt > (entryName.length()-8)) { - throw new IllegalArgumentException("Invalid chunk name " + entryName); - } - - namePrefix = entryName.substring(0, splitAt+1); - String ids = entryName.substring(splitAt+1); - chunkId = Integer.parseInt(ids.substring(0, 4), 16); - type = Integer.parseInt(ids.substring(4, 8), 16); - } + protected Chunk(String namePrefix, int chunkId, int type) { + this.namePrefix = namePrefix; + this.chunkId = chunkId; + this.type = type; + } protected Chunk(int chunkId, int type) { - namePrefix = DEFAULT_NAME_PREFIX; - this.chunkId = chunkId; - this.type = type; + this(DEFAULT_NAME_PREFIX, chunkId, type); } /** diff --git a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/MessageSubmissionChunk.java b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/MessageSubmissionChunk.java index 01793aba59..7ca6fee3f6 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/MessageSubmissionChunk.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/MessageSubmissionChunk.java @@ -44,8 +44,8 @@ public class MessageSubmissionChunk extends Chunk { /** * Creates a Byte Chunk. */ - public MessageSubmissionChunk(String entryName) { - super(entryName); + public MessageSubmissionChunk(String namePrefix, int chunkId, int type) { + super(namePrefix, chunkId, type); } /** diff --git a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java index b1f859d04f..5dfe116294 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java @@ -36,8 +36,8 @@ public class StringChunk extends Chunk { /** * Creates a String Chunk. */ - public StringChunk(String entryName) { - super(entryName); + public StringChunk(String namePrefix, int chunkId, int type) { + super(namePrefix, chunkId, type); } /** diff --git a/src/scratchpad/src/org/apache/poi/hsmf/parsers/POIFSChunkParser.java b/src/scratchpad/src/org/apache/poi/hsmf/parsers/POIFSChunkParser.java index 34a4d27d7e..3a38d1b3d0 100644 --- a/src/scratchpad/src/org/apache/poi/hsmf/parsers/POIFSChunkParser.java +++ b/src/scratchpad/src/org/apache/poi/hsmf/parsers/POIFSChunkParser.java @@ -102,39 +102,47 @@ public final class POIFSChunkParser { * Creates a chunk, and gives it to its parent group */ protected static void process(DocumentNode entry, ChunkGroup grouping) { - if(entry.getName().length() < 9) { + String entryName = entry.getName(); + + if(entryName.length() < 9) { // Name in the wrong format return; } - if(entry.getName().indexOf('_') == -1) { + if(entryName.indexOf('_') == -1) { // Name in the wrong format return; } - // See if we can get a type for it - String idType = entry.getName().substring(entry.getName().length()-8); - String idS = idType.substring(0, 4); - String typeS = idType.substring(4); + // Split it into its parts + int splitAt = entryName.lastIndexOf('_'); + if(splitAt == -1 || splitAt > (entryName.length()-8)) { + throw new IllegalArgumentException("Invalid chunk name " + entryName); + } + + // Now try to turn it into id + type + String namePrefix = entryName.substring(0, splitAt+1); + String ids = entryName.substring(splitAt+1); try { - int id = Integer.parseInt(idS, 16); - int type = Integer.parseInt(typeS, 16); + int chunkId = Integer.parseInt(ids.substring(0, 4), 16); + int type = Integer.parseInt(ids.substring(4, 8), 16); + Chunk chunk = null; // Special cases based on the ID - switch(id) { + switch(chunkId) { case Chunks.SUBMISSION_ID_DATE: - chunk = new MessageSubmissionChunk(entry.getName()); + chunk = new MessageSubmissionChunk(namePrefix, chunkId, type); break; default: // Nothing special about this ID // So, do the usual thing which is by type switch(type) { case Types.BINARY: - chunk = new ByteChunk(entry.getName()); + chunk = new ByteChunk(namePrefix, chunkId, type); break; case Types.ASCII_STRING: case Types.UNICODE_STRING: - chunk = new StringChunk(entry.getName()); + chunk = new StringChunk(namePrefix, chunkId, type); break; } } diff --git a/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestChunkData.java b/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestChunkData.java index 39440d13b6..4e2e9a7cdc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestChunkData.java +++ b/src/scratchpad/testcases/org/apache/poi/hsmf/datatypes/TestChunkData.java @@ -40,7 +40,7 @@ public final class TestChunkData extends TestCase { assertEquals(0x0200, chunk.getChunkId()); assertEquals(0x001E, chunk.getType()); - chunk = new StringChunk("__substg1.0_0200001E"); + chunk = new StringChunk("__substg1.0_", 0x0200, 0x001E); assertEquals("__substg1.0_0200001E", chunk.getEntryName()); assertEquals(0x0200, chunk.getChunkId()); assertEquals(0x001E, chunk.getType()); -- 2.39.5