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.
*/
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.

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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;
}
}

View File

@ -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());