瀏覽代碼

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
tags/REL_3_8_BETA4
Sergey Vladimirov 13 年之前
父節點
當前提交
5471280290

+ 3
- 3
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;


+ 4
- 4
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 );
}

+ 46
- 0
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;
}

}

+ 42
- 93
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<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 );
}
}
}

+ 24
- 0
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);

+ 5
- 8
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<PlexOfField> fieldsPlexes = fieldsTables
.getFieldsPLCF( ALL_TYPES[i] );
.getFieldsPLCF( part );
String result = dumpPlexes( fieldsPlexes );
assertEquals( EXPECTED[i], result );
}

+ 3
- 3
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<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() );

Loading…
取消
儲存