001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.util;
021:
022: import java.io.IOException;
023: import java.util.Comparator;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.Properties;
027: import java.util.TreeMap;
028:
029: import org.apache.log4j.Logger;
030:
031: /**
032: * Insert the type's description here.
033: * Creation date: (05/10/00 18:21:36)
034: * @author Michael Salzmann
035: */
036:
037: public class PropertiesUtils {
038: /** Logging. */
039: protected final static Logger LOG = Logger
040: .getLogger(PropertiesUtils.class);
041:
042: /** Default constructor. */
043: protected PropertiesUtils() {
044: super ();
045: }
046:
047: /**
048: * find all properties whose name starts with the given prefix,
049: * strip the prefix from the name, and put them into a hashmap with
050: * their new, shortened name. Example: prefix is "foo", property name
051: * is "foo.bar" -> will result in new property name "bar".
052: *
053: * Creation date: (05/09/00 17:53:25)
054: * @return HashMap
055: */
056: public static HashMap<String, String> selectProperties(
057: Properties props, String prefix) {
058: String p;
059: Enumeration<?> enm;
060: HashMap<String, String> result = new HashMap<String, String>();
061:
062: prefix += '.';
063: enm = props.propertyNames();
064: while (enm.hasMoreElements()) {
065: p = (String) enm.nextElement();
066: if (p.startsWith(prefix)) {
067: String suffix = p
068: .substring(prefix.length(), p.length());
069: result.put(suffix, props.getProperty(p));
070: }
071: }
072:
073: return result;
074: }
075:
076: /**
077: * Select the Properties with the given prefix and return them in a
078: * TreeMap, using the natural Ordering.
079: * @return com.sun.java.util.TreeMap. If prefix is null or 0 <
080: * prefix.length() return all Properties sorted. If props is null
081: * return an empty TreeMap
082: */
083: public static TreeMap<String, String> selectPropertiesSorted(
084: Properties props, String prefix) {
085: return selectPropertiesSorted(null, props, prefix);
086: }
087:
088: /**
089: * Select the Properties with the given prefix and return them in a
090: * TreeMap, using the given Comparator for Ordering.
091: * If the Comparator is null, the natural Ordering is used.
092: * @return com.sun.java.util.TreeMap. If prefix is null or 0 <
093: * prefix.length() return all Properties sorted. If props is null
094: * return an empty TreeMap
095: */
096: public static TreeMap<String, String> selectPropertiesSorted(
097: Comparator<String> comp, Properties props, String prefix) {
098: TreeMap<String, String> rc = null;
099: if (comp != null) {
100: rc = new TreeMap<String, String>(comp);
101: } else {
102: rc = new TreeMap<String, String>();
103: }
104: if (props != null) {
105: if (prefix != null && 0 < prefix.length()) {
106: String dottedPrefix = prefix + '.';
107: Enumeration<?> enm = props.propertyNames();
108: String pKey = null;
109: String newKey = null;
110: while (enm.hasMoreElements()) {
111: pKey = (String) enm.nextElement();
112: if (pKey.startsWith(dottedPrefix)) {
113: try {
114: newKey = pKey.substring(dottedPrefix
115: .length(), pKey.length());
116: rc.put(newKey, props.getProperty(pKey));
117: } catch (IndexOutOfBoundsException e) {
118: }
119: }
120: }
121: } else {
122: Enumeration<?> enm = props.propertyNames();
123: while (enm.hasMoreElements()) {
124: String key = (String) enm.nextElement();
125: String value = props.getProperty(key);
126: rc.put(key, value);
127: }
128: }
129: }
130: return rc;
131: }
132:
133: public static int getInteger(Properties props, String key)
134: throws IOException {
135: String value;
136:
137: value = getString(props, key);
138: try {
139: return Integer.parseInt(value);
140: } catch (NumberFormatException e) {
141: throw new IOException("number expected for property '"
142: + key + "': " + value);
143: }
144: }
145:
146: public static String getString(Properties props, String key)
147: throws IOException {
148: String value;
149:
150: value = props.getProperty(key);
151: if (value == null) {
152: throw new IOException("property not found: " + key);
153: }
154: return value;
155: }
156: }
|