teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.themes; ingo@340: ingo@340: import java.util.HashMap; ingo@340: import java.util.Map; ingo@340: ingo@340: import org.w3c.dom.Document; ingo@340: import org.w3c.dom.Element; ingo@340: import org.w3c.dom.Node; ingo@340: teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; ingo@340: ingo@340: ingo@340: /** ingo@340: * @author Ingo Weinzierl ingo@340: */ ingo@340: public class DefaultTheme implements Theme { ingo@340: ingo@340: /** The name of the theme.*/ ingo@340: protected String name; ingo@340: ingo@340: /** The description of the theme.*/ ingo@340: protected String description; ingo@340: ingo@1668: protected String facet; ingo@1668: ingo@1668: protected int index; ingo@1668: ingo@340: ingo@340: /** The map storing the fields of this theme.*/ ingo@340: protected Map fields; ingo@340: ingo@340: /** The map storing the attributes of this theme.*/ ingo@340: protected Map attr; ingo@340: ingo@340: ingo@340: /** ingo@340: * Initializes the components of this Theme. ingo@340: */ ingo@340: public DefaultTheme(String name, String description) { ingo@340: this.name = name; ingo@340: this.description = description; ingo@340: this.fields = new HashMap(); ingo@340: this.attr = new HashMap(); ingo@340: } ingo@340: ingo@340: ingo@340: public void init(Node config) { ingo@340: } ingo@340: ingo@340: ingo@340: public String getName() { ingo@340: return name; ingo@340: } ingo@340: ingo@340: ingo@340: public String getDescription() { ingo@340: return description; ingo@340: } ingo@340: ingo@340: ingo@1668: public String getFacet() { ingo@1668: return facet; ingo@1668: } ingo@1668: ingo@1668: ingo@1668: public void setFacet(String facet) { ingo@1668: this.facet = facet; ingo@1668: } ingo@1668: ingo@1668: ingo@1668: public int getIndex() { ingo@1668: return index; ingo@1668: } ingo@1668: ingo@1668: ingo@1668: public void setIndex(int index) { ingo@1668: this.index = index; ingo@1668: } ingo@1668: ingo@1668: ingo@340: public void addAttribute(String name, String value) { ingo@340: if (name != null && value != null) { ingo@340: attr.put(name, value); ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: public String getAttribute(String name) { ingo@340: return attr.get(name); ingo@340: } ingo@340: ingo@340: ingo@340: public void addField(String name, ThemeField field) { ingo@340: if (name != null && field != null) { ingo@340: fields.put(name, field); ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: public void setFieldValue(String name, Object value) { ingo@340: if (name != null && value != null) { ingo@340: ThemeField field = fields.get(name); ingo@340: ingo@340: if (field != null) { ingo@340: field.setValue(value); ingo@340: } ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: public ThemeField getField(String name) { ingo@340: return fields.get(name); ingo@340: } ingo@340: ingo@340: ingo@340: public String getFieldType(String name) { ingo@340: ThemeField field = fields.get(name); ingo@340: ingo@340: return field != null ? field.getType() : null; ingo@340: } ingo@340: ingo@340: ingo@340: public Object getFieldValue(String name) { ingo@340: ThemeField field = fields.get(name); ingo@340: ingo@340: return field != null ? field.getValue() : null; ingo@340: } ingo@340: ingo@340: ingo@340: public Document toXML() { ingo@340: Document doc = XMLUtils.newDocument(); ingo@340: ingo@340: ElementCreator cr = new ElementCreator(doc, null, null); ingo@340: ingo@340: Element theme = cr.create("theme"); ingo@1668: theme.setAttribute("facet", facet); ingo@1668: theme.setAttribute("index", String.valueOf(index)); ingo@340: ingo@340: appendAttributes(cr, theme); ingo@340: appendFields(cr, theme); ingo@340: ingo@340: doc.appendChild(theme); ingo@340: ingo@340: return doc; ingo@340: } ingo@340: ingo@340: ingo@340: /** ingo@340: * Appends the attributes configured for this theme. ingo@340: * ingo@340: * @param cr The ElementCreator. ingo@340: * @param theme The document root element. ingo@340: */ ingo@340: protected void appendAttributes(ElementCreator cr, Element theme) { ingo@340: sascha@3732: for (Map.Entry entry: attr.entrySet()) { sascha@3732: String key = entry.getKey(); sascha@3732: String val = entry.getValue(); ingo@340: sascha@3732: if (key != null && val != null) { sascha@3732: cr.addAttr(theme, key, val); ingo@340: } ingo@340: } ingo@340: } ingo@340: ingo@340: ingo@340: /** ingo@340: * Appends the fields configured for this theme. ingo@340: * ingo@340: * @param cr The ElementCreator. ingo@340: * @param theme The document root element. ingo@340: */ ingo@340: protected void appendFields(ElementCreator cr, Element theme) { ingo@340: teichmann@4049: for (ThemeField field: fields.values()) { ingo@340: Document doc = field.toXML(); ingo@340: Node root = doc.getFirstChild(); ingo@340: ingo@340: theme.appendChild(theme.getOwnerDocument().importNode(root, true)); ingo@340: } ingo@340: } ingo@340: } ingo@340: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :