]> source.dussan.org Git - poi.git/commitdiff
introduce DocumentPart enum and simplify fields access API
authorSergey Vladimirov <sergey@apache.org>
Tue, 19 Jul 2011 11:49:48 +0000 (11:49 +0000)
committerSergey Vladimirov <sergey@apache.org>
Tue, 19 Jul 2011 11:49:48 +0000 (11:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148280 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java
src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java
src/scratchpad/src/org/apache/poi/hwpf/model/DocumentPart.java [new file with mode: 0644]
src/scratchpad/src/org/apache/poi/hwpf/model/FieldsTables.java
src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java
src/scratchpad/testcases/org/apache/poi/hwpf/TestFieldsTables.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java

index d83b91b7bb43ebdf6fabe2f5dc7f95043980e085..236ac67ebd5216e48b90f8d543c62726b4df6945 100644 (file)
@@ -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;
 
index 76ca6992d88294062516fdffcd1a4c711803f33c..1c255354273472f2d774f2eaee7134a84dfcb385 100644 (file)
@@ -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 (file)
index 0000000..90bfa1b
--- /dev/null
@@ -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;
+    }
+
+}
index 0141f49e94be40ca723268bce00c56aabe865efd..74f394ea2c040283ccaeca1e3ac05b750651a54d 100644 (file)
@@ -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<Integer, PlexOfCps> _tables;
-    private Map<Integer, Map<Integer, Field>> _fieldsByOffset;
+    private Map<DocumentPart, PlexOfCps> _tables;
+    private Map<DocumentPart, Map<Integer, Field>> _fieldsByOffset;
 
     public FieldsTables( byte[] tableStream, FileInformationBlock fib )
     {
-        _tables = new HashMap<Integer, PlexOfCps>( PLCFFLDTXBX - PLCFFLDATN + 1 );
-        _fieldsByOffset = new HashMap<Integer, Map<Integer, Field>>(
-                PLCFFLDTXBX - PLCFFLDATN + 1 );
+        _tables = new HashMap<DocumentPart, PlexOfCps>(
+                DocumentPart.values().length );
+        _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 );
-            _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<Integer, Field> map = _fieldsByOffset.get( Integer
-                .valueOf( documentPart ) );
+        Map<Integer, Field> 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<Field> getFields( int documentPart )
+    public Collection<Field> getFields( DocumentPart part )
     {
-        Map<Integer, Field> map = _fieldsByOffset.get( Integer
-                .valueOf( documentPart ) );
+        Map<Integer, Field> map = _fieldsByOffset.get( part );
         if ( map == null || map.isEmpty() )
             return Collections.emptySet();
 
         return Collections.unmodifiableCollection( map.values() );
     }
 
-    public ArrayList<PlexOfField> getFieldsPLCF( int type )
+    @Deprecated
+    public ArrayList<PlexOfField> getFieldsPLCF( int partIndex )
+    {
+        return getFieldsPLCF( DocumentPart.values()[partIndex] );
+    }
+
+    public ArrayList<PlexOfField> getFieldsPLCF( DocumentPart documentPart )
     {
-        return toArrayList( _tables.get( Integer.valueOf( type ) ) );
+        return toArrayList( _tables.get( documentPart ) );
     }
 
     private static ArrayList<PlexOfField> 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 );
         }
     }
 }
index 8444c69877c8fd50afcdedb533de7e69480766b9..10254158a227173469edd92d782893e698f74fba 100644 (file)
@@ -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);
index 4f3407157a654e49bcf21a42dffa772a66441f8a..40b7169b60d1f621b1630ea0ff5f3560cd071841 100644 (file)
@@ -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<PlexOfField> fieldsPlexes = fieldsTables
-                    .getFieldsPLCF( ALL_TYPES[i] );
+                    .getFieldsPLCF( part );
             String result = dumpPlexes( fieldsPlexes );
             assertEquals( EXPECTED[i], result );
         }
index 60b4df7a33eaea50b612858c82d30b4c4a3f9f6a..a3f1bd31c0fd7a1a546b57f0ed1aa8e4599132a1 100644 (file)
@@ -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<PlexOfField> expectedFields = doc1.getFieldsTables()
-                    .getFieldsPLCF( FieldsTables.PLCFFLDMOM );
+                    .getFieldsPLCF( DocumentPart.MAIN );
             List<PlexOfField> actualFields = doc2.getFieldsTables()
-                    .getFieldsPLCF( FieldsTables.PLCFFLDMOM );
+                    .getFieldsPLCF( DocumentPart.MAIN );
             assertEquals( expectedFields.size(), actualFields.size() );
 
             assertTableStructures( doc1.getRange(), doc2.getRange() );