1 16 17 package org.apache.poi.hwpf.model; 18 19 import java.util.ArrayList ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.util.List ; 23 24 import org.apache.poi.util.LittleEndian; 25 import org.apache.poi.hwpf.model.io.*; 26 27 30 public class SectionTable 31 { 32 private static final int SED_SIZE = 12; 33 34 protected ArrayList _sections = new ArrayList (); 35 protected List _text; 36 37 public SectionTable() 38 { 39 } 40 41 42 public SectionTable(byte[] documentStream, byte[] tableStream, int offset, 43 int size, int fcMin, 44 List tpt) 45 { 46 PlexOfCps sedPlex = new PlexOfCps(tableStream, offset, size, SED_SIZE); 47 _text = tpt; 48 49 int length = sedPlex.length(); 50 51 for (int x = 0; x < length; x++) 52 { 53 GenericPropertyNode node = sedPlex.getProperty(x); 54 SectionDescriptor sed = new SectionDescriptor(node.getBytes(), 0); 55 56 int fileOffset = sed.getFc(); 57 58 if (fileOffset == 0xffffffff) 60 { 61 _sections.add(new SEPX(sed, CPtoFC(node.getStart()), CPtoFC(node.getEnd()), new byte[0])); 62 } 63 else 64 { 65 int sepxSize = LittleEndian.getShort(documentStream, fileOffset); 67 byte[] buf = new byte[sepxSize]; 68 fileOffset += LittleEndian.SHORT_SIZE; 69 System.arraycopy(documentStream, fileOffset, buf, 0, buf.length); 70 _sections.add(new SEPX(sed, CPtoFC(node.getStart()), CPtoFC(node.getEnd()), buf)); 71 } 72 } 73 } 74 75 public void adjustForInsert(int listIndex, int length) 76 { 77 int size = _sections.size(); 78 SEPX sepx = (SEPX)_sections.get(listIndex); 79 sepx.setEnd(sepx.getEnd() + length); 80 81 for (int x = listIndex + 1; x < size; x++) 82 { 83 sepx = (SEPX)_sections.get(x); 84 sepx.setStart(sepx.getStart() + length); 85 sepx.setEnd(sepx.getEnd() + length); 86 } 87 } 88 89 private int CPtoFC(int CP) 96 { 97 TextPiece TP = null; 98 99 for(int i=_text.size()-1; i>-1; i--) 100 { 101 TP = (TextPiece)_text.get(i); 102 103 if(CP >= TP.getCP()) break; 104 } 105 int FC = TP.getPieceDescriptor().getFilePosition(); 106 int offset = CP - TP.getCP(); 107 if(TP.usesUnicode()) offset*=2; 108 FC = FC+offset-((TextPiece)_text.get(0)).getPieceDescriptor().getFilePosition(); 109 return FC; 110 } 111 112 private int FCtoCP(int fc) 114 { 115 int size = _text.size(); 116 int cp = 0; 117 for (int x = 0; x < size; x++) 118 { 119 TextPiece piece = (TextPiece)_text.get(x); 120 121 if (fc <= piece.getEnd()) 122 { 123 cp += ((fc - piece.getStart())/ (piece.usesUnicode() ? 2 : 1)); 124 break; 125 } 126 else 127 { 128 cp += ((piece.getEnd() - piece.getStart())/ (piece.usesUnicode() ? 2 : 1)); 129 } 130 } 131 return cp; 132 } 133 134 135 public ArrayList getSections() 136 { 137 return _sections; 138 } 139 140 public void writeTo(HWPFFileSystem sys, int fcMin) 141 throws IOException 142 { 143 HWPFOutputStream docStream = sys.getStream("WordDocument"); 144 HWPFOutputStream tableStream = sys.getStream("1Table"); 145 146 int offset = docStream.getOffset(); 147 int len = _sections.size(); 148 PlexOfCps plex = new PlexOfCps(SED_SIZE); 149 150 for (int x = 0; x < len; x++) 151 { 152 SEPX sepx = (SEPX)_sections.get(x); 153 byte[] grpprl = sepx.getGrpprl(); 154 155 byte[] shortBuf = new byte[2]; 158 LittleEndian.putShort(shortBuf, (short)grpprl.length); 159 160 docStream.write(shortBuf); 161 docStream.write(grpprl); 162 163 SectionDescriptor sed = sepx.getSectionDescriptor(); 165 sed.setFc(offset); 166 167 169 170 173 GenericPropertyNode property = new GenericPropertyNode(FCtoCP(sepx.getStart()), FCtoCP(sepx.getEnd()), sed.toByteArray()); 176 177 178 plex.addProperty(property); 179 180 offset = docStream.getOffset(); 181 } 182 tableStream.write(plex.toByteArray()); 183 } 184 } 185 | Popular Tags |