Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java @ 340:b36fd8f21e6a
Implementation of interfaces and its default implementations and a factory to work with themes.
flys-artifacts/trunk@1739 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 21 Apr 2011 08:44:41 +0000 |
parents | |
children | f72c63713099 |
comparison
equal
deleted
inserted
replaced
339:4509ba8fae68 | 340:b36fd8f21e6a |
---|---|
1 package de.intevation.flys.themes; | |
2 | |
3 import javax.xml.xpath.XPathConstants; | |
4 | |
5 import org.apache.log4j.Logger; | |
6 | |
7 import org.w3c.dom.Document; | |
8 import org.w3c.dom.NamedNodeMap; | |
9 import org.w3c.dom.Node; | |
10 import org.w3c.dom.NodeList; | |
11 | |
12 import de.intevation.artifacts.common.utils.XMLUtils; | |
13 | |
14 | |
15 /** | |
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
17 */ | |
18 public class ThemeFactory { | |
19 | |
20 private static Logger logger = Logger.getLogger(ThemeFactory.class); | |
21 | |
22 private ThemeFactory() { | |
23 } | |
24 | |
25 | |
26 /** | |
27 * Creates a new theme from <i>config</i>. | |
28 * | |
29 * @param themeCfg The theme config document that is used to fetch parent | |
30 * themes. | |
31 * @param config The theme config node. | |
32 * | |
33 * @return a new theme. | |
34 */ | |
35 public static Theme createTheme(Document themeCfg, Node config) { | |
36 String name = getName(config); | |
37 String desc = getDescription(config); | |
38 | |
39 logger.debug("Create new theme: " + name); | |
40 | |
41 Theme theme = new DefaultTheme(name, desc); | |
42 | |
43 parseInherits(themeCfg, config, theme); | |
44 parseFields(config, theme); | |
45 parseAttrs(config, theme); | |
46 | |
47 String tmp = theme.getAttribute("type"); | |
48 if (tmp == null || !tmp.equals("virtual")) { | |
49 de.intevation.flys.artifacts.XMLDebug.out(theme.toXML()); | |
50 } | |
51 | |
52 return theme; | |
53 } | |
54 | |
55 | |
56 protected static String getName(Node config) { | |
57 return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING); | |
58 } | |
59 | |
60 | |
61 protected static String getDescription(Node config) { | |
62 return (String) XMLUtils.xpath(config, "@desc", XPathConstants.STRING); | |
63 } | |
64 | |
65 | |
66 protected static void parseInherits(Document themeCfg, Node cfg, Theme t) { | |
67 logger.debug("ThemeFactory.parseInherits"); | |
68 | |
69 NodeList inherits = (NodeList) XMLUtils.xpath( | |
70 cfg, "inherits/inherit", XPathConstants.NODESET); | |
71 | |
72 int num = inherits != null ? inherits.getLength() : 0; | |
73 | |
74 if (num == 0) { | |
75 logger.debug("Theme does not inherit from other themes."); | |
76 return; | |
77 } | |
78 | |
79 logger.debug("Theme inherits from " + num + " other themes."); | |
80 | |
81 for (int i = 0; i < num; i++) { | |
82 Node inherit = inherits.item(i); | |
83 String from = (String) XMLUtils.xpath( | |
84 inherit, "@from", XPathConstants.STRING); | |
85 | |
86 Node tmp = getThemeNode(themeCfg, from); | |
87 | |
88 parseInherits(themeCfg, tmp, t); | |
89 parseFields(tmp, t); | |
90 } | |
91 } | |
92 | |
93 | |
94 protected static Node getThemeNode(Document themeCfg, String name) { | |
95 if (name == null) { | |
96 logger.warn("Cannot search theme config without name!"); | |
97 return null; | |
98 } | |
99 | |
100 logger.debug("Search for theme: " + name); | |
101 | |
102 String xpath = "/themes/theme[@name='" + name + "']"; | |
103 | |
104 return (Node) XMLUtils.xpath(themeCfg, xpath, XPathConstants.NODE); | |
105 } | |
106 | |
107 | |
108 protected static void parseFields(Node config, Theme theme) { | |
109 if (config == null || theme == null) { | |
110 logger.warn("Parsing fields without node or theme is senseless!"); | |
111 return; | |
112 } | |
113 | |
114 NodeList fields = (NodeList) XMLUtils.xpath( | |
115 config, "fields/field", XPathConstants.NODESET); | |
116 | |
117 int num = fields != null ? fields.getLength() : 0; | |
118 | |
119 logger.debug("Found " + num + " own fields in this theme."); | |
120 | |
121 if (num == 0) { | |
122 logger.debug("Theme has no own fields."); | |
123 return; | |
124 } | |
125 | |
126 for (int i = 0; i < num; i++) { | |
127 Node field = fields.item(i); | |
128 | |
129 addField(theme, field); | |
130 } | |
131 } | |
132 | |
133 | |
134 protected static void addField(Theme theme, Node field) { | |
135 String name = (String) XMLUtils.xpath( | |
136 field, "@name", XPathConstants.STRING); | |
137 | |
138 logger.debug("Add field: " + name); | |
139 | |
140 NamedNodeMap attrs = field.getAttributes(); | |
141 | |
142 int num = attrs != null ? attrs.getLength() : 0; | |
143 | |
144 if (num == 0) { | |
145 logger.warn("This field has no attributes."); | |
146 return; | |
147 } | |
148 | |
149 ThemeField theField = new DefaultThemeField(name); | |
150 | |
151 for (int i = 0; i < num; i++) { | |
152 Node attr = attrs.item(i); | |
153 | |
154 String key = attr.getNodeName(); | |
155 String value = attr.getNodeValue(); | |
156 | |
157 theField.setAttribute(key, value); | |
158 } | |
159 | |
160 theme.addField(name, theField); | |
161 } | |
162 | |
163 | |
164 protected static void parseAttrs(Node config, Theme theme) { | |
165 NamedNodeMap attrs = config.getAttributes(); | |
166 | |
167 int num = attrs != null ? attrs.getLength() : 0; | |
168 | |
169 if (num == 0) { | |
170 logger.debug("Theme has no attributes set."); | |
171 return; | |
172 } | |
173 | |
174 for (int i = 0; i < num; i++) { | |
175 Node attr = attrs.item(i); | |
176 | |
177 String name = attr.getNodeName(); | |
178 String value = attr.getNodeValue(); | |
179 | |
180 theme.addAttribute(name, value); | |
181 } | |
182 } | |
183 } | |
184 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |