KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRProperties


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRPropertiesMap;
39 import net.sf.jasperreports.engine.JRRuntimeException;
40
41 /**
42  * Class that provides static methods for loading, getting and setting properties.
43  * <p>
44  * The following actions are performed:
45  * <ul>
46  * <li>The default (hardcoded) properties are set.</li>
47  * <li>If the system property "net.sf.jasperreports.properties" has been set
48  * then the specified proprties file is loaded.</li>
49  * <li>Otherwise "jasperreports.properties" is loaded if found in the classpath.</li>
50  * <li>For backward compatibility, system properties like "jasper.reports.compile.xml.validation"
51  * are checked and their values are used. This way of specifying properties is deprecated.</li>
52  *
53  * @author Lucian Chirita (lucianc@users.sourceforge.net)
54  * @version $Id: JRProperties.java 1435 2006-10-13 13:16:47 +0300 (Fri, 13 Oct 2006) teodord $
55  */

56 public class JRProperties
57 {
58     /**
59      * The default properties file.
60      */

61     protected static final String JavaDoc DEFAULT_PROPERTIES_FILE = "jasperreports.properties";
62     
63     /**
64      * The prefix used by all properties.
65      */

66     public static final String JavaDoc PROPERTY_PREFIX = "net.sf.jasperreports.";
67     
68     /**
69      * The name of the system property that specifies the properties file name.
70      */

71     public static final String JavaDoc PROPERTIES_FILE = PROPERTY_PREFIX + "properties";
72     
73     /**
74      * The name of the class to be used for report compilation.
75      * <p>
76      * No default value.
77      */

78     public static final String JavaDoc COMPILER_CLASS = PROPERTY_PREFIX + "compiler.class";
79     
80     /**
81      * Whether to validate the xml report when compiling.
82      * <p>
83      * Defaults to <code>true</code>.
84      */

85     public static final String JavaDoc COMPILER_XML_VALIDATION = PROPERTY_PREFIX + "compiler.xml.validation";
86     
87     /**
88      * Whether to keep the java file generated when the report is compiled.
89      * <p>
90      * Defaults to <code>false</code>.
91      */

92     public static final String JavaDoc COMPILER_KEEP_JAVA_FILE = PROPERTY_PREFIX + "compiler.keep.java.file";
93     
94     /**
95      * The temporary directory used by the report compiler.
96      * <p>
97      * Defaults to <code>System.getProperty("user.dir")</code>.
98      */

99     public static final String JavaDoc COMPILER_TEMP_DIR = PROPERTY_PREFIX + "compiler.temp.dir";
100     
101     /**
102      * The classpath used by the report compiler.
103      * <p>
104      * Defaults to <code>System.getProperty("java.class.path")</code>.
105      */

106     public static final String JavaDoc COMPILER_CLASSPATH = PROPERTY_PREFIX + "compiler.classpath";
107     
108     /**
109      * Validation flag used by the XML exporter.
110      * <p>
111      * Defaults to <code>true</code>.
112      */

113     public static final String JavaDoc EXPORT_XML_VALIDATION = PROPERTY_PREFIX + "export.xml.validation";
114     
115     /**
116      * Prefix of properties that specify font files for the PDF exporter.
117      */

118     public static final String JavaDoc PDF_FONT_FILES_PREFIX = PROPERTY_PREFIX + "export.pdf.font.";
119     
120     /**
121      * Prefix of properties that specify font directories for the PDF exporter.
122      */

123     public static final String JavaDoc PDF_FONT_DIRS_PREFIX = PROPERTY_PREFIX + "export.pdf.fontdir.";
124     
125     /**
126      * Prefix for query executer factory properties.
127      * <p/>
128      * To obtain query executer factories, a property having the query language appended to this prefix is used
129      * to get the query executer factory name.
130      */

131     public static final String JavaDoc QUERY_EXECUTER_FACTORY_PREFIX = PROPERTY_PREFIX + "query.executer.factory.";
132     
133     
134     /**
135      * Property specifying the {@link net.sf.jasperreports.engine.fill.JRSubreportRunnerFactory JRSubreportRunnerFactory}
136      * implementation to use for creating subreport runners.
137      */

138     public static final String JavaDoc SUBREPORT_RUNNER_FACTORY = PROPERTY_PREFIX + "subreport.runner.factory";
139     
140     /**
141      * Property that provides a default value for the
142      * {@link net.sf.jasperreports.engine.export.JRPdfExporterParameter#FORCE_LINEBREAK_POLICY JRPdfExporterParameter.FORCE_LINEBREAK_POLICY}
143      * PDF exporter parameter.
144      *
145      * @see net.sf.jasperreports.engine.export.JRPdfExporterParameter#FORCE_LINEBREAK_POLICY
146      */

147     public static final String JavaDoc PDF_FORCE_LINEBREAK_POLICY = PROPERTY_PREFIX + "export.pdf.force.linebreak.policy";
148     
149     
150     protected static Properties JavaDoc props;
151     
152     protected static Properties JavaDoc savedProps;
153     
154     static
155     {
156         initProperties();
157     }
158
159     /**
160      * Loads the properties.
161      */

162     protected static void initProperties()
163     {
164         try
165         {
166             Properties JavaDoc defaults = getDefaults();
167             String JavaDoc propFile = getSystemProperty(PROPERTIES_FILE);
168             if (propFile == null)
169             {
170                 props = loadProperties(DEFAULT_PROPERTIES_FILE, defaults);
171                 if (props == null)
172                 {
173                     props = new Properties JavaDoc(defaults);
174                 }
175             }
176             else
177             {
178                 props = loadProperties(propFile, defaults);
179                 if (props == null)
180                 {
181                     throw new JRRuntimeException("Could not load properties file \"" + propFile + "\"");
182                 }
183             }
184
185             loadSystemProperties();
186         }
187         catch (JRException e)
188         {
189             throw new JRRuntimeException("Error loading the properties", e);
190         }
191     }
192     
193     protected static void loadSystemProperties()
194     {
195         loadSystemProperty("jasper.reports.compiler.class", COMPILER_CLASS);
196         loadSystemProperty("jasper.reports.compile.xml.validation", COMPILER_XML_VALIDATION);
197         loadSystemProperty("jasper.reports.export.xml.validation", EXPORT_XML_VALIDATION);
198         loadSystemProperty("jasper.reports.compile.keep.java.file", COMPILER_KEEP_JAVA_FILE);
199         loadSystemProperty("jasper.reports.compile.temp", COMPILER_TEMP_DIR);
200         loadSystemProperty("jasper.reports.compile.class.path", COMPILER_CLASSPATH);
201     }
202     
203     /**
204      * Sets the default properties.
205      *
206      * @return the default properties
207      */

208     protected static Properties JavaDoc getDefaults() throws JRException
209     {
210         Properties JavaDoc defaults = new Properties JavaDoc();
211         
212         InputStream JavaDoc is = JRProperties.class.getResourceAsStream("/default.jasperreports.properties");
213         
214         if (is == null)
215         {
216             throw new JRException("Default properties file not found.");
217         }
218
219         try
220         {
221             defaults.load(is);
222         }
223         catch (IOException JavaDoc e)
224         {
225             throw new JRException("Failed to load default properties.", e);
226         }
227         finally
228         {
229             try
230             {
231                 is.close();
232             }
233             catch (IOException JavaDoc e)
234             {
235             }
236         }
237         
238         String JavaDoc userDir = getSystemProperty("user.dir");
239         if (userDir != null)
240         {
241             defaults.setProperty(COMPILER_TEMP_DIR, userDir);
242         }
243         
244         String JavaDoc classPath = getSystemProperty("java.class.path");
245         if (classPath != null)
246         {
247             defaults.setProperty(COMPILER_CLASSPATH, classPath);
248         }
249
250         return defaults;
251     }
252
253     /**
254      *
255      */

256     protected static String JavaDoc getSystemProperty(String JavaDoc propertyName)
257     {
258         try
259         {
260             return System.getProperty(propertyName);
261         }
262         catch (SecurityException JavaDoc e)
263         {
264             // This could fail if we are in the applet viewer or some other
265
// restrictive environment, but it should be safe to simply return null.
266
// We cannot log this properly using a logging API,
267
// as we want to keep applet JAR dependencies to a minimum.
268
return null;
269         }
270     }
271
272     protected static void loadSystemProperty(String JavaDoc sysKey, String JavaDoc propKey)
273     {
274         String JavaDoc val = getSystemProperty(sysKey);
275         if (val != null)
276         {
277             props.setProperty(propKey, val);
278         }
279     }
280
281     /**
282      * Loads a properties file from the classpath.
283      *
284      * @param name the resource name
285      * @param defaults the default properties
286      * @return the loaded properties if the resource is found, <code>null</code> otherwise
287      * @throws JRException
288      */

289     public static Properties JavaDoc loadProperties (String JavaDoc name, Properties JavaDoc defaults) throws JRException
290     {
291         Properties JavaDoc properties = null;
292         
293         InputStream JavaDoc is = null;
294         
295         try
296         {
297             is = JRLoader.getLocationInputStream(name);
298         }
299         catch (SecurityException JavaDoc e)
300         {
301             // This could fail if we are in the applet viewer or some other
302
// restrictive environment, but most of the time it should be safe to ignore.
303
// We cannot log this properly using a logging API,
304
// as we want to keep applet JAR dependencies to a minimum.
305
}
306         
307         if (is != null)
308         {
309             properties = new Properties JavaDoc(defaults);
310             try
311             {
312                 properties.load(is);
313             }
314             catch (IOException JavaDoc e)
315             {
316                 throw new JRException("Failed to load properties file \"" + name + "\"", e);
317             }
318             finally
319             {
320                 try
321                 {
322                     is.close();
323                 }
324                 catch (IOException JavaDoc e)
325                 {
326                 }
327             }
328         }
329         
330         return properties;
331     }
332     
333     /**
334      * Returns the value of the property.
335      *
336      * @param key the key
337      * @return the property value
338      */

339     public static String JavaDoc getProperty (String JavaDoc key)
340     {
341         return props.getProperty(key);
342     }
343     
344     /**
345      * Returns a property as a boolean value.
346      *
347      * @param key the key
348      * @return the property value as a boolean
349      */

350     public static boolean getBooleanProperty (String JavaDoc key)
351     {
352         return asBoolean(props.getProperty(key));
353     }
354     
355     /**
356      * Returns a property as an integer value.
357      *
358      * @param key the key
359      * @return the property value as an integer
360      */

361     public static int getIntegerProperty (String JavaDoc key)
362     {
363         return asInteger(props.getProperty(key));
364     }
365
366     /**
367      * Converts a <code>String</code> value into a <code>boolean</code>.
368      *
369      * @param value the value
370      * @return the value as a <code>boolean</code>
371      */

372     public static boolean asBoolean(String JavaDoc value)
373     {
374         return Boolean.valueOf(value).booleanValue();
375     }
376
377     /**
378      * Converts a <code>String</code> value into a <code>int</code>.
379      *
380      * @param value the value
381      * @return the value as a <code>int</code>
382      */

383     public static int asInteger(String JavaDoc value)
384     {
385         return Integer.parseInt(value);
386     }
387     
388     /**
389      * Sets the value of a property.
390      *
391      * @param key the key
392      * @param value the value
393      */

394     public static void setProperty (String JavaDoc key, String JavaDoc value)
395     {
396         props.setProperty(key, value);
397     }
398     
399     /**
400      * Sets the value of a property.
401      *
402      * @param key the key
403      * @param value the value
404      */

405     public static void setProperty (String JavaDoc key, boolean value)
406     {
407         props.setProperty(key, String.valueOf(value));
408     }
409     
410     /**
411      * Saves a copy of the current properties.
412      *
413      * @see #restoreProperties()
414      */

415     public static void backupProperties ()
416     {
417         savedProps = (Properties JavaDoc) props.clone();
418     }
419     
420     /**
421      * Restores previously saved properties.
422      *
423      * @see #backupProperties()
424      */

425     public static void restoreProperties ()
426     {
427         if (savedProps != null)
428         {
429             try
430             {
431                 props.clear();
432                 props.putAll(savedProps);
433             }
434             finally
435             {
436                 savedProps = null;
437             }
438         }
439     }
440     
441     /**
442      * Class used by {@link JRProperties#getProperties(String)}.
443      *
444      * @author Lucian Chirita
445      */

446     public static class PropertySuffix
447     {
448         protected final String JavaDoc key;
449         protected final String JavaDoc suffix;
450         protected final String JavaDoc value;
451         
452         public PropertySuffix (String JavaDoc key, String JavaDoc suffix, String JavaDoc value)
453         {
454             this.key = key;
455             this.suffix = suffix;
456             this.value = value;
457         }
458         
459         public String JavaDoc getKey()
460         {
461             return key;
462         }
463         
464         public String JavaDoc getSuffix ()
465         {
466             return suffix;
467         }
468         
469         public String JavaDoc getValue ()
470         {
471             return value;
472         }
473     }
474     
475     /**
476      * Returns the list of all properties for a key prefix.
477      *
478      * @param prefix the key prefix
479      * @return a list of {@link PropertySuffix PropertySuffix} objects containing the suffix of the key and the value
480      */

481     public static List JavaDoc getProperties (String JavaDoc prefix)
482     {
483         int prefixLength = prefix.length();
484         List JavaDoc values = new ArrayList JavaDoc();
485         for (Enumeration JavaDoc names = props.propertyNames(); names.hasMoreElements();)
486         {
487             String JavaDoc name = (String JavaDoc) names.nextElement();
488             if (name.startsWith(prefix))
489             {
490                 String JavaDoc suffix = name.substring(prefixLength);
491                 String JavaDoc value = props.getProperty(name);
492                 values.add(new PropertySuffix(name, suffix, value));
493             }
494         }
495         return values;
496     }
497
498     /**
499      * Returns the value of a property, looking first in the supplied properties map
500      * and then in the system properties.
501      *
502      * @param propertiesMap the properties map
503      * @param key the key
504      * @return the property value
505      */

506     public static String JavaDoc getProperty (JRPropertiesMap propertiesMap, String JavaDoc key)
507     {
508         String JavaDoc value = null;
509         if (propertiesMap != null)
510         {
511             value = propertiesMap.getProperty(key);
512         }
513         
514         if (value == null)
515         {
516             value = props.getProperty(key);
517         }
518         
519         return value;
520     }
521
522     /**
523      * Returns the value of a property as a boolean, looking first in the supplied properties map
524      * and then in the system properties.
525      *
526      * @param propertiesMap the properties map
527      * @param key the key
528      * @param defaultValue the default value used if the property is not found
529      * @return the property value
530      */

531     public static boolean getBooleanProperty (JRPropertiesMap propertiesMap, String JavaDoc key, boolean defaultValue)
532     {
533         String JavaDoc value = getProperty(propertiesMap, key);
534         
535         return value == null ? defaultValue : asBoolean(value);
536     }
537
538     /**
539      * Returns the value of a property as an integer, looking first in the supplied properties map
540      * and then in the system properties.
541      *
542      * @param propertiesMap the properties map
543      * @param key the key
544      * @param defaultValue the default value used if the property is not found
545      * @return the property value
546      */

547     public static int getIntegerProperty (JRPropertiesMap propertiesMap, String JavaDoc key, int defaultValue)
548     {
549         String JavaDoc value = getProperty(propertiesMap, key);
550         
551         return value == null ? defaultValue : asInteger(value);
552     }
553
554     /**
555      * Converts a <code>String</code> value into a <code>long</code>.
556      *
557      * @param value the value
558      * @return the value as a <code>long</code>
559      */

560     public static long asLong(String JavaDoc value)
561     {
562         return Long.parseLong(value);
563     }
564     
565     /**
566      * Returns a property as a long value.
567      *
568      * @param key the key
569      * @return the property value as a long
570      */

571     public static long getLongProperty (String JavaDoc key)
572     {
573         return asLong(props.getProperty(key));
574     }
575
576     /**
577      * Returns the value of a property as a long, looking first in the supplied properties map
578      * and then in the system properties.
579      *
580      * @param propertiesMap the properties map
581      * @param key the key
582      * @param defaultValue the default value used if the property is not found
583      * @return the property value
584      */

585     public static long getLongProperty (JRPropertiesMap propertiesMap, String JavaDoc key, int defaultValue)
586     {
587         String JavaDoc value = getProperty(propertiesMap, key);
588         
589         return value == null ? defaultValue : asLong(value);
590     }
591 }
592
Popular Tags