1 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 ; 25 import java.io.UnsupportedEncodingException ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 32 public class TextPieceTable 33 { 34 protected ArrayList _textPieces = new ArrayList (); 35 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 45 { 46 PlexOfCps pieceTable = new PlexOfCps(tableStream, offset, size, PieceDescriptor.getSizeInBytes()); 48 49 int length = pieceTable.length(); 51 PieceDescriptor[] pieces = new PieceDescriptor[length]; 52 53 for (int x = 0; x < length; x++) 56 { 57 GenericPropertyNode node = pieceTable.getProperty(x); 58 pieces[x] = new PieceDescriptor(node.getBytes(), 0); 59 60 } 65 66 int firstPieceFilePosition = pieces[0].getFilePosition(); 67 _cpMin = firstPieceFilePosition - fcMin; 68 69 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 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 getTextPieces() 103 { 104 return _textPieces; 105 } 106 107 public byte[] writeTo(HWPFOutputStream docStream) 108 throws IOException 109 { 110 111 PlexOfCps textPlex = new PlexOfCps(PieceDescriptor.getSizeInBytes()); 112 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 pd.setFilePosition(docStream.getOffset()); 133 134 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 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 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 |