All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jasperreports.engine.util.JRProperties Maven / Gradle / Ivy

There is a newer version: 6.21.3
Show newest version
/*
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2011 Jaspersoft Corporation. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see .
 */
package net.sf.jasperreports.engine.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPropertiesHolder;
import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.JRPropertiesUtil;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.design.JRCompiler;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.xml.JRReportSaxParserFactory;
import net.sf.jasperreports.engine.xml.PrintSaxParserFactory;

/**
 * Class that provides static methods for loading, getting and setting properties.
 * 

* The following actions are performed: *

    *
  • The default (hardcoded) properties are set.
  • *
  • If the system property "net.sf.jasperreports.properties" has been set * then the specified proprties file is loaded.
  • *
  • Otherwise "jasperreports.properties" is loaded if found in the classpath.
  • *
  • For backward compatibility, system properties like "jasper.reports.compile.xml.validation" * are checked and their values are used. This way of specifying properties is deprecated.
  • *
*

* * @author Lucian Chirita ([email protected]) * @version $Id: JRProperties.java 5666 2012-09-14 14:08:19Z teodord $ * @deprecated Replaced by {@link JRPropertiesUtil}. */ public final class JRProperties { /** * The default properties file. * @deprecated Replaced by {@link DefaultJasperReportsContext#DEFAULT_PROPERTIES_FILE}. */ protected static final String DEFAULT_PROPERTIES_FILE = "jasperreports.properties"; /** * The prefix used by all properties. * @deprecated Replaced by {@link JRPropertiesUtil#PROPERTY_PREFIX}. */ public static final String PROPERTY_PREFIX = "net.sf.jasperreports."; /** * The name of the system property that specifies the properties file name. * @deprecated Replaced by {@link DefaultJasperReportsContext#PROPERTIES_FILE}. */ public static final String PROPERTIES_FILE = PROPERTY_PREFIX + "properties"; /** * The name of the class to be used for report compilation. *

* No default value. * * @deprecated Replaced by {@link net.sf.jasperreports.engine.design.JRCompiler#COMPILER_PREFIX}. */ public static final String COMPILER_CLASS = PROPERTY_PREFIX + "compiler.class"; /** * Whether to validate the xml report when compiling. *

* Defaults to true. * @deprecated Replaced by {@link JRReportSaxParserFactory#COMPILER_XML_VALIDATION}. */ public static final String COMPILER_XML_VALIDATION = PROPERTY_PREFIX + "compiler.xml.validation"; /** * Whether to keep the java file generated when the report is compiled. *

* Defaults to false. * @deprecated Replaced by {@link JRCompiler#COMPILER_KEEP_JAVA_FILE}. */ public static final String COMPILER_KEEP_JAVA_FILE = PROPERTY_PREFIX + "compiler.keep.java.file"; /** * The temporary directory used by the report compiler. *

* Defaults to System.getProperty("user.dir"). * @deprecated Replaced by {@link JRCompiler#COMPILER_TEMP_DIR}. */ public static final String COMPILER_TEMP_DIR = PROPERTY_PREFIX + "compiler.temp.dir"; /** * The classpath used by the report compiler. *

* Defaults to System.getProperty("java.class.path"). * @deprecated Replaced by {@link JRCompiler#COMPILER_CLASSPATH}. */ public static final String COMPILER_CLASSPATH = PROPERTY_PREFIX + "compiler.classpath"; /** * Validation flag used by the XML exporter. *

* Defaults to true. * @deprecated Replaced by {@link PrintSaxParserFactory#EXPORT_XML_VALIDATION}. */ public static final String EXPORT_XML_VALIDATION = PROPERTY_PREFIX + "export.xml.validation"; /** * Prefix of properties that specify font files for the PDF exporter. * @deprecated Replaced by {@link JRPdfExporter#PDF_FONT_FILES_PREFIX}. */ public static final String PDF_FONT_FILES_PREFIX = PROPERTY_PREFIX + "export.pdf.font."; /** * Prefix of properties that specify font directories for the PDF exporter. * @deprecated Replaced by {@link JRPdfExporter#PDF_FONT_DIRS_PREFIX}. */ public static final String PDF_FONT_DIRS_PREFIX = PROPERTY_PREFIX + "export.pdf.fontdir."; /** * @deprecated Replaced by {@link net.sf.jasperreports.engine.query#QUERY_EXECUTER_FACTORY_PREFIX}. */ public static final String QUERY_EXECUTER_FACTORY_PREFIX = PROPERTY_PREFIX + "query.executer.factory."; // FIXME remove volatile after we get rid of restoreProperties() protected static volatile ConcurrentHashMap properties; protected static HashMap savedProps; static { initProperties(); } /** * Loads the properties. */ protected static void initProperties() { try { Properties defaults = getDefaults(); String propFile = getSystemProperty(PROPERTIES_FILE); Properties loadedProps; if (propFile == null) { loadedProps = loadProperties(DEFAULT_PROPERTIES_FILE, defaults); if (loadedProps == null) { loadedProps = new Properties(defaults); } } else { loadedProps = loadProperties(propFile, defaults); if (loadedProps == null) { throw new JRRuntimeException("Could not load properties file \"" + propFile + "\""); } } //FIXME configurable concurrency level? properties = new ConcurrentHashMap(); for (Enumeration names = loadedProps.propertyNames(); names.hasMoreElements();) { String name = (String) names.nextElement(); String value = loadedProps.getProperty(name); properties.put(name, value); } loadSystemProperties(); } catch (JRException e) { throw new JRRuntimeException("Error loading the properties", e); } } protected static void loadSystemProperties() { loadSystemProperty("jasper.reports.compiler.class", COMPILER_CLASS); loadSystemProperty("jasper.reports.compile.xml.validation", COMPILER_XML_VALIDATION); loadSystemProperty("jasper.reports.export.xml.validation", EXPORT_XML_VALIDATION); loadSystemProperty("jasper.reports.compile.keep.java.file", COMPILER_KEEP_JAVA_FILE); loadSystemProperty("jasper.reports.compile.temp", COMPILER_TEMP_DIR); loadSystemProperty("jasper.reports.compile.class.path", COMPILER_CLASSPATH); } /** * Sets the default properties. * * @return the default properties */ protected static Properties getDefaults() throws JRException { Properties defaults = new Properties(); InputStream is = JRProperties.class.getResourceAsStream("/default.jasperreports.properties"); if (is == null) { throw new JRException("Default properties file not found."); } try { defaults.load(is); } catch (IOException e) { throw new JRException("Failed to load default properties.", e); } finally { try { is.close(); } catch (IOException e) { } } String userDir = getSystemProperty("user.dir"); if (userDir != null) { defaults.setProperty(COMPILER_TEMP_DIR, userDir); } String classPath = getSystemProperty("java.class.path"); if (classPath != null) { defaults.setProperty(COMPILER_CLASSPATH, classPath); } return defaults; } /** * */ protected static String getSystemProperty(String propertyName) { try { return System.getProperty(propertyName); } catch (SecurityException e) { // This could fail if we are in the applet viewer or some other // restrictive environment, but it should be safe to simply return null. // We cannot log this properly using a logging API, // as we want to keep applet JAR dependencies to a minimum. return null; } } protected static void loadSystemProperty(String sysKey, String propKey) { String val = getSystemProperty(sysKey); if (val != null) { properties.put(propKey, val); } } /** * Loads a properties file from the classpath. * * @param name the resource name * @param defaults the default properties * @return the loaded properties if the resource is found, null otherwise * @throws JRException */ public static Properties loadProperties (String name, Properties defaults) throws JRException { Properties properties = null; InputStream is = null; try { is = JRLoader.getLocationInputStream(name); } catch (SecurityException e) { // This could fail if we are in the applet viewer or some other // restrictive environment, but most of the time it should be safe to ignore. // We cannot log this properly using a logging API, // as we want to keep applet JAR dependencies to a minimum. } if (is != null) { properties = new Properties(defaults); try { properties.load(is); } catch (IOException e) { throw new JRException("Failed to load properties file \"" + name + "\"", e); } finally { try { is.close(); } catch (IOException e) { } } } return properties; } /** * Returns the value of the property. * * @param key the key * @return the property value */ public static String getProperty (String key) { return properties.get(key); } /** * Returns a property as a boolean value. * * @param key the key * @return the property value as a boolean */ public static boolean getBooleanProperty (String key) { return asBoolean(properties.get(key)); } /** * Returns a property as an integer value. * * @param key the key * @return the property value as an integer */ public static int getIntegerProperty (String key) { return asInteger(properties.get(key)); } /** * Returns a property as a float value. * * @param key the key * @return the property value as a float */ public static float getFloatProperty (String key) { return asFloat(properties.get(key)); } /** * Converts a String value into a boolean. * * @param value the value * @return the value as a boolean */ public static boolean asBoolean(String value) { return Boolean.valueOf(value).booleanValue(); } /** * Converts a String value into a int. * * @param value the value * @return the value as a int */ public static int asInteger(String value) { return Integer.parseInt(value); } /** * Converts a String value into a float. * * @param value the value * @return the value as a float */ public static float asFloat(String value) { return Float.parseFloat(value); } /** * Sets the value of a property. * * @param key the key * @param value the value */ public static void setProperty (String key, String value) { properties.put(key, value); DefaultJasperReportsContext.getInstance().setProperty(key, value); // we need to do this because now all JR code is using DefaultJasperReportsContext by default, instead of JRProperties } /** * Sets the value of a property. * * @param key the key * @param value the value */ public static void setProperty (String key, boolean value) { setProperty(key, String.valueOf(value)); } /** * Removes the value set for a property. * *

* This method removes values set in jasperreports.properties or via the * {@link #setProperty(String, String) setProperty} method. * Built-in default property values are not affected; if the property has * a default value it will be used after calling this method for the * property. *

* * @param key the property key */ public static void removePropertyValue (String key) { properties.remove(key); } /** * Saves a copy of the current properties. * * @see #restoreProperties() */ //FIXME implement per thread properties instead of this public static void backupProperties () { savedProps = new HashMap(properties); } /** * Restores previously saved properties. * * @see #backupProperties() */ //FIXME implement per thread properties instead of this public static void restoreProperties () { if (savedProps != null) { try { ConcurrentHashMap newProps = new ConcurrentHashMap(savedProps); properties = newProps; } finally { savedProps = null; } } } /** * Class used by {@link JRProperties#getProperties(String)}. * * @author Lucian Chirita */ public static class PropertySuffix { protected final String key; protected final String suffix; protected final String value; public PropertySuffix (String key, String suffix, String value) { this.key = key; this.suffix = suffix; this.value = value; } public String getKey() { return key; } public String getSuffix () { return suffix; } public String getValue () { return value; } } /** * Returns the list of all properties for a key prefix. * * @param prefix the key prefix * @return a list of {@link PropertySuffix PropertySuffix} objects containing the suffix of the key and the value */ public static List getProperties (String prefix) { int prefixLength = prefix.length(); List values = new ArrayList(); for (Map.Entry entry : properties.entrySet()) { String name = entry.getKey(); if (name.startsWith(prefix)) { String suffix = name.substring(prefixLength); String value = entry.getValue(); values.add(new PropertySuffix(name, suffix, value)); } } return values; } /** * Returns the list of all properties for a key prefix. * * Only this holder's own properties are considered, and not global * properties. * * @param propertiesHolder the properties holder * @param prefix the key prefix * @return a list of {@link PropertySuffix PropertySuffix} objects containing the suffix of the key and the value * @see #getAllProperties(JRPropertiesHolder, String) */ public static List getProperties(JRPropertiesHolder propertiesHolder, String prefix) { return getProperties(getOwnProperties(propertiesHolder), prefix); } /** * Returns the list of all properties for a key prefix, including global * properties. * * @param propertiesHolder the properties holder * @param prefix the key prefix * @return a list of {@link PropertySuffix PropertySuffix} objects containing the suffix of the key and the value * @see #getProperties(JRPropertiesHolder, String) */ public static List getAllProperties(JRPropertiesHolder propertiesHolder, String prefix) { return getAllProperties(getOwnProperties(propertiesHolder), prefix); } /** * Returns the list of all properties for a key prefix. * * Only properties from propertiesMap are considered, and * not global properties. * * @param propertiesMap the properties map * @param prefix the key prefix * @return a list of {@link PropertySuffix PropertySuffix} objects containing the suffix of the key and the value * @see #getAllProperties(JRPropertiesMap, String) */ public static List getProperties(JRPropertiesMap propertiesMap, String prefix) { int prefixLength = prefix.length(); List values = new ArrayList(); if (propertiesMap != null) { String[] propertyNames = propertiesMap.getPropertyNames(); for (int i = 0; i < propertyNames.length; i++) { String name = propertyNames[i]; if (name.startsWith(prefix)) { String suffix = name.substring(prefixLength); String value = propertiesMap.getProperty(name); values.add(new PropertySuffix(name, suffix, value)); } } } return values; } /** * Returns the list of all properties for a key prefix, including global * properties. * * @param propertiesMap the properties map * @param prefix the key prefix * @return a list of {@link PropertySuffix PropertySuffix} objects containing the suffix of the key and the value * @see #getProperties(JRPropertiesMap, String) */ public static List getAllProperties(JRPropertiesMap propertiesMap, String prefix) { List own = getProperties(propertiesMap, prefix); List global = getProperties(prefix); List collected; if (own.isEmpty()) { collected = global; } else { if (!global.isEmpty()) { Set ownSuffixes = new HashSet(); for (Iterator it = own.iterator(); it.hasNext();) { PropertySuffix prop = it.next(); ownSuffixes.add(prop.getSuffix()); } for (Iterator it = global.iterator(); it.hasNext();) { PropertySuffix prop = it.next(); if (!ownSuffixes.contains(prop.getSuffix())) { own.add(prop); } } } collected = own; } return collected; } /** * Returns the value of a property, looking first in the supplied properties holder * and then in the system properties. * * @param propertiesHolder the properties holder * @param key the key * @return the property value */ public static String getProperty (JRPropertiesHolder propertiesHolder, String key) { String value = null; while (propertiesHolder != null && value == null) { if (propertiesHolder.hasProperties()) { value = propertiesHolder.getPropertiesMap().getProperty(key); } propertiesHolder = propertiesHolder.getParentProperties(); } if (value == null) { value = properties.get(key); } return value; } /** * Returns the value of a property, looking for it in several properties holders * and then in the system properties. * * @param key the key * @param propertiesHolders the properties holders * @return the property value */ public static String getProperty (String key, JRPropertiesHolder ... propertiesHolders) { String value = null; main: for (JRPropertiesHolder propertiesHolder : propertiesHolders) { while (propertiesHolder != null) { if (propertiesHolder.hasProperties()) { String prop = propertiesHolder.getPropertiesMap().getProperty(key); if (prop != null) { value = prop; break main; } } propertiesHolder = propertiesHolder.getParentProperties(); } } if (value == null) { value = properties.get(key); } return value; } /** * Returns the value of a property, looking first in the supplied properties map * and then in the system properties. * * @param propertiesMap the properties map * @param key the key * @return the property value */ public static String getProperty (JRPropertiesMap propertiesMap, String key) { String value = null; if (propertiesMap != null) { value = propertiesMap.getProperty(key); } if (value == null) { value = properties.get(key); } return value; } /** * Returns the value of a property as a boolean, looking first in the supplied properties holder * and then in the system properties. * * @param propertiesHolder the properties holder * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static boolean getBooleanProperty (JRPropertiesHolder propertiesHolder, String key, boolean defaultValue) { String value = getProperty(propertiesHolder, key); return value == null ? defaultValue : asBoolean(value); } /** * Returns the value of a property as a Boolean object, looking first in the supplied properties holder * and then in the system properties. * * @param propertiesHolder the properties holder * @param key the key * @return the property value or null if the property is not defined */ public static Boolean getBooleanProperty (JRPropertiesHolder propertiesHolder, String key) { String value = getProperty(propertiesHolder, key); return value == null ? null : Boolean.valueOf(value); } /** * Returns the value of a property as a boolean, looking first in the supplied properties map * and then in the system properties. * * @param propertiesMap the properties map * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static boolean getBooleanProperty (JRPropertiesMap propertiesMap, String key, boolean defaultValue) { String value = getProperty(propertiesMap, key); return value == null ? defaultValue : asBoolean(value); } /** * Returns the value of a property as an integer, looking first in the supplied properties holder * and then in the system properties. * * @param propertiesHolder the properties holder * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static int getIntegerProperty (JRPropertiesHolder propertiesHolder, String key, int defaultValue) { String value = getProperty(propertiesHolder, key); return value == null ? defaultValue : asInteger(value); } /** * Returns the value of a property as an integer, looking first in the supplied properties map * and then in the system properties. * * @param propertiesMap the properties map * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static int getIntegerProperty (JRPropertiesMap propertiesMap, String key, int defaultValue) { String value = getProperty(propertiesMap, key); return value == null ? defaultValue : asInteger(value); } /** * Returns the value of a property as an integer. * * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static int getIntegerProperty (String key, int defaultValue) { String value = getProperty(key); return value == null ? defaultValue : asInteger(value); } /** * Returns the value of a property as a float, looking first in the supplied properties holder * and then in the system properties. * * @param propertiesHolder the properties holder * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static float getFloatProperty (JRPropertiesHolder propertiesHolder, String key, float defaultValue) { String value = getProperty(propertiesHolder, key); return value == null ? defaultValue : asFloat(value); } /** * Returns the value of a property as a float, looking first in the supplied properties map * and then in the system properties. * * @param propertiesMap the properties map * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static float getFloatProperty (JRPropertiesMap propertiesMap, String key, float defaultValue) { String value = getProperty(propertiesMap, key); return value == null ? defaultValue : asFloat(value); } /** * Returns the value of a property as a float. * * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static float getFloatProperty (String key, float defaultValue) { String value = getProperty(key); return value == null ? defaultValue : asFloat(value); } /** * Converts a String value into a long. * * @param value the value * @return the value as a long */ public static long asLong(String value) { return Long.parseLong(value); } /** * Returns a property as a long value. * * @param key the key * @return the property value as a long */ public static long getLongProperty (String key) { return asLong(properties.get(key)); } /** * Returns the value of a property as a long, looking first in the supplied properties map * and then in the system properties. * * @param propertiesMap the properties map * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static long getLongProperty (JRPropertiesMap propertiesMap, String key, int defaultValue) { String value = getProperty(propertiesMap, key); return value == null ? defaultValue : asLong(value); } /** * Returns the value of a property as a long, looking first in the supplied properties holder * and then in the system properties. * * @param propertiesHolder the properties holder * @param key the key * @param defaultValue the default value used if the property is not found * @return the property value */ public static long getLongProperty (JRPropertiesHolder propertiesHolder, String key, int defaultValue) { String value = getProperty(propertiesHolder, key); return value == null ? defaultValue : asLong(value); } protected static JRPropertiesMap getOwnProperties(JRPropertiesHolder propertiesHolder) { return propertiesHolder.hasProperties() ? propertiesHolder.getPropertiesMap() : null; } /** * Copies properties from one object to another. * *

* The properties to be copied are determined by one or more JasperReports * properties having a specified prefix. The values of these properties * are interpreted as prefixes of properties to copy. *

* * @param source the source properties holder * @param destination the destination properties holder * @param tranferPropertiesPrefix the prefix of the JasperReports properties * that specify the object properties to copy */ public static void transferProperties(JRPropertiesHolder source, JRPropertiesHolder destination, String tranferPropertiesPrefix) { if (!source.hasProperties()) { return; } transfer(source.getPropertiesMap(), destination, tranferPropertiesPrefix); } /** * Copies properties from one object to another. * * @param source the source properties * @param destination the destination properties holder * @param tranferPropertiesPrefix the prefix of the JasperReports properties * that specify the object properties to copy * @see #transferProperties(JRPropertiesHolder, JRPropertiesHolder, String) */ public static void transferProperties(JRPropertiesMap source, JRPropertiesHolder destination, String tranferPropertiesPrefix) { if (source == null || !source.hasProperties()) { return; } transfer(source, destination, tranferPropertiesPrefix); } protected static void transfer(JRPropertiesMap source, JRPropertiesHolder destination, String tranferPropertiesPrefix) { List transferPrefixProps = getProperties(tranferPropertiesPrefix);//FIXME cache this for (Iterator prefixIt = transferPrefixProps.iterator(); prefixIt.hasNext();) { JRProperties.PropertySuffix transferPrefixProp = prefixIt.next(); String transferPrefix = transferPrefixProp.getValue(); if (transferPrefix != null && transferPrefix.length() > 0) { List transferProps = getProperties(source, transferPrefix); for (Iterator propIt = transferProps.iterator(); propIt.hasNext();) { JRProperties.PropertySuffix property = propIt.next(); String value = property.getValue(); destination.getPropertiesMap().setProperty(property.getKey(), value); } } } } /** * Returns a property as a Character value. * * @param key the key * @return the property value as a Character * @see #asCharacter(String) */ public static Character getCharacterProperty(String key) { return asCharacter(properties.get(key)); } /** * Returns the value of a property as a Character value, * looking first in the supplied properties holder and then in the * system properties. * * @param propertiesMap the properties map * @param key the key * @return the property value as a Character */ public static Character getCharacterProperty(JRPropertiesMap propertiesMap, String key) { String value = getProperty(propertiesMap, key); return asCharacter(value); } /** * Converts a String into a Character value. * *

* If the String value is null or the empty string, * null is returned. Otherwise, the method returns * the first character in the string. * * @param value the String value * @return the value converted to Character */ public static Character asCharacter(String value) { return value == null || value.length() == 0 ? null : new Character(value.charAt(0)); } private JRProperties() { } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy