Package framework.beans.report

Source Code of framework.beans.report.ReportBaseConstant

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package framework.beans.report;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.JRCostumFileSource;
import net.sf.jasperreports.engine.util.JRResourcesUtil;

/**
*
* @author finder
*/
public class ReportBaseConstant {
  public static final  String          SUBREPORT_PARAMETER_NAME = "report_params";
  public static final  String          SUBREPORT_DATASOURCE_NAME = "table_datasouce";
  public static final  String          SUBREPORT_DATA_ARRAY_NAME = "table_data";
  public static final  String          SUBREPORT_TABLE_ID = "table_id";
  public static final  String          SUBREPORT_PART_ID = "part_id";
  public static final  String          SUBREPORT_TABLE_TYPE_ID = "table_type_id";

  static final private Map<String, String[]>    FILE_EXTENSION_MAP = new HashMap<String, String[]>();
  public static final String[]          JASPER_REPORT_EXTENSION = new String[] {".jasper", ".jrxml", ".xml"};

  static{
    FILE_EXTENSION_MAP.put(".jasper", new String[] {".jrxml", ".xml"});
    FILE_EXTENSION_MAP.put(".jrxml", new String[] {".jasper"});
    FILE_EXTENSION_MAP.put(".xml", new String[] {".jasper"});
    //FILE_EXTEBSION_MAP.put(null, new String[] {".jasper", ".jrxml"});
  }

  static public ArrayList<String> remapFile(String fileName){
    ArrayList<String>        target = new ArrayList<String>();
    target.add(fileName);
    int                exPos = fileName.lastIndexOf('.');
    if (exPos >= 0){
      String        ex = fileName.substring(exPos, fileName.length());
      String[]      otherEx = FILE_EXTENSION_MAP.get(ex);
      if (otherEx != null){
        String        baseName = fileName.substring(0, exPos);
        for (String newEx : otherEx) {
          target.add(baseName + newEx);
        }
      }
    }
    return target;
  }

  private static final String      REPORT_EXTRA_FONT_LOCATION = "/framework/utils/resources/report/";
  private static final String[]    REPORT_EXTRA_FONT_LIST = {"lucon.ttf", "times.ttf", "cour.ttf"};
 
  private static boolean        fontIsLoaded;

  static public void initReportsFonts() throws IOException{
    if (fontIsLoaded){
      return;
    }
    fontIsLoaded = true;
    JRResourcesUtil.setBaseFileSource(new JRCostumFileSource() {
      @Override
      public InputStream getInputStream(String fileName) {
        return ReportBaseConstant.getJRExtraResource(fileName);
      }
    });
    for (String name : REPORT_EXTRA_FONT_LIST) {
      InputStream is = ReportBaseConstant.class.getResourceAsStream(REPORT_EXTRA_FONT_LOCATION + name);
      if (is == null){
        throw new IOException("Шрифт " + name + " не найден");
      }
      try {
        try {
          Font fnt = Font.createFont(Font.TRUETYPE_FONT, is);
          GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fnt);

        }
        catch (FontFormatException ex) {
          throw new IOException("Ошибка загрузки шрифта", ex);
        }
      }
      finally{
        is.close();
      }
    }
  }

  static public InputStream getJRExtraResource(String resName){
    InputStream      is = ReportBaseConstant.class.getResourceAsStream(REPORT_EXTRA_FONT_LOCATION + resName);
    return is;
  }
}
TOP

Related Classes of framework.beans.report.ReportBaseConstant

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.