From 54712802902213362c67a081ce7611dcf56b0ef1 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Tue, 19 Jul 2011 11:49:48 +0000 Subject: [PATCH] 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 --- .../hwpf/converter/AbstractWordConverter.java | 6 +- .../org/apache/poi/hwpf/dev/HWPFLister.java | 8 +- .../apache/poi/hwpf/model/DocumentPart.java | 46 ++++++ .../apache/poi/hwpf/model/FieldsTables.java | 135 ++++++------------ .../poi/hwpf/model/FileInformationBlock.java | 24 ++++ .../org/apache/poi/hwpf/TestFieldsTables.java | 13 +- .../poi/hwpf/usermodel/TestProblems.java | 6 +- 7 files changed, 127 insertions(+), 111 deletions(-) create mode 100644 src/scratchpad/src/org/apache/poi/hwpf/model/DocumentPart.java diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java index d83b91b7bb..236ac67ebd 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java @@ -23,8 +23,8 @@ import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocumentCore; 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.FieldsTables; import org.apache.poi.hwpf.model.ListFormatOverride; import org.apache.poi.hwpf.model.ListTables; import org.apache.poi.hwpf.usermodel.CharacterRun; @@ -111,7 +111,7 @@ public abstract class AbstractWordConverter { Field aliveField = ( (HWPFDocument) hwpfDocument ) .getFieldsTables().lookupFieldByStartOffset( - FieldsTables.PLCFFLDMOM, + DocumentPart.MAIN, characterRun.getStartOffset() ); if ( aliveField != null ) { @@ -309,7 +309,7 @@ public abstract class AbstractWordConverter HWPFDocument hwpfDocument = (HWPFDocument) wordDocument; Field field = hwpfDocument.getFieldsTables().lookupFieldByStartOffset( - FieldsTables.PLCFFLDMOM, startOffset ); + DocumentPart.MAIN, startOffset ); if ( field == null ) return null; diff --git a/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java b/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java index 76ca6992d8..1c25535427 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java @@ -36,7 +36,7 @@ import org.apache.poi.hwpf.HWPFDocumentCore; import org.apache.poi.hwpf.HWPFOldDocument; import org.apache.poi.hwpf.OldWordFileFormatException; 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.GenericPropertyNode; import org.apache.poi.hwpf.model.PAPFormattedDiskPage; @@ -327,11 +327,11 @@ public final class HWPFLister 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 - .getFieldsTables().getFields( i ) ) + .getFieldsTables().getFields( part ) ) { System.out.println( field ); } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/DocumentPart.java b/src/scratchpad/src/org/apache/poi/hwpf/model/DocumentPart.java new file mode 100644 index 0000000000..90bfa1b1f5 --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/DocumentPart.java @@ -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; + } + +} diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/FieldsTables.java b/src/scratchpad/src/org/apache/poi/hwpf/model/FieldsTables.java index 0141f49e94..74f394ea2c 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/FieldsTables.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/FieldsTables.java @@ -55,50 +55,60 @@ public class FieldsTables /** * annotation subdocument */ + @Deprecated public static final int PLCFFLDATN = 0; /** * endnote subdocument */ + @Deprecated public static final int PLCFFLDEDN = 1; /** * footnote subdocument */ + @Deprecated public static final int PLCFFLDFTN = 2; /** * header subdocument */ + @Deprecated public static final int PLCFFLDHDR = 3; /** * header textbox subdoc */ + @Deprecated public static final int PLCFFLDHDRTXBX = 4; /** * main document */ + @Deprecated public static final int PLCFFLDMOM = 5; /** * textbox subdoc */ + @Deprecated public static final int PLCFFLDTXBX = 6; // The size in bytes of the FLD data structure private static final int FLD_SIZE = 2; - private Map _tables; - private Map> _fieldsByOffset; + private Map _tables; + private Map> _fieldsByOffset; public FieldsTables( byte[] tableStream, FileInformationBlock fib ) { - _tables = new HashMap( PLCFFLDTXBX - PLCFFLDATN + 1 ); - _fieldsByOffset = new HashMap>( - PLCFFLDTXBX - PLCFFLDATN + 1 ); + _tables = new HashMap( + DocumentPart.values().length ); + _fieldsByOffset = new HashMap>( + DocumentPart.values().length ); - for ( int i = PLCFFLDATN; i <= PLCFFLDTXBX; i++ ) + for ( DocumentPart documentPart : DocumentPart.values() ) { - final PlexOfCps plexOfCps = readPLCF( tableStream, fib, i ); - _fieldsByOffset.put( Integer.valueOf( i ), - parseFieldStructure( plexOfCps ) ); - _tables.put( Integer.valueOf( i ), plexOfCps ); + final PlexOfCps plexOfCps = readPLCF( tableStream, fib, + documentPart ); + + _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 map = _fieldsByOffset.get( Integer - .valueOf( documentPart ) ); + Map map = _fieldsByOffset.get( documentPart); if ( map == null || map.isEmpty() ) return null; @@ -299,44 +308,10 @@ public class FieldsTables } private PlexOfCps readPLCF( byte[] tableStream, FileInformationBlock fib, - int type ) + DocumentPart documentPart ) { - int start = 0; - int length = 0; - - 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; - } + int start = fib.getFieldsPlcfOffset( documentPart ); + int length = fib.getFieldsPlcfLength( documentPart ); if ( start <= 0 || length <= 0 ) return null; @@ -344,19 +319,24 @@ public class FieldsTables return new PlexOfCps( tableStream, start, length, FLD_SIZE ); } - public Collection getFields( int documentPart ) + public Collection getFields( DocumentPart part ) { - Map map = _fieldsByOffset.get( Integer - .valueOf( documentPart ) ); + Map map = _fieldsByOffset.get( part ); if ( map == null || map.isEmpty() ) return Collections.emptySet(); return Collections.unmodifiableCollection( map.values() ); } - public ArrayList getFieldsPLCF( int type ) + @Deprecated + public ArrayList getFieldsPLCF( int partIndex ) + { + return getFieldsPLCF( DocumentPart.values()[partIndex] ); + } + + public ArrayList getFieldsPLCF( DocumentPart documentPart ) { - return toArrayList( _tables.get( Integer.valueOf( type ) ) ); + return toArrayList( _tables.get( documentPart ) ); } private static ArrayList toArrayList( PlexOfCps plexOfCps ) @@ -377,8 +357,8 @@ public class FieldsTables return fields; } - private int savePlex( PlexOfCps plexOfCps, int type, - FileInformationBlock fib, HWPFOutputStream outputStream ) + private int savePlex( FileInformationBlock fib, DocumentPart documentPart, + PlexOfCps plexOfCps, HWPFOutputStream outputStream ) throws IOException { if ( plexOfCps == null || plexOfCps.length() == 0 ) @@ -391,39 +371,8 @@ public class FieldsTables outputStream.write( data ); - switch ( type ) - { - 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; - } + fib.setFieldsPlcfOffset( documentPart, start ); + fib.setFieldsPlcfLength( documentPart, length ); return length; } @@ -431,10 +380,10 @@ public class FieldsTables public void write( FileInformationBlock fib, HWPFOutputStream tableStream ) throws IOException { - for ( int i = PLCFFLDATN; i <= PLCFFLDTXBX; i++ ) + for ( DocumentPart part : DocumentPart.values() ) { - PlexOfCps plexOfCps = _tables.get( Integer.valueOf( i ) ); - savePlex( plexOfCps, i, fib, tableStream ); + PlexOfCps plexOfCps = _tables.get( part ); + savePlex( fib, part, plexOfCps, tableStream ); } } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java b/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java index 8444c69877..10254158a2 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java @@ -460,6 +460,30 @@ public final class FileInformationBlock extends FIBAbstractType _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() { return _fieldHandler.getFieldOffset(FIBFieldHandler.PLCFFLDATN); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/TestFieldsTables.java b/src/scratchpad/testcases/org/apache/poi/hwpf/TestFieldsTables.java index 4f3407157a..40b7169b60 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/TestFieldsTables.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/TestFieldsTables.java @@ -21,8 +21,7 @@ package org.apache.poi.hwpf; 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.FileInformationBlock; import org.apache.poi.hwpf.model.PlexOfField; @@ -36,10 +35,6 @@ import org.apache.poi.hwpf.model.PlexOfField; */ 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[] = { @@ -85,10 +80,12 @@ public class TestFieldsTables extends HWPFTestCase 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 fieldsPlexes = fieldsTables - .getFieldsPLCF( ALL_TYPES[i] ); + .getFieldsPLCF( part ); String result = dumpPlexes( fieldsPlexes ); assertEquals( EXPECTED[i], result ); } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java index 60b4df7a33..a3f1bd31c0 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java @@ -32,7 +32,7 @@ import org.apache.poi.hwpf.HWPFTestCase; import org.apache.poi.hwpf.HWPFTestDataSamples; import org.apache.poi.hwpf.extractor.Word6Extractor; 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.StyleSheet; import org.apache.poi.util.IOUtils; @@ -554,9 +554,9 @@ public final class TestProblems extends HWPFTestCase { .getCharacterTable().getTextRuns().size() ); List expectedFields = doc1.getFieldsTables() - .getFieldsPLCF( FieldsTables.PLCFFLDMOM ); + .getFieldsPLCF( DocumentPart.MAIN ); List actualFields = doc2.getFieldsTables() - .getFieldsPLCF( FieldsTables.PLCFFLDMOM ); + .getFieldsPLCF( DocumentPart.MAIN ); assertEquals( expectedFields.size(), actualFields.size() ); assertTableStructures( doc1.getRange(), doc2.getRange() ); -- 2.39.5