comparison flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java @ 1190:f514894ec2fd

merged flys-artifacts/2.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:17 +0200
parents 88a669785863
children d2a17e990c70
comparison
equal deleted inserted replaced
917:b48c36076e17 1190:f514894ec2fd
1 package de.intevation.flys.themes;
2
3 import java.util.Map;
4
5 import javax.xml.xpath.XPathConstants;
6
7 import org.apache.log4j.Logger;
8
9 import org.w3c.dom.Document;
10 import org.w3c.dom.NamedNodeMap;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14 import de.intevation.artifacts.common.utils.XMLUtils;
15
16 import de.intevation.flys.artifacts.context.FLYSContext;
17
18
19 /**
20 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
21 */
22 public class ThemeFactory {
23
24 private static Logger logger = Logger.getLogger(ThemeFactory.class);
25
26 private ThemeFactory() {
27 }
28
29
30 /**
31 * Creates a new theme from <i>config</i>.
32 *
33 * @param themeCfg The theme config document that is used to fetch parent
34 * themes.
35 * @param config The theme config node.
36 *
37 * @return a new theme.
38 */
39 public static Theme createTheme(Document themeCfg, Node config) {
40 String name = getName(config);
41 String desc = getDescription(config);
42
43 logger.debug("Create new theme: " + name);
44
45 Theme theme = new DefaultTheme(name, desc);
46
47 parseInherits(themeCfg, config, theme);
48 parseFields(config, theme);
49 parseAttrs(config, theme);
50
51 return theme;
52 }
53
54
55 /**
56 * Returns the theme for a specified output type and facet.
57 *
58 * @param c The FLYSContext that stores the mappings and themes.
59 * @param name The name of the mapping.
60 *
61 * @return a theme.
62 */
63 public static Theme getTheme(FLYSContext c, String name) {
64 if (c == null || name == null) {
65 logger.warn("Cannot search for theme.");
66 return null;
67 }
68
69 Map<String, String> map = (Map<String, String>)
70 c.get(FLYSContext.THEME_MAPPING);
71
72 Map<String, Theme> t = (Map<String, Theme>)
73 c.get(FLYSContext.THEMES);
74
75 if (map == null || map.size() == 0 || t == null || t.size() == 0) {
76 logger.warn("No mappings or themes found. Cannot retrieve theme!");
77 return null;
78 }
79
80 String themeName = map.get(name);
81
82 if (themeName == null) {
83 logger.warn("No theme found for mapping: " + name);
84 }
85
86 return t.get(themeName);
87 }
88
89
90 protected static String getName(Node config) {
91 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING);
92 }
93
94
95 protected static String getDescription(Node config) {
96 return (String) XMLUtils.xpath(config, "@desc", XPathConstants.STRING);
97 }
98
99
100 protected static void parseInherits(Document themeCfg, Node cfg, Theme t) {
101 logger.debug("ThemeFactory.parseInherits");
102
103 NodeList inherits = (NodeList) XMLUtils.xpath(
104 cfg, "inherits/inherit", XPathConstants.NODESET);
105
106 int num = inherits != null ? inherits.getLength() : 0;
107
108 if (num == 0) {
109 logger.debug("Theme does not inherit from other themes.");
110 return;
111 }
112
113 logger.debug("Theme inherits from " + num + " other themes.");
114
115 for (int i = 0; i < num; i++) {
116 Node inherit = inherits.item(i);
117 String from = (String) XMLUtils.xpath(
118 inherit, "@from", XPathConstants.STRING);
119
120 Node tmp = getThemeNode(themeCfg, from);
121
122 parseInherits(themeCfg, tmp, t);
123 parseFields(tmp, t);
124 }
125 }
126
127
128 protected static Node getThemeNode(Document themeCfg, String name) {
129 if (name == null) {
130 logger.warn("Cannot search theme config without name!");
131 return null;
132 }
133
134 logger.debug("Search for theme: " + name);
135
136 String xpath = "/themes/theme[@name='" + name + "']";
137
138 return (Node) XMLUtils.xpath(themeCfg, xpath, XPathConstants.NODE);
139 }
140
141
142 protected static void parseFields(Node config, Theme theme) {
143 if (config == null || theme == null) {
144 logger.warn("Parsing fields without node or theme is senseless!");
145 return;
146 }
147
148 NodeList fields = (NodeList) XMLUtils.xpath(
149 config, "fields/field", XPathConstants.NODESET);
150
151 int num = fields != null ? fields.getLength() : 0;
152
153 logger.debug("Found " + num + " own fields in this theme.");
154
155 if (num == 0) {
156 logger.debug("Theme has no own fields.");
157 return;
158 }
159
160 for (int i = 0; i < num; i++) {
161 Node field = fields.item(i);
162
163 addField(theme, field);
164 }
165 }
166
167
168 protected static void addField(Theme theme, Node field) {
169 String name = (String) XMLUtils.xpath(
170 field, "@name", XPathConstants.STRING);
171
172 logger.debug("Add field: " + name);
173
174 NamedNodeMap attrs = field.getAttributes();
175
176 int num = attrs != null ? attrs.getLength() : 0;
177
178 if (num == 0) {
179 logger.warn("This field has no attributes.");
180 return;
181 }
182
183 ThemeField theField = new DefaultThemeField(name);
184
185 for (int i = 0; i < num; i++) {
186 Node attr = attrs.item(i);
187
188 String key = attr.getNodeName();
189 String value = attr.getNodeValue();
190
191 theField.setAttribute(key, value);
192 }
193
194 theme.addField(name, theField);
195 }
196
197
198 protected static void parseAttrs(Node config, Theme theme) {
199 NamedNodeMap attrs = config.getAttributes();
200
201 int num = attrs != null ? attrs.getLength() : 0;
202
203 if (num == 0) {
204 logger.debug("Theme has no attributes set.");
205 return;
206 }
207
208 for (int i = 0; i < num; i++) {
209 Node attr = attrs.item(i);
210
211 String name = attr.getNodeName();
212 String value = attr.getNodeValue();
213
214 theme.addAttribute(name, value);
215 }
216 }
217 }
218 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org