001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.util;
019:
020: import java.util.Properties;
021:
022: /**
023: * This implementation supports parameters substitution in property value.
024: * @see #getProperty(String)
025: * @version $Id$
026: */
027: public class ExtendedProperties extends Properties {
028: private static final long serialVersionUID = 8904709563073950956L;
029:
030: /**
031: * @see java.util.Properties#Properties()
032: */
033: public ExtendedProperties() {
034: super ();
035: }
036:
037: /**
038: * @see java.util.Properties#Properties(java.util.Properties)
039: */
040: public ExtendedProperties(Properties defs) {
041: super (defs);
042: }
043:
044: /**
045: * Any parameter like <code>${propertyName}</code> in property value will
046: * be replaced with the value of property with name
047: * <code>propertyName</code>.
048: * <p>For example, for the following set of
049: * properties:
050: * <pre>
051: * param1=abcd
052: * param2=efgh
053: * param3=Alphabet starts with: ${param1}${param2}
054: * </pre>
055: * The call <code>props.getProperty("param3")</code> returns:
056: * <pre>Alphabet starts with: abcdefgh</pre>
057: * Note also that call <code>props.get("param3")</code> returns:
058: * <pre>Alphabet starts with: ${param1}${param2}</pre>
059: * So the {@link java.util.Map#get(java.lang.Object)} works as usual and
060: * returns raw (not expanded with substituted parameters) property value.
061: * </p>
062: * @see java.util.Properties#getProperty(java.lang.String)
063: */
064: @Override
065: public String getProperty(String key) {
066: String result = super .getProperty(key);
067: return (result == null) ? null : expandValue(result);
068: }
069:
070: /**
071: * @see java.util.Properties#getProperty(java.lang.String, java.lang.String)
072: */
073: @Override
074: public String getProperty(String key, String defaultValue) {
075: String result = getProperty(key);
076: return (result == null) ? expandValue(defaultValue) : result;
077: }
078:
079: /**
080: * @param prefix string, each property key should start with (this prefix
081: * will NOT be included into new key)
082: * @return sub-properties
083: */
084: public ExtendedProperties getSubset(final String prefix) {
085: return getSubset(prefix, ""); //$NON-NLS-1$
086: }
087:
088: /**
089: * @param prefix string, each property key should start with
090: * @param newPrefix new prefix to be added to each key instead of existing
091: * prefix
092: * @return sub-properties
093: */
094: public ExtendedProperties getSubset(final String prefix,
095: final String newPrefix) {
096: ExtendedProperties result = new ExtendedProperties();
097: for (Object object : keySet()) {
098: String key = object.toString();
099: 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 expandValue(final String value) {
109: if ((value == null) || (value.length() < 4)) {
110: return value;
111: }
112: StringBuilder result = new StringBuilder(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 paramName = result.substring(p1 + 2, p2);
118: String 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: }
|