1 19 20 package jxl.biff; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.text.DateFormat ; 26 import java.text.NumberFormat ; 27 import java.io.IOException ; 28 29 import common.Assert; 30 import common.Logger; 31 32 import jxl.write.biff.File; 33 import jxl.format.Colour; 34 import jxl.format.RGB; 35 36 39 public class FormattingRecords 40 { 41 44 private static Logger logger = Logger.getLogger(FormattingRecords.class); 45 46 50 private HashMap formats; 51 52 55 private ArrayList formatsList; 56 57 60 private ArrayList xfRecords; 61 62 65 private int nextCustomIndexNumber; 66 67 70 private Fonts fonts; 71 72 75 private PaletteRecord palette; 76 77 80 private static final int customFormatStartIndex = 0xa4; 81 82 86 private static final int maxFormatRecordsIndex = 0x1b9; 87 88 92 private static final int minXFRecords = 21; 93 94 99 public FormattingRecords(Fonts f) 100 { 101 xfRecords = new ArrayList (10); 102 formats = new HashMap (10); 103 formatsList = new ArrayList (10); 104 fonts = f; 105 nextCustomIndexNumber = customFormatStartIndex; 106 } 107 108 117 public final void addStyle(XFRecord xf) 118 throws NumFormatRecordsException 119 { 120 if (!xf.isInitialized()) 121 { 122 int pos = xfRecords.size(); 123 xf.initialize(pos, this, fonts); 124 xfRecords.add(xf); 125 } 126 else 127 { 128 if (xf.getXFIndex() >= xfRecords.size()) 132 { 133 xfRecords.add(xf); 134 } 135 } 136 } 137 138 146 public final void addFormat(DisplayFormat fr) 147 throws NumFormatRecordsException 148 { 149 if (fr.isInitialized() && 152 fr.getFormatIndex() >= maxFormatRecordsIndex) 153 { 154 logger.warn("Format index exceeds Excel maximum - assigning custom " + 155 "number"); 156 fr.initialize(nextCustomIndexNumber); 157 nextCustomIndexNumber++; 158 } 159 160 if (!fr.isInitialized()) 162 { 163 fr.initialize(nextCustomIndexNumber); 164 nextCustomIndexNumber++; 165 } 166 167 if (nextCustomIndexNumber > maxFormatRecordsIndex) 168 { 169 nextCustomIndexNumber = maxFormatRecordsIndex; 170 throw new NumFormatRecordsException(); 171 } 172 173 174 if (fr.getFormatIndex() >= nextCustomIndexNumber) 175 { 176 nextCustomIndexNumber = fr.getFormatIndex() + 1; 177 } 178 179 if (!fr.isBuiltIn()) 180 { 181 formatsList.add(fr); 182 formats.put(new Integer (fr.getFormatIndex()), fr); 183 } 184 } 185 186 194 public final boolean isDate(int pos) 195 { 196 XFRecord xfr = (XFRecord) xfRecords.get(pos); 197 198 if (xfr.isDate()) 199 { 200 return true; 201 } 202 203 FormatRecord fr = (FormatRecord) 204 formats.get(new Integer (xfr.getFormatRecord())); 205 206 return fr == null ? false : fr.isDate(); 207 } 208 209 216 public final DateFormat getDateFormat(int pos) 217 { 218 XFRecord xfr = (XFRecord) xfRecords.get(pos); 219 220 if (xfr.isDate()) 221 { 222 return xfr.getDateFormat(); 223 } 224 225 FormatRecord fr = (FormatRecord) 226 formats.get(new Integer (xfr.getFormatRecord())); 227 228 if (fr == null) 229 { 230 return null; 231 } 232 233 return fr.isDate() ? fr.getDateFormat() : null; 234 } 235 236 243 public final NumberFormat getNumberFormat(int pos) 244 { 245 XFRecord xfr = (XFRecord) xfRecords.get(pos); 246 247 if (xfr.isNumber()) 248 { 249 return xfr.getNumberFormat(); 250 } 251 252 FormatRecord fr = (FormatRecord) 253 formats.get(new Integer (xfr.getFormatRecord())); 254 255 if (fr == null) 256 { 257 return null; 258 } 259 260 return fr.isNumber() ? fr.getNumberFormat() : null; 261 } 262 263 269 FormatRecord getFormatRecord(int index) 270 { 271 return (FormatRecord) 272 formats.get(new Integer (index)); 273 } 274 280 public void write(File outputFile) throws IOException 281 { 282 Iterator i = formatsList.iterator(); 284 FormatRecord fr = null; 285 while (i.hasNext()) 286 { 287 fr = (FormatRecord) i.next(); 288 outputFile.write(fr); 289 } 290 291 i = xfRecords.iterator(); 293 XFRecord xfr = null; 294 while (i.hasNext()) 295 { 296 xfr = (XFRecord) i.next(); 297 outputFile.write(xfr); 298 } 299 300 BuiltInStyle style = new BuiltInStyle(0x10, 3); 302 outputFile.write(style); 303 304 style = new BuiltInStyle(0x11, 6); 305 outputFile.write(style); 306 307 style = new BuiltInStyle(0x12, 4); 308 outputFile.write(style); 309 310 style = new BuiltInStyle(0x13, 7); 311 outputFile.write(style); 312 313 style = new BuiltInStyle(0x0, 0); 314 outputFile.write(style); 315 316 style = new BuiltInStyle(0x14, 5); 317 outputFile.write(style); 318 } 319 320 325 protected final Fonts getFonts() 326 { 327 return fonts; 328 } 329 330 337 public final XFRecord getXFRecord(int index) 338 { 339 return (XFRecord) xfRecords.get(index); 340 } 341 342 349 protected final int getNumberOfFormatRecords() 350 { 351 return formatsList.size(); 352 } 353 354 359 public IndexMapping rationalizeFonts() 360 { 361 return fonts.rationalize(); 362 } 363 364 373 public IndexMapping rationalize(IndexMapping fontMapping, 374 IndexMapping formatMapping) 375 { 376 XFRecord xfr = null; 380 for (Iterator it = xfRecords.iterator(); it.hasNext();) 381 { 382 xfr = (XFRecord) it.next(); 383 384 if (xfr.getFormatRecord() >= customFormatStartIndex) 385 { 386 xfr.setFormatIndex(formatMapping.getNewIndex(xfr.getFormatRecord())); 387 } 388 389 xfr.setFontIndex(fontMapping.getNewIndex(xfr.getFontIndex())); 390 } 391 392 ArrayList newrecords = new ArrayList (minXFRecords); 393 IndexMapping mapping = new IndexMapping(xfRecords.size()); 394 int numremoved = 0; 395 396 for (int i = 0; i < minXFRecords; i++) 398 { 399 newrecords.add(xfRecords.get(i)); 400 mapping.setMapping(i, i); 401 } 402 403 for (int i = minXFRecords; i < xfRecords.size(); i++) 405 { 406 XFRecord xf = (XFRecord) xfRecords.get(i); 407 408 boolean duplicate = false; 410 for (Iterator it = newrecords.iterator(); 411 it.hasNext() && !duplicate;) 412 { 413 XFRecord xf2 = (XFRecord) it.next(); 414 if (xf2.equals(xf)) 415 { 416 duplicate = true; 417 mapping.setMapping(i, mapping.getNewIndex(xf2.getXFIndex())); 418 numremoved++; 419 } 420 } 421 422 if (!duplicate) 424 { 425 newrecords.add(xf); 426 mapping.setMapping(i, i - numremoved); 427 } 428 } 429 430 for (Iterator i = xfRecords.iterator(); i.hasNext();) 434 { 435 XFRecord xf = (XFRecord) i.next(); 436 xf.rationalize(mapping); 437 } 438 439 xfRecords = newrecords; 441 442 return mapping; 443 } 444 445 453 public IndexMapping rationalizeDisplayFormats() 454 { 455 ArrayList newformats = new ArrayList (); 456 int numremoved = 0; 457 IndexMapping mapping = new IndexMapping(nextCustomIndexNumber); 458 459 Iterator i = formatsList.iterator(); 461 DisplayFormat df = null; 462 DisplayFormat df2 = null; 463 boolean duplicate = false; 464 while (i.hasNext()) 465 { 466 df = (DisplayFormat) i.next(); 467 468 Assert.verify(!df.isBuiltIn()); 469 470 Iterator i2 = newformats.iterator(); 472 duplicate = false; 473 while (i2.hasNext() && !duplicate) 474 { 475 df2 = (DisplayFormat) i2.next(); 476 if (df2.equals(df)) 477 { 478 duplicate = true; 479 mapping.setMapping(df.getFormatIndex(), 480 mapping.getNewIndex(df2.getFormatIndex())); 481 numremoved++; 482 } 483 } 484 485 if (!duplicate) 487 { 488 newformats.add(df); 489 int indexnum = df.getFormatIndex() - numremoved; 490 if (indexnum > maxFormatRecordsIndex) 491 { 492 logger.warn("Too many number formats - using default format."); 493 indexnum = 0; } 495 mapping.setMapping(df.getFormatIndex(), 496 df.getFormatIndex() - numremoved); 497 } 498 } 499 500 formatsList = newformats; 502 503 i = formatsList.iterator(); 505 506 while (i.hasNext()) 507 { 508 df = (DisplayFormat) i.next(); 509 df.initialize(mapping.getNewIndex(df.getFormatIndex())); 510 } 511 512 return mapping; 513 } 514 515 520 public PaletteRecord getPalette() 521 { 522 return palette; 523 } 524 525 530 public void setPalette(PaletteRecord pr) 531 { 532 palette = pr; 533 } 534 535 543 public void setColourRGB(Colour c, int r, int g, int b) 544 { 545 if (palette == null) 546 { 547 palette = new PaletteRecord(); 548 } 549 palette.setColourRGB(c, r, g, b); 550 } 551 552 557 public RGB getColourRGB(Colour c) 558 { 559 if (palette == null) 560 { 561 return c.getDefaultRGB(); 562 } 563 564 return palette.getColourRGB(c); 565 } 566 } 567 | Popular Tags |