KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > PropertiesHelper


1 //$Id: PropertiesHelper.java,v 1.3 2004/08/10 05:06:14 oneovthafew Exp $
2
package org.hibernate.util;
3
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9
10 public final class PropertiesHelper {
11
12     public static boolean getBoolean(String JavaDoc property, Properties JavaDoc properties) {
13         return Boolean.valueOf( properties.getProperty(property) ).booleanValue();
14     }
15
16     public static boolean getBoolean(String JavaDoc property, Properties JavaDoc properties, boolean defaultValue) {
17         String JavaDoc setting = properties.getProperty(property);
18         return (setting==null) ? defaultValue : Boolean.valueOf(setting).booleanValue();
19     }
20
21     public static int getInt(String JavaDoc property, Properties JavaDoc properties, int defaultValue) {
22         String JavaDoc propValue = properties.getProperty(property);
23         return (propValue==null) ? defaultValue : Integer.parseInt(propValue);
24     }
25
26     public static String JavaDoc getString(String JavaDoc property, Properties JavaDoc properties, String JavaDoc defaultValue) {
27         String JavaDoc propValue = properties.getProperty(property);
28         return (propValue==null) ? defaultValue : propValue;
29     }
30
31     public static Integer JavaDoc getInteger(String JavaDoc property, Properties JavaDoc properties) {
32         String JavaDoc propValue = properties.getProperty(property);
33         return (propValue==null) ? null : Integer.valueOf(propValue);
34     }
35
36     public static Map JavaDoc toMap(String JavaDoc property, String JavaDoc delim, Properties JavaDoc properties) {
37         Map JavaDoc map = new HashMap JavaDoc();
38         String JavaDoc propValue = properties.getProperty(property);
39         if (propValue!=null) {
40             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(propValue, delim);
41             while ( tokens.hasMoreTokens() ) {
42                 map.put(
43                     tokens.nextToken(),
44                     tokens.hasMoreElements() ? tokens.nextToken() : ""
45                 );
46             }
47         }
48         return map;
49     }
50
51     public static String JavaDoc[] toStringArray(String JavaDoc property, String JavaDoc delim, Properties JavaDoc properties) {
52         return toStringArray( properties.getProperty(property), delim );
53     }
54
55     public static String JavaDoc[] toStringArray(String JavaDoc propValue, String JavaDoc delim) {
56         if (propValue!=null) {
57             return StringHelper.split(delim, propValue);
58         }
59         else {
60             return ArrayHelper.EMPTY_STRING_ARRAY;
61         }
62     }
63     
64     /**
65      * replace a property by a starred version
66      *
67      * @param props properties to check
68      * @param key proeprty to mask
69      * @return cloned and masked properties
70      */

71     public static Properties JavaDoc maskOut(Properties JavaDoc props, String JavaDoc key) {
72         Properties JavaDoc clone = (Properties JavaDoc) props.clone();
73         if (clone.get(key) != null) {
74             clone.setProperty(key, "****");
75         }
76         return clone;
77     }
78     
79
80
81     private PropertiesHelper() {}
82 }
83
84
85
86
87
88
89
Popular Tags