ingo@340: package de.intevation.flys.themes;
ingo@340: 
ingo@345: import java.util.Map;
ingo@345: 
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@345: import de.intevation.flys.artifacts.context.FLYSContext;
ingo@345: 
ingo@340: 
ingo@340: /**
ingo@340:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
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 <i>config</i>.
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:         return theme;
ingo@340:     }
ingo@340: 
ingo@340: 
ingo@345:     /**
ingo@345:      * Returns the theme for a specified output type and facet.
ingo@345:      *
ingo@345:      * @param c The FLYSContext that stores the mappings and themes.
ingo@345:      * @param name The name of the mapping.
ingo@345:      *
ingo@345:      * @return a theme.
ingo@345:      */
ingo@345:     public static Theme getTheme(FLYSContext c, String name) {
ingo@345:         if (c == null || name == null) {
ingo@345:             logger.warn("Cannot search for theme.");
ingo@345:             return null;
ingo@345:         }
ingo@345: 
ingo@345:         Map<String, String> map = (Map<String, String>)
ingo@345:             c.get(FLYSContext.THEME_MAPPING);
ingo@345: 
ingo@345:         Map<String, Theme> t = (Map<String, Theme>)
ingo@345:             c.get(FLYSContext.THEMES);
ingo@345: 
ingo@345:         if (map == null || map.size() == 0 || t == null || t.size() == 0) {
ingo@345:             logger.warn("No mappings or themes found. Cannot retrieve theme!");
ingo@345:             return null;
ingo@345:         }
ingo@345: 
ingo@345:         String themeName = map.get(name);
ingo@345: 
ingo@345:         if (themeName == null) {
ingo@345:             logger.warn("No theme found for mapping: " + name);
ingo@345:         }
ingo@345: 
ingo@345:         return t.get(themeName);
ingo@345:     }
ingo@345: 
ingo@345: 
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 :