KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import org.apache.poi.poifs.common.POIFSConstants;
25 import org.apache.poi.util.LittleEndian;
26 import org.apache.poi.hwpf.model.io.*;
27 import org.apache.poi.hwpf.sprm.SprmBuffer;
28
29 /**
30  * This class holds all of the character formatting properties.
31  *
32  * @author Ryan Ackley
33  */

34 public class CHPBinTable
35 {
36   /** List of character properties.*/
37   protected ArrayList JavaDoc _textRuns = new ArrayList JavaDoc();
38
39
40   public CHPBinTable()
41   {
42   }
43
44   /**
45    * Constructor used to read a binTable in from a Word document.
46    *
47    * @param documentStream
48    * @param tableStream
49    * @param offset
50    * @param size
51    * @param fcMin
52    */

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