FileDocCategorySizeDatePackage
SectionTable.javaAPI DocApache Poi 3.0.15684Mon Jan 01 18:55:32 GMT 2007org.apache.poi.hwpf.model

SectionTable

public class SectionTable extends Object
author
Ryan Ackley

Fields Summary
private static final int
SED_SIZE
protected ArrayList
_sections
protected List
_text
Constructors Summary
public SectionTable()


   
  
  
public SectionTable(byte[] documentStream, byte[] tableStream, int offset, int size, int fcMin, List tpt)

    PlexOfCps sedPlex = new PlexOfCps(tableStream, offset, size, SED_SIZE);
    _text = tpt;

    int length = sedPlex.length();

    for (int x = 0; x < length; x++)
    {
      GenericPropertyNode node = sedPlex.getProperty(x);
      SectionDescriptor sed = new SectionDescriptor(node.getBytes(), 0);

      int fileOffset = sed.getFc();

      // check for the optimization
      if (fileOffset == 0xffffffff)
      {
        _sections.add(new SEPX(sed, CPtoFC(node.getStart()), CPtoFC(node.getEnd()), new byte[0]));
      }
      else
      {
        // The first short at the offset is the size of the grpprl.
        int sepxSize = LittleEndian.getShort(documentStream, fileOffset);
        byte[] buf = new byte[sepxSize];
        fileOffset += LittleEndian.SHORT_SIZE;
        System.arraycopy(documentStream, fileOffset, buf, 0, buf.length);
        _sections.add(new SEPX(sed, CPtoFC(node.getStart()), CPtoFC(node.getEnd()), buf));
      }
    }
  
Methods Summary
private intCPtoFC(int CP)

      TextPiece TP = null;

      for(int i=_text.size()-1; i>-1; i--)
      {
        TP = (TextPiece)_text.get(i);

        if(CP >= TP.getCP()) break;
      }
      int FC = TP.getPieceDescriptor().getFilePosition();
      int offset = CP - TP.getCP();
      if(TP.usesUnicode()) offset*=2;
      FC = FC+offset-((TextPiece)_text.get(0)).getPieceDescriptor().getFilePosition();
      return FC;
    
private intFCtoCP(int fc)

     int size = _text.size();
     int cp = 0;
     for (int x = 0; x < size; x++)
     {
       TextPiece piece = (TextPiece)_text.get(x);

       if (fc <= piece.getEnd())
       {
         cp += ((fc - piece.getStart())/ (piece.usesUnicode() ? 2 : 1));
         break;
       }
       else
       {
         cp += ((piece.getEnd() - piece.getStart())/ (piece.usesUnicode() ? 2 : 1));
       }
     }
     return cp;
   
public voidadjustForInsert(int listIndex, int length)

    int size = _sections.size();
    SEPX sepx = (SEPX)_sections.get(listIndex);
    sepx.setEnd(sepx.getEnd() + length);

    for (int x = listIndex + 1; x < size; x++)
    {
      sepx = (SEPX)_sections.get(x);
      sepx.setStart(sepx.getStart() + length);
      sepx.setEnd(sepx.getEnd() + length);
    }
  
public java.util.ArrayListgetSections()

    return _sections;
  
public voidwriteTo(org.apache.poi.hwpf.model.io.HWPFFileSystem sys, int fcMin)

    HWPFOutputStream docStream = sys.getStream("WordDocument");
    HWPFOutputStream tableStream = sys.getStream("1Table");

    int offset = docStream.getOffset();
    int len = _sections.size();
    PlexOfCps plex = new PlexOfCps(SED_SIZE);

    for (int x = 0; x < len; x++)
    {
      SEPX sepx = (SEPX)_sections.get(x);
      byte[] grpprl = sepx.getGrpprl();

      // write the sepx to the document stream. starts with a 2 byte size
      // followed by the grpprl
      byte[] shortBuf = new byte[2];
      LittleEndian.putShort(shortBuf, (short)grpprl.length);

      docStream.write(shortBuf);
      docStream.write(grpprl);

      // set the fc in the section descriptor
      SectionDescriptor sed = sepx.getSectionDescriptor();
      sed.setFc(offset);

      // add the section descriptor bytes to the PlexOfCps.


      // original line -
      //GenericPropertyNode property = new GenericPropertyNode(sepx.getStart(), sepx.getEnd(), sed.toByteArray());

      // Line using Ryan's FCtoCP() conversion method -
      // unable to observe any effect on our testcases when using this code - piers
      GenericPropertyNode property = new GenericPropertyNode(FCtoCP(sepx.getStart()), FCtoCP(sepx.getEnd()), sed.toByteArray());


      plex.addProperty(property);

      offset = docStream.getOffset();
    }
    tableStream.write(plex.toByteArray());