Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5827 amar.kumar 1
/*
2
 *	Copyright 2005 stat4j.org
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *	You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.sourceforge.stat4j.util;
18
 
19
import java.util.Enumeration;
20
import java.util.MissingResourceException;
21
import java.util.Properties;
22
import java.util.ResourceBundle;
23
 
24
/**
25
 * Name:		Util.java
26
 * Date:		Sep 4, 2004
27
 * Description:
28
 * 
29
 * 
30
 * @author Lara D'Abreo
31
 */
32
public class Util {
33
 
34
	public static ResourceBundle bundle =
35
		ResourceBundle.getBundle("stat4j");
36
 
37
	public static Object createObject(String className) {
38
		try {
39
			Class c = Class.forName(className);
40
 
41
			return c.newInstance();
42
 
43
		} catch (Exception e) {
44
 
45
			return null;
46
		}
47
	}
48
 
49
	public static String getCategory() {
50
		return bundle.containsKey("logcategory") ? bundle.getString("logcategory") : "stat4j";
51
	}
52
 
53
	public static Properties createProperties(String context) {
54
		Properties p = new Properties();
55
 
56
		Enumeration keys = bundle.getKeys();
57
 
58
		while (keys.hasMoreElements()) {
59
			String key = (String) keys.nextElement();
60
			if (key.startsWith(context)) {
61
				String k = key.substring(context.length());
62
				String value = bundle.getString(key);
63
 
64
				p.put(k, value);
65
 
66
			} //fi
67
		} //end while
68
 
69
		return p;
70
	}
71
 
72
	public static String getValue(String key) {
73
		try {
74
			return bundle.getString(key);
75
		}catch (MissingResourceException e) {
76
			return null;
77
		}
78
	}
79
 
80
	public static boolean getAsBoolean(String val, boolean dfault) {
81
		try {
82
			if (val == null) {
83
				return dfault;
84
			}
85
			String s = val.trim().toLowerCase();
86
			if (s.length() == 0) {
87
				return dfault;
88
			};
89
			return (
90
				s.equals("true")
91
					|| s.equals("yes")
92
					|| s.equals("T")
93
					|| s.equals("Y"));
94
		} catch (Exception e) {
95
			return dfault;
96
		}
97
	}
98
 
99
}