View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program 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
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.config;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  /**
24   * The base class for generic configurations. It is a hierarchical structure of
25   * configuration entries, where entries can be added, inherited and overridden.
26   * In addition to {@link GenericConfig} it is possible to get all configs
27   * starting with a specified prefix.
28   *
29   * This can be useful for configs like
30   * ch.elca.el4j.a = aaa
31   * ch.elca.el4j.b = bbb
32   * where you can use <code>getSubConfig("ch.elca.el4j")</code>.
33   *
34   *
35   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/config/HierarchicalGenericConfig.java $
36   *
37   * @author Stefan Wismer (SWI)
38   */
39  public class HierarchicalGenericConfig extends GenericConfig {
40  	/**
41  	 * The prefix for this config (used in subconfigs).
42  	 */
43  	protected String m_prefix = "";
44  	
45  	/** {@inheritDoc} */
46  	@Override
47  	public Map<String, Object> getMap() {
48  		Map<String, Object> map = new HashMap<String, Object>();
49  		
50  		for (String key : m_map.keySet()) {
51  			if (key.startsWith("#")) {
52  				HierarchicalGenericConfig child = (HierarchicalGenericConfig)
53  					m_map.get(key);
54  				map.putAll(child.getMap());
55  			} else {
56  				map.put(m_prefix + key, m_map.get(key));
57  			}
58  		}
59  		return map;
60  	}
61  	
62  	/** {@inheritDoc} */
63  	@Override
64  	public void setMap(Map<String, Object> map) {
65  		m_map.clear();
66  		setOverrideMap(map);
67  	}
68  	
69  	/** {@inheritDoc} */
70  	@Override
71  	public void setOverrideMap(Map<String, Object> map) {
72  		for (String key : map.keySet()) {
73  			add(key, map.get(key));
74  		}
75  	}
76  	
77  	/** {@inheritDoc} */
78  	@Override
79  	public void setOverrideMap(Properties properties) {
80  		for (Object key : properties.keySet()) {
81  			add((String) key, properties.get(key));
82  		}
83  	}
84  	
85  	/** {@inheritDoc} */
86  	@Override
87  	public void add(String key, Object value) {
88  		if (key.contains(".")) {
89  			String childKey = key.substring(0, key.indexOf("."));
90  			HierarchicalGenericConfig child = getSubConfig(childKey);
91  			
92  			// create child if it doesn't exist yet
93  			if (child == null) {
94  				child = new HierarchicalGenericConfig();
95  				child.setPrefix(m_prefix + childKey + ".");
96  			}
97  			
98  			child.add(key.substring(key.indexOf(".") + 1), value);
99  			
100 			m_map.put("#" + childKey, child);
101 		} else {
102 			m_map.put(key, value);
103 		}
104 	}
105 	
106 	/** {@inheritDoc} */
107 	@Override
108 	public Object get(String key) {
109 		if (key.contains(".")) {
110 			HierarchicalGenericConfig child = (HierarchicalGenericConfig)
111 				m_map.get("#" + key.substring(0, key.indexOf(".")));
112 			return child.get(key.substring(key.indexOf(".") + 1));
113 		} else {
114 			return m_map.get(key);
115 		}
116 	}
117 	
118 	/**
119 	 * @param prefix    the prefix of the all configuration entries to get
120 	 * @return          all entries having the specified prefix
121 	 */
122 	public HierarchicalGenericConfig getSubConfig(String prefix) {
123 		if (prefix.contains(".")) {
124 			HierarchicalGenericConfig child = (HierarchicalGenericConfig)
125 				m_map.get("#" + prefix.substring(0, prefix.indexOf(".")));
126 			return child.getSubConfig(
127 				prefix.substring(prefix.indexOf(".") + 1));
128 		} else {
129 			return (HierarchicalGenericConfig) m_map.get("#" + prefix);
130 		}
131 	}
132 	
133 	/**
134 	 * @return    all configuration children
135 	 */
136 	public Map<String, Object> getChildren() {
137 		Map<String, Object> map = new HashMap<String, Object>();
138 		
139 		for (String key : m_map.keySet()) {
140 			if (key.startsWith("#")) {
141 				map.put(key.substring(1), m_map.get(key));
142 			} else {
143 				map.put(key, m_map.get(key));
144 			}
145 		}
146 		
147 		return map;
148 	}
149 	
150 	/** {@inheritDoc} */
151 	@Override
152 	public String toString() {
153 		StringBuffer buffer = new StringBuffer();
154 		Map<String, Object> map = getMap();
155 		for (String key : map.keySet()) {
156 			buffer.append(key);
157 			buffer.append(" = ");
158 			buffer.append(map.get(key));
159 			buffer.append(System.getProperty("line.separator"));
160 		}
161 		return buffer.toString();
162 	}
163 
164 	/**
165 	 * @return Returns the prefix.
166 	 */
167 	public String getPrefix() {
168 		return m_prefix;
169 	}
170 
171 	/**
172 	 * @param prefix Is the prefix to set.
173 	 */
174 	public void setPrefix(String prefix) {
175 		m_prefix = prefix;
176 	}
177 }