KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > config > PropertiesPlaceholderConfigurer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.beans.config;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import org.springframework.beans.propertyeditors.PropertiesEditor;
30 import org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer;
31
32 /**
33  * PropertyPlaceholderConfigurer that accepts wildcards to populate properties
34  * that expect a java.util.Properties value.
35  * <p>
36  * Example:
37  * <pre>
38  * &lt;bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"&gt;
39  * &lt;property name="freemarkerSettings" ref="${freemarker.*}" /&gt;
40  * &lt;/bean&gt;
41  * </pre>
42  *
43  * The configurer will look for all properties start start with
44  * '<code>freemarker.</code>'. So having a properties file like this ...
45  *
46  * <pre>
47  * freemarker.number_format = 0.######
48  * freemarker.locale = en_US
49  * </pre>
50  *
51  * ... would be equivalent to writing:
52  * <pre>
53  * &lt;bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"&gt;
54  * &lt;property name="freemarkerSettings"&gt;
55  * &lt;value&gt;
56  * number_format = 0.######
57  * locale = en_US
58  * &lt/value&gt;
59  * &lt;/property&gt;
60  * &lt;/bean&gt;
61  * </pre>
62  *
63  * Spring's {@link PropertiesEditor} will then take care of converting the
64  * String value into a <code>java.util.Properties</code> object.
65  *
66  * @author Felix Gnass [fgnass at neteye dot de]
67  * @since 6.4
68  */

69 public class PropertiesPlaceholderConfigurer extends
70         ServletContextPropertyPlaceholderConfigurer {
71     
72     protected String JavaDoc resolvePlaceholder(String JavaDoc placeholder, Properties JavaDoc props) {
73         int i = placeholder.indexOf('*');
74         if (i != -1) {
75             return resolveAll(props, placeholder.substring(0, i));
76         }
77         return super.resolvePlaceholder(placeholder, props);
78     }
79     
80     protected String JavaDoc resolveAll(Properties JavaDoc props, String JavaDoc prefix) {
81         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
82         Enumeration JavaDoc names = props.propertyNames();
83         while (names.hasMoreElements()) {
84             String JavaDoc name = (String JavaDoc) names.nextElement();
85             if (name.startsWith(prefix) && name.length() > prefix.length()) {
86                 sb.append(name.substring(prefix.length()));
87                 sb.append('=');
88                 sb.append(props.getProperty(name));
89                 sb.append('\n');
90             }
91         }
92         return sb.toString();
93     }
94 }
95
Popular Tags