changeset 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 03a8f9796571
children c065a1a90ba0
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.java artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSettings.java artifact-database/src/main/java/de/intevation/artifactdatabase/state/Section.java artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java
diffstat 5 files changed, 231 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Dec 14 09:41:44 2011 +0000
+++ b/ChangeLog	Wed Dec 14 12:20:06 2011 +0000
@@ -1,3 +1,19 @@
+2011-12-14  Ingo Weinzierl <ingo@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java:
+	  Added a removeSection(Section) method.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/Section.java:
+	  Improved the interface to allow section having subsections. Therefore,
+	  addSubsection(Section), getSubsectionCount() and getSubsection(int) have
+	  been added. In addition, a getId() method has been added which is used
+	  in toXML() to create a new DOM Node. The Node name is the result of
+	  getId().
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSettings.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.java:
+	  Default implementations for Settings and Section.
+
 2011-12-14  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java:
--- /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 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSettings.java	Wed Dec 14 12:20:06 2011 +0000
@@ -0,0 +1,65 @@
+package de.intevation.artifactdatabase.state;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import de.intevation.artifactdatabase.state.Section;
+import de.intevation.artifactdatabase.state.Settings;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class DefaultSettings implements Settings {
+
+    protected List<Section> sections;
+
+    public DefaultSettings() {
+        sections = new ArrayList<Section>();
+    }
+
+    @Override
+    public void addSection(Section section) {
+        if (section != null) {
+            sections.add(section);
+        }
+    }
+
+    @Override
+    public int getSectionCount() {
+        return sections.size();
+    }
+
+    @Override
+    public Section getSection(int pos) {
+        if (pos >= 0 && pos < getSectionCount()) {
+            return sections.get(pos);
+        }
+
+        return null;
+    }
+
+    @Override
+    public void removeSection(Section section) {
+        if (section != null) {
+            sections.remove(section);
+        }
+    }
+
+    @Override
+    public void toXML(Node parent) {
+        Document owner    = parent.getOwnerDocument();
+        Element  settings = owner.createElement("settings");
+
+        parent.appendChild(settings);
+
+        for (Section section: sections) {
+            section.toXML(settings);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/state/Section.java	Wed Dec 14 09:41:44 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/Section.java	Wed Dec 14 12:20:06 2011 +0000
@@ -12,6 +12,36 @@
 public interface Section extends Serializable {
 
     /**
+     * Returns an ID for this Section.
+     *
+     * @return an ID for this Section.
+     */
+    String getId();
+
+    /**
+     * Adds a new subsection to this Section object.
+     *
+     * @param subsection the new Section.
+     */
+    void addSubsection(Section subsection);
+
+    /**
+     * Returns the number of subsections in this Section.
+     *
+     * @return the number of subsections.
+     */
+    int getSubsectionCount();
+
+    /**
+     * Returns a subsection at position <i>pos</i>.
+     *
+     * @param pos The position of the target subsection.
+     *
+     * @return the subsection at position <i>pos</i>.
+     */
+    Section getSubsection(int pos);
+
+    /**
      * Adds a new Attribute to this Section.
      *
      * @param key The key that is used to store/retrieve the Attribute.
@@ -37,7 +67,7 @@
 
     /**
      * Transforms this Section into XML using Attribute.toXML() for each
-     * Attribute stored in this Section.
+     * Attribute and Section.toXML() for each subsection stored in this Section.
      *
      * @param parent The parent node.
      */
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java	Wed Dec 14 09:41:44 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java	Wed Dec 14 12:20:06 2011 +0000
@@ -35,6 +35,13 @@
     Section getSection(int pos);
 
     /**
+     * Removes a Section if it is existing in this Settings.
+     *
+     * @param section The section that should be removed.
+     */
+    void removeSection(Section section);
+
+    /**
      * Transforms this Settings object into a XML representation. Therefore,
      * each Section object's <i>toXML</i> method is called to append its XML
      * representation to the final document.

http://dive4elements.wald.intevation.org