KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.apache.poi.hwpf.model;
19
20
21 import org.apache.poi.hwpf.model.io.HWPFOutputStream;
22 import org.apache.poi.poifs.common.POIFSConstants;
23
24 import java.io.IOException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * @author Ryan Ackley
31  */

32 public class TextPieceTable
33 {
34   protected ArrayList JavaDoc _textPieces = new ArrayList JavaDoc();
35   //int _multiple;
36
int _cpMin;
37
38   public TextPieceTable()
39   {
40   }
41
42   public TextPieceTable(byte[] documentStream, byte[] tableStream, int offset,
43                         int size, int fcMin)
44     throws UnsupportedEncodingException JavaDoc
45   {
46     // get our plex of PieceDescriptors
47
PlexOfCps pieceTable = new PlexOfCps(tableStream, offset, size, PieceDescriptor.getSizeInBytes());
48
49     //_multiple = 2;
50
int length = pieceTable.length();
51     PieceDescriptor[] pieces = new PieceDescriptor[length];
52
53     // iterate through piece descriptors raw bytes and create
54
// PieceDescriptor objects
55
for (int x = 0; x < length; x++)
56     {
57       GenericPropertyNode node = pieceTable.getProperty(x);
58       pieces[x] = new PieceDescriptor(node.getBytes(), 0);
59
60 // if (!pieces[x].isUnicode())
61
// {
62
// _multiple = 1;
63
// }
64
}
65
66     int firstPieceFilePosition = pieces[0].getFilePosition();
67     _cpMin = firstPieceFilePosition - fcMin;
68
69     // using the PieceDescriptors, build our list of TextPieces.
70
for (int x = 0; x < pieces.length; x++)
71     {
72       int start = pieces[x].getFilePosition();
73       PropertyNode node = pieceTable.getProperty(x);
74       int nodeStart = node.getStart();
75
76       // multiple will be 2 if there is only one piece and its unicode. Some
77
// type of optimization.
78
boolean unicode = pieces[x].isUnicode();
79
80       int multiple = 1;
81       if (unicode)
82       {
83         multiple = 2;
84       }
85       int nodeEnd = ((node.getEnd() - nodeStart) * multiple) + nodeStart;
86       int textSize = nodeEnd - nodeStart;
87
88
89       byte[] buf = new byte[textSize];
90       System.arraycopy(documentStream, start, buf, 0, textSize);
91
92       int startFilePosition = start - firstPieceFilePosition;
93       _textPieces.add(new TextPiece(startFilePosition, startFilePosition+textSize, buf, pieces[x], node.getStart()));
94     }
95   }
96
97   public int getCpMin()
98   {
99     return _cpMin;
100   }
101
102   public List JavaDoc getTextPieces()
103   {
104     return _textPieces;
105   }
106
107   public byte[] writeTo(HWPFOutputStream docStream)
108     throws IOException JavaDoc
109   {
110
111     PlexOfCps textPlex = new PlexOfCps(PieceDescriptor.getSizeInBytes());
112     //int fcMin = docStream.getOffset();
113

114     int size = _textPieces.size();
115     int bumpDown = 0;
116     for (int x = 0; x < size; x++)
117     {
118       TextPiece next = (TextPiece)_textPieces.get(x);
119       PieceDescriptor pd = next.getPieceDescriptor();
120
121       int offset = docStream.getOffset();
122       int mod = (offset % POIFSConstants.BIG_BLOCK_SIZE);
123       if (mod != 0)
124       {
125         mod = POIFSConstants.BIG_BLOCK_SIZE - mod;
126         byte[] buf = new byte[mod];
127         docStream.write(buf);
128       }
129
130
131       // set the text piece position to the current docStream offset.
132
pd.setFilePosition(docStream.getOffset());
133
134       // write the text to the docstream and save the piece descriptor to the
135
// plex which will be written later to the tableStream.
136
//if (_multiple == 1 && pd.isUnicode() &&
137
docStream.write(next.getRawBytes());
138
139       int nodeStart = next.getStart();
140       int multiple = 1;
141       if (pd.isUnicode())
142       {
143         multiple = 2;
144       }
145       textPlex.addProperty(new GenericPropertyNode(nodeStart - bumpDown,
146         ((next.getEnd() - nodeStart)/multiple + nodeStart) - bumpDown,
147         pd.toByteArray()));
148
149       if (pd.isUnicode())
150       {
151         bumpDown += ((next.getEnd() - nodeStart)/multiple);
152       }
153
154
155     }
156
157     return textPlex.toByteArray();
158
159   }
160
161
162   public int adjustForInsert(int listIndex, int length)
163   {
164     int size = _textPieces.size();
165
166     TextPiece tp = (TextPiece)_textPieces.get(listIndex);
167
168     //The text piece stores the length on file.
169
length = length * (tp.usesUnicode() ? 2 : 1);
170     tp.setEnd(tp.getEnd() + length);
171     for (int x = listIndex + 1; x < size; x++)
172     {
173       tp = (TextPiece)_textPieces.get(x);
174       tp.setStart(tp.getStart() + length);
175       tp.setEnd(tp.getEnd() + length);
176     }
177     return length;
178   }
179
180
181   public boolean equals(Object JavaDoc o)
182   {
183     TextPieceTable tpt = (TextPieceTable)o;
184
185     int size = tpt._textPieces.size();
186     if (size == _textPieces.size())
187     {
188       for (int x = 0; x < size; x++)
189       {
190         if (!tpt._textPieces.get(x).equals(_textPieces.get(x)))
191         {
192           return false;
193         }
194       }
195       return true;
196     }
197     return false;
198   }
199 }
200
Popular Tags