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) 2006 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.services.xmlmerge.config;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.LinkedHashSet;
24  import java.util.Map;
25  import java.util.Properties;
26  import java.util.Set;
27  
28  import ch.elca.el4j.services.xmlmerge.ConfigurationException;
29  
30  /**
31   * Reads the {@link ch.elca.el4j.services.xmlmerge.factory.XPathOperationFactory}
32   * configuration from a property file or a map.
33   *
34   * @svnLink $Revision: 3883 $;$Date: 2009-08-04 15:35:01 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/xml_merge/common/src/main/java/ch/elca/el4j/services/xmlmerge/config/PropertyXPathConfigurer.java $
35   *
36   * @author Laurent Bovet (LBO)
37   * @author Alex Mathey (AMA)
38   */
39  public class PropertyXPathConfigurer extends AbstractXPathConfigurer {
40  
41  	/**
42  	 * Default action pathname.
43  	 */
44  	public static final String DEFAULT_ACTION_KEY = "action.default";
45  
46  	/**
47  	 * Default mapper pathname.
48  	 */
49  	public static final String DEFAULT_MAPPER_KEY = "mapper.default";
50  
51  	/**
52  	 * Default matcher pathname.
53  	 */
54  	public static final String DEFAULT_MATCHER_KEY = "matcher.default";
55  
56  	/**
57  	 * XPath pathname prefix.
58  	 */
59  	public static final String PATH_PREFIX = "xpath.";
60  
61  	/**
62  	 * Mapper pathname prefix.
63  	 */
64  	public static final String MAPPER_PREFIX = "mapper.";
65  
66  	/**
67  	 * Matcher pathname prefix.
68  	 */
69  	public static final String MATCHER_PREFIX = "matcher.";
70  
71  	/**
72  	 * Action pathname prefix.
73  	 */
74  	public static final String ACTION_PREFIX = "action.";
75  	
76  	/**
77  	 * Configuration properties.
78  	 */
79  	Properties m_props;
80  
81  	/**
82  	 * Set of XPath paths.
83  	 */
84  	Set m_paths = new LinkedHashSet();
85  
86  	/**
87  	 * Creates a PropertyXPathConfigurer which reads the configuration from a
88  	 * properties file.
89  	 *
90  	 * @param propString
91  	 *            A string representing the name of a properties file
92  	 * @throws ConfigurationException
93  	 *             If an error occurred during the creation of the
94  	 *             configurer
95  	 */
96  	public PropertyXPathConfigurer(String propString)
97  		throws ConfigurationException {
98  		m_props = new Properties();
99  		try {
100 			m_props.load(new ByteArrayInputStream(propString.getBytes()));
101 		} catch (IOException ioe) {
102 			// Should not happen
103 			throw new ConfigurationException(ioe);
104 		}
105 	}
106 
107 	/**
108 	 * Creates a PropertyXPathConfigurer which reads the configuration from a
109 	 * map.
110 	 *
111 	 * @param map
112 	 *            A map containing configuration properties
113 	 */
114 	public PropertyXPathConfigurer(Map map) {
115 		m_props = new Properties();
116 		m_props.putAll(map);
117 	}
118 
119 	/**
120 	 * Creates a PropertyXPathConfigurer which reads the configuration from a
121 	 * <code>Properties</code> object.
122 	 *
123 	 * @param properties
124 	 *            The configuration properties
125 	 */
126 	public PropertyXPathConfigurer(Properties properties) {
127 		m_props = properties;
128 	}
129 
130 	/**
131 	 * {@inheritDoc}
132 	 */
133 	protected void readConfiguration() throws ConfigurationException {
134 		String token;
135 
136 		token = m_props.getProperty(DEFAULT_ACTION_KEY);
137 		if (token != null) {
138 			setDefaultAction(token);
139 		}
140 
141 		token = m_props.getProperty(DEFAULT_MAPPER_KEY);
142 		if (token != null) {
143 			setDefaultMapper(token);
144 		}
145 
146 		token = m_props.getProperty(DEFAULT_MATCHER_KEY);
147 		if (token != null) {
148 			setDefaultMatcher(token);
149 		}
150 
151 		Enumeration keys = m_props.keys();
152 
153 		while (keys.hasMoreElements()) {
154 			String key = (String) keys.nextElement();
155 
156 			if (key.startsWith(PATH_PREFIX)) {
157 				m_paths.add(key.substring(PATH_PREFIX.length()));
158 			}
159 		}
160 
161 		Iterator it = m_paths.iterator();
162 		while (it.hasNext()) {
163 			String path = (String) it.next();
164 
165 			token = m_props.getProperty(ACTION_PREFIX + path);
166 			if (token != null) {
167 				addAction(m_props.getProperty(PATH_PREFIX + path), token);
168 			}
169 			token = m_props.getProperty(MAPPER_PREFIX + path);
170 			if (token != null) {
171 				addMapper(m_props.getProperty(PATH_PREFIX + path), token);
172 			}
173 			token = m_props.getProperty(MATCHER_PREFIX + path);
174 			if (token != null) {
175 				addMatcher(m_props.getProperty(PATH_PREFIX + path), token);
176 			}
177 		}
178 
179 	}
180 
181 }