1 2 17 18 package org.apache.poi.hssf.record; 19 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.poi.util.LittleEndian; 28 29 41 public class PageBreakRecord extends Record { 42 public static final short HORIZONTAL_SID = (short)0x1B; 43 public static final short VERTICAL_SID = (short)0x1A; 44 public short sid; 45 private short numBreaks; 46 private List breaks; 47 private Map BreakMap; 48 49 56 public class Break 57 { 58 59 public short main; 60 public short subFrom; 61 public short subTo; 62 63 public Break(short main, short subFrom, short subTo) 64 { 65 this.main = main; 66 this.subFrom = subFrom; 67 this.subTo = subTo; 68 } 69 } 70 71 public PageBreakRecord() 72 { 73 74 } 75 76 80 public PageBreakRecord(short sid) { 81 super(); 82 this.sid = sid; 83 } 84 85 public PageBreakRecord(short id, short size, byte data[]) 86 { 87 super(id, size, data); 88 this.sid = id; 89 } 90 91 public PageBreakRecord(short id, short size, byte data[], int offset) 92 { 93 super(id, size, data, offset); 94 this.sid = id; 95 } 96 97 protected void fillFields(byte data[], short size, int offset) 98 { 99 short loadedBreaks = LittleEndian.getShort(data, 0 + offset); 100 setNumBreaks(loadedBreaks); 101 int pos = 2; 102 for(int k = 0; k < loadedBreaks; k++) 103 { 104 addBreak((short)(LittleEndian.getShort(data, pos + offset) - 1), LittleEndian.getShort(data, pos + 2 + offset), LittleEndian.getShort(data, pos + 4 + offset)); 105 pos += 6; 106 } 107 108 } 109 110 public short getSid() 111 { 112 return sid; 113 } 114 115 public int serialize(int offset, byte data[]) 116 { 117 int recordsize = getRecordSize(); 118 int pos = 6; 119 LittleEndian.putShort(data, offset + 0, getSid()); 120 LittleEndian.putShort(data, offset + 2, (short)(recordsize - 4)); 121 LittleEndian.putShort(data, offset + 4, getNumBreaks()); 122 for(Iterator iterator = getBreaksIterator(); iterator.hasNext();) 123 { 124 Break Break = (Break)iterator.next(); 125 LittleEndian.putShort(data, offset + pos, (short)(Break.main + 1)); 126 pos += 2; 127 LittleEndian.putShort(data, offset + pos, Break.subFrom); 128 pos += 2; 129 LittleEndian.putShort(data, offset + pos, Break.subTo); 130 pos += 2; 131 } 132 133 return recordsize; 134 } 135 136 protected void validateSid(short id) 137 { 138 if(id != HORIZONTAL_SID && id != VERTICAL_SID) 139 throw new RecordFormatException("NOT A HorizontalPageBreak or VerticalPageBreak RECORD!! " + id); 140 else 141 return; 142 } 143 144 public short getNumBreaks() 145 { 146 return breaks != null ? (short)breaks.size() : numBreaks; 147 } 148 149 public void setNumBreaks(short numBreaks) 150 { 151 this.numBreaks = numBreaks; 152 } 153 154 public Iterator getBreaksIterator() 155 { 156 if(breaks == null) 157 return Collections.EMPTY_LIST.iterator(); 158 else 159 return breaks.iterator(); 160 } 161 162 public String toString() 163 { 164 StringBuffer retval = new StringBuffer (); 165 166 if (getSid() != HORIZONTAL_SID && getSid()!= VERTICAL_SID) 167 return "[INVALIDPAGEBREAK]\n .sid ="+getSid()+"[INVALIDPAGEBREAK]"; 168 169 String label; 170 String mainLabel; 171 String subLabel; 172 173 if (getSid() == HORIZONTAL_SID) { 174 label = "HORIZONTALPAGEBREAK"; 175 mainLabel = "row"; 176 subLabel = "col"; 177 } else { 178 label = "VERTICALPAGEBREAK"; 179 mainLabel = "column"; 180 subLabel = "row"; 181 } 182 183 retval.append("["+label+"]").append("\n"); 184 retval.append(" .sid =").append(getSid()).append("\n"); 185 retval.append(" .numbreaks =").append(getNumBreaks()).append("\n"); 186 Iterator iterator = getBreaksIterator(); 187 for(int k = 0; k < getNumBreaks(); k++) 188 { 189 Break region = (Break)iterator.next(); 190 191 retval.append(" .").append(mainLabel).append(" (zero-based) =").append(region.main).append("\n"); 192 retval.append(" .").append(subLabel).append("From =").append(region.subFrom).append("\n"); 193 retval.append(" .").append(subLabel).append("To =").append(region.subTo).append("\n"); 194 } 195 196 retval.append("["+label+"]").append("\n"); 197 return retval.toString(); 198 } 199 200 206 public void addBreak(short main, short subFrom, short subTo) 207 { 208 if(breaks == null) 209 { 210 breaks = new ArrayList (getNumBreaks() + 10); 211 BreakMap = new HashMap (); 212 } 213 Integer key = new Integer (main); 214 Break region = (Break)BreakMap.get(key); 215 if(region != null) 216 { 217 region.main = main; 218 region.subFrom = subFrom; 219 region.subTo = subTo; 220 } else 221 { 222 region = new Break(main, subFrom, subTo); 223 breaks.add(region); 224 } 225 BreakMap.put(key, region); 226 } 227 228 232 public void removeBreak(short main) 233 { 234 Integer rowKey = new Integer (main); 235 Break region = (Break)BreakMap.get(rowKey); 236 breaks.remove(region); 237 BreakMap.remove(rowKey); 238 } 239 240 public int getRecordSize() 241 { 242 return 6 + getNumBreaks() * 6; 243 } 244 245 250 public Break getBreak(short main) 251 { 252 Integer rowKey = new Integer (main); 253 return (Break)BreakMap.get(rowKey); 254 } 255 256 259 public Object clone() { 260 PageBreakRecord record = new PageBreakRecord(getSid()); 261 Iterator iterator = getBreaksIterator(); 262 while (iterator.hasNext()) { 263 Break original = (Break)iterator.next(); 264 record.addBreak(original.main, original.subFrom, original.subTo); 265 } 266 return record; 267 } 268 269 270 } 271 | Popular Tags |