add dump fields feature

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148270 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-19 11:03:09 +00:00
parent 92e5199a95
commit 88a00c69c5
2 changed files with 46 additions and 2 deletions

View File

@ -36,6 +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.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;
@ -101,7 +102,8 @@ public final class HWPFLister
+ "\t\t[--chpx] [--chpxProperties] [--chpxSprms]\n" + "\t\t[--chpx] [--chpxProperties] [--chpxSprms]\n"
+ "\t\t[--papx] [--papxProperties]\n" + "\t\t[--papx] [--papxProperties]\n"
+ "\t\t[--paragraphs] [--paragraphsSprms] [--paragraphsText]\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 ); System.exit( 1 );
} }
@ -119,6 +121,7 @@ public final class HWPFLister
boolean outputPapx = false; boolean outputPapx = false;
boolean outputPapxProperties = false; boolean outputPapxProperties = false;
boolean outputFields = false;
boolean outputPictures = false; boolean outputPictures = false;
boolean writereadback = false; boolean writereadback = false;
@ -149,6 +152,8 @@ public final class HWPFLister
if ( "--papxProperties".equals( arg ) ) if ( "--papxProperties".equals( arg ) )
outputPapxProperties = true; outputPapxProperties = true;
if ( "--fields".equals( arg ) )
outputFields = true;
if ( "--pictures".equals( arg ) ) if ( "--pictures".equals( arg ) )
outputPictures = true; outputPictures = true;
@ -191,6 +196,12 @@ public final class HWPFLister
outputParagraphsText ); outputParagraphsText );
} }
if ( outputFields )
{
System.out.println( "== FIELDS ==" );
lister.dumpFields();
}
if ( outputPictures ) if ( outputPictures )
{ {
System.out.println( "== PICTURES ==" ); System.out.println( "== PICTURES ==" );
@ -306,6 +317,27 @@ public final class HWPFLister
System.out.println( fib ); 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 public void dumpPapx( boolean withProperties ) throws Exception
{ {
if ( _doc instanceof HWPFDocument ) if ( _doc instanceof HWPFDocument )

View File

@ -22,6 +22,8 @@ package org.apache.poi.hwpf.model;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -234,7 +236,7 @@ public class FieldsTables
* This is port and adaptation of Arrays.binarySearch from Java 6 (Apache * This is port and adaptation of Arrays.binarySearch from Java 6 (Apache
* Harmony). * Harmony).
*/ */
public static <T> int binarySearch( GenericPropertyNode[] array, private static <T> int binarySearch( GenericPropertyNode[] array,
int startIndex, int endIndex, int requiredStartOffset ) int startIndex, int endIndex, int requiredStartOffset )
{ {
checkIndexForBinarySearch( array.length, startIndex, endIndex ); checkIndexForBinarySearch( array.length, startIndex, endIndex );
@ -342,6 +344,16 @@ 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 )
{
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 ) public ArrayList<PlexOfField> getFieldsPLCF( int type )
{ {
return toArrayList( _tables.get( Integer.valueOf( type ) ) ); return toArrayList( _tables.get( Integer.valueOf( type ) ) );