📄 hdfobjectfactory.java
字号:
/* ==================================================================== 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.==================================================================== */ /* * HDFObjectFactory.java * * Created on February 24, 2002, 2:17 PM */package org.apache.poi.hdf.model;//import java.io;import java.util.ArrayList;import java.io.InputStream;import java.io.FileInputStream;import java.io.IOException;import java.util.List;import java.util.TreeSet;import org.apache.poi.hdf.model.hdftypes.*;import org.apache.poi.hdf.event.HDFLowLevelParsingListener;import org.apache.poi.hdf.model.util.BTreeSet;import org.apache.poi.hdf.model.util.ParsingState;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.poifs.filesystem.POIFSDocument;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.util.LittleEndian;/** * The Object Factory takes in a stream and creates the low level objects * that represent the data. * @author andy */public class HDFObjectFactory{ /** OLE stuff*/ private POIFSFileSystem _filesystem; /** The FIB*/ private FileInformationBlock _fib; /** Used to set up the object model*/ private HDFLowLevelParsingListener _listener; /** parsing state for characters */ private ParsingState _charParsingState; /** parsing state for paragraphs */ private ParsingState _parParsingState; /** main document stream buffer*/ byte[] _mainDocument; /** table stream buffer*/ byte[] _tableBuffer; public static void main(String args[]) { try { HDFObjectFactory f = new HDFObjectFactory(new FileInputStream("c:\\test.doc")); int k = 0; } catch(Throwable t) { t.printStackTrace(); } } /** Creates a new instance of HDFObjectFactory * * @param istream The InputStream that is the Word document * */ protected HDFObjectFactory(InputStream istream, HDFLowLevelParsingListener l) throws IOException { if (l == null) { _listener = new HDFObjectModel(); } else { _listener = l; } //do Ole stuff _filesystem = new POIFSFileSystem(istream); DocumentEntry headerProps = (DocumentEntry)_filesystem.getRoot().getEntry("WordDocument"); _mainDocument = new byte[headerProps.getSize()]; _filesystem.createDocumentInputStream("WordDocument").read(_mainDocument); _fib = new FileInformationBlock(_mainDocument); initTableStream(); initTextPieces(); initFormattingProperties(); } /** Creates a new instance of HDFObjectFactory * * @param istream The InputStream that is the Word document * */ public HDFObjectFactory(InputStream istream) throws IOException { this(istream, null); } public static List getTypes(InputStream istream) throws IOException { List results = new ArrayList(1); //do Ole stuff POIFSFileSystem filesystem = new POIFSFileSystem(istream); DocumentEntry headerProps = (DocumentEntry)filesystem.getRoot().getEntry("WordDocument"); byte[] mainDocument = new byte[headerProps.getSize()]; filesystem.createDocumentInputStream("WordDocument").read(mainDocument); FileInformationBlock fib = new FileInformationBlock(mainDocument); results.add(fib); return results; } /** * Initializes the table stream * * @throws IOException */ private void initTableStream() throws IOException { String tablename = null; if(_fib.isFWhichTblStm()) { tablename="1Table"; } else { tablename="0Table"; } DocumentEntry tableEntry = (DocumentEntry)_filesystem.getRoot().getEntry(tablename); //load the table stream into a buffer int size = tableEntry.getSize(); _tableBuffer = new byte[size]; _filesystem.createDocumentInputStream(tablename).read(_tableBuffer); } /** * Initializes the text pieces. Text is divided into pieces because some * "pieces" may only contain unicode characters. * * @throws IOException */ private void initTextPieces() throws IOException { int pos = _fib.getFcClx(); //skips through the prms before we reach the piece table. These contain data //for actual fast saved files while (_tableBuffer[pos] == 1) { pos++; int skip = LittleEndian.getShort(_tableBuffer, pos); pos += 2 + skip; } if(_tableBuffer[pos] != 2) { throw new IOException("The text piece table is corrupted"); } else { //parse out the text pieces int pieceTableSize = LittleEndian.getInt(_tableBuffer, ++pos); pos += 4; int pieces = (pieceTableSize - 4) / 12; for (int x = 0; x < pieces; x++) { int filePos = LittleEndian.getInt(_tableBuffer, pos + ((pieces + 1) * 4) + (x * 8) + 2); boolean unicode = false; if ((filePos & 0x40000000) == 0) { unicode = true; } else { unicode = false; filePos &= ~(0x40000000);//gives me FC in doc stream filePos /= 2; } int totLength = LittleEndian.getInt(_tableBuffer, pos + (x + 1) * 4) - LittleEndian.getInt(_tableBuffer, pos + (x * 4)); TextPiece piece = new TextPiece(filePos, totLength, unicode); _listener.text(piece); } } } /** * initializes all of the formatting properties for a Word Document */ private void initFormattingProperties() { createStyleSheet(); createListTables(); createFontTable(); initDocumentProperties(); initSectionProperties(); //initCharacterProperties(); //initParagraphProperties(); } private void initCharacterProperties(int charOffset, PlexOfCps charPlcf, int start, int end) { //Initialize paragraph property stuff //int currentCharPage = _charParsingState.getCurrentPage(); int charPlcfLen = charPlcf.length(); int currentPageIndex = _charParsingState.getCurrentPageIndex(); FormattedDiskPage fkp = _charParsingState.getFkp(); int currentChpxIndex = _charParsingState.getCurrentPropIndex(); int currentArraySize = fkp.size(); //get the character runs for this paragraph int charStart = 0; int charEnd = 0; //add the character runs do { if (currentChpxIndex < currentArraySize) { charStart = fkp.getStart(currentChpxIndex); charEnd = fkp.getEnd(currentChpxIndex); byte[] chpx = fkp.getGrpprl(currentChpxIndex); _listener.characterRun(new ChpxNode(Math.max(charStart, start), Math.min(charEnd, end), chpx)); if (charEnd < end) { currentChpxIndex++; } else { _charParsingState.setState(currentPageIndex, fkp, currentChpxIndex); break; } } else { int currentCharPage = LittleEndian.getInt(_tableBuffer, charOffset + charPlcf.getStructOffset(++currentPageIndex)); byte[] byteFkp = new byte[512]; System.arraycopy(_mainDocument, (currentCharPage * 512), byteFkp, 0, 512); fkp = new CHPFormattedDiskPage(byteFkp); currentChpxIndex = 0; currentArraySize = fkp.size(); } } while(currentPageIndex < charPlcfLen); } private void initParagraphProperties(int parOffset, PlexOfCps parPlcf, int charOffset, PlexOfCps charPlcf, int start, int end) { //Initialize paragraph property stuff //int currentParPage = _parParsingState.getCurrentPage(); int parPlcfLen = parPlcf.length(); int currentPageIndex = _parParsingState.getCurrentPageIndex(); FormattedDiskPage fkp = _parParsingState.getFkp(); int currentPapxIndex = _parParsingState.getCurrentPropIndex(); int currentArraySize = fkp.size(); do { if (currentPapxIndex < currentArraySize) { int parStart = fkp.getStart(currentPapxIndex); int parEnd = fkp.getEnd(currentPapxIndex); byte[] papx = fkp.getGrpprl(currentPapxIndex); _listener.paragraph(new PapxNode(Math.max(parStart, start), Math.min(parEnd, end), papx)); initCharacterProperties(charOffset, charPlcf, Math.max(start, parStart), Math.min(parEnd, end)); if (parEnd < end) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -