]> source.dussan.org Git - poi.git/commitdiff
add dump fields feature
authorSergey Vladimirov <sergey@apache.org>
Tue, 19 Jul 2011 11:03:09 +0000 (11:03 +0000)
committerSergey Vladimirov <sergey@apache.org>
Tue, 19 Jul 2011 11:03:09 +0000 (11:03 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148270 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java
src/scratchpad/src/org/apache/poi/hwpf/model/FieldsTables.java

index aceb51572c9b0a5cdb0ed1a01639c9d0696036d6..76ca6992d88294062516fdffcd1a4c711803f33c 100644 (file)
@@ -36,6 +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.FileInformationBlock;
 import org.apache.poi.hwpf.model.GenericPropertyNode;
 import org.apache.poi.hwpf.model.PAPFormattedDiskPage;
@@ -101,7 +102,8 @@ public final class HWPFLister
                             + "\t\t[--chpx] [--chpxProperties] [--chpxSprms]\n"
                             + "\t\t[--papx] [--papxProperties]\n"
                             + "\t\t[--paragraphs] [--paragraphsSprms] [--paragraphsText]\n"
-                            + "\t\t[--pictures]\n" + "\t\t[--writereadback]\n" );
+                            + "\t\t[--fields]\n" + "\t\t[--pictures]\n"
+                            + "\t\t[--writereadback]\n" );
             System.exit( 1 );
         }
 
@@ -119,6 +121,7 @@ public final class HWPFLister
         boolean outputPapx = false;
         boolean outputPapxProperties = false;
 
+        boolean outputFields = false;
         boolean outputPictures = false;
 
         boolean writereadback = false;
@@ -149,6 +152,8 @@ public final class HWPFLister
             if ( "--papxProperties".equals( arg ) )
                 outputPapxProperties = true;
 
+            if ( "--fields".equals( arg ) )
+                outputFields = true;
             if ( "--pictures".equals( arg ) )
                 outputPictures = true;
 
@@ -191,6 +196,12 @@ public final class HWPFLister
                     outputParagraphsText );
         }
 
+        if ( outputFields )
+        {
+            System.out.println( "== FIELDS ==" );
+            lister.dumpFields();
+        }
+
         if ( outputPictures )
         {
             System.out.println( "== PICTURES ==" );
@@ -306,6 +317,27 @@ public final class HWPFLister
         System.out.println( fib );
     }
 
+    private void dumpFields()
+    {
+        if ( !( _doc instanceof HWPFDocument ) )
+        {
+            System.out.println( "Word 95 not supported so far" );
+            return;
+        }
+
+        HWPFDocument document = (HWPFDocument) _doc;
+
+        for ( int i = FieldsTables.PLCFFLDATN; i <= FieldsTables.PLCFFLDTXBX; i++ )
+        {
+            System.out.println( "=== Document part: " + i + " ===" );
+            for ( org.apache.poi.hwpf.model.Field field : document
+                    .getFieldsTables().getFields( i ) )
+            {
+                System.out.println( field );
+            }
+        }
+    }
+
     public void dumpPapx( boolean withProperties ) throws Exception
     {
         if ( _doc instanceof HWPFDocument )
index 9b156ce831c46a5ab69bb38162d7e6304fe01c90..0141f49e94be40ca723268bce00c56aabe865efd 100644 (file)
@@ -22,6 +22,8 @@ package org.apache.poi.hwpf.model;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
@@ -234,7 +236,7 @@ public class FieldsTables
      * This is port and adaptation of Arrays.binarySearch from Java 6 (Apache
      * Harmony).
      */
-    public static <T> int binarySearch( GenericPropertyNode[] array,
+    private static <T> int binarySearch( GenericPropertyNode[] array,
             int startIndex, int endIndex, int requiredStartOffset )
     {
         checkIndexForBinarySearch( array.length, startIndex, endIndex );
@@ -342,6 +344,16 @@ public class FieldsTables
         return new PlexOfCps( tableStream, start, length, FLD_SIZE );
     }
 
+    public Collection<Field> getFields( int documentPart )
+    {
+        Map<Integer, Field> map = _fieldsByOffset.get( Integer
+                .valueOf( documentPart ) );
+        if ( map == null || map.isEmpty() )
+            return Collections.emptySet();
+
+        return Collections.unmodifiableCollection( map.values() );
+    }
+
     public ArrayList<PlexOfField> getFieldsPLCF( int type )
     {
         return toArrayList( _tables.get( Integer.valueOf( type ) ) );