ingo@340: package de.intevation.flys.themes; ingo@340: ingo@340: import javax.xml.xpath.XPathConstants; ingo@340: ingo@340: import org.apache.log4j.Logger; ingo@340: ingo@340: import org.w3c.dom.Document; ingo@340: import org.w3c.dom.NamedNodeMap; ingo@340: import org.w3c.dom.Node; ingo@340: import org.w3c.dom.NodeList; ingo@340: ingo@340: import de.intevation.artifacts.common.utils.XMLUtils; ingo@340: ingo@340: ingo@340: /** ingo@340: * @author Ingo Weinzierl ingo@340: */ ingo@340: public class ThemeFactory { ingo@340: ingo@340: private static Logger logger = Logger.getLogger(ThemeFactory.class); ingo@340: ingo@340: private ThemeFactory() { ingo@340: } ingo@340: ingo@340: ingo@340: /** ingo@340: * Creates a new theme from config. ingo@340: * ingo@340: * @param themeCfg The theme config document that is used to fetch parent ingo@340: * themes. ingo@340: * @param config The theme config node. ingo@340: * ingo@340: * @return a new theme. ingo@340: */ ingo@340: public static Theme createTheme(Document themeCfg, Node config) { ingo@340: String name = getName(config); ingo@340: String desc = getDescription(config); ingo@340: ingo@340: logger.debug("Create new theme: " + name); ingo@340: ingo@340: Theme theme = new DefaultTheme(name, desc); ingo@340: ingo@340: parseInherits(themeCfg, config, theme); ingo@340: parseFields(config, theme); ingo@340: parseAttrs(config, theme); ingo@340: ingo@340: String tmp = theme.getAttribute("type"); ingo@340: if (tmp == null || !tmp.equals("virtual")) { ingo@340: de.intevation.flys.artifacts.XMLDebug.out(theme.toXML()); ingo@340: } ingo@340: ingo@340: return theme; ingo@340: } ingo@340: ingo@340: ingo@340: protected static String getName(Node config) { ingo@340: return (String) XMLUtils.xpath(config, "@name", XPathConstants.STRING); ingo@340: } ingo@340: ingo@340: ingo@340: protected static String getDescription(Node config) { ingo@340: return (String) XMLUtils.xpath(config, "@desc", XPathConstants.STRING); ingo@340: } ingo@340: ingo@340: ingo@340: protected static void parseInherits(Document themeCfg, Node cfg, Theme t) { ingo@340: logger.debug("ThemeFactory.parseInherits"); ingo@340: ingo@340: NodeList inherits = (NodeList) XMLUtils.xpath( ingo@340: cfg, "inherits/inherit", XPathConstants.NODESET); ingo@340: ingo@340: int num = inherits != null ? inherits.getLength() : 0; ingo@340: ingo@340: if (num == 0) { ingo@340: logger.debug("Theme does not inherit from other themes."); ingo@340: return; ingo@340: } ingo@340: ingo@340: logger.debug("Theme inherits from " + num + " other themes."); ingo@340: ingo@340: for (int i = 0; i < num; i++) { ingo@340: Node inherit = inherits.item(i); ingo@340: String from = (String) XMLUtils.xpath( ingo@340: inherit, "@from", XPathConstants.STRING); ingo@340: ingo@340: Node tmp = getThemeNode(themeCfg, from); ingo@340: ingo@340: parseInherits(themeCfg, tmp, t); ingo@340: parseFields(tmp, t); ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: protected static Node getThemeNode(Document themeCfg, String name) { ingo@340: if (name == null) { ingo@340: logger.warn("Cannot search theme config without name!"); ingo@340: return null; ingo@340: } ingo@340: ingo@340: logger.debug("Search for theme: " + name); ingo@340: ingo@340: String xpath = "/themes/theme[@name='" + name + "']"; ingo@340: ingo@340: return (Node) XMLUtils.xpath(themeCfg, xpath, XPathConstants.NODE); ingo@340: } ingo@340: ingo@340: ingo@340: protected static void parseFields(Node config, Theme theme) { ingo@340: if (config == null || theme == null) { ingo@340: logger.warn("Parsing fields without node or theme is senseless!"); ingo@340: return; ingo@340: } ingo@340: ingo@340: NodeList fields = (NodeList) XMLUtils.xpath( ingo@340: config, "fields/field", XPathConstants.NODESET); ingo@340: ingo@340: int num = fields != null ? fields.getLength() : 0; ingo@340: ingo@340: logger.debug("Found " + num + " own fields in this theme."); ingo@340: ingo@340: if (num == 0) { ingo@340: logger.debug("Theme has no own fields."); ingo@340: return; ingo@340: } ingo@340: ingo@340: for (int i = 0; i < num; i++) { ingo@340: Node field = fields.item(i); ingo@340: ingo@340: addField(theme, field); ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: protected static void addField(Theme theme, Node field) { ingo@340: String name = (String) XMLUtils.xpath( ingo@340: field, "@name", XPathConstants.STRING); ingo@340: ingo@340: logger.debug("Add field: " + name); ingo@340: ingo@340: NamedNodeMap attrs = field.getAttributes(); ingo@340: ingo@340: int num = attrs != null ? attrs.getLength() : 0; ingo@340: ingo@340: if (num == 0) { ingo@340: logger.warn("This field has no attributes."); ingo@340: return; ingo@340: } ingo@340: ingo@340: ThemeField theField = new DefaultThemeField(name); ingo@340: ingo@340: for (int i = 0; i < num; i++) { ingo@340: Node attr = attrs.item(i); ingo@340: ingo@340: String key = attr.getNodeName(); ingo@340: String value = attr.getNodeValue(); ingo@340: ingo@340: theField.setAttribute(key, value); ingo@340: } ingo@340: ingo@340: theme.addField(name, theField); ingo@340: } ingo@340: ingo@340: ingo@340: protected static void parseAttrs(Node config, Theme theme) { ingo@340: NamedNodeMap attrs = config.getAttributes(); ingo@340: ingo@340: int num = attrs != null ? attrs.getLength() : 0; ingo@340: ingo@340: if (num == 0) { ingo@340: logger.debug("Theme has no attributes set."); ingo@340: return; ingo@340: } ingo@340: ingo@340: for (int i = 0; i < num; i++) { ingo@340: Node attr = attrs.item(i); ingo@340: ingo@340: String name = attr.getNodeName(); ingo@340: String value = attr.getNodeValue(); ingo@340: ingo@340: theme.addAttribute(name, value); ingo@340: } ingo@340: } ingo@340: } ingo@340: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :