diff artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.java @ 359:f93edbfcf2bc

Improved the Settings and Section interfaces and added default implementations for both. artifacts/trunk@3416 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 14 Dec 2011 12:20:06 +0000
parents
children ab3900303f25
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.java	Wed Dec 14 12:20:06 2011 +0000
@@ -0,0 +1,112 @@
+package de.intevation.artifactdatabase.state;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import de.intevation.artifactdatabase.state.Attribute;
+import de.intevation.artifactdatabase.state.Section;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class DefaultSection implements Section {
+
+    protected String id;
+
+    protected List<Section> subsections;
+
+    protected Map<String, Attribute> attributes;
+
+
+    /**
+     * Creates a new DefaultSection instance. <b>Note, that the <i>id</i> is used
+     * as Node name of the new Element that is created in toXML().</b>
+     */
+    public DefaultSection(String id) {
+        this.id          = id;
+        this.attributes  = new HashMap<String, Attribute>();
+        this.subsections = new ArrayList<Section>();
+    }
+
+
+    @Override
+    public String getId() {
+        return id;
+    }
+
+
+    @Override
+    public void addSubsection(Section subsection) {
+        if (subsection != null) {
+            subsections.add(subsection);
+        }
+    }
+
+
+    @Override
+    public int getSubsectionCount() {
+        return subsections.size();
+    }
+
+
+    @Override
+    public Section getSubsection(int pos) {
+        if (pos >= 0 && pos < getSubsectionCount()) {
+            return subsections.get(pos);
+        }
+
+        return null;
+    }
+
+
+    @Override
+    public void addAttribute(String key, Attribute attribute) {
+        if (key != null && key.length() > 0 && attribute != null) {
+            attributes.put(key, attribute);
+        }
+    }
+
+
+    @Override
+    public Attribute getAttribute(String key) {
+        if (key == null || key.length() == 0) {
+            return null;
+        }
+
+        return attributes.get(key);
+    }
+
+
+    @Override
+    public Set<String> getKeys() {
+        return attributes.keySet();
+    }
+
+
+    @Override
+    public void toXML(Node parent) {
+        Document owner     = parent.getOwnerDocument();
+        Element  sectionEl = owner.createElement(getId());
+
+        parent.appendChild(sectionEl);
+
+        for (String key: getKeys()) {
+            Attribute attr = getAttribute(key);
+            attr.toXML(sectionEl);
+        }
+
+        for (int i = 0, n = getSubsectionCount(); i < n; i++) {
+            Section subsection = getSubsection(i);
+            subsection.toXML(sectionEl);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org