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) 2009 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.env.xml.handlers;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.springframework.core.io.Resource;
24  import org.xml.sax.Attributes;
25  import org.xml.sax.helpers.DefaultHandler;
26  
27  import ch.elca.el4j.env.InvalidEnvXmlContentException;
28  import ch.elca.el4j.env.xml.ResolverUtils;
29  
30  /**
31   * An {@link EnvGroupHandler} that handles inheritable properties (which can also be abstract or final).
32   *
33   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/env/src/main/java/ch/elca/el4j/env/xml/handlers/AbstractInheritablePropertyHandler.java $
34   *
35   * @author Stefan Wismer (SWI)
36   */
37  public abstract class AbstractInheritablePropertyHandler extends DefaultHandler implements EnvGroupHandler {
38  	/**
39  	 * The ordinary properties.
40  	 */
41  	protected Properties m_properties = new Properties();
42  	
43  	/**
44  	 * The abstract property names and the resource they are declared.
45  	 */
46  	protected Map<String, Resource> m_abstractProperties = new HashMap<String, Resource>();
47  	
48  	/**
49  	 * The final property names and the resource they are declared.
50  	 */
51  	protected Map<String, Resource> m_finalProperties = new HashMap<String, Resource>();
52  	
53  	/**
54  	 * The resource currently being propcessed. 
55  	 */
56  	protected Resource m_currentResource;
57  	
58  	/** {@inheritDoc} */
59  	public void startResource(Resource resource) {
60  		m_currentResource = resource;
61  	}
62  	
63  	/**
64  	 * Add a property given by an xml tag having the attributes 'name' (required), 'value' and 'type'.
65  	 * @param attributes    the tag attributes
66  	 */
67  	protected void addProperty(Attributes attributes) {
68  		String entryName = attributes.getValue("name");
69  		String entryValue = attributes.getValue("value");
70  		String entryType = attributes.getValue("type");
71  		
72  		if (entryValue == null) {
73  			entryValue = "${" + entryName + "}";
74  		}
75  		
76  		if ("abstract".equalsIgnoreCase(entryType)) {
77  			m_abstractProperties.put(entryName, m_currentResource);
78  		} else {
79  			if ("final".equalsIgnoreCase(entryType)) {
80  				m_properties.put(entryName, entryValue);
81  				m_finalProperties.put(entryName, m_currentResource);
82  			} else {
83  				if (m_finalProperties.containsKey(entryName)) {
84  					throw new InvalidEnvXmlContentException(
85  						"It is not allowed to overwrite final property '" + entryName + "' in "
86  						+ m_currentResource.toString()
87  						+ ".\nAlternatively, you might want to recompile artifact containing '"
88  						+ m_finalProperties.get(entryName).toString());
89  				}
90  			}
91  			m_abstractProperties.remove(entryName);
92  			m_properties.put(entryName, entryValue);
93  		}
94  	}
95  	
96  	/**
97  	 * Remove a property given by an xml tag having the attribute 'name' (required).
98  	 * @param attributes    the tag attributes
99  	 */
100 	protected void removeProperty(Attributes attributes) {
101 		String entryName = attributes.getValue("name");
102 		m_properties.remove(entryName);
103 		m_abstractProperties.remove(entryName);
104 		m_finalProperties.remove(entryName);
105 	}
106 	
107 	/** {@inheritDoc} */
108 	public void filterData(Properties properties) {
109 		for (Object objectKey : m_properties.keySet()) {
110 			String key = (String) objectKey;
111 			String value = m_properties.getProperty(key);
112 			
113 			String resolvedValue;
114 			if (m_finalProperties.containsKey(key)) {
115 				resolvedValue = properties.getProperty(key);
116 			} else {
117 				resolvedValue = ResolverUtils.resolve(value, properties);
118 				
119 				// add property to resolved properties map
120 				properties.setProperty(key, resolvedValue);
121 			}
122 			m_properties.setProperty(key, resolvedValue);
123 		}
124 	}
125 	
126 	
127 	/** {@inheritDoc} */
128 	public Object getData() {
129 		// env plugin already warns if it detects abstract properties
130 		/*if (!m_abstractProperties.isEmpty()) {
131 			for (String abstractProperty : m_abstractProperties.keySet()) {
132 				s_logger.warn(
133 					"Abstract property '" + abstractProperty + "' defined in '"
134 					+ m_abstractProperties.get(abstractProperty).toString() + "' must be set.");
135 			}
136 		}*/
137 		return m_properties;
138 	}
139 }