1 16 17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements; 18 19 import java.io.IOException ; 20 import java.util.Hashtable ; 21 22 import org.apache.cocoon.components.elementprocessor.ElementProcessor; 23 import org.apache.cocoon.components.elementprocessor.types.Attribute; 24 import org.apache.cocoon.components.elementprocessor.types.BooleanConverter; 25 import org.apache.cocoon.components.elementprocessor.types.BooleanResult; 26 import org.apache.cocoon.components.elementprocessor.types.NumericConverter; 27 import org.apache.cocoon.components.elementprocessor.types.NumericResult; 28 import org.apache.cocoon.components.elementprocessor.types.Validator; 29 30 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 31 import org.apache.poi.hssf.usermodel.HSSFDataFormat; 32 import org.apache.poi.hssf.util.HSSFColor; 33 34 46 public class EPStyle extends BaseElementProcessor { 47 private static final String _general_format = "General"; 48 private HorizontalAlignment _h_align; 49 private VerticalAlignment _v_align; 50 private BooleanResult _wrap_text; 51 private StyleOrientation _orient; 52 private NumericResult _shade; 53 private NumericResult _indent; 54 private ColorCode _fore; 55 private ColorCode _back; 56 private ColorCode _pattern_color; 57 private String _format; 58 private static final String _h_align_attribute = "HAlign"; 59 private static final String _v_align_attribute = "VAlign"; 60 private static final String _wrap_text_attribute = "WrapText"; 61 private static final String _orient_attribute = "Orient"; 62 private static final String _shade_attribute = "Shade"; 63 private static final String _indent_attribute = "Indent"; 64 private static final String _fore_attribute = "Fore"; 65 private static final String _back_attribute = "Back"; 66 private static final String _pattern_color_attribute = "PatternColor"; 67 private static final String _format_attribute = "Format"; 68 69 private boolean invalid; 70 71 private static final Validator _shade_validator = new Validator() { 72 public IOException validate(final Number number) { 73 return StyleShading.isValid(number.intValue()) ? null 74 : new IOException ("\"" + number + "\" is not a legal value"); 75 } 76 }; 77 78 81 public EPStyle() { 82 super(null); 83 _h_align = null; 84 _v_align = null; 85 _wrap_text = null; 86 _orient = null; 87 _shade = null; 88 _indent = null; 89 _fore = null; 90 _back = null; 91 _pattern_color = null; 92 _format = null; 93 } 94 95 102 public void initialize(final Attribute[] attributes, 103 final ElementProcessor parent) throws IOException { 104 super.initialize(attributes, parent); 105 106 EPStyleRegion sregion = (EPStyleRegion)parent; 107 108 if (sregion.isValid()) { 109 Hashtable colorhash = sregion.getColorHash(); 110 111 HSSFCellStyle style = sregion.getStyle(); 112 short cnvhalign = 113 convertAlignment(getHorizontalAlignment().getCode()); 114 style.setAlignment(cnvhalign); 115 short cnvvalign = 116 convertVAlignment(getVerticalAlignment().getCode()); 117 style.setVerticalAlignment(cnvvalign); 118 style.setFillPattern((short)getShade()); 119 120 Workbook workbook = getWorkbook(); 121 HSSFDataFormat dataformat = workbook.createDataFormat(); 122 if (getShade() == 1) { 123 if (getLogger().isDebugEnabled()) { 126 getLogger().debug("shade = 1"); 127 } 128 HSSFColor color = 129 (HSSFColor)colorhash.get(getBackgroundColor().toString()); 130 if (color == null) { 131 if (getLogger().isDebugEnabled()) { 132 getLogger().debug("s1 BG couldn't find color for " 133 + getBackgroundColor().toString()); 134 } 135 color = new HSSFColor.WHITE(); 136 } 137 style.setFillForegroundColor(color.getIndex()); 138 color = (HSSFColor)colorhash.get(getPatternColor().toString()); 139 if (color == null) { 140 if (getLogger().isDebugEnabled()) { 141 getLogger().debug("s1 PC couldn't find color for " 142 + getPatternColor().toString()); 143 } 144 color = new HSSFColor.BLACK(); 145 } 146 style.setFillBackgroundColor(color.getIndex()); 147 } else { 148 HSSFColor color = 149 (HSSFColor)colorhash.get(getBackgroundColor().toString()); 150 if (color == null) { 151 if (getLogger().isDebugEnabled()) { 152 getLogger().debug( 153 "BG couldn't find color for " 154 + getBackgroundColor().toString()); 155 } 156 color = new HSSFColor.BLACK(); 157 } 158 style.setFillBackgroundColor(color.getIndex()); 159 color = (HSSFColor)colorhash.get(getPatternColor().toString()); 160 if (color == null) { 161 if (getLogger().isDebugEnabled()) { 162 getLogger().debug("PC couldn't find color for " 163 + getPatternColor().toString()); 164 } 165 color = new HSSFColor.WHITE(); 166 } 167 style.setFillForegroundColor(color.getIndex()); 168 } 169 style.setWrapText(getWrapText()); 170 style.setLocked(true); 171 172 String format = null; 173 try { 174 format = getFormat(); 175 } catch (NullPointerException e) { 176 format = _general_format; 177 } 178 179 if (!_general_format.equals(format)) { 180 short valuenumber; 181 format = kludgeForGnumericMisformats(format); 182 format = kludgeForGnumericDateDivergence(format); 183 if (getLogger().isDebugEnabled()) { 184 getLogger().debug("setting format to " + format); 185 } 186 Object o = 187 workbook.getValidate(format, dataformat.getFormat(format)); 188 Short sh = null; 189 sh = (Short )o; 190 valuenumber = sh.shortValue(); 191 style.setDataFormat(valuenumber); 192 } 193 } else { 194 invalid = true; 195 } 196 } 197 198 202 public boolean isHorizontalGeneral() throws IOException { 203 return getHorizontalAlignment().isGeneral(); 204 } 205 206 210 public boolean isHorizontalLeft() throws IOException { 211 return getHorizontalAlignment().isLeft(); 212 } 213 214 218 public boolean isHorizontalRight() throws IOException { 219 return getHorizontalAlignment().isRight(); 220 } 221 222 226 public boolean isHorizontalCenter() throws IOException { 227 return getHorizontalAlignment().isCenter(); 228 } 229 230 234 public boolean isHorizontalFill() throws IOException { 235 return getHorizontalAlignment().isFill(); 236 } 237 238 242 public boolean isHorizontalJustify() throws IOException { 243 return getHorizontalAlignment().isJustify(); 244 } 245 246 250 public boolean isHorizontalCenterAcrossSelection() throws IOException { 251 return getHorizontalAlignment().isCenterAcrossSelection(); 252 } 253 254 258 public boolean isVerticalTop() throws IOException { 259 return getVerticalAlignment().isTop(); 260 } 261 262 266 public boolean isVerticalBottom() throws IOException { 267 return getVerticalAlignment().isBottom(); 268 } 269 270 274 public boolean isVerticalCenter() throws IOException { 275 return getVerticalAlignment().isCenter(); 276 } 277 278 282 public boolean isVerticalJustify() throws IOException { 283 return getVerticalAlignment().isJustify(); 284 } 285 286 290 public boolean getWrapText() throws IOException { 291 if (_wrap_text == null) { 292 _wrap_text = 293 BooleanConverter.extractBoolean(getValue(_wrap_text_attribute)); 294 } 295 return _wrap_text.booleanValue(); 296 } 297 298 302 public boolean isStyleOrientationHoriz() throws IOException { 303 return getStyleOrientation().isHoriz(); 304 } 305 306 310 public boolean isStyleOrientationVertHorizText() throws IOException { 311 return getStyleOrientation().isVertHorizText(); 312 } 313 314 318 public boolean isStyleOrientationVertVertText() throws IOException { 319 return getStyleOrientation().isVertVertText(); 320 } 321 322 326 public boolean isStyleOrientationVertVertText2() throws IOException { 327 return getStyleOrientation().isVertVertText2(); 328 } 329 330 334 public int getShade() throws IOException { 335 if (_shade == null) { 336 _shade = NumericConverter.extractInteger( 337 getValue(_shade_attribute), _shade_validator); 338 } 339 return _shade.intValue(); 340 } 341 342 346 public int getIndent() throws IOException { 347 if (_indent == null) { 348 _indent = 349 NumericConverter.extractInteger(getValue(_indent_attribute)); 350 } 351 return _indent.intValue(); 352 } 353 354 358 public ColorCode getForegroundColor() throws IOException { 359 if (_fore == null) { 360 _fore = new ColorCode(getValue(_fore_attribute)); 361 } 362 return _fore; 363 } 364 365 369 public ColorCode getBackgroundColor() throws IOException { 370 if (_back == null) { 371 _back = new ColorCode(getValue(_back_attribute)); 372 } 373 return _back; 374 } 375 376 380 public ColorCode getPatternColor() throws IOException { 381 if (_pattern_color == null) { 382 _pattern_color = new ColorCode(getValue(_pattern_color_attribute)); 383 } 384 return _pattern_color; 385 } 386 387 391 public String getFormat() throws IOException { 392 if (_format == null) { 393 _format = getValue(_format_attribute); 394 398 } 399 return _format; 400 } 401 402 private HorizontalAlignment getHorizontalAlignment() throws IOException { 403 if (_h_align == null) { 404 _h_align = new HorizontalAlignment(getValue(_h_align_attribute)); 405 } 406 return _h_align; 407 } 408 409 private VerticalAlignment getVerticalAlignment() throws IOException { 410 if (_v_align == null) { 411 _v_align = new VerticalAlignment(getValue(_v_align_attribute)); 412 } 413 return _v_align; 414 } 415 416 private StyleOrientation getStyleOrientation() throws IOException { 417 if (_orient == null) { 418 _orient = new StyleOrientation(getValue(_orient_attribute)); 419 } 420 return _orient; 421 } 422 423 428 Hashtable getColorHash() { 429 return ((EPStyleRegion)getAncestor(EPStyleRegion.class)).getColorHash(); 430 } 431 432 435 HSSFCellStyle getStyle() { 436 return ((EPStyleRegion)getAncestor(EPStyleRegion.class)).getStyle(); 437 } 438 439 443 public boolean isValid() { 444 return (!invalid); 445 } 446 447 450 private short convertAlignment(short alignment) { 451 short retval = HSSFCellStyle.ALIGN_GENERAL; 453 switch (alignment) { 454 case 1 : 455 retval = HSSFCellStyle.ALIGN_GENERAL; 456 break; 457 case 2 : 458 retval = HSSFCellStyle.ALIGN_LEFT; 459 break; 460 case 4 : 461 retval = HSSFCellStyle.ALIGN_RIGHT; 462 break; 463 case 8 : 464 retval = HSSFCellStyle.ALIGN_CENTER; 465 break; 466 case 16 : 467 retval = HSSFCellStyle.ALIGN_FILL; 468 break; 469 case 32 : 470 retval = HSSFCellStyle.ALIGN_JUSTIFY; 471 break; 472 case 64 : 473 retval = HSSFCellStyle.ALIGN_CENTER_SELECTION; 474 break; 475 default : 476 retval = HSSFCellStyle.ALIGN_GENERAL; 477 } 478 return retval; 479 } 480 481 484 private short convertVAlignment(short alignment) { 485 short retval = HSSFCellStyle.VERTICAL_TOP; 487 switch (alignment) { 488 case 1 : 489 retval = HSSFCellStyle.VERTICAL_TOP; 490 break; 491 case 2 : 492 retval = HSSFCellStyle.VERTICAL_BOTTOM; 493 break; 494 case 4 : 495 retval = HSSFCellStyle.VERTICAL_CENTER; 496 break; 497 case 8 : 498 retval = HSSFCellStyle.VERTICAL_JUSTIFY; 499 break; 500 default : 501 retval = HSSFCellStyle.VERTICAL_TOP; 502 } 503 return retval; 504 } 505 506 512 private String kludgeForGnumericMisformats(String format) { 513 String retval = format; 514 if (getLogger().isDebugEnabled()) { 515 getLogger().debug("going out of the format kludger " + retval); 516 getLogger().debug("first )=" + format.indexOf(')')); 517 getLogger().debug("first (=" + format.indexOf('(')); 518 } 519 if (format.indexOf(')') < format.indexOf('(') 520 && (format.indexOf(')') != -1)) { 521 retval = "(" + format; 522 } 523 if (getLogger().isDebugEnabled()) { 524 getLogger().debug("going out of the format kludger " + retval); 525 } 526 return retval; 527 } 528 529 private String kludgeForGnumericDateDivergence(String format) { 530 String retval = format; 531 if (getLogger().isDebugEnabled()) { 532 getLogger().debug( 533 "going into the format kludgeForGnumericDateDivergence" 534 + retval); 535 } 536 537 if (retval.equals("mm/dd/yy")) { 538 retval = "m/d/yy"; 539 } else if (retval.equals("dd-mmm-yy")) { 540 retval = "d-mmm-yy"; 541 } else if (retval.equals("dd-mmm")) { 542 retval = "d-mmm"; 543 } 544 if (getLogger().isDebugEnabled()) { 545 getLogger().debug( 546 "going out of the format kludgeForGnumericDateDivergence" 547 + retval); 548 } 549 return retval; 550 } 551 552 } | Popular Tags |