KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > model > PAPBinTable


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hwpf.model;
18
19 import java.util.ArrayList JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import org.apache.poi.hwpf.model.io.*;
24 import org.apache.poi.hwpf.sprm.SprmBuffer;
25
26 import org.apache.poi.poifs.common.POIFSConstants;
27 import org.apache.poi.util.LittleEndian;
28
29 /**
30  * This class represents the bin table of Word document but it also serves as a
31  * holder for all of the paragraphs of document that have been loaded into
32  * memory.
33  *
34  * @author Ryan Ackley
35  */

36 public class PAPBinTable
37 {
38   protected ArrayList JavaDoc _paragraphs = new ArrayList JavaDoc();
39   byte[] _dataStream;
40
41   public PAPBinTable()
42   {
43   }
44
45   public PAPBinTable(byte[] documentStream, byte[] tableStream, byte[] dataStream, int offset,
46                      int size, int fcMin)
47   {
48     PlexOfCps binTable = new PlexOfCps(tableStream, offset, size, 4);
49
50     int length = binTable.length();
51     for (int x = 0; x < length; x++)
52     {
53       GenericPropertyNode node = binTable.getProperty(x);
54
55       int pageNum = LittleEndian.getInt(node.getBytes());
56       int pageOffset = POIFSConstants.BIG_BLOCK_SIZE * pageNum;
57
58       PAPFormattedDiskPage pfkp = new PAPFormattedDiskPage(documentStream,
59         dataStream, pageOffset, fcMin);
60
61       int fkpSize = pfkp.size();
62
63       for (int y = 0; y < fkpSize; y++)
64       {
65         _paragraphs.add(pfkp.getPAPX(y));
66       }
67     }
68     _dataStream = dataStream;
69   }
70
71   public void insert(int listIndex, int cpStart, SprmBuffer buf)
72   {
73     PAPX forInsert = new PAPX(cpStart, cpStart, buf, _dataStream);
74     if (listIndex == _paragraphs.size())
75     {
76        _paragraphs.add(forInsert);
77     }
78     else
79     {
80       PAPX currentPap = (PAPX)_paragraphs.get(listIndex);
81       if (currentPap != null && currentPap.getStart() < cpStart)
82       {
83         SprmBuffer clonedBuf = null;
84         try
85         {
86           clonedBuf = (SprmBuffer)currentPap.getSprmBuf().clone();
87         }
88         catch (CloneNotSupportedException JavaDoc exc)
89         {
90           exc.printStackTrace();
91         }
92         currentPap.setEnd(cpStart);
93         PAPX splitPap = new PAPX(cpStart, currentPap.getEnd(), clonedBuf, _dataStream);
94         _paragraphs.add(++listIndex, forInsert);
95         _paragraphs.add(++listIndex, splitPap);
96       }
97       else
98       {
99         _paragraphs.add(listIndex, forInsert);
100       }
101     }
102
103   }
104
105   public void adjustForDelete(int listIndex, int offset, int length)
106   {
107     int size = _paragraphs.size();
108     int endMark = offset + length;
109     int endIndex = listIndex;
110
111     PAPX papx = (PAPX)_paragraphs.get(endIndex);
112     while (papx.getEnd() < endMark)
113     {
114       papx = (PAPX)_paragraphs.get(++endIndex);
115     }
116     if (listIndex == endIndex)
117     {
118       papx = (PAPX)_paragraphs.get(endIndex);
119       papx.setEnd((papx.getEnd() - endMark) + offset);
120     }
121     else
122     {
123       papx = (PAPX)_paragraphs.get(listIndex);
124       papx.setEnd(offset);
125       for (int x = listIndex + 1; x < endIndex; x++)
126       {
127         papx = (PAPX)_paragraphs.get(x);
128         papx.setStart(offset);
129         papx.setEnd(offset);
130       }
131       papx = (PAPX)_paragraphs.get(endIndex);
132       papx.setEnd((papx.getEnd() - endMark) + offset);
133     }
134
135     for (int x = endIndex + 1; x < size; x++)
136     {
137       papx = (PAPX)_paragraphs.get(x);
138       papx.setStart(papx.getStart() - length);
139       papx.setEnd(papx.getEnd() - length);
140     }
141   }
142
143
144   public void adjustForInsert(int listIndex, int length)
145   {
146     int size = _paragraphs.size();
147     PAPX papx = (PAPX)_paragraphs.get(listIndex);
148     papx.setEnd(papx.getEnd() + length);
149
150     for (int x = listIndex + 1; x < size; x++)
151     {
152       papx = (PAPX)_paragraphs.get(x);
153       papx.setStart(papx.getStart() + length);
154       papx.setEnd(papx.getEnd() + length);
155     }
156   }
157
158
159   public ArrayList JavaDoc getParagraphs()
160   {
161     return _paragraphs;
162   }
163
164   public void writeTo(HWPFFileSystem sys, int fcMin)
165     throws IOException JavaDoc
166   {
167
168     HWPFOutputStream docStream = sys.getStream("WordDocument");
169     OutputStream JavaDoc tableStream = sys.getStream("1Table");
170
171     PlexOfCps binTable = new PlexOfCps(4);
172
173     // each FKP must start on a 512 byte page.
174
int docOffset = docStream.getOffset();
175     int mod = docOffset % POIFSConstants.BIG_BLOCK_SIZE;
176     if (mod != 0)
177     {
178       byte[] padding = new byte[POIFSConstants.BIG_BLOCK_SIZE - mod];
179       docStream.write(padding);
180     }
181
182     // get the page number for the first fkp
183
docOffset = docStream.getOffset();
184     int pageNum = docOffset/POIFSConstants.BIG_BLOCK_SIZE;
185
186     // get the ending fc
187
int endingFc = ((PropertyNode)_paragraphs.get(_paragraphs.size() - 1)).getEnd();
188     endingFc += fcMin;
189
190
191     ArrayList JavaDoc overflow = _paragraphs;
192     do
193     {
194       PropertyNode startingProp = (PropertyNode)overflow.get(0);
195       int start = startingProp.getStart() + fcMin;
196
197       PAPFormattedDiskPage pfkp = new PAPFormattedDiskPage(_dataStream);
198       pfkp.fill(overflow);
199
200       byte[] bufFkp = pfkp.toByteArray(fcMin);
201       docStream.write(bufFkp);
202       overflow = pfkp.getOverflow();
203
204       int end = endingFc;
205       if (overflow != null)
206       {
207         end = ((PropertyNode)overflow.get(0)).getStart() + fcMin;
208       }
209
210       byte[] intHolder = new byte[4];
211       LittleEndian.putInt(intHolder, pageNum++);
212       binTable.addProperty(new GenericPropertyNode(start, end, intHolder));
213
214     }
215     while (overflow != null);
216     tableStream.write(binTable.toByteArray());
217   }
218
219
220 }
221
222
Popular Tags