# HG changeset patch # User Ingo Weinzierl # Date 1323865206 0 # Node ID f93edbfcf2bcdd367e2e7fb72cf5904c36ae2ab7 # Parent 03a8f9796571e5032b5ae2d3a97f917d8660af6b Improved the Settings and Section interfaces and added default implementations for both. artifacts/trunk@3416 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 03a8f9796571 -r f93edbfcf2bc ChangeLog --- 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 + + * 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 * artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java: diff -r 03a8f9796571 -r f93edbfcf2bc artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.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 Ingo Weinzierl + */ +public class DefaultSection implements Section { + + protected String id; + + protected List
subsections; + + protected Map attributes; + + + /** + * Creates a new DefaultSection instance. Note, that the id is used + * as Node name of the new Element that is created in toXML(). + */ + public DefaultSection(String id) { + this.id = id; + this.attributes = new HashMap(); + this.subsections = new ArrayList
(); + } + + + @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 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 : diff -r 03a8f9796571 -r f93edbfcf2bc artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSettings.java --- /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 Ingo Weinzierl + */ +public class DefaultSettings implements Settings { + + protected List
sections; + + public DefaultSettings() { + sections = new ArrayList
(); + } + + @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 : diff -r 03a8f9796571 -r f93edbfcf2bc artifact-database/src/main/java/de/intevation/artifactdatabase/state/Section.java --- 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 pos. + * + * @param pos The position of the target subsection. + * + * @return the subsection at position pos. + */ + 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. */ diff -r 03a8f9796571 -r f93edbfcf2bc artifact-database/src/main/java/de/intevation/artifactdatabase/state/Settings.java --- 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 toXML method is called to append its XML * representation to the final document.