KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > util > ExtendedProperties


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2005 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.util;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 /**
25  * This implementation supports parameters substitution in property value.
26  * @see #getProperty(String)
27  * @version $Id: ExtendedProperties.java,v 1.1 2005/12/03 16:04:04 ddimon Exp $
28  */

29 public class ExtendedProperties extends Properties JavaDoc {
30     private static final long serialVersionUID = 8904709563073950956L;
31
32     /**
33      * @see java.util.Properties#Properties()
34      */

35     public ExtendedProperties() {
36         super();
37     }
38
39     /**
40      * @see java.util.Properties#Properties(java.util.Properties)
41      */

42     public ExtendedProperties(Properties JavaDoc defs) {
43         super(defs);
44     }
45
46     /**
47      * Any parameter like <code>${propertyName}</code> in property value will
48      * be replaced with the value of property with name
49      * <code>propertyName</code>.
50      * <p>For example, for the following set of
51      * properties:
52      * <pre>
53      * param1=abcd
54      * param2=efgh
55      * param3=Alphabet starts with: ${param1}${param2}
56      * </pre>
57      * The call <code>props.getProperty("param3")</code> returns:
58      * <pre>Alphabet starts with: abcdefgh</pre>
59      * Note also that call <code>props.get("param3")</code> returns:
60      * <pre>Alphabet starts with: ${param1}${param2}</pre>
61      * So the {@link java.util.Map#get(java.lang.Object)} works as usual and
62      * returns raw (not expanded with substituted parameters) property value.
63      * </p>
64      * @see java.util.Properties#getProperty(java.lang.String)
65      */

66     public String JavaDoc getProperty(String JavaDoc key) {
67         String JavaDoc result = super.getProperty(key);
68         return (result == null) ? null : expandValue(result);
69     }
70     
71     /**
72      * @see java.util.Properties#getProperty(java.lang.String, java.lang.String)
73      */

74     public String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue) {
75         String JavaDoc result = getProperty(key);
76         return (result == null) ? expandValue(defaultValue) : result;
77     }
78     
79     /**
80      * @param prefix string, each property key should start with (this prefix
81      * will NOT be included into new key)
82      * @return sub-properties
83      */

84     public ExtendedProperties getSubset(final String JavaDoc prefix) {
85         return getSubset(prefix, ""); //$NON-NLS-1$
86
}
87     
88     /**
89      * @param prefix string, each property key should start with
90      * @param newPrefix new prefix to be added to each key instead of existing
91      * prefix
92      * @return sub-properties
93      */

94     public ExtendedProperties getSubset(final String JavaDoc prefix,
95             final String JavaDoc newPrefix) {
96         ExtendedProperties result = new ExtendedProperties();
97         for (Iterator JavaDoc it = keySet().iterator(); it.hasNext();) {
98             String JavaDoc key = it.next().toString();
99             if (!key.startsWith(prefix) || key.equals(prefix)) {
100                 continue;
101             }
102             result.put(key.substring(prefix.length()) + newPrefix,
103                     getProperty(key));
104         }
105         return result;
106     }
107     
108     private String JavaDoc expandValue(final String JavaDoc value) {
109         if ((value == null) || (value.length() < 4)) {
110             return value;
111         }
112         StringBuffer JavaDoc result = new StringBuffer JavaDoc(value.length());
113         result.append(value);
114         int p1 = result.indexOf("${"); //$NON-NLS-1$
115
int p2 = result.indexOf("}", p1 + 2); //$NON-NLS-1$
116
while ((p1 >= 0) && (p2 > p1)) {
117             String JavaDoc paramName = result.substring(p1 + 2, p2);
118             String JavaDoc paramValue = getProperty(paramName);
119             if (paramValue != null) {
120                 result.replace(p1, p2 + 1, paramValue);
121                 p1 += paramValue.length();
122             } else {
123                 p1 = p2 + 1;
124             }
125             p1 = result.indexOf("${", p1); //$NON-NLS-1$
126
p2 = result.indexOf("}", p1 + 2); //$NON-NLS-1$
127
}
128         return result.toString();
129     }
130 }
131
Popular Tags