KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > PropertyExpander


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.util;
20
21 import java.util.Map JavaDoc;
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 /**
26  * Property expansion utility. This utility provides static methods to expand properties appearing in strings.
27  *
28  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a> (Portions based on code from David Graff submitted for
29  * ROL-613)
30  * @since Roller 1.3
31  */

32 public class PropertyExpander {
33     private PropertyExpander() {
34     }
35
36     // The pattern for a system property. Matches ${property.name}, with the interior matched reluctantly.
37
private static final Pattern JavaDoc EXPANSION_PATTERN = Pattern.compile("(\\$\\{([^}]+?)\\})", java.util.regex.Pattern.MULTILINE);
38
39     /**
40      * Expand property expressions in the input. Expands property expressions of the form <code>${propertyname}</code>
41      * in the input, replacing each such expression with the value associated to the respective key
42      * <code>propertyname</code> in the supplied map. If for a given expression, the property is undefined (has null
43      * value) in the map supplied, that expression is left unexpanded in the resulting string.
44      * <p/>
45      * Note that expansion is not recursive. If the value of one property contains another expression, the expression
46      * appearing in the value will not be expanded further.
47      *
48      * @param input the input string. This may be null, in which case null is returned.
49      * @param props the map of property values to use for expansion. This map should have <code>String</code> keys and
50      * <code>String</code> values. Any object of class {@link java.util.Properties} works here, as will
51      * other implementations of such maps.
52      * @return the result of replacing property expressions with the values of the corresponding properties from the
53      * supplied property map, null if the input string is null.
54      */

55     public static String JavaDoc expandProperties(String JavaDoc input, Map JavaDoc props) {
56         if (input == null) return null;
57
58         Matcher JavaDoc matcher = EXPANSION_PATTERN.matcher(input);
59
60         StringBuffer JavaDoc expanded = new StringBuffer JavaDoc(input.length());
61         while (matcher.find()) {
62             String JavaDoc propName = matcher.group(2);
63             String JavaDoc value = (String JavaDoc) props.get(propName);
64             // if no value is found, use a value equal to the original expression
65
if (value == null) value = matcher.group(0);
66             // Fake a literal replacement since Matcher.quoteReplacement() is not present in 1.4.
67
matcher.appendReplacement(expanded, "");
68             expanded.append(value);
69         }
70         matcher.appendTail(expanded);
71
72         return expanded.toString();
73     }
74
75     /**
76      * Expand system properties in the input string. This is equivalent to calling <code>expandProperties(input,
77      * System.getProperties())</code>.
78      *
79      * @param input
80      * @return the result of replacing property expressions with the values of the corresponding system properties.
81      * @see System#getProperties()
82      */

83     public static String JavaDoc expandSystemProperties(String JavaDoc input) {
84         return expandProperties(input, System.getProperties());
85     }
86 }
87
Popular Tags