Advertisement
Guest User

Handling Encoding with DynamicJasper

a guest
Jul 7th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.00 KB | None | 0 0
  1. /*
  2.  * DynamicJasper: A library for creating reports dynamically by specifying
  3.  * columns, groups, styles, etc. at runtime. It also saves a lot of development
  4.  * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
  5.  *
  6.  * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  *
  11.  * License as published by the Free Software Foundation; either
  12.  *
  13.  * version 2.1 of the License, or (at your option) any later version.
  14.  *
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19.  *
  20.  * Lesser General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU Lesser General Public
  23.  * License along with this library; if not, write to the Free Software
  24.  *
  25.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  26.  *
  27.  *
  28.  */
  29.  
  30. package ar.com.fdvs.dj.test.encoding;
  31.  
  32.  
  33. import ar.com.fdvs.dj.domain.DJCalculation;
  34. import ar.com.fdvs.dj.domain.DJValueFormatter;
  35. import ar.com.fdvs.dj.domain.DynamicReport;
  36. import ar.com.fdvs.dj.domain.Style;
  37. import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
  38. import ar.com.fdvs.dj.domain.constants.Font;
  39. import ar.com.fdvs.dj.test.BaseDjReportTest;
  40. import ar.com.fdvs.dj.test.ReportExporter;
  41. import ar.com.fdvs.dj.test.domain.Product;
  42. import net.sf.jasperreports.engine.JRDataSource;
  43. import net.sf.jasperreports.engine.JRExporterParameter;
  44. import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
  45. import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
  46. import net.sf.jasperreports.view.JasperDesignViewer;
  47. import net.sf.jasperreports.view.JasperViewer;
  48. import org.mozilla.universalchardet.UniversalDetector;
  49.  
  50. import java.io.ByteArrayInputStream;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.text.SimpleDateFormat;
  54. import java.util.*;
  55.  
  56. public class UTF8ReportTest extends BaseDjReportTest {
  57.  
  58.     public static final String PDF_FONT_ENCODING = "ISO-8859-2";
  59.  
  60.     /**
  61.      * For a list of supported encodings: http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html
  62.      */
  63.  
  64.  
  65.     public DynamicReport buildReport() throws Exception {
  66.  
  67.  
  68.         Style titleStyle = Style.createBlankStyle("title");
  69.         Style subtitleStyle = Style.createBlankStyle("subtitleStyle");
  70.         Style headerStyle = Style.createBlankStyle("headerStyle");
  71.         Style detailStyle = Style.createBlankStyle("detailStyle");
  72.  
  73.         String encoding = PDF_FONT_ENCODING;
  74.  
  75.         log.debug("ENCODING IS: " + encoding);
  76.  
  77.         titleStyle.setFont(new Font(18, "Arial", "Arial", encoding, false));
  78.         subtitleStyle.setFont(new Font(14, "Arial", "Arial", encoding, false));
  79.         detailStyle.setFont(new Font(10, "Arial", "Arial", encoding, false));
  80.  
  81.         /**
  82.          * Creates the DynamicReportBuilder and sets the basic options for
  83.          * the report
  84.          */
  85.         FastReportBuilder drb = new FastReportBuilder();
  86.         drb.addColumn("State", "state", String.class.getName(), 30)
  87.                 .addColumn("Branch", "branch", String.class.getName(), 30)
  88.                 .addColumn("Product Line", "productLine", String.class.getName(), 50)
  89.                 .addColumn("Item", "item", String.class.getName(), 50)
  90.                 .addColumn("Item Code", "id", Long.class.getName(), 30, true)
  91.                 .addColumn("Quantity", "quantity", Long.class.getName(), 60, true)
  92.                 .addColumn("Amount", "amount", Float.class.getName(), 70, true)
  93.                 .addGroups(2)
  94.                 .setTitle("Report with Polish letters")
  95.                 .setSubtitle("ĄĆ\tĘ\tŁ\tŃ\tÓ\tŚ\tŹ\tŻ\tą\tć\tę\tł\tń\tó\tś\tź\tż")
  96.                 .setPrintBackgroundOnOddRows(true)
  97.                 .setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
  98.                 .setUseFullPageWidth(true);
  99.  
  100.         drb.addGlobalFooterVariable(drb.getColumn(4), DJCalculation.COUNT, null, new DJValueFormatter() {
  101.  
  102.             public String getClassName() {
  103.                 return String.class.getName();
  104.             }
  105.  
  106.  
  107.             public Object evaluate(Object value, Map fields, Map variables, Map parameters) {
  108.                 return (value == null ? "0" : value.toString()) + " Clients";
  109.             }
  110.         });
  111.  
  112.  
  113.         DynamicReport dr = drb.build();
  114.  
  115.         return dr;
  116.     }
  117.  
  118.     public static Collection getDummyCollection() {
  119.  
  120.         SimpleDateFormat dateFormat = new SimpleDateFormat();
  121.         dateFormat.applyPattern("dd/MM/yyyy");
  122.  
  123.         List col = new ArrayList();
  124.  
  125.         //The collection is ordered by State, Branch and Product Line
  126.         col.add(new Product(1l, "book", "ąćęłńóśźż", "abcdefghaijkslmnopqĄĆĘŁŃÓŚŹŻĄĆĘŁŃÓŚŹŻĄĆĘŁŃÓŚŹŻĄĆĘŁŃÓŚŹŻĄĆĘŁŃÓŚŹŻĄĆĘŁ", "Main Street", new Long("2500"), new Float("5")));
  127.         col.add(new Product(1l, "book", "áñö", "ĄĆĘŁŃÓŚŹŻ", "Main Street", new Long("2500"), new Float("5")));
  128.         return col;
  129.     }
  130.  
  131.     @Override
  132.     protected void exportReport() throws Exception {
  133.         Map<JRExporterParameter, Object> exportParams = new HashMap<JRExporterParameter, Object>();
  134.         ReportExporter.exportReport(jp, System.getProperty("user.dir") + "/target/reports/" + this.getClass().getName() + ".pdf", exportParams);
  135.         exportToJRXML();
  136.     }
  137.  
  138.     @Override
  139.     protected JRDataSource getDataSource() {
  140.         return new JRBeanCollectionDataSource(getDummyCollection());
  141.     }
  142.  
  143.     public static void main(String[] args) throws Exception {
  144.         UTF8ReportTest test = new UTF8ReportTest();
  145.         test.testReport();
  146.         test.exportToJRXML();
  147.         JasperViewer.viewReport(test.jp);    //finally display the report report
  148.         JasperDesignViewer.viewReportDesign(test.jr);
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement