KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > PropertiesMerger


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16 package scriptella.configuration;
17
18 import scriptella.expression.PropertiesSubstitutor;
19 import scriptella.spi.ParametersCallback;
20 import scriptella.util.CollectionUtils;
21 import scriptella.util.PropertiesMap;
22
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 /**
27  * A merger class for external and local xml properties.
28  * <p>This class may be used as a {@link ParametersCallback}
29  * in properties substitution. Use {@link #getSubstitutor()} for substitution.
30  *
31  * @author Fyodor Kupolov
32  * @version 1.0
33  */

34 class PropertiesMerger implements ParametersCallback {
35     private PropertiesMap properties;
36     private PropertiesSubstitutor substitutor=new PropertiesSubstitutor(this);
37
38     public PropertiesMerger() {
39         properties = new PropertiesMap();
40     }
41
42     public PropertiesMerger(Properties JavaDoc properties) {
43         this.properties = new PropertiesMap(CollectionUtils.asMap(properties));
44     }
45
46     public PropertiesMerger(Map<String JavaDoc,?> properties) {
47         this.properties = new PropertiesMap(properties);
48     }
49
50     /**
51      * Adds properties and expands their values by evaluating expressions and property references.
52      *
53      * @param properties properties to add to scripts context.
54      * @see #getSubstitutor()
55      */

56     void addProperties(final Map<String JavaDoc, ?> properties) {
57         for (Map.Entry<String JavaDoc, ?> entry : properties.entrySet()) {
58             Object JavaDoc v = entry.getValue();
59             if (v instanceof CharSequence JavaDoc) {
60                 v = substitutor.substitute(v.toString());
61             }
62             this.properties.put(entry.getKey(), v);
63         }
64     }
65
66     public Object JavaDoc getParameter(final String JavaDoc name) {
67         return properties.get(name);
68     }
69
70     /**
71      * Returns a property substitutor which is using properties of this merger.
72      * <p>The later changes to this class properties are automatically visible to the returned substitutor.
73      * @return properties substitutor based on this merger properties.
74      */

75     public PropertiesSubstitutor getSubstitutor() {
76         return substitutor;
77     }
78
79
80     public String JavaDoc toString() {
81         return "PropertiesMerger{" +
82                 properties +
83                 '}';
84     }
85 }
86
Popular Tags