Class JRPropertiesUtil


  • public final class JRPropertiesUtil
    extends Object
    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 (lucianc@users.sourceforge.net)
    • Field Detail

      • EXCEPTION_MESSAGE_KEY_LOAD_PROPERTIES_FILE_FAILURE

        public static final String EXCEPTION_MESSAGE_KEY_LOAD_PROPERTIES_FILE_FAILURE
        See Also:
        Constant Field Values
      • EXCEPTION_MESSAGE_KEY_LOAD_PROPERTIES_FAILURE

        public static final String EXCEPTION_MESSAGE_KEY_LOAD_PROPERTIES_FAILURE
        See Also:
        Constant Field Values
    • Method Detail

      • loadProperties

        public static Properties loadProperties​(String name,
                                                Properties defaults)
                                         throws JRException
        Loads a properties file from the classpath.
        Parameters:
        name - the resource name
        defaults - the default properties
        Returns:
        the loaded properties if the resource is found, null otherwise
        Throws:
        JRException
      • getProperty

        public String getProperty​(String key)
        Returns the value of the property.
        Parameters:
        key - the key
        Returns:
        the property value
      • getProperty

        public String getProperty​(String key,
                                  String defaultValue)
        Returns the value of the property.
        Parameters:
        key - the key
        defaultValue - the value to return when the property is not set
        Returns:
        the property value
      • setProperty

        public void setProperty​(String key,
                                String value)
      • removeProperty

        public void removeProperty​(String key)
      • getBooleanProperty

        public boolean getBooleanProperty​(String key)
        Returns a property as a boolean value.
        Parameters:
        key - the key
        Returns:
        the property value as a boolean
      • getBooleanProperty

        public boolean getBooleanProperty​(String key,
                                          boolean defaultValue)
        Returns a property as a boolean value.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the property value as a boolean
      • getIntegerProperty

        public int getIntegerProperty​(String key)
        Returns a property as an integer value.
        Parameters:
        key - the key
        Returns:
        the property value as an integer
      • getFloatProperty

        public float getFloatProperty​(String key)
        Returns a property as a float value.
        Parameters:
        key - the key
        Returns:
        the property value as a float
      • asBoolean

        public static boolean asBoolean​(String value)
        Converts a String value into a boolean.
        Parameters:
        value - the value
        Returns:
        the value as a boolean
      • asBoolean

        public static boolean asBoolean​(String value,
                                        boolean defaultValue)
      • asInteger

        public static int asInteger​(String value)
        Converts a String value into a int.
        Parameters:
        value - the value
        Returns:
        the value as a int
      • asFloat

        public static float asFloat​(String value)
        Converts a String value into a float.
        Parameters:
        value - the value
        Returns:
        the value as a float
      • asDouble

        public static double asDouble​(String value)
        Converts a String value into a double.
        Parameters:
        value - the value
        Returns:
        the value as a double
      • getProperty

        public String getProperty​(JRPropertiesHolder propertiesHolder,
                                  String key)
        Returns the value of a property, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        Returns:
        the property value
      • getProperty

        public String getProperty​(JRPropertiesHolder propertiesHolder,
                                  String key,
                                  String defaultValue)
        Returns the value of a property, looking first in the supplied properties holder and then in the system properties, using a default value if the property is not found.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        defaultValue - the value to return if no property is found
        Returns:
        the property value
      • getProperty

        public String getProperty​(String key,
                                  JRPropertiesHolder... propertiesHolders)
        Returns the value of a property, looking for it in several properties holders and then in the system properties.
        Parameters:
        key - the key
        propertiesHolders - the properties holders
        Returns:
        the property value
      • getProperty

        public String getProperty​(JRPropertiesMap propertiesMap,
                                  String key)
        Returns the value of a property, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        Returns:
        the property value
      • getBooleanProperty

        public boolean getBooleanProperty​(JRPropertiesHolder propertiesHolder,
                                          String key,
                                          boolean defaultValue)
        Returns the value of a property as a boolean, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getBooleanProperty

        public boolean getBooleanProperty​(String key,
                                          boolean defaultValue,
                                          JRPropertiesHolder... propertiesHolders)
        Returns the value of a property as a boolean, looking first in several properties holders and then in the system properties.
        Parameters:
        key - the key
        defaultValue - the default value used if the property is not found
        propertiesHolders - the properties holders
        Returns:
        the property value
      • getBooleanProperty

        public boolean getBooleanProperty​(JRPropertiesMap propertiesMap,
                                          String key,
                                          boolean defaultValue)
        Returns the value of a property as a boolean, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getBooleanProperty

        public Boolean getBooleanProperty​(JRPropertiesMap propertiesMap,
                                          String key)
        Returns the value of a property as a boolean, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        Returns:
        the property value
      • getBooleanProperty

        public Boolean getBooleanProperty​(JRPropertiesHolder propertiesHolder,
                                          String key)
        Returns the value of a property as a Boolean, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        Returns:
        the property value
      • getIntegerProperty

        public Integer getIntegerProperty​(JRPropertiesMap propertiesMap,
                                          String key)
        Returns the value of a property as an Integer, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        Returns:
        the property value
      • getIntegerProperty

        public Integer getIntegerProperty​(JRPropertiesHolder propertiesHolder,
                                          String key)
        Returns the value of a property as an Integer, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        Returns:
        the property value
      • getIntegerProperty

        public int getIntegerProperty​(JRPropertiesHolder propertiesHolder,
                                      String key,
                                      int defaultValue)
        Returns the value of a property as an integer, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getIntegerProperty

        public int getIntegerProperty​(JRPropertiesMap propertiesMap,
                                      String key,
                                      int defaultValue)
        Returns the value of a property as an integer, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getIntegerProperty

        public int getIntegerProperty​(String key,
                                      int defaultValue)
        Returns the value of a property as an integer.
        Parameters:
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getFloatProperty

        public Float getFloatProperty​(JRPropertiesHolder propertiesHolder,
                                      String key)
        Returns the value of a property as a Float, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        Returns:
        the property value
      • getFloatProperty

        public float getFloatProperty​(JRPropertiesHolder propertiesHolder,
                                      String key,
                                      float defaultValue)
        Returns the value of a property as a float, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getFloatProperty

        public float getFloatProperty​(String key,
                                      float defaultValue,
                                      JRPropertiesHolder... propertiesHolders)
        Returns the value of a property as a float, looking first in several properties holders and then in the system properties.
        Parameters:
        key - the key
        defaultValue - the default value used if the property is not found
        propertiesHolders - the properties holders
        Returns:
        the property value
      • getFloatProperty

        public float getFloatProperty​(JRPropertiesMap propertiesMap,
                                      String key,
                                      float defaultValue)
        Returns the value of a property as a float, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getFloatProperty

        public float getFloatProperty​(String key,
                                      float defaultValue)
        Returns the value of a property as a float.
        Parameters:
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • asLong

        public static long asLong​(String value)
        Converts a String value into a long.
        Parameters:
        value - the value
        Returns:
        the value as a long
      • getLongProperty

        public long getLongProperty​(String key)
        Returns a property as a long value.
        Parameters:
        key - the key
        Returns:
        the property value as a long
      • getLongProperty

        public long getLongProperty​(String key,
                                    long defaultValue)
        Returns a property as a long value.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the property value as a long
      • getLongProperty

        public long getLongProperty​(JRPropertiesMap propertiesMap,
                                    String key,
                                    long defaultValue)
        Returns the value of a property as a long, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getLongProperty

        public long getLongProperty​(JRPropertiesHolder propertiesHolder,
                                    String key,
                                    long defaultValue)
        Returns the value of a property as a long, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        defaultValue - the default value used if the property is not found
        Returns:
        the property value
      • getDoubleProperty

        public Double getDoubleProperty​(JRPropertiesHolder propertiesHolder,
                                        String key)
        Returns the value of a property as a Double, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        Returns:
        the property value
      • transferProperties

        public void transferProperties​(JRPropertiesHolder source,
                                       JRPropertiesHolder destination,
                                       String tranferPropertiesPrefix)
        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.

        Parameters:
        source - the source properties holder
        destination - the destination properties holder
        tranferPropertiesPrefix - the prefix of the JasperReports properties that specify the object properties to copy
      • getCharacterProperty

        public Character getCharacterProperty​(String key)
        Returns a property as a Character value.
        Parameters:
        key - the key
        Returns:
        the property value as a Character
        See Also:
        asCharacter(String)
      • getCharacterProperty

        public Character getCharacterProperty​(JRPropertiesHolder propertiesHolder,
                                              String key)
        Returns the value of a property as a Character value, looking first in the supplied properties holder and then in the system properties.
        Parameters:
        propertiesHolder - the properties holder
        key - the key
        Returns:
        the property value as a Character
      • getCharacterProperty

        public Character getCharacterProperty​(JRPropertiesMap propertiesMap,
                                              String key)
        Returns the value of a property as a Character value, looking first in the supplied properties map and then in the system properties.
        Parameters:
        propertiesMap - the properties map
        key - the key
        Returns:
        the property value as a Character
      • asCharacter

        public static Character asCharacter​(String 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.

        Parameters:
        value - the String value
        Returns:
        the value converted to Character
      • getLocalizedProperty

        public String getLocalizedProperty​(String property,
                                           Locale locale)