1 28 package net.sf.jasperreports.engine; 29 30 import java.awt.font.TextAttribute ; 31 import java.io.File ; 32 import java.io.InputStream ; 33 import java.math.BigDecimal ; 34 import java.math.BigInteger ; 35 import java.net.URL ; 36 import java.net.URLStreamHandlerFactory ; 37 import java.text.DateFormat ; 38 import java.text.NumberFormat ; 39 import java.text.ParseException ; 40 import java.util.ArrayList ; 41 import java.util.Date ; 42 import java.util.HashMap ; 43 import java.util.LinkedList ; 44 import java.util.List ; 45 import java.util.Locale ; 46 import java.util.Map ; 47 import java.util.TimeZone ; 48 49 import net.sf.jasperreports.engine.base.JRBaseBox; 50 import net.sf.jasperreports.engine.export.data.BooleanTextValue; 51 import net.sf.jasperreports.engine.export.data.DateTextValue; 52 import net.sf.jasperreports.engine.export.data.NumberTextValue; 53 import net.sf.jasperreports.engine.export.data.StringTextValue; 54 import net.sf.jasperreports.engine.export.data.TextValue; 55 import net.sf.jasperreports.engine.util.DefaultFormatFactory; 56 import net.sf.jasperreports.engine.util.FormatFactory; 57 import net.sf.jasperreports.engine.util.JRClassLoader; 58 import net.sf.jasperreports.engine.util.JRDataUtils; 59 import net.sf.jasperreports.engine.util.JRFontUtil; 60 import net.sf.jasperreports.engine.util.JRLoader; 61 import net.sf.jasperreports.engine.util.JRResourcesUtil; 62 import net.sf.jasperreports.engine.util.JRStyledText; 63 import net.sf.jasperreports.engine.util.JRStyledTextParser; 64 65 import org.xml.sax.SAXException ; 66 67 68 72 public abstract class JRAbstractExporter implements JRExporter 73 { 74 75 78 81 protected Map parameters = new HashMap (); 82 83 86 protected List jasperPrintList = null; 87 protected JasperPrint jasperPrint = null; 88 protected boolean isModeBatch = true; 89 protected int startPageIndex = 0; 90 protected int endPageIndex = 0; 91 protected int globalOffsetX = 0; 92 protected int globalOffsetY = 0; 93 protected ClassLoader classLoader = null; 94 protected boolean classLoaderSet = false; 95 protected URLStreamHandlerFactory urlHandlerFactory = null; 96 protected boolean urlHandlerFactorySet = false; 97 98 101 private LinkedList elementOffsetStack = new LinkedList (); 102 private int elementOffsetX = globalOffsetX; 103 private int elementOffsetY = globalOffsetY; 104 105 private Map penBoxes = new HashMap (); 107 110 protected final JRStyledTextParser styledTextParser = new JRStyledTextParser(); 111 112 115 protected Map dateFormatCache = new HashMap (); 116 protected Map numberFormatCache = new HashMap (); 117 118 119 122 protected JRAbstractExporter() 123 { 124 } 125 126 127 130 public void reset() 131 { 132 parameters = new HashMap (); 133 elementOffsetStack = new LinkedList (); 134 penBoxes = new HashMap (); 135 } 136 137 138 141 public void setParameter(JRExporterParameter parameter, Object value) 142 { 143 parameters.put(parameter, value); 144 } 145 146 147 150 public Object getParameter(JRExporterParameter parameter) 151 { 152 return parameters.get(parameter); 153 } 154 155 156 159 public void setParameters(Map parameters) 160 { 161 this.parameters = parameters; 162 } 163 164 165 168 public Map getParameters() 169 { 170 return parameters; 171 } 172 173 174 177 public abstract void exportReport() throws JRException; 178 179 180 183 protected void setOffset() 184 { 185 Integer offsetX = (Integer )parameters.get(JRExporterParameter.OFFSET_X); 186 if (offsetX != null) 187 { 188 globalOffsetX = offsetX.intValue(); 189 } 190 else 191 { 192 globalOffsetX = 0; 193 } 194 195 Integer offsetY = (Integer )parameters.get(JRExporterParameter.OFFSET_Y); 196 if (offsetY != null) 197 { 198 globalOffsetY = offsetY.intValue(); 199 } 200 else 201 { 202 globalOffsetY = 0; 203 } 204 205 elementOffsetX = globalOffsetX; 206 elementOffsetY = globalOffsetY; 207 } 208 209 210 213 protected void setExportContext() 214 { 215 classLoaderSet = false; 216 urlHandlerFactorySet = false; 217 218 classLoader = (ClassLoader )parameters.get(JRExporterParameter.CLASS_LOADER); 219 if (classLoader != null) 220 { 221 JRResourcesUtil.setThreadClassLoader(classLoader); 222 classLoaderSet = true; 223 } 224 225 urlHandlerFactory = (URLStreamHandlerFactory ) parameters.get(JRExporterParameter.URL_HANDLER_FACTORY); 226 if (urlHandlerFactory != null) 227 { 228 JRResourcesUtil.setThreadURLHandlerFactory(urlHandlerFactory); 229 urlHandlerFactorySet = true; 230 } 231 } 232 233 234 237 protected void resetExportContext() 238 { 239 if (classLoaderSet) 240 { 241 JRResourcesUtil.resetClassLoader(); 242 } 243 244 if (urlHandlerFactorySet) 245 { 246 JRResourcesUtil.resetThreadURLHandlerFactory(); 247 } 248 } 249 250 251 254 protected void setClassLoader() 255 { 256 setExportContext(); 257 } 258 259 260 263 protected void resetClassLoader() 264 { 265 resetExportContext(); 266 } 267 268 269 272 protected void setInput() throws JRException 273 { 274 jasperPrintList = (List )parameters.get(JRExporterParameter.JASPER_PRINT_LIST); 275 if (jasperPrintList == null) 276 { 277 isModeBatch = false; 278 279 jasperPrint = (JasperPrint)parameters.get(JRExporterParameter.JASPER_PRINT); 280 if (jasperPrint == null) 281 { 282 InputStream is = (InputStream )parameters.get(JRExporterParameter.INPUT_STREAM); 283 if (is != null) 284 { 285 jasperPrint = (JasperPrint)JRLoader.loadObject(is); 286 } 287 else 288 { 289 URL url = (URL )parameters.get(JRExporterParameter.INPUT_URL); 290 if (url != null) 291 { 292 jasperPrint = (JasperPrint)JRLoader.loadObject(url); 293 } 294 else 295 { 296 File file = (File )parameters.get(JRExporterParameter.INPUT_FILE); 297 if (file != null) 298 { 299 jasperPrint = (JasperPrint)JRLoader.loadObject(file); 300 } 301 else 302 { 303 String fileName = (String )parameters.get(JRExporterParameter.INPUT_FILE_NAME); 304 if (fileName != null) 305 { 306 jasperPrint = (JasperPrint)JRLoader.loadObject(fileName); 307 } 308 else 309 { 310 throw new JRException("No input source supplied to the exporter."); 311 } 312 } 313 } 314 } 315 } 316 317 jasperPrintList = new ArrayList (); 318 jasperPrintList.add(jasperPrint); 319 } 320 else 321 { 322 isModeBatch = true; 323 324 if (jasperPrintList.size() == 0) 325 { 326 throw new JRException("Empty input source supplied to the exporter in batch mode."); 327 } 328 329 jasperPrint = (JasperPrint)jasperPrintList.get(0); 330 } 331 } 332 333 334 337 protected void setPageRange() throws JRException 338 { 339 int lastPageIndex = -1; 340 if (jasperPrint.getPages() != null) 341 { 342 lastPageIndex = jasperPrint.getPages().size() - 1; 343 } 344 345 Integer start = (Integer )parameters.get(JRExporterParameter.START_PAGE_INDEX); 346 if (start == null) 347 { 348 startPageIndex = 0; 349 } 350 else 351 { 352 startPageIndex = start.intValue(); 353 if (startPageIndex < 0 || startPageIndex > lastPageIndex) 354 { 355 throw new JRException("Start page index out of range : " + startPageIndex + " of " + lastPageIndex); 356 } 357 } 358 359 Integer end = (Integer )parameters.get(JRExporterParameter.END_PAGE_INDEX); 360 if (end == null) 361 { 362 endPageIndex = lastPageIndex; 363 } 364 else 365 { 366 endPageIndex = end.intValue(); 367 if (endPageIndex < startPageIndex || endPageIndex > lastPageIndex) 368 { 369 throw new JRException("End page index out of range : " + endPageIndex + " (" + startPageIndex + " : " + lastPageIndex + ")"); 370 } 371 } 372 373 Integer index = (Integer )parameters.get(JRExporterParameter.PAGE_INDEX); 374 if (index != null) 375 { 376 int pageIndex = index.intValue(); 377 if (pageIndex < 0 || pageIndex > lastPageIndex) 378 { 379 throw new JRException("Page index out of range : " + pageIndex + " of " + lastPageIndex); 380 } 381 startPageIndex = pageIndex; 382 endPageIndex = pageIndex; 383 } 384 } 385 386 387 390 protected JRStyledText getStyledText(JRPrintText textElement, boolean setBackcolor) 391 { 392 JRStyledText styledText = null; 393 394 String text = textElement.getText(); 395 if (text != null) 396 { 397 Map attributes = new HashMap (); 398 attributes.putAll(JRFontUtil.setAttributes(attributes, textElement)); 399 attributes.put(TextAttribute.FOREGROUND, textElement.getForecolor()); 400 if (setBackcolor && textElement.getMode() == JRElement.MODE_OPAQUE) 401 { 402 attributes.put(TextAttribute.BACKGROUND, textElement.getBackcolor()); 403 } 404 405 if (textElement.isStyledText()) 406 { 407 try 408 { 409 styledText = styledTextParser.parse(attributes, text); 410 } 411 catch (SAXException e) 412 { 413 } 415 } 416 417 if (styledText == null) 418 { 419 styledText = new JRStyledText(); 420 styledText.append(text); 421 styledText.addRun(new JRStyledText.Run(attributes, 0, text.length())); 422 } 423 } 424 425 return styledText; 426 } 427 428 429 protected JRStyledText getStyledText(JRPrintText textElement) 430 { 431 return getStyledText(textElement, true); 432 } 433 434 435 438 protected void setOutput() 439 { 440 } 441 442 443 451 protected int getOffsetX() 452 { 453 return elementOffsetX; 454 } 455 456 457 465 protected int getOffsetY() 466 { 467 return elementOffsetY; 468 } 469 470 471 483 protected void setFrameElementsOffset(JRPrintFrame frame, boolean relative) 484 { 485 if (relative) 486 { 487 setElementOffsets(0, 0); 488 } 489 else 490 { 491 int topPadding = frame.getTopPadding(); 492 int leftPadding = frame.getLeftPadding(); 493 494 setElementOffsets(getOffsetX() + frame.getX() + leftPadding, getOffsetY() + frame.getY() + topPadding); 495 } 496 } 497 498 499 private void setElementOffsets(int offsetX, int offsetY) 500 { 501 elementOffsetStack.addLast(new int[]{elementOffsetX, elementOffsetY}); 502 503 elementOffsetX = offsetX; 504 elementOffsetY = offsetY; 505 } 506 507 508 512 protected void restoreElementOffsets() 513 { 514 int[] offsets = (int[]) elementOffsetStack.removeLast(); 515 elementOffsetX = offsets[0]; 516 elementOffsetY = offsets[1]; 517 } 518 519 520 protected JRBox getBox(JRPrintGraphicElement element) 521 { 522 byte pen = element.getPen(); 523 Object key = new Byte (pen); 524 JRBox box = (JRBox) penBoxes.get(key); 525 526 if (box == null) 527 { 528 box = new JRBaseBox(pen, element.getForecolor()); 529 530 penBoxes.put(key, box); 531 } 532 533 return box; 534 } 535 536 537 protected String getTextFormatFactoryClass(JRPrintText text) 538 { 539 String formatFactoryClass = text.getFormatFactoryClass(); 540 if (formatFactoryClass == null) 541 { 542 formatFactoryClass = jasperPrint.getFormatFactoryClass(); 543 } 544 return formatFactoryClass; 545 } 546 547 protected Locale getTextLocale(JRPrintText text) 548 { 549 String localeCode = text.getLocaleCode(); 550 if (localeCode == null) 551 { 552 localeCode = jasperPrint.getLocaleCode(); 553 } 554 return localeCode == null ? null : JRDataUtils.getLocale(localeCode); 555 } 556 557 protected TimeZone getTextTimeZone(JRPrintText text) 558 { 559 String tzId = text.getTimeZoneId(); 560 if (tzId == null) 561 { 562 tzId = jasperPrint.getTimeZoneId(); 563 } 564 return tzId == null ? null : JRDataUtils.getTimeZone(tzId); 565 } 566 567 protected TextValue getTextValue(JRPrintText text, String textStr) 568 { 569 TextValue textValue; 570 if (text.getValueClassName() == null) 571 { 572 textValue = getTextValueString(text, textStr); 573 } 574 else 575 { 576 try 577 { 578 Class valueClass = JRClassLoader.loadClassForName(text.getValueClassName()); 579 if (java.lang.Number .class.isAssignableFrom(valueClass)) 580 { 581 textValue = getNumberCellValue(text, textStr); 582 } 583 else if (Date .class.isAssignableFrom(valueClass)) 584 { 585 textValue = getDateCellValue(text, textStr); 586 } 587 else if (Boolean .class.equals(valueClass)) 588 { 589 textValue = getBooleanCellValue(text, textStr); 590 } 591 else 592 { 593 textValue = getTextValueString(text, textStr); 594 } 595 } 596 catch (ParseException e) 597 { 598 textValue = getTextValueString(text, textStr); 600 } 601 catch (ClassNotFoundException e) 602 { 603 textValue = getTextValueString(text, textStr); 605 } 606 } 607 return textValue; 608 } 609 610 protected TextValue getTextValueString(JRPrintText text, String textStr) 611 { 612 return new StringTextValue(textStr); 613 } 614 615 protected TextValue getBooleanCellValue(JRPrintText text, String textStr) 616 { 617 Boolean value = null; 618 if (textStr != null || textStr.length() > 0) 619 { 620 value = Boolean.valueOf(textStr); 621 } 622 return new BooleanTextValue(textStr, value); 623 } 624 625 protected TextValue getDateCellValue(JRPrintText text, String textStr) throws ParseException 626 { 627 TextValue textValue; 628 String pattern = text.getPattern(); 629 if (pattern == null || pattern.trim().length() == 0) 630 { 631 textValue = getTextValueString(text, textStr); 632 } 633 else 634 { 635 DateFormat dateFormat = getDateFormat(getTextFormatFactoryClass(text), pattern, getTextLocale(text), getTextTimeZone(text)); 636 637 Date value = null; 638 if (textStr != null && textStr.length() > 0) 639 { 640 value = dateFormat.parse(textStr); 641 } 642 textValue = new DateTextValue(textStr, value, pattern); 643 } 644 return textValue; 645 } 646 647 protected TextValue getNumberCellValue(JRPrintText text, String textStr) throws ParseException , ClassNotFoundException 648 { 649 TextValue textValue; 650 String pattern = text.getPattern(); 651 if (pattern == null || pattern.trim().length() == 0) 652 { 653 if (textStr != null && textStr.length() > 0) 654 { 655 Number value = defaultParseNumber(textStr, JRClassLoader.loadClassForName(text.getValueClassName())); 656 657 if (value != null) 658 { 659 textValue = new NumberTextValue(textStr, value, null); 660 } 661 else 662 { 663 textValue = getTextValueString(text, textStr); 664 } 665 } 666 else 667 { 668 textValue = new NumberTextValue(textStr, null, null); 669 } 670 } 671 else 672 { 673 NumberFormat numberFormat = getNumberFormat(getTextFormatFactoryClass(text), pattern, getTextLocale(text)); 674 675 Number value = null; 676 if (textStr != null && textStr.length() > 0) 677 { 678 value = numberFormat.parse(textStr); 679 } 680 textValue = new NumberTextValue(textStr, value, pattern); 681 } 682 return textValue; 683 } 684 685 686 protected Number defaultParseNumber(String textStr, Class valueClass) 687 { 688 Number value = null; 689 try 690 { 691 if (valueClass.equals(Byte .class)) 692 { 693 value = Byte.valueOf(textStr); 694 } 695 else if (valueClass.equals(Short .class)) 696 { 697 value = Short.valueOf(textStr); 698 } 699 else if (valueClass.equals(Integer .class)) 700 { 701 value = Integer.valueOf(textStr); 702 } 703 else if (valueClass.equals(Long .class)) 704 { 705 value = Long.valueOf(textStr); 706 } 707 else if (valueClass.equals(Float .class)) 708 { 709 value = Float.valueOf(textStr); 710 } 711 else if (valueClass.equals(Double .class)) 712 { 713 value = Double.valueOf(textStr); 714 } 715 else if (valueClass.equals(BigInteger .class)) 716 { 717 value = new BigInteger (textStr); 718 } 719 else if (valueClass.equals(BigDecimal .class)) 720 { 721 value = new BigDecimal (textStr); 722 } 723 } 724 catch (NumberFormatException e) 725 { 726 } 728 return value; 729 } 730 731 732 protected DateFormat getDateFormat(String formatFactoryClass, String pattern, Locale lc, TimeZone tz) 733 { 734 String key = formatFactoryClass 735 + "|" + pattern 736 + "|" + JRDataUtils.getLocaleCode(lc) 737 + "|" + JRDataUtils.getTimeZoneId(tz); 738 DateFormat dateFormat = (DateFormat )dateFormatCache.get(key); 739 if (dateFormat == null) 740 { 741 FormatFactory formatFactory = DefaultFormatFactory.createFormatFactory(formatFactoryClass); dateFormat = formatFactory.createDateFormat(pattern, lc, tz); 743 dateFormatCache.put(key, dateFormat); 744 } 745 return dateFormat; 746 } 747 748 749 protected NumberFormat getNumberFormat(String formatFactoryClass, String pattern, Locale lc) 750 { 751 String key = formatFactoryClass 752 + "|" + pattern 753 + "|" + JRDataUtils.getLocaleCode(lc); 754 NumberFormat numberFormat = (NumberFormat )numberFormatCache.get(key); 755 if (numberFormat == null) 756 { 757 FormatFactory formatFactory = DefaultFormatFactory.createFormatFactory(formatFactoryClass); numberFormat = formatFactory.createNumberFormat(pattern, lc); 759 dateFormatCache.put(key, numberFormat); 760 } 761 return numberFormat; 762 } 763 764 } 765 | Popular Tags |