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 { 930 boolean ignorePagination = fillContext.isIgnorePagination(); 931 parameterValues.put(JRParameter.IS_IGNORE_PAGINATION, ignorePagination ? Boolean.TRUE : Boolean.FALSE); 932 } 933 934 if (fillContext.isIgnorePagination()) 935 { 936 isTitleNewPage = false; 937 isSummaryNewPage = false; 938 if (groups != null) 939 { 940 for (int i = 0; i < groups.length; i++) 941 { 942 groups[i].setStartNewPage(false); 943 groups[i].setResetPageNumber(false); 944 groups[i].setStartNewColumn(false); 945 } 946 } 947 setPageHeight(Integer.MAX_VALUE); 948 } 949 } 950 951 952 957 protected Locale getLocale() 958 { 959 return mainDataset.locale; 960 } 961 962 963 968 protected TimeZone getTimeZone() 969 { 970 return mainDataset.timeZone; 971 } 972 973 974 979 protected FormatFactory getFormatFactory() 980 { 981 return formatFactory; 982 } 983 984 985 988 protected Format getDateFormat(String pattern) 989 { 990 Locale lc = getLocale(); 991 TimeZone tz = getTimeZone(); 992 String key = pattern + "|" + JRDataUtils.getLocaleCode(lc) + "|" + JRDataUtils.getTimeZoneId(tz); 993 Format format = (Format )dateFormatCache.get(key); 994 if (format == null) 995 { 996 format = getFormatFactory().createDateFormat(pattern, lc, tz); 997 if (format != null) 998 { 999 dateFormatCache.put(key, format); 1000 } 1001 } 1002 return format; 1003 } 1004 1005 1006 1009 protected Format getNumberFormat(String pattern) 1010 { 1011 Locale lc = getLocale(); 1012 String key = pattern + "|" + JRDataUtils.getLocaleCode(lc); 1013 Format format = (Format )numberFormatCache.get(key); 1014 if (format == null) 1015 { 1016 format = getFormatFactory().createNumberFormat(pattern, lc); 1017 if (format != null) 1018 { 1019 numberFormatCache.put(key, format); 1020 } 1021 } 1022 return format; 1023 } 1024 1025 1026 protected boolean hasMasterFormatFactory() 1027 { 1028 return 1029 !isSubreport() 1030 || getFormatFactory().getClass().getName().equals( 1031 fillContext.getMasterFormatFactory().getClass().getName() 1032 ); 1033 } 1034 1035 1036 protected boolean hasMasterLocale() 1037 { 1038 return !isSubreport() || getLocale().equals(fillContext.getMasterLocale()); 1039 } 1040 1041 1042 protected boolean hasMasterTimeZone() 1043 { 1044 return !isSubreport() || getTimeZone().equals(fillContext.getMasterTimeZone()); 1045 } 1046 1047 1048 1055 protected void setParameter(String parameterName, Object value) throws JRException 1056 { 1057 mainDataset.setParameter(parameterName, value); 1058 } 1059 1060 1061 1068 protected void setParameter(JRFillParameter parameter, Object value) throws JRException 1069 { 1070 mainDataset.setParameter(parameter, value); 1071 } 1072 1073 1076 protected boolean next() throws JRException 1077 { 1078 return mainDataset.next(); 1079 } 1080 1081 private void resolveBoundElements(Map boundElementsMap, byte evaluation, JREvaluationTime evaluationTime) throws JRException 1082 { 1083 if (boundElementsMap != null) 1084 { 1085 for (Iterator it = boundElementsMap.entrySet().iterator(); it.hasNext();) 1086 { 1087 Map.Entry entry = (Map.Entry ) it.next(); 1088 JRPrintElement element = (JRPrintElement) entry.getKey(); 1089 JRFillElement fillElement = (JRFillElement) entry.getValue(); 1090 1091 fillElement.resolveElement(element, evaluation, evaluationTime); 1092 } 1093 } 1094 } 1095 1096 protected void resolveBoundElements(JREvaluationTime evaluationTime, byte evaluation) throws JRException 1097 { 1098 BoundElementMap boundElementsMap = (BoundElementMap) boundElements.get(evaluationTime); 1099 if (isPerPageBoundElements) 1100 { 1101 Map perPageElementsMap = boundElementsMap.getMap(); 1102 for (Iterator it = perPageElementsMap.entrySet().iterator(); it.hasNext();) 1103 { 1104 Map.Entry entry = (Map.Entry ) it.next(); 1105 JRPrintPage page = (JRPrintPage) entry.getKey(); 1107 page.getElements(); 1108 Map elementsMap = (Map ) entry.getValue(); 1109 resolveBoundElements(elementsMap, evaluation, evaluationTime); 1110 } 1111 1112 boundElementsMap.clear(); 1113 } 1114 else 1115 { 1116 resolveBoundElements(boundElementsMap.getMap(), evaluation, evaluationTime); 1117 boundElementsMap.clear(); 1118 } 1119 } 1120 1121 1124 protected void resolveReportBoundElements() throws JRException 1125 { 1126 resolveBoundElements(JREvaluationTime.EVALUATION_TIME_REPORT, JRExpression.EVALUATION_DEFAULT); 1127 } 1128 1129 1135 protected void resolvePageBoundElements(byte evaluation) throws JRException 1136 { 1137 resolveBoundElements(JREvaluationTime.EVALUATION_TIME_PAGE, evaluation); 1138 } 1139 1140 1146 protected void resolveColumnBoundElements(byte evaluation) throws JRException 1147 { 1148 resolveBoundElements(JREvaluationTime.EVALUATION_TIME_COLUMN, evaluation); 1149 } 1150 1151 1158 protected void resolveGroupBoundElements(byte evaluation, boolean isFinal) throws JRException 1159 { 1160 if (groups != null && groups.length > 0) 1161 { 1162 for (int i = 0; i < groups.length; i++) 1163 { 1164 JRFillGroup group = groups[i]; 1165 1166 if ((group.hasChanged() && group.isFooterPrinted()) || isFinal) 1167 { 1168 String groupName = group.getName(); 1169 1170 resolveBoundElements(JREvaluationTime.getGroupEvaluationTime(groupName), evaluation); 1171 } 1172 } 1173 } 1174 } 1175 1176 protected JRPrintPage newPage() 1177 { 1178 JRPrintPage page; 1179 1180 if (virtualizer != null) 1181 { 1182 JRVirtualPrintPage virtualPage = new JRVirtualPrintPage(jasperPrint, virtualizer, fillContext.getVirtualizationContext()); 1183 1184 addIdentityDataProviders(virtualPage, this); 1185 1186 page = virtualPage; 1187 } 1188 else 1189 { 1190 page = new JRBasePrintPage(); 1191 } 1192 1193 return page; 1194 } 1195 1196 1203 protected Object getVariableValue(String variableName) 1204 { 1205 return mainDataset.getVariableValue(variableName); 1206 } 1207 1208 1217 protected void resolveBandBoundElements(JRFillBand band, byte evaluation) throws JRException 1218 { 1219 resolveBoundElements(JREvaluationTime.getBandEvaluationTime(band), evaluation); 1220 } 1221 1222 1223 1231 protected void addVariableCalculationReq(String variableName, byte calculation) 1232 { 1233 mainDataset.addVariableCalculationReq(variableName, calculation); 1234 } 1235 1236 1237 1242 public void cancelFill() throws JRException 1243 { 1244 if (!fillContext.cancelRunningQuery()) 1245 { 1246 Thread t = fillingThread; 1247 if (t != null) 1248 { 1249 t.interrupt(); 1250 } 1251 } 1252 } 1253 1254 1255 protected void registerSubfiller(JRBaseFiller subfiller) 1256 { 1257 if (subfillers == null) 1258 { 1259 subfillers = new HashSet (); 1260 } 1261 1262 if (subfillers.add(subfiller) && fillContext.isUsingVirtualizer()) 1263 { 1264 subfiller.identityPages = new ArrayList (); 1265 1266 JRVirtualPrintPage masterPrintPage = (JRVirtualPrintPage) fillContext.getPrintPage(); 1267 subfiller.identityPages.add(masterPrintPage); 1268 addIdentityDataProviders(masterPrintPage, subfiller); 1269 } 1270 } 1271 1272 protected void unregisterSubfiller(JRBaseFiller subfiller) 1273 { 1274 if (subfillers != null && subfillers.remove(subfiller) && fillContext.isUsingVirtualizer()) 1275 { 1276 removeIdentityDataProviders(subfiller); 1277 } 1278 } 1279 1280 private static void addIdentityDataProviders(JRVirtualPrintPage page, JRBaseFiller filler) 1281 { 1282 page.addIdentityDataProvider(filler); 1283 1284 if (filler.subfillers != null) 1285 { 1286 for (Iterator i = filler.subfillers.iterator(); i.hasNext();) 1287 { 1288 JRBaseFiller subfiller = (JRBaseFiller) i.next(); 1289 1290 subfiller.identityPages.add(page); 1291 addIdentityDataProviders(page, subfiller); 1292 } 1293 } 1294 } 1295 1296 private void removeIdentityDataProviders(JRBaseFiller filler) 1297 { 1298 if (filler.identityPages != null) 1299 { 1300 for (Iterator it = filler.identityPages.iterator(); it.hasNext();) 1301 { 1302 JRVirtualPrintPage page = (JRVirtualPrintPage) it.next(); 1303 1304 page.removeIdentityDataProvider(filler); 1305 } 1306 1307 filler.identityPages = null; 1308 } 1309 } 1310 1311 1312 protected void addPage(JRPrintPage page) 1313 { 1314 if (!isSubreport()) 1315 { 1316 jasperPrint.addPage(page); 1317 fillContext.setPrintPage(page); 1318 } 1319 } 1320 1321 1322 protected static final class PageIdentityDataProvider implements JRVirtualPrintPage.IdentityDataProvider 1323 { 1324 private static final Map providers = new HashMap (); 1325 1326 private final JRPrintPage printPage; 1327 1328 protected PageIdentityDataProvider(JRPrintPage printPage) 1329 { 1330 this.printPage = printPage; 1331 } 1332 1333 public JRVirtualPrintPage.ObjectIDPair[] getIdentityData(JRVirtualPrintPage page) 1334 { 1335 return null; 1336 } 1337 1338 public void setIdentityData(JRVirtualPrintPage page, JRVirtualPrintPage.ObjectIDPair[] identityData) 1339 { 1340 if (identityData != null && identityData.length > 0) 1341 { 1342 Map idMap = new HashMap (); 1343 for (int i = 0; i < identityData.length; i++) 1344 { 1345 idMap.put(new Integer (identityData[i].getIdentity()), identityData[i].getObject()); 1346 } 1347 1348 for (ListIterator i = printPage.getElements().listIterator(); i.hasNext();) 1349 { 1350 Object element = i.next(); 1351 Integer id = new Integer (System.identityHashCode(element)); 1352 1353 Object idObject = idMap.get(id); 1354 if (idObject != null) 1355 { 1356 i.set(idObject); 1357 } 1358 } 1359 } 1360 } 1361 1362 public static JRVirtualPrintPage.IdentityDataProvider getIdentityDataProvider(JRPrintPage printPage) 1363 { 1364 JRVirtualPrintPage.IdentityDataProvider provider = (JRVirtualPrintPage.IdentityDataProvider) providers.get(printPage); 1365 if (provider == null) 1366 { 1367 provider = new PageIdentityDataProvider(printPage); 1368 providers.put(printPage, provider); 1369 } 1370 return provider; 1371 } 1372 1373 public static JRVirtualPrintPage.IdentityDataProvider removeIdentityDataProvider(JRPrintPage printPage) 1374 { 1375 JRVirtualPrintPage.IdentityDataProvider provider = (JRVirtualPrintPage.IdentityDataProvider) providers.remove(printPage); 1376 return provider; 1377 } 1378 } 1379 1380 1381 protected void addPageIdentityDataProvider() 1382 { 1383 JRVirtualPrintPage.IdentityDataProvider pageProvider = PageIdentityDataProvider.getIdentityDataProvider(printPage); 1384 JRVirtualPrintPage masterPage = (JRVirtualPrintPage) fillContext.getPrintPage(); 1385 masterPage.addIdentityDataProvider(pageProvider); 1386 } 1387 1388 1389 protected void removePageIdentityDataProvider() 1390 { 1391 JRVirtualPrintPage.IdentityDataProvider pageProvider = PageIdentityDataProvider.removeIdentityDataProvider(printPage); 1392 if (pageProvider != null) 1393 { 1394 ((JRVirtualPrintPage) fillContext.getPrintPage()).removeIdentityDataProvider(pageProvider); 1395 } 1396 } 1397 1398 1399 1406 protected Object evaluateExpression(JRExpression expression, byte evaluation) throws JRException 1407 { 1408 return mainDataset.calculator.evaluate(expression, evaluation); 1409 } 1410 1411 1412 private void createDatasets(JRFillObjectFactory factory) throws JRException 1413 { 1414 datasetMap = new HashMap (); 1415 1416 JRDataset[] datasets = jasperReport.getDatasets(); 1417 if (datasets != null && datasets.length > 0) 1418 { 1419 for (int i = 0; i < datasets.length; i++) 1420 { 1421 JRFillDataset fillDataset = factory.getDataset(datasets[i]); 1422 fillDataset.createCalculator(jasperReport); 1423 1424 datasetMap.put(datasets[i].getName(), fillDataset); 1425 } 1426 } 1427 } 1428 1429 1430 private void initDatasets(JRFillObjectFactory factory) 1431 { 1432 for (Iterator it = datasetMap.values().iterator(); it.hasNext();) 1433 { 1434 JRFillDataset dataset = (JRFillDataset) it.next(); 1435 dataset.inheritFromMain(); 1436 dataset.initElementDatasets(factory); 1437 } 1438 } 1439 1440 1441 protected byte getWhenResourceMissingType() 1442 { 1443 return mainDataset.whenResourceMissingType; 1444 } 1445 1446 1447 1452 protected JasperReport getJasperReport() 1453 { 1454 return jasperReport; 1455 } 1456 1457 1458 protected boolean isBandOverFlowAllowed() 1459 { 1460 return bandOverFlowAllowed; 1461 } 1462 1463 1464 protected void setBandOverFlowAllowed(boolean splittableBand) 1465 { 1466 this.bandOverFlowAllowed = splittableBand; 1467 } 1468 1469 protected int getMasterColumnCount() 1470 { 1471 JRBaseFiller filler = parentFiller; 1472 int colCount = 1; 1473 1474 while (filler != null) 1475 { 1476 colCount *= filler.columnCount; 1477 filler = filler.parentFiller; 1478 } 1479 1480 return colCount; 1481 } 1482 1483 1484 public JRFillDataset getMainDataset() 1485 { 1486 return mainDataset; 1487 } 1488 1489 1490 protected void addBoundElement(JRFillElement element, JRPrintElement printElement, byte evaluationType, JRGroup group, JRFillBand band) 1491 { 1492 JREvaluationTime evaluationTime = JREvaluationTime.getEvaluationTime(evaluationType, group, band); 1493 addBoundElement(element, printElement, evaluationTime); 1494 } 1495 1496 1497 protected void addBoundElement(JRFillElement element, JRPrintElement printElement, JREvaluationTime evaluationTime) 1498 { 1499 BoundElementMap boundElementsMap = (BoundElementMap) boundElements.get(evaluationTime); 1500 boundElementsMap.put(printElement, element); 1501 } 1502 1503 1504 1505 1514 public JRVirtualPrintPage.ObjectIDPair[] getIdentityData(JRVirtualPrintPage page) 1515 { 1516 Map allElements = new HashMap (); 1517 List identityList = new ArrayList (); 1518 1519 for (Iterator it = boundElements.values().iterator(); it.hasNext();) 1520 { 1521 BoundElementMap pageBoundElementsMap = (BoundElementMap) it.next(); 1522 Map map = pageBoundElementsMap.getMap(page); 1523 if (map != null && !map.isEmpty()) 1524 { 1525 Map idMap = new HashMap (); 1526 1527 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) 1528 { 1529 Map.Entry entry = (Map.Entry ) iter.next(); 1530 Object key = entry.getKey(); 1531 Integer id = (Integer ) allElements.get(key); 1532 if (id == null) 1533 { 1534 JRVirtualPrintPage.ObjectIDPair idPair = new JRVirtualPrintPage.ObjectIDPair(key); 1535 identityList.add(idPair); 1536 1537 id = new Integer (idPair.getIdentity()); 1538 allElements.put(key, id); 1539 } 1540 idMap.put(id, entry.getValue()); 1541 } 1542 pageBoundElementsMap.putMap(page, idMap); 1543 } 1544 } 1545 1546 JRVirtualPrintPage.ObjectIDPair[] identityData = null; 1547 if (!identityList.isEmpty()) 1548 { 1549 identityData = new JRVirtualPrintPage.ObjectIDPair[identityList.size()]; 1550 identityList.toArray(identityData); 1551 } 1552 1553 return identityData; 1554 } 1555 1556 1569 public void setIdentityData(JRVirtualPrintPage page, JRVirtualPrintPage.ObjectIDPair[] identityData) 1570 { 1571 if (identityData == null || identityData.length == 0) 1572 { 1573 return; 1574 } 1575 1576 for (Iterator it = boundElements.values().iterator(); it.hasNext();) 1577 { 1578 BoundElementMap pageBoundElementsMap = (BoundElementMap) it.next(); 1579 Map idMap = pageBoundElementsMap.getMap(page); 1580 if (idMap != null && !idMap.isEmpty()) 1581 { 1582 Map map = new HashMap (); 1583 1584 for (int i = 0; i < identityData.length; i++) 1585 { 1586 JRVirtualPrintPage.ObjectIDPair idPair = identityData[i]; 1587 Integer id = new Integer (idPair.getIdentity()); 1588 1589 Object value = idMap.get(id); 1590 if (value != null) 1591 { 1592 map.put(idPair.getObject(), value); 1593 } 1594 } 1595 1596 pageBoundElementsMap.putMap(page, map); 1597 } 1598 } 1599 } 1600 1601 1602 1613 protected final boolean isEmpty(JRBand band) 1614 { 1615 return band == null || 1616 (band.getHeight() == 0 1617 && (band.getElements() == null || band.getElements().length == 0) 1618 && band.getPrintWhenExpression() == null); 1619 } 1620 1621 protected void setSubreportRunner(JRSubreportRunner runner) 1622 { 1623 this.subreportRunner = runner; 1624 } 1625 1626 protected void suspendSubreportRunner() throws JRException 1627 { 1628 if (subreportRunner == null) 1629 { 1630 throw new JRRuntimeException("No subreport runner set."); 1631 } 1632 1633 subreportRunner.suspend(); 1634 } 1635} 1636 | Popular Tags |