01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hwpf.model;
19:
20: import java.io.IOException;
21:
22: import org.apache.poi.util.LittleEndian;
23: import org.apache.poi.hwpf.model.io.*;
24:
25: public class ComplexFileTable {
26:
27: private static final byte GRPPRL_TYPE = 1;
28: private static final byte TEXT_PIECE_TABLE_TYPE = 2;
29:
30: protected TextPieceTable _tpt;
31:
32: public ComplexFileTable() {
33: _tpt = new TextPieceTable();
34: }
35:
36: public ComplexFileTable(byte[] documentStream, byte[] tableStream,
37: int offset, int fcMin) throws IOException {
38: //skips through the prms before we reach the piece table. These contain data
39: //for actual fast saved files
40: while (tableStream[offset] == GRPPRL_TYPE) {
41: offset++;
42: int skip = LittleEndian.getShort(tableStream, offset);
43: offset += LittleEndian.SHORT_SIZE + skip;
44: }
45: if (tableStream[offset] != TEXT_PIECE_TABLE_TYPE) {
46: throw new IOException("The text piece table is corrupted");
47: } else {
48: int pieceTableSize = LittleEndian.getInt(tableStream,
49: ++offset);
50: offset += LittleEndian.INT_SIZE;
51: _tpt = new TextPieceTable(documentStream, tableStream,
52: offset, pieceTableSize, fcMin);
53: }
54: }
55:
56: public TextPieceTable getTextPieceTable() {
57: return _tpt;
58: }
59:
60: public void writeTo(HWPFFileSystem sys) throws IOException {
61: HWPFOutputStream docStream = sys.getStream("WordDocument");
62: HWPFOutputStream tableStream = sys.getStream("1Table");
63:
64: tableStream.write(TEXT_PIECE_TABLE_TYPE);
65:
66: byte[] table = _tpt.writeTo(docStream);
67:
68: byte[] numHolder = new byte[LittleEndian.INT_SIZE];
69: LittleEndian.putInt(numHolder, table.length);
70: tableStream.write(numHolder);
71: tableStream.write(table);
72: }
73:
74: }
|