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
This commit is contained in:
Nick Burch 2010-01-08 13:49:09 +00:00
parent ff94e5c61b
commit 52695c1811
6 changed files with 34 additions and 35 deletions

View File

@ -36,10 +36,9 @@ public class ByteChunk extends Chunk {
/** /**
* Creates a Byte Chunk. * Creates a Byte Chunk.
*/ */
public ByteChunk(String entryName) { public ByteChunk(String namePrefix, int chunkId, int type) {
super(entryName); super(namePrefix, chunkId, type);
} }
/** /**
* Create a Byte Chunk, with the specified * Create a Byte Chunk, with the specified
* type. * type.

View File

@ -28,21 +28,13 @@ abstract public class Chunk {
protected int type; protected int type;
protected String namePrefix; protected String namePrefix;
protected Chunk(String entryName) { protected Chunk(String namePrefix, int chunkId, int type) {
int splitAt = entryName.lastIndexOf('_'); this.namePrefix = namePrefix;
if(splitAt == -1 || splitAt > (entryName.length()-8)) { this.chunkId = chunkId;
throw new IllegalArgumentException("Invalid chunk name " + entryName); this.type = type;
} }
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(int chunkId, int type) { protected Chunk(int chunkId, int type) {
namePrefix = DEFAULT_NAME_PREFIX; this(DEFAULT_NAME_PREFIX, chunkId, type);
this.chunkId = chunkId;
this.type = type;
} }
/** /**

View File

@ -44,8 +44,8 @@ public class MessageSubmissionChunk extends Chunk {
/** /**
* Creates a Byte Chunk. * Creates a Byte Chunk.
*/ */
public MessageSubmissionChunk(String entryName) { public MessageSubmissionChunk(String namePrefix, int chunkId, int type) {
super(entryName); super(namePrefix, chunkId, type);
} }
/** /**

View File

@ -36,8 +36,8 @@ public class StringChunk extends Chunk {
/** /**
* Creates a String Chunk. * Creates a String Chunk.
*/ */
public StringChunk(String entryName) { public StringChunk(String namePrefix, int chunkId, int type) {
super(entryName); super(namePrefix, chunkId, type);
} }
/** /**

View File

@ -102,39 +102,47 @@ public final class POIFSChunkParser {
* Creates a chunk, and gives it to its parent group * Creates a chunk, and gives it to its parent group
*/ */
protected static void process(DocumentNode entry, ChunkGroup grouping) { 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 // Name in the wrong format
return; return;
} }
if(entry.getName().indexOf('_') == -1) { if(entryName.indexOf('_') == -1) {
// Name in the wrong format // Name in the wrong format
return; return;
} }
// See if we can get a type for it // Split it into its parts
String idType = entry.getName().substring(entry.getName().length()-8); int splitAt = entryName.lastIndexOf('_');
String idS = idType.substring(0, 4); if(splitAt == -1 || splitAt > (entryName.length()-8)) {
String typeS = idType.substring(4); 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 { try {
int id = Integer.parseInt(idS, 16); int chunkId = Integer.parseInt(ids.substring(0, 4), 16);
int type = Integer.parseInt(typeS, 16); int type = Integer.parseInt(ids.substring(4, 8), 16);
Chunk chunk = null; Chunk chunk = null;
// Special cases based on the ID // Special cases based on the ID
switch(id) { switch(chunkId) {
case Chunks.SUBMISSION_ID_DATE: case Chunks.SUBMISSION_ID_DATE:
chunk = new MessageSubmissionChunk(entry.getName()); chunk = new MessageSubmissionChunk(namePrefix, chunkId, type);
break; break;
default: default:
// Nothing special about this ID // Nothing special about this ID
// So, do the usual thing which is by type // So, do the usual thing which is by type
switch(type) { switch(type) {
case Types.BINARY: case Types.BINARY:
chunk = new ByteChunk(entry.getName()); chunk = new ByteChunk(namePrefix, chunkId, type);
break; break;
case Types.ASCII_STRING: case Types.ASCII_STRING:
case Types.UNICODE_STRING: case Types.UNICODE_STRING:
chunk = new StringChunk(entry.getName()); chunk = new StringChunk(namePrefix, chunkId, type);
break; break;
} }
} }

View File

@ -40,7 +40,7 @@ public final class TestChunkData extends TestCase {
assertEquals(0x0200, chunk.getChunkId()); assertEquals(0x0200, chunk.getChunkId());
assertEquals(0x001E, chunk.getType()); assertEquals(0x001E, chunk.getType());
chunk = new StringChunk("__substg1.0_0200001E"); chunk = new StringChunk("__substg1.0_", 0x0200, 0x001E);
assertEquals("__substg1.0_0200001E", chunk.getEntryName()); assertEquals("__substg1.0_0200001E", chunk.getEntryName());
assertEquals(0x0200, chunk.getChunkId()); assertEquals(0x0200, chunk.getChunkId());
assertEquals(0x001E, chunk.getType()); assertEquals(0x001E, chunk.getType());