| 1 28 29 33 34 package net.sf.jasperreports.engine.fill; 35 36 import java.net.URLStreamHandlerFactory ; 37 import java.sql.Connection ; 38 import java.text.Format ; 39 import java.util.ArrayList ; 40 import java.util.HashMap ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.ListIterator ; 45 import java.util.Locale ; 46 import java.util.Map ; 47 import java.util.Set ; 48 import java.util.TimeZone ; 49 50 import net.sf.jasperreports.engine.JRAbstractScriptlet; 51 import net.sf.jasperreports.engine.JRBand; 52 import net.sf.jasperreports.engine.JRConstants; 53 import net.sf.jasperreports.engine.JRDataSource; 54 import net.sf.jasperreports.engine.JRDataset; 55 import net.sf.jasperreports.engine.JRDefaultStyleProvider; 56 import net.sf.jasperreports.engine.JRException; 57 import net.sf.jasperreports.engine.JRExpression; 58 import net.sf.jasperreports.engine.JRGroup; 59 import net.sf.jasperreports.engine.JRParameter; 60 import net.sf.jasperreports.engine.JRPrintElement; 61 import net.sf.jasperreports.engine.JRPrintPage; 62 import net.sf.jasperreports.engine.JRReport; 63 import net.sf.jasperreports.engine.JRReportFont; 64 import net.sf.jasperreports.engine.JRRuntimeException; 65 import net.sf.jasperreports.engine.JRStyle; 66 import net.sf.jasperreports.engine.JRVirtualizer; 67 import net.sf.jasperreports.engine.JasperPrint; 68 import net.sf.jasperreports.engine.JasperReport; 69 import net.sf.jasperreports.engine.base.JRBasePrintPage; 70 import net.sf.jasperreports.engine.base.JRVirtualPrintPage; 71 import net.sf.jasperreports.engine.util.DefaultFormatFactory; 72 import net.sf.jasperreports.engine.util.FormatFactory; 73 import net.sf.jasperreports.engine.util.JRDataUtils; 74 import net.sf.jasperreports.engine.util.JRGraphEnvInitializer; 75 import net.sf.jasperreports.engine.util.JRResourcesUtil; 76 import net.sf.jasperreports.engine.util.JRStyledTextParser; 77 78 79 83 public abstract class JRBaseFiller implements JRDefaultStyleProvider, JRVirtualPrintPage.IdentityDataProvider{ 85 86 94 public class BoundElementMap 95 { 96 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 97 98 private final Map map; 99 100 BoundElementMap() 101 { 102 map = new HashMap (); 103 } 104 105 108 public Object put(Object key, Object value, JRPrintPage keyPage) 109 { 110 Map pageMap = (Map ) map.get(keyPage); 111 if (pageMap == null) 112 { 113 pageMap = new HashMap (); 114 map.put(keyPage, pageMap); 115 } 116 117 return pageMap.put(key, value); 118 } 119 120 124 public Object put(Object key, Object value) 125 { 126 if (isPerPageBoundElements) 127 { 128 return put(key, value, fillContext.getPrintPage()); 129 } 130 131 return map.put(key, value); 132 } 133 134 public void clear() 135 { 136 map.clear(); 137 } 138 139 public Map getMap() 140 { 141 return map; 142 } 143 144 public Map getMap(JRPrintPage page) 145 { 146 return (Map ) map.get(page); 147 } 148 149 public Map putMap(JRPrintPage page, Map valueMap) 150 { 151 return (Map ) map.put(page, valueMap); 152 } 153 } 154 155 158 protected JRBaseFiller parentFiller = null; 159 160 private JRStyledTextParser styledTextParser = new JRStyledTextParser(); 161 162 165 private boolean isInterrupted = false; 166 167 170 protected String name = null; 171 172 protected int columnCount = 1; 173 174 protected byte printOrder = JRReport.PRINT_ORDER_VERTICAL; 175 176 protected int pageWidth = 0; 177 178 protected int pageHeight = 0; 179 180 protected byte orientation = JRReport.ORIENTATION_PORTRAIT; 181 182 protected byte whenNoDataType = JRReport.WHEN_NO_DATA_TYPE_NO_PAGES; 183 184 protected int columnWidth = 0; 185 186 protected int columnSpacing = 0; 187 188 protected int leftMargin = 0; 189 190 protected int rightMargin = 0; 191 192 protected int topMargin = 0; 193 194 protected int bottomMargin = 0; 195 196 protected boolean isTitleNewPage = false; 197 198 protected boolean isSummaryNewPage = false; 199 200 protected boolean isFloatColumnFooter = false; 201 202 205 protected byte whenResourceMissingType = JRReport.WHEN_RESOURCE_MISSING_TYPE_NULL; 206 207 protected JRReportFont defaultFont = null; 208 209 protected JRReportFont[] fonts = null; 210 211 protected JRStyle defaultStyle = null; 212 213 protected JRStyle[] styles = null; 214 215 218 protected JRFillDataset mainDataset; 219 220 protected JRFillGroup[] groups = null; 221 222 protected JRFillBand missingFillBand = null; 223 224 protected JRFillBand background = null; 225 226 protected JRFillBand title = null; 227 228 protected JRFillBand pageHeader = null; 229 230 protected JRFillBand columnHeader = null; 231 232 protected JRFillBand detail = null; 233 234 protected JRFillBand columnFooter = null; 235 236 protected JRFillBand pageFooter = null; 237 238 protected JRFillBand lastPageFooter = null; 239 240 protected JRFillBand summary = null; 241 242 protected JRVirtualizer virtualizer = null; 243 244 protected ClassLoader reportClassLoader = null; 245 246 protected FormatFactory formatFactory = null; 247 248 protected URLStreamHandlerFactory urlHandlerFactory; 249 250 protected Map loadedSubreports = null; 251 252 protected JRFillContext fillContext; 253 254 257 protected Map boundElements; 258 259 262 protected JasperPrint jasperPrint = null; 263 264 protected JRPrintPage printPage = null; 265 266 protected int printPageStretchHeight = 0; 267 268 272 protected List bands; 273 274 277 protected Set subfillers; 278 279 private List identityPages; 280 281 private Thread fillingThread; 282 283 protected JRCalculator calculator; 284 285 protected JRAbstractScriptlet scriptlet; 286 287 290 protected Map datasetMap; 291 292 295 protected JasperReport jasperReport; 296 297 private boolean bandOverFlowAllowed; 298 299 protected boolean isPerPageBoundElements; 300 301 304 protected Map dateFormatCache = new HashMap (); 305 protected Map numberFormatCache = new HashMap (); 306 307 private JRSubreportRunner subreportRunner; 308 309 310 313 protected boolean isCreatingNewPage = false; 314 protected boolean isNewPage = false; 315 protected boolean isNewColumn = false; 316 protected boolean isNewGroup = true; 317 318 protected int columnIndex = 0; 319 320 protected int offsetX = 0; 321 protected int offsetY = 0; 322 protected int columnHeaderOffsetY = 0; 323 protected int columnFooterOffsetY = 0; 324 protected int lastPageColumnFooterOffsetY = 0; 325 326 protected boolean isLastPageFooter = false; 327 328 329 332 protected JRBaseFiller(JasperReport jasperReport, JREvaluator initEvaluator, JRBaseFiller parentFiller) throws JRException 333 { 334 JRGraphEnvInitializer.initializeGraphEnv(); 335 336 this.jasperReport = jasperReport; 337 338 339 this.parentFiller = parentFiller; 340 341 if (parentFiller == null) 342 { 343 fillContext = new JRFillContext(); 344 } 345 else 346 { 347 fillContext = parentFiller.fillContext; 348 } 349 350 351 name = jasperReport.getName(); 352 columnCount = jasperReport.getColumnCount(); 353 printOrder = jasperReport.getPrintOrder(); 354 pageWidth = jasperReport.getPageWidth(); 355 pageHeight = jasperReport.getPageHeight(); 356 orientation = jasperReport.getOrientation(); 357 whenNoDataType = jasperReport.getWhenNoDataType(); 358 columnWidth = jasperReport.getColumnWidth(); 359 columnSpacing = jasperReport.getColumnSpacing(); 360 leftMargin = jasperReport.getLeftMargin(); 361 rightMargin = jasperReport.getRightMargin(); 362 topMargin = jasperReport.getTopMargin(); 363 bottomMargin = jasperReport.getBottomMargin(); 364 isTitleNewPage = jasperReport.isTitleNewPage(); 365 isSummaryNewPage = jasperReport.isSummaryNewPage(); 366 isFloatColumnFooter = jasperReport.isFloatColumnFooter(); 367 whenResourceMissingType = jasperReport.getWhenResourceMissingType(); 368 369 jasperPrint = new JasperPrint(); 370 371 if (initEvaluator == null) 372 { 373 calculator = JRFillDataset.createCalculator(jasperReport, jasperReport.getMainDataset()); 374 } 375 else 376 { 377 calculator = new JRCalculator(initEvaluator); 378 } 379 380 381 JRFillObjectFactory factory = new JRFillObjectFactory(this); 382 383 384 defaultFont = factory.getReportFont(jasperReport.getDefaultFont()); 385 386 387 JRReportFont[] jrFonts = jasperReport.getFonts(); 388 if (jrFonts != null && jrFonts.length > 0) 389 { 390 fonts = new JRReportFont[jrFonts.length]; 391 for (int i = 0; i < fonts.length; i++) 392 { 393 fonts[i] = factory.getReportFont(jrFonts[i]); 394 } 395 } 396 397 createDatasets(factory); 398 mainDataset = factory.getDataset(jasperReport.getMainDataset()); 399 groups = mainDataset.groups; 400 401 402 defaultStyle = factory.getStyle(jasperReport.getDefaultStyle()); 403 404 405 JRStyle[] jrStyles = jasperReport.getStyles(); 406 if (jrStyles != null && jrStyles.length > 0) 407 { 408 styles = new JRStyle[jrStyles.length]; 409 for (int i = 0; i < styles.length; i++) 410 { 411 styles[i] = factory.getStyle(jrStyles[i]); 412 } 413 } 414 415 416 missingFillBand = factory.getBand(null); 417 background = factory.getBand(jasperReport.getBackground()); 418 title = factory.getBand(jasperReport.getTitle()); 419 pageHeader = factory.getBand(jasperReport.getPageHeader()); 420 columnHeader = factory.getBand(jasperReport.getColumnHeader()); 421 detail = factory.getBand(jasperReport.getDetail()); 422 columnFooter = factory.getBand(jasperReport.getColumnFooter()); 423 pageFooter = factory.getBand(jasperReport.getPageFooter()); 424 lastPageFooter = factory.getBand(jasperReport.getLastPageFooter()); 425 if (isEmpty(jasperReport.getSummary())) 426 { 427 summary = missingFillBand; 428 } 429 else 430 { 431 summary = factory.getBand(jasperReport.getSummary()); 432 } 433 434 mainDataset.initElementDatasets(factory); 435 initDatasets(factory); 436 437 mainDataset.checkVariableCalculationReqs(factory); 438 439 440 mainDataset.setCalculator(calculator); 441 mainDataset.initCalculator(); 442 443 initBands(); 444 } 445 446 447 452 protected Map getParametersMap() 453 { 454 return mainDataset.parametersMap; 455 } 456 457 458 463 protected Map getFieldsMap() 464 { 465 return mainDataset.fieldsMap; 466 } 467 468 469 474 protected Map getVariablesMap() 475 { 476 return mainDataset.variablesMap; 477 } 478 479 480 486 protected JRFillVariable getVariable(String variableName) 487 { 488 return (JRFillVariable) mainDataset.variablesMap.get(variableName); 489 } 490 491 492 498 protected JRFillField getField(String fieldName) 499 { 500 return (JRFillField) mainDataset.fieldsMap.get(fieldName); 501 } 502 503 private void initBands() 504 { 505 bands = new ArrayList (8 + (groups == null ? 0 : (2 * groups.length))); 506 bands.add(title); 507 bands.add(summary); 508 bands.add(pageHeader); 509 bands.add(pageFooter); 510 bands.add(lastPageFooter); 511 bands.add(columnHeader); 512 bands.add(columnFooter); 513 bands.add(detail); 514 515 if (groups != null && groups.length > 0) 516 { 517 for (int i = 0; i < groups.length; i++) 518 { 519 JRFillGroup group = groups[i]; 520 bands.add(group.getGroupHeader()); 521 bands.add(group.getGroupFooter()); 522 } 523 } 524 525 initBandsNowEvaluationTimes(); 526 } 527 528 529 private void initBandsNowEvaluationTimes() 530 { 531 JREvaluationTime[] groupEvaluationTimes; 532 if (groups == null) 533 { 534 groupEvaluationTimes = new JREvaluationTime[0]; 535 } 536 else 537 { 538 groupEvaluationTimes = new JREvaluationTime[groups.length]; 539 for (int i = 0; i < groups.length; i++) 540 { 541 groupEvaluationTimes[i] = JREvaluationTime.getGroupEvaluationTime(groups[i].getName()); 542 } 543 544 for (int i = 0; i < groups.length; i++) 545 { 546 JRGroup group = groups[i]; 547 JRFillBand footer = (JRFillBand) group.getGroupFooter(); 548 549 for (int j = i; j < groupEvaluationTimes.length; ++j) 550 { 551 footer.addNowEvaluationTime(groupEvaluationTimes[j]); 552 } 553 } 554 } 555 556 columnFooter.addNowEvaluationTime(JREvaluationTime.EVALUATION_TIME_COLUMN); 557 558 pageFooter.addNowEvaluationTime(JREvaluationTime.EVALUATION_TIME_COLUMN); 559 pageFooter.addNowEvaluationTime(JREvaluationTime.EVALUATION_TIME_PAGE); 560 561 summary.addNowEvaluationTimes(groupEvaluationTimes); 562 } 563 564 565 568 public JRStyledTextParser getStyledTextParser() 569 { 570 return styledTextParser; 571 } 572 573 576 public JasperPrint getJasperPrint() 577 { 578 return jasperPrint; 579 } 580 581 584 public JRReportFont getDefaultFont() 585 { 586 return defaultFont; 587 } 588 589 592 public JRStyle getDefaultStyle() 593 { 594 return defaultStyle; 595 } 596 597 600 protected boolean isSubreport() 601 { 602 return (parentFiller != null); 603 } 604 605 608 protected boolean isInterrupted() 609 { 610 return (isInterrupted || (parentFiller != null && parentFiller.isInterrupted())); 611 } 612 613 616 protected void setInterrupted(boolean isInterrupted) 617 { 618 this.isInterrupted = isInterrupted; 619 } 620 621 624 protected JRPrintPage getCurrentPage() 625 { 626 return printPage; 627 } 628 629 632 protected JRReportFont[] getFonts() 633 { 634 return fonts; 635 } 636 637 640 protected int getCurrentPageStretchHeight() 641 { 642 return printPageStretchHeight; 643 } 644 645 648 protected abstract void setPageHeight(int pageHeight); 649 650 651 public JasperPrint fill(Map parameterValues, Connection conn) throws JRException 652 { 653 if (parameterValues == null) 654 { 655 parameterValues = new HashMap (); 656 } 657 658 setConnectionParameterValue(parameterValues, conn); 659 660 return fill(parameterValues); 661 } 662 663 664 protected void setConnectionParameterValue(Map parameterValues, Connection conn) 665 { 666 mainDataset.setConnectionParameterValue(parameterValues, conn); 667 } 668 669 670 public JasperPrint fill(Map parameterValues, JRDataSource ds) throws JRException 671 { 672 if (parameterValues == null) 673 { 674 parameterValues = new HashMap (); 675 } 676 677 setDatasourceParameterValue(parameterValues, ds); 678 679 return fill(parameterValues); 680 } 681 682 683 protected void setDatasourceParameterValue(Map parameterValues, JRDataSource ds) 684 { 685 mainDataset.setDatasourceParameterValue(parameterValues, ds); 686 } 687 688 689 public JasperPrint fill(Map parameterValues) throws JRException 690 { 691 if (parameterValues == null) 692 { 693 parameterValues = new HashMap (); 694 } 695 696 fillingThread = Thread.currentThread(); 697 boolean urlHandlerFactorySet = false; 698 boolean classLoaderSet = false; 699 try 700 { 701 702 setParameters(parameterValues); 703 704 classLoaderSet = setClassLoader(parameterValues); 705 urlHandlerFactorySet = setUrlHandlerFactory(parameterValues); 706 707 if (parentFiller != null) 708 { 709 parentFiller.registerSubfiller(this); 710 } 711 712 jasperPrint.setName(name); 713 jasperPrint.setPageWidth(pageWidth); 714 jasperPrint.setPageHeight(pageHeight); 715 jasperPrint.setOrientation(orientation); 716 717 jasperPrint.setDefaultFont(defaultFont); 718 719 jasperPrint.setFormatFactoryClass(jasperReport.getFormatFactoryClass()); 720 jasperPrint.setLocaleCode(JRDataUtils.getLocaleCode(getLocale())); 721 jasperPrint.setTimeZoneId(JRDataUtils.getTimeZoneId(getTimeZone())); 722 723 724 if (fonts != null && fonts.length > 0) 725 { 726 for (int i = 0; i < fonts.length; i++) 727 { 728 jasperPrint.addFont(fonts[i], true); 729 } 730 } 731 732 jasperPrint.setDefaultStyle(defaultStyle); 733 734 735 if (styles != null && styles.length > 0) 736 { 737 for (int i = 0; i < styles.length; i++) 738 { 739 jasperPrint.addStyle(styles[i], true); 740 } 741 } 742 743 loadedSubreports = new HashMap (); 744 745 createBoundElemementMaps(); 746 747 748 mainDataset.start(); 749 750 751 fillReport(); 752 753 759 return jasperPrint; 760 } 761 finally 762 { 763 mainDataset.closeDatasource(); 764 765 if (parentFiller != null) 766 { 767 parentFiller.unregisterSubfiller(this); 768 } 769 770 fillingThread = null; 771 772 killSubfillerThreads(); 774 775 if (classLoaderSet) 776 { 777 JRResourcesUtil.resetClassLoader(); 778 } 779 780 if (urlHandlerFactorySet) 781 { 782 JRResourcesUtil.resetThreadURLHandlerFactory(); 783 } 784 } 785 } 786 787 788 private void createBoundElemementMaps() 789 { 790 boundElements = new HashMap (); 791 792 createBoundElementMaps(JREvaluationTime.EVALUATION_TIME_REPORT); 793 createBoundElementMaps(JREvaluationTime.EVALUATION_TIME_PAGE); 794 createBoundElementMaps(JREvaluationTime.EVALUATION_TIME_COLUMN); 795 796 if (groups != null) 797 { 798 for (int i = 0; i < groups.length; i++) 799 { 800 createBoundElementMaps(JREvaluationTime.getGroupEvaluationTime(groups[i].getName())); 801 } 802 } 803 804 for (Iterator i = bands.iterator(); i.hasNext();) 805 { 806 JRFillBand band = (JRFillBand) i.next(); 807 createBoundElementMaps(JREvaluationTime.getBandEvaluationTime(band)); 808 } 809 } 810 811 812 private void createBoundElementMaps(JREvaluationTime evaluationTime) 813 { 814 BoundElementMap boundElementsMap = new BoundElementMap(); 815 boundElements.put(evaluationTime, boundElementsMap); 816 } 817 818 819 private void killSubfillerThreads() 820 { 821 if (subfillers != null && !subfillers.isEmpty()) 822 { 823 for (Iterator it = subfillers.iterator(); it.hasNext();) 824 { 825 JRBaseFiller subfiller = (JRBaseFiller) it.next(); 826 if (subfiller.fillingThread != null) 827 { 828 subfiller.fillingThread.interrupt(); 829 } 830 } 831 } 832 } 833 834 835 838 protected abstract void fillReport() throws JRException; 839 840 843 protected void setParameters(Map parameterValues) throws JRException 844 { 845 if (!isSubreport()) 846 { 847 848 virtualizer = (JRVirtualizer) parameterValues.get(JRParameter.REPORT_VIRTUALIZER); 849 850 if (virtualizer != null) 851 { 852 fillContext.setUsingVirtualizer(true); 853 fillContext.setPerPageBoundElements(true); 854 JRVirtualizationContext.register(fillContext.getVirtualizationContext(), jasperPrint); 855 } 856 } 857 858 isPerPageBoundElements = fillContext.isPerPageBoundElements(); 859 860 setFormatFactory(parameterValues); 861 862 setIgnorePagination(parameterValues); 863 864 mainDataset.setParameterValues(parameterValues); 865 866 this.scriptlet = mainDataset.scriptlet; 867 868 if (!isSubreport()) 869 { 870 fillContext.setMasterFormatFactory(getFormatFactory()); 871 fillContext.setMasterLocale(getLocale()); 872 fillContext.setMasterTimeZone(getTimeZone()); 873 } 874 } 875 876 877 private void setFormatFactory(Map parameterValues) 878 { 879 formatFactory = (FormatFactory)parameterValues.get(JRParameter.REPORT_FORMAT_FACTORY); 880 if (formatFactory == null) 881 { 882 formatFactory = DefaultFormatFactory.createFormatFactory(jasperReport.getFormatFactoryClass()); 883 parameterValues.put(JRParameter.REPORT_FORMAT_FACTORY, formatFactory); 884 } 885 } 886 887 888 private boolean setClassLoader(Map parameterValues) 889 { 890 reportClassLoader = (ClassLoader ) parameterValues.get(JRParameter.REPORT_CLASS_LOADER); 891 boolean setClassLoader = reportClassLoader != null; 892 if (setClassLoader) 893 { 894 JRResourcesUtil.setThreadClassLoader(reportClassLoader); 895 } 896 return setClassLoader; 897 } 898 899 900 private boolean setUrlHandlerFactory(Map parameterValues) 901 { 902 urlHandlerFactory = (URLStreamHandlerFactory ) parameterValues.get(JRParameter.REPORT_URL_HANDLER_FACTORY); 903 boolean setUrlHandlerFactory = urlHandlerFactory != null; 904 if (setUrlHandlerFactory) 905 { 906 JRResourcesUtil.setThreadURLHandlerFactory(urlHandlerFactory); 907 } 908 return setUrlHandlerFactory; 909 } 910 911 912 private void setIgnorePagination(Map parameterValues) 913 { 914 if (parentFiller == null) { 916 Boolean isIgnorePaginationParam = (Boolean ) parameterValues.get(JRParameter.IS_IGNORE_PAGINATION); 917 if (isIgnorePaginationParam != null) 918 { 919 fillContext.setIgnorePagination(isIgnorePaginationParam.booleanValue()); 920 } 921 else 922 { 923 boolean ignorePagination = jasperReport.isIgnorePagination(); 924 fillContext.setIgnorePagination(ignorePagination); 925 parameterValues.put(JRParameter.IS_IGNORE_PAGINATION, ignorePagination ? Boolean.TRUE : Boolean.FALSE); 926 } 927 } 928 else 929 {
|