introduce DocumentPart enum and simplify fields access API

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148280 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-19 11:49:48 +00:00
parent 88a00c69c5
commit 05c7df2042
7 changed files with 127 additions and 111 deletions

View File

@ -23,8 +23,8 @@ import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore; import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.converter.FontReplacer.Triplet; import org.apache.poi.hwpf.converter.FontReplacer.Triplet;
import org.apache.poi.hwpf.model.DocumentPart;
import org.apache.poi.hwpf.model.Field; import org.apache.poi.hwpf.model.Field;
import org.apache.poi.hwpf.model.FieldsTables;
import org.apache.poi.hwpf.model.ListFormatOverride; import org.apache.poi.hwpf.model.ListFormatOverride;
import org.apache.poi.hwpf.model.ListTables; import org.apache.poi.hwpf.model.ListTables;
import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.CharacterRun;
@ -111,7 +111,7 @@ public abstract class AbstractWordConverter
{ {
Field aliveField = ( (HWPFDocument) hwpfDocument ) Field aliveField = ( (HWPFDocument) hwpfDocument )
.getFieldsTables().lookupFieldByStartOffset( .getFieldsTables().lookupFieldByStartOffset(
FieldsTables.PLCFFLDMOM, DocumentPart.MAIN,
characterRun.getStartOffset() ); characterRun.getStartOffset() );
if ( aliveField != null ) if ( aliveField != null )
{ {
@ -309,7 +309,7 @@ public abstract class AbstractWordConverter
HWPFDocument hwpfDocument = (HWPFDocument) wordDocument; HWPFDocument hwpfDocument = (HWPFDocument) wordDocument;
Field field = hwpfDocument.getFieldsTables().lookupFieldByStartOffset( Field field = hwpfDocument.getFieldsTables().lookupFieldByStartOffset(
FieldsTables.PLCFFLDMOM, startOffset ); DocumentPart.MAIN, startOffset );
if ( field == null ) if ( field == null )
return null; return null;

View File

@ -36,7 +36,7 @@ import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.HWPFOldDocument; import org.apache.poi.hwpf.HWPFOldDocument;
import org.apache.poi.hwpf.OldWordFileFormatException; import org.apache.poi.hwpf.OldWordFileFormatException;
import org.apache.poi.hwpf.model.CHPX; import org.apache.poi.hwpf.model.CHPX;
import org.apache.poi.hwpf.model.FieldsTables; import org.apache.poi.hwpf.model.DocumentPart;
import org.apache.poi.hwpf.model.FileInformationBlock; import org.apache.poi.hwpf.model.FileInformationBlock;
import org.apache.poi.hwpf.model.GenericPropertyNode; import org.apache.poi.hwpf.model.GenericPropertyNode;
import org.apache.poi.hwpf.model.PAPFormattedDiskPage; import org.apache.poi.hwpf.model.PAPFormattedDiskPage;
@ -327,11 +327,11 @@ public final class HWPFLister
HWPFDocument document = (HWPFDocument) _doc; HWPFDocument document = (HWPFDocument) _doc;
for ( int i = FieldsTables.PLCFFLDATN; i <= FieldsTables.PLCFFLDTXBX; i++ ) for ( DocumentPart part : DocumentPart.values() )
{ {
System.out.println( "=== Document part: " + i + " ===" ); System.out.println( "=== Document part: " + part + " ===" );
for ( org.apache.poi.hwpf.model.Field field : document for ( org.apache.poi.hwpf.model.Field field : document
.getFieldsTables().getFields( i ) ) .getFieldsTables().getFields( part ) )
{ {
System.out.println( field ); System.out.println( field );
} }

View File

@ -0,0 +1,46 @@
package org.apache.poi.hwpf.model;
public enum DocumentPart {
/**
* annotation subdocument
*/
ANNOTATIONS( FIBFieldHandler.PLCFFLDATN ),
/**
* endnote subdocument
*/
ENDNOTES( FIBFieldHandler.PLCFFLDEDN ),
/**
* footnote subdocument
*/
FOOTNOTES( FIBFieldHandler.PLCFFLDFTN ),
/**
* header subdocument
*/
HEADER( FIBFieldHandler.PLCFFLDHDR ),
/**
* header textbox subdoc
*/
HEADER_TEXTBOX( FIBFieldHandler.PLCFFLDHDRTXBX ),
/**
* main document
*/
MAIN( FIBFieldHandler.PLCFFLDMOM ),
/**
* textbox subdoc
*/
TEXTBOX( FIBFieldHandler.PLCFFLDTXBX );
private final int fibHandlerFieldsField;
private DocumentPart( final int fibHandlerField )
{
this.fibHandlerFieldsField = fibHandlerField;
}
public int getFibHandlerFieldsPosition()
{
return fibHandlerFieldsField;
}
}

View File

@ -55,50 +55,60 @@ public class FieldsTables
/** /**
* annotation subdocument * annotation subdocument
*/ */
@Deprecated
public static final int PLCFFLDATN = 0; public static final int PLCFFLDATN = 0;
/** /**
* endnote subdocument * endnote subdocument
*/ */
@Deprecated
public static final int PLCFFLDEDN = 1; public static final int PLCFFLDEDN = 1;
/** /**
* footnote subdocument * footnote subdocument
*/ */
@Deprecated
public static final int PLCFFLDFTN = 2; public static final int PLCFFLDFTN = 2;
/** /**
* header subdocument * header subdocument
*/ */
@Deprecated
public static final int PLCFFLDHDR = 3; public static final int PLCFFLDHDR = 3;
/** /**
* header textbox subdoc * header textbox subdoc
*/ */
@Deprecated
public static final int PLCFFLDHDRTXBX = 4; public static final int PLCFFLDHDRTXBX = 4;
/** /**
* main document * main document
*/ */
@Deprecated
public static final int PLCFFLDMOM = 5; public static final int PLCFFLDMOM = 5;
/** /**
* textbox subdoc * textbox subdoc
*/ */
@Deprecated
public static final int PLCFFLDTXBX = 6; public static final int PLCFFLDTXBX = 6;
// The size in bytes of the FLD data structure // The size in bytes of the FLD data structure
private static final int FLD_SIZE = 2; private static final int FLD_SIZE = 2;
private Map<Integer, PlexOfCps> _tables; private Map<DocumentPart, PlexOfCps> _tables;
private Map<Integer, Map<Integer, Field>> _fieldsByOffset; private Map<DocumentPart, Map<Integer, Field>> _fieldsByOffset;
public FieldsTables( byte[] tableStream, FileInformationBlock fib ) public FieldsTables( byte[] tableStream, FileInformationBlock fib )
{ {
_tables = new HashMap<Integer, PlexOfCps>( PLCFFLDTXBX - PLCFFLDATN + 1 ); _tables = new HashMap<DocumentPart, PlexOfCps>(
_fieldsByOffset = new HashMap<Integer, Map<Integer, Field>>( DocumentPart.values().length );
PLCFFLDTXBX - PLCFFLDATN + 1 ); _fieldsByOffset = new HashMap<DocumentPart, Map<Integer, Field>>(
DocumentPart.values().length );
for ( int i = PLCFFLDATN; i <= PLCFFLDTXBX; i++ ) for ( DocumentPart documentPart : DocumentPart.values() )
{ {
final PlexOfCps plexOfCps = readPLCF( tableStream, fib, i ); final PlexOfCps plexOfCps = readPLCF( tableStream, fib,
_fieldsByOffset.put( Integer.valueOf( i ), documentPart );
parseFieldStructure( plexOfCps ) );
_tables.put( Integer.valueOf( i ), plexOfCps ); _fieldsByOffset
.put( documentPart, parseFieldStructure( plexOfCps ) );
_tables.put( documentPart, plexOfCps );
} }
} }
@ -288,10 +298,9 @@ public class FieldsTables
} }
} }
public Field lookupFieldByStartOffset( int documentPart, int offset ) public Field lookupFieldByStartOffset( DocumentPart documentPart, int offset )
{ {
Map<Integer, Field> map = _fieldsByOffset.get( Integer Map<Integer, Field> map = _fieldsByOffset.get( documentPart);
.valueOf( documentPart ) );
if ( map == null || map.isEmpty() ) if ( map == null || map.isEmpty() )
return null; return null;
@ -299,44 +308,10 @@ public class FieldsTables
} }
private PlexOfCps readPLCF( byte[] tableStream, FileInformationBlock fib, private PlexOfCps readPLCF( byte[] tableStream, FileInformationBlock fib,
int type ) DocumentPart documentPart )
{ {
int start = 0; int start = fib.getFieldsPlcfOffset( documentPart );
int length = 0; int length = fib.getFieldsPlcfLength( documentPart );
switch ( type )
{
case PLCFFLDATN:
start = fib.getFcPlcffldAtn();
length = fib.getLcbPlcffldAtn();
break;
case PLCFFLDEDN:
start = fib.getFcPlcffldEdn();
length = fib.getLcbPlcffldEdn();
break;
case PLCFFLDFTN:
start = fib.getFcPlcffldFtn();
length = fib.getLcbPlcffldFtn();
break;
case PLCFFLDHDR:
start = fib.getFcPlcffldHdr();
length = fib.getLcbPlcffldHdr();
break;
case PLCFFLDHDRTXBX:
start = fib.getFcPlcffldHdrtxbx();
length = fib.getLcbPlcffldHdrtxbx();
break;
case PLCFFLDMOM:
start = fib.getFcPlcffldMom();
length = fib.getLcbPlcffldMom();
break;
case PLCFFLDTXBX:
start = fib.getFcPlcffldTxbx();
length = fib.getLcbPlcffldTxbx();
break;
default:
break;
}
if ( start <= 0 || length <= 0 ) if ( start <= 0 || length <= 0 )
return null; return null;
@ -344,19 +319,24 @@ public class FieldsTables
return new PlexOfCps( tableStream, start, length, FLD_SIZE ); return new PlexOfCps( tableStream, start, length, FLD_SIZE );
} }
public Collection<Field> getFields( int documentPart ) public Collection<Field> getFields( DocumentPart part )
{ {
Map<Integer, Field> map = _fieldsByOffset.get( Integer Map<Integer, Field> map = _fieldsByOffset.get( part );
.valueOf( documentPart ) );
if ( map == null || map.isEmpty() ) if ( map == null || map.isEmpty() )
return Collections.emptySet(); return Collections.emptySet();
return Collections.unmodifiableCollection( map.values() ); return Collections.unmodifiableCollection( map.values() );
} }
public ArrayList<PlexOfField> getFieldsPLCF( int type ) @Deprecated
public ArrayList<PlexOfField> getFieldsPLCF( int partIndex )
{ {
return toArrayList( _tables.get( Integer.valueOf( type ) ) ); return getFieldsPLCF( DocumentPart.values()[partIndex] );
}
public ArrayList<PlexOfField> getFieldsPLCF( DocumentPart documentPart )
{
return toArrayList( _tables.get( documentPart ) );
} }
private static ArrayList<PlexOfField> toArrayList( PlexOfCps plexOfCps ) private static ArrayList<PlexOfField> toArrayList( PlexOfCps plexOfCps )
@ -377,8 +357,8 @@ public class FieldsTables
return fields; return fields;
} }
private int savePlex( PlexOfCps plexOfCps, int type, private int savePlex( FileInformationBlock fib, DocumentPart documentPart,
FileInformationBlock fib, HWPFOutputStream outputStream ) PlexOfCps plexOfCps, HWPFOutputStream outputStream )
throws IOException throws IOException
{ {
if ( plexOfCps == null || plexOfCps.length() == 0 ) if ( plexOfCps == null || plexOfCps.length() == 0 )
@ -391,39 +371,8 @@ public class FieldsTables
outputStream.write( data ); outputStream.write( data );
switch ( type ) fib.setFieldsPlcfOffset( documentPart, start );
{ fib.setFieldsPlcfLength( documentPart, length );
case PLCFFLDATN:
fib.setFcPlcffldAtn( start );
fib.setLcbPlcffldAtn( length );
break;
case PLCFFLDEDN:
fib.setFcPlcffldEdn( start );
fib.setLcbPlcffldEdn( length );
break;
case PLCFFLDFTN:
fib.setFcPlcffldFtn( start );
fib.setLcbPlcffldFtn( length );
break;
case PLCFFLDHDR:
fib.setFcPlcffldHdr( start );
fib.setLcbPlcffldHdr( length );
break;
case PLCFFLDHDRTXBX:
fib.setFcPlcffldHdrtxbx( start );
fib.setLcbPlcffldHdrtxbx( length );
break;
case PLCFFLDMOM:
fib.setFcPlcffldMom( start );
fib.setLcbPlcffldMom( length );
break;
case PLCFFLDTXBX:
fib.setFcPlcffldTxbx( start );
fib.setLcbPlcffldTxbx( length );
break;
default:
return 0;
}
return length; return length;
} }
@ -431,10 +380,10 @@ public class FieldsTables
public void write( FileInformationBlock fib, HWPFOutputStream tableStream ) public void write( FileInformationBlock fib, HWPFOutputStream tableStream )
throws IOException throws IOException
{ {
for ( int i = PLCFFLDATN; i <= PLCFFLDTXBX; i++ ) for ( DocumentPart part : DocumentPart.values() )
{ {
PlexOfCps plexOfCps = _tables.get( Integer.valueOf( i ) ); PlexOfCps plexOfCps = _tables.get( part );
savePlex( plexOfCps, i, fib, tableStream ); savePlex( fib, part, plexOfCps, tableStream );
} }
} }
} }

View File

@ -460,6 +460,30 @@ public final class FileInformationBlock extends FIBAbstractType
_fieldHandler.clearFields(); _fieldHandler.clearFields();
} }
public int getFieldsPlcfOffset( DocumentPart documentPart )
{
return _fieldHandler.getFieldOffset( documentPart
.getFibHandlerFieldsPosition() );
}
public int getFieldsPlcfLength( DocumentPart documentPart )
{
return _fieldHandler.getFieldSize( documentPart
.getFibHandlerFieldsPosition() );
}
public void setFieldsPlcfOffset( DocumentPart documentPart, int offset )
{
_fieldHandler.setFieldOffset(
documentPart.getFibHandlerFieldsPosition(), offset );
}
public void setFieldsPlcfLength( DocumentPart documentPart, int length )
{
_fieldHandler.setFieldSize( documentPart.getFibHandlerFieldsPosition(),
length );
}
public int getFcPlcffldAtn() public int getFcPlcffldAtn()
{ {
return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDATN); return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDATN);

View File

@ -21,8 +21,7 @@ package org.apache.poi.hwpf;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.poi.hwpf.model.FieldDescriptor; import org.apache.poi.hwpf.model.DocumentPart;
import org.apache.poi.hwpf.model.FieldsTables; import org.apache.poi.hwpf.model.FieldsTables;
import org.apache.poi.hwpf.model.FileInformationBlock; import org.apache.poi.hwpf.model.FileInformationBlock;
import org.apache.poi.hwpf.model.PlexOfField; import org.apache.poi.hwpf.model.PlexOfField;
@ -36,10 +35,6 @@ import org.apache.poi.hwpf.model.PlexOfField;
*/ */
public class TestFieldsTables extends HWPFTestCase public class TestFieldsTables extends HWPFTestCase
{ {
private static final int ALL_TYPES[] = { FieldsTables.PLCFFLDATN,
FieldsTables.PLCFFLDEDN, FieldsTables.PLCFFLDFTN,
FieldsTables.PLCFFLDHDR, FieldsTables.PLCFFLDHDRTXBX,
FieldsTables.PLCFFLDMOM, FieldsTables.PLCFFLDTXBX };
private static final String EXPECTED[] = { private static final String EXPECTED[] = {
@ -85,10 +80,12 @@ public class TestFieldsTables extends HWPFTestCase
FieldsTables fieldsTables = new FieldsTables( tableStream, fib ); FieldsTables fieldsTables = new FieldsTables( tableStream, fib );
for ( int i = 0; i < ALL_TYPES.length; i++ ) for ( int i = 0; i < DocumentPart.values().length; i++ )
{ {
DocumentPart part = DocumentPart.values()[i];
ArrayList<PlexOfField> fieldsPlexes = fieldsTables ArrayList<PlexOfField> fieldsPlexes = fieldsTables
.getFieldsPLCF( ALL_TYPES[i] ); .getFieldsPLCF( part );
String result = dumpPlexes( fieldsPlexes ); String result = dumpPlexes( fieldsPlexes );
assertEquals( EXPECTED[i], result ); assertEquals( EXPECTED[i], result );
} }

View File

@ -32,7 +32,7 @@ import org.apache.poi.hwpf.HWPFTestCase;
import org.apache.poi.hwpf.HWPFTestDataSamples; import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.apache.poi.hwpf.extractor.Word6Extractor; import org.apache.poi.hwpf.extractor.Word6Extractor;
import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.model.FieldsTables; import org.apache.poi.hwpf.model.DocumentPart;
import org.apache.poi.hwpf.model.PlexOfField; import org.apache.poi.hwpf.model.PlexOfField;
import org.apache.poi.hwpf.model.StyleSheet; import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
@ -554,9 +554,9 @@ public final class TestProblems extends HWPFTestCase {
.getCharacterTable().getTextRuns().size() ); .getCharacterTable().getTextRuns().size() );
List<PlexOfField> expectedFields = doc1.getFieldsTables() List<PlexOfField> expectedFields = doc1.getFieldsTables()
.getFieldsPLCF( FieldsTables.PLCFFLDMOM ); .getFieldsPLCF( DocumentPart.MAIN );
List<PlexOfField> actualFields = doc2.getFieldsTables() List<PlexOfField> actualFields = doc2.getFieldsTables()
.getFieldsPLCF( FieldsTables.PLCFFLDMOM ); .getFieldsPLCF( DocumentPart.MAIN );
assertEquals( expectedFields.size(), actualFields.size() ); assertEquals( expectedFields.size(), actualFields.size() );
assertTableStructures( doc1.getRange(), doc2.getRange() ); assertTableStructures( doc1.getRange(), doc2.getRange() );