1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37 public abstract class AbstractInheritablePropertyHandler extends DefaultHandler implements EnvGroupHandler {
38
39
40
41 protected Properties m_properties = new Properties();
42
43
44
45
46 protected Map<String, Resource> m_abstractProperties = new HashMap<String, Resource>();
47
48
49
50
51 protected Map<String, Resource> m_finalProperties = new HashMap<String, Resource>();
52
53
54
55
56 protected Resource m_currentResource;
57
58
59 public void startResource(Resource resource) {
60 m_currentResource = resource;
61 }
62
63
64
65
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
98
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
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
120 properties.setProperty(key, resolvedValue);
121 }
122 m_properties.setProperty(key, resolvedValue);
123 }
124 }
125
126
127
128 public Object getData() {
129
130
131
132
133
134
135
136
137 return m_properties;
138 }
139 }