KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        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.util.List JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 import org.apache.poi.util.LittleEndian;
24
25 /**
26  * Represents a CHP fkp. The style properties for paragraph and character runs
27  * are stored in fkps. There are PAP fkps for paragraph properties and CHP fkps
28  * for character run properties. The first part of the fkp for both CHP and PAP
29  * fkps consists of an array of 4 byte int offsets that represent a
30  * Paragraph's or Character run's text offset in the main stream. The ending
31  * offset is the next value in the array. For example, if an fkp has X number of
32  * Paragraph's stored in it then there are (x + 1) 4 byte ints in the beginning
33  * array. The number X is determined by the last byte in a 512 byte fkp.
34  *
35  * CHP and PAP fkps also store the compressed styles(grpprl) that correspond to
36  * the offsets on the front of the fkp. The offset of the grpprls is determined
37  * differently for CHP fkps and PAP fkps.
38  *
39  * @author Ryan Ackley
40  */

41 public class CHPFormattedDiskPage extends FormattedDiskPage
42 {
43     private static final int FC_SIZE = 4;
44
45     private ArrayList JavaDoc _chpxList = new ArrayList JavaDoc();
46     private ArrayList JavaDoc _overFlow;
47
48
49     public CHPFormattedDiskPage()
50     {
51     }
52
53     /**
54      * This constructs a CHPFormattedDiskPage from a raw fkp (512 byte array
55      * read from a Word file).
56      */

57     public CHPFormattedDiskPage(byte[] documentStream, int offset, int fcMin)
58     {
59       super(documentStream, offset);
60
61       for (int x = 0; x < _crun; x++)
62       {
63         _chpxList.add(new CHPX(getStart(x) - fcMin, getEnd(x) - fcMin, getGrpprl(x)));
64       }
65     }
66
67     public CHPX getCHPX(int index)
68     {
69       return (CHPX)_chpxList.get(index);
70     }
71
72     public void fill(List JavaDoc filler)
73     {
74       _chpxList.addAll(filler);
75     }
76
77     public ArrayList JavaDoc getOverflow()
78     {
79       return _overFlow;
80     }
81
82     /**
83      * Gets the chpx for the character run at index in this fkp.
84      *
85      * @param index The index of the chpx to get.
86      * @return a chpx grpprl.
87      */

88     protected byte[] getGrpprl(int index)
89     {
90         int chpxOffset = 2 * LittleEndian.getUnsignedByte(_fkp, _offset + (((_crun + 1) * 4) + index));
91
92         //optimization if offset == 0 use "Normal" style
93
if(chpxOffset == 0)
94         {
95             return new byte[0];
96         }
97
98         int size = LittleEndian.getUnsignedByte(_fkp, _offset + chpxOffset);
99
100         byte[] chpx = new byte[size];
101
102         System.arraycopy(_fkp, _offset + ++chpxOffset, chpx, 0, size);
103         return chpx;
104     }
105
106     protected byte[] toByteArray(int fcMin)
107     {
108       byte[] buf = new byte[512];
109       int size = _chpxList.size();
110       int grpprlOffset = 511;
111       int offsetOffset = 0;
112       int fcOffset = 0;
113
114       // total size is currently the size of one FC
115
int totalSize = FC_SIZE + 2;
116
117       int index = 0;
118       for (; index < size; index++)
119       {
120         int grpprlLength = ((CHPX)_chpxList.get(index)).getGrpprl().length;
121
122         // check to see if we have enough room for an FC, the grpprl offset,
123
// the grpprl size byte and the grpprl.
124
totalSize += (FC_SIZE + 2 + grpprlLength);
125         // if size is uneven we will have to add one so the first grpprl falls
126
// on a word boundary
127
if (totalSize > 511 + (index % 2))
128         {
129           totalSize -= (FC_SIZE + 2 + grpprlLength);
130           break;
131         }
132
133         // grpprls must fall on word boundaries
134
if ((1 + grpprlLength) % 2 > 0)
135         {
136           totalSize += 1;
137         }
138       }
139
140       // see if we couldn't fit some
141
if (index != size)
142       {
143         _overFlow = new ArrayList JavaDoc();
144         _overFlow.addAll(_chpxList.subList(index, size));
145       }
146
147       // index should equal number of CHPXs that will be in this fkp now.
148
buf[511] = (byte)index;
149
150       offsetOffset = (FC_SIZE * index) + FC_SIZE;
151       //grpprlOffset = offsetOffset + index + (grpprlOffset % 2);
152

153       CHPX chpx = null;
154       for (int x = 0; x < index; x++)
155       {
156         chpx = (CHPX)_chpxList.get(x);
157         byte[] grpprl = chpx.getGrpprl();
158
159         LittleEndian.putInt(buf, fcOffset, chpx.getStart() + fcMin);
160         grpprlOffset -= (1 + grpprl.length);
161         grpprlOffset -= (grpprlOffset % 2);
162         buf[offsetOffset] = (byte)(grpprlOffset/2);
163         buf[grpprlOffset] = (byte)grpprl.length;
164         System.arraycopy(grpprl, 0, buf, grpprlOffset + 1, grpprl.length);
165
166         offsetOffset += 1;
167         fcOffset += FC_SIZE;
168       }
169       // put the last chpx's end in
170
LittleEndian.putInt(buf, fcOffset, chpx.getEnd() + fcMin);
171       return buf;
172     }
173
174 }
175
Popular Tags