1 16 17 package org.apache.poi.hwpf.model; 18 19 import java.util.List ; 20 import java.util.ArrayList ; 21 import java.io.OutputStream ; 22 import java.io.IOException ; 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 34 public class CHPBinTable 35 { 36 37 protected ArrayList _textRuns = new ArrayList (); 38 39 40 public CHPBinTable() 41 { 42 } 43 44 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 getTextRuns() 156 { 157 return _textRuns; 158 } 159 160 public void writeTo(HWPFFileSystem sys, int fcMin) 161 throws IOException 162 { 163 164 HWPFOutputStream docStream = sys.getStream("WordDocument"); 165 OutputStream tableStream = sys.getStream("1Table"); 166 167 PlexOfCps binTable = new PlexOfCps(4); 168 169 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 docOffset = docStream.getOffset(); 180 int pageNum = docOffset/POIFSConstants.BIG_BLOCK_SIZE; 181 182 int endingFc = ((PropertyNode)_textRuns.get(_textRuns.size() - 1)).getEnd(); 184 endingFc += fcMin; 185 186 187 ArrayList 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 |