ingo@340: package de.intevation.flys.themes;
ingo@340: 
ingo@1747: import java.util.List;
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;
felix@1822: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@340: 
ingo@340: /**
ingo@340:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
felix@1822:  *
felix@1822:  * Mapping-matching rules:
felix@1822:  *
ingo@340:  */
ingo@340: public class ThemeFactory {
ingo@340: 
ingo@340:     private static Logger logger = Logger.getLogger(ThemeFactory.class);
ingo@340: 
felix@1822:     /** Trivial, hidden constructor. */
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:     /**
felix@1822:      * Get first matching theme for facet.
felix@1822:      *
felix@1828:      * @param name    Name to match "from" of theme mapping.
felix@1822:      * @param pattern String to 'compare' to pattern in mapping.
felix@1828:      * @param output  Name of the current output
felix@1822:      *
felix@1822:      * @return First matching theme.
felix@1822:      */
felix@1828:     public static Theme getTheme(
felix@1828:         FLYSContext c,
felix@1828:         String name,
felix@1828:         String pattern,
felix@1828:         String output)
felix@1828:     {
felix@1828:         logger.debug("Search theme for: " + name + " - pattern: " + pattern);
felix@1822: 
ingo@345:         if (c == null || name == null) {
ingo@345:             logger.warn("Cannot search for theme.");
ingo@345:             return null;
ingo@345:         }
ingo@345: 
felix@1822:         // Fetch mapping and themes.
ingo@1747:         Map<String, List<ThemeMapping>> map = (Map<String, List<ThemeMapping>>)
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: 
felix@1822:         FLYSArtifact artifact = (FLYSArtifact) c.get(FLYSContext.ARTIFACT_KEY);
felix@1822: 
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@1747:         List<ThemeMapping> mapping = map.get(name);
ingo@345: 
ingo@1747:         if (mapping == null) {
felix@1822:             logger.warn("No theme found for mapping: " + name);
ingo@1747:             return null;
ingo@345:         }
ingo@345: 
felix@1822:         // Take first mapping of which all conditions are satisfied.
ingo@1747:         for (ThemeMapping tm: mapping) {
felix@1822:             if (name.equals(tm.getFrom())
felix@1822:                 && tm.applyPattern(pattern)
felix@1828:                 && tm.masterAttrMatches(artifact)
felix@1828:                 && tm.outputMatches(output))
felix@1822:             {
felix@1822:                 return t.get(tm.getTo());
ingo@1747:             }
ingo@1747:         }
ingo@1747: 
ingo@1747:         String msg =
ingo@1747:             "No theme found for '" + name +
felix@1828:             "' with pattern '" + pattern + "' and output " + output + ".";
ingo@1747: 
ingo@1747:         logger.warn(msg);
ingo@1747: 
ingo@1747:         return null;
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 :