hwpfdocument.java

来自「EXCEL read and write」· Java 代码 · 共 657 行 · 第 1/2 页

JAVA
657
字号
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */package org.apache.poi.hwpf;import java.io.InputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.PushbackInputStream;import java.io.IOException;import java.io.OutputStream;import java.io.ByteArrayInputStream;import java.util.Iterator;import org.apache.poi.EncryptedDocumentException;import org.apache.poi.POIDocument;import org.apache.poi.poifs.filesystem.DirectoryNode;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.common.POIFSConstants;import org.apache.poi.hwpf.model.*;import org.apache.poi.hwpf.model.io.*;import org.apache.poi.hwpf.usermodel.*;/** * * This class acts as the bucket that we throw all of the Word data structures * into. * * @author Ryan Ackley */public class HWPFDocument extends POIDocument//  implements Cloneable{  /** The FIB */  protected FileInformationBlock _fib;  /** And for making sense of CP lengths in the FIB */  protected CPSplitCalculator _cpSplit;  /** main document stream buffer*/  protected byte[] _mainStream;  /** table stream buffer*/  protected byte[] _tableStream;  /** data stream buffer*/  protected byte[] _dataStream;  /** Document wide Properties*/  protected DocumentProperties _dop;  /** Contains text of the document wrapped in a obfuscated Word data  * structure*/  protected ComplexFileTable _cft;  protected TextPieceTable _tpt;  /** Contains formatting properties for text*/  protected CHPBinTable _cbt;  /** Contains formatting properties for paragraphs*/  protected PAPBinTable _pbt;  /** Contains formatting properties for sections.*/  protected SectionTable _st;  /** Holds styles for this document.*/  protected StyleSheet _ss;  /** Holds fonts for this document.*/  protected FontTable _ft;  /** Hold list tables */  protected ListTables _lt;  /** Holds the save history for this document. */  protected SavedByTable _sbt;    /** Holds pictures table */  protected PicturesTable _pictures;    /** Holds FSBA (shape) information */  protected FSPATable _fspa;    /** Escher Drawing Group information */  protected EscherRecordHolder _dgg;  /** Holds Office Art objects */  protected ShapesTable _officeArts;  protected HWPFDocument()  {     super(null, null);  }  /**   * Takens an InputStream, verifies that it's not RTF, builds a   *  POIFSFileSystem from it, and returns that.   */  public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException {	// Open a PushbackInputStream, so we can peek at the first few bytes	PushbackInputStream pis = new PushbackInputStream(istream,6);	byte[] first6 = new byte[6];	pis.read(first6);	// Does it start with {\rtf ? If so, it's really RTF	if(first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r'		&& first6[3] == 't' && first6[4] == 'f') {		throw new IllegalArgumentException("The document is really a RTF file");	}	// OK, so it's not RTF	// Open a POIFSFileSystem on the (pushed back) stream	pis.unread(first6);	return new POIFSFileSystem(pis);  }  /**   * This constructor loads a Word document from an InputStream.   *   * @param istream The InputStream that contains the Word document.   * @throws IOException If there is an unexpected IOException from the passed   *         in InputStream.   */  public HWPFDocument(InputStream istream) throws IOException  {    //do Ole stuff    this( verifyAndBuildPOIFS(istream) );  }    /**   * This constructor loads a Word document from a POIFSFileSystem   *   * @param pfilesystem The POIFSFileSystem that contains the Word document.   * @throws IOException If there is an unexpected IOException from the passed   *         in POIFSFileSystem.   */  public HWPFDocument(POIFSFileSystem pfilesystem) throws IOException  {	this(pfilesystem.getRoot(), pfilesystem);  }    /**   * This constructor loads a Word document from a specific point   *  in a POIFSFileSystem, probably not the default.   * Used typically to open embeded documents.   *   * @param pfilesystem The POIFSFileSystem that contains the Word document.   * @throws IOException If there is an unexpected IOException from the passed   *         in POIFSFileSystem.   */  public HWPFDocument(DirectoryNode directory, POIFSFileSystem pfilesystem) throws IOException  {    // Sort out the hpsf properties	super(directory, pfilesystem);    readProperties();        // read in the main stream.    DocumentEntry documentProps = (DocumentEntry)       directory.getEntry("WordDocument");    _mainStream = new byte[documentProps.getSize()];        directory.createDocumentInputStream("WordDocument").read(_mainStream);    // Create our FIB, and check for the doc being encrypted    _fib = new FileInformationBlock(_mainStream);    _cpSplit = new CPSplitCalculator(_fib);    if(_fib.isFEncrypted()) {    	throw new EncryptedDocumentException("Cannot process encrypted word files!");    }    // use the fib to determine the name of the table stream.    String name = "0Table";    if (_fib.isFWhichTblStm())    {      name = "1Table";    }    // Grab the table stream.    DocumentEntry tableProps;	try {		tableProps =			(DocumentEntry)directory.getEntry(name);	} catch(FileNotFoundException fnfe) {		throw new IllegalStateException("Table Stream '" + name + "' wasn't found - Either the document is corrupt, or is Word95 (or earlier)");	}    // read in the table stream.    _tableStream = new byte[tableProps.getSize()];    directory.createDocumentInputStream(name).read(_tableStream);    _fib.fillVariableFields(_mainStream, _tableStream);    // read in the data stream.    try    {      DocumentEntry dataProps =          (DocumentEntry)directory.getEntry("Data");      _dataStream = new byte[dataProps.getSize()];      filesystem.createDocumentInputStream("Data").read(_dataStream);    }    catch(java.io.FileNotFoundException e)    {        _dataStream = new byte[0];    }    // Get the cp of the start of text in the main stream    // The latest spec doc says this is always zero!    int fcMin = 0;    //fcMin = _fib.getFcMin()     // Start to load up our standard structures.    _dop = new DocumentProperties(_tableStream, _fib.getFcDop());    _cft = new ComplexFileTable(_mainStream, _tableStream, _fib.getFcClx(), fcMin);    _tpt = _cft.getTextPieceTable();        // Word XP and later all put in a zero filled buffer in    //  front of the text. This screws up the system for offsets,    //  which assume we always start at zero. This is an adjustment.    int cpMin = _tpt.getCpMin();        // Now load the rest of the properties, which need to be adjusted    //  for where text really begin    _cbt = new CHPBinTable(_mainStream, _tableStream, _fib.getFcPlcfbteChpx(), _fib.getLcbPlcfbteChpx(), cpMin, _tpt);    _pbt = new PAPBinTable(_mainStream, _tableStream, _dataStream, _fib.getFcPlcfbtePapx(), _fib.getLcbPlcfbtePapx(), cpMin, _tpt);        // Read FSPA and Escher information    _fspa = new FSPATable(_tableStream, _fib.getFcPlcspaMom(), _fib.getLcbPlcspaMom(), getTextTable().getTextPieces());        if (_fib.getFcDggInfo() != 0)    {        _dgg = new EscherRecordHolder(_tableStream, _fib.getFcDggInfo(), _fib.getLcbDggInfo());    } else    {        _dgg = new EscherRecordHolder();    }        // read in the pictures stream    _pictures = new PicturesTable(this, _dataStream, _mainStream, _fspa, _dgg);    // And the art shapes stream    _officeArts = new ShapesTable(_tableStream, _fib);    _st = new SectionTable(_mainStream, _tableStream, _fib.getFcPlcfsed(), _fib.getLcbPlcfsed(), fcMin, _tpt, _cpSplit);    _ss = new StyleSheet(_tableStream, _fib.getFcStshf());    _ft = new FontTable(_tableStream, _fib.getFcSttbfffn(), _fib.getLcbSttbfffn());    int listOffset = _fib.getFcPlcfLst();    int lfoOffset = _fib.getFcPlfLfo();    if (listOffset != 0 && _fib.getLcbPlcfLst() != 0)    {      _lt = new ListTables(_tableStream, _fib.getFcPlcfLst(), _fib.getFcPlfLfo());    }    int sbtOffset = _fib.getFcSttbSavedBy();    int sbtLength = _fib.getLcbSttbSavedBy();    if (sbtOffset != 0 && sbtLength != 0)    {      _sbt = new SavedByTable(_tableStream, sbtOffset, sbtLength);    }    PlexOfCps plc = new PlexOfCps(_tableStream, _fib.getFcPlcffldMom(), _fib.getLcbPlcffldMom(), 2);    for (int x = 0; x < plc.length(); x++)    {      GenericPropertyNode node = plc.getProperty(x);      byte[] fld = node.getBytes();      int breakpoint = 0;    }  }  public StyleSheet getStyleSheet()  {    return _ss;  }  public FileInformationBlock getFileInformationBlock()  {    return _fib;  }  public CPSplitCalculator getCPSplitCalculator()  {	return _cpSplit;  }  public DocumentProperties getDocProperties()  {    return _dop;  }    /**   * Returns the range that covers all text in the   *  file, including main text, footnotes, headers   *  and comments   */  public Range getOverallRange() {	  // hack to get the ending cp of the document, Have to revisit this.	  java.util.List text = _tpt.getTextPieces();	  PropertyNode p = (PropertyNode)text.get(text.size() - 1);      return new Range(0, p.getEnd(), this);  }  /**   * Returns the range which covers the whole of the   *  document, but excludes any headers and footers.   */  public Range getRange() {	  // First up, trigger a full-recalculate	  // Needed in case of deletes etc	  getOverallRange();	  	  // Now, return the real one

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?