Mercurial > dive4elements > framework
comparison 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 |
comparison
equal
deleted
inserted
replaced
358:03a8f9796571 | 359:f93edbfcf2bc |
---|---|
1 package de.intevation.artifactdatabase.state; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.HashMap; | |
5 import java.util.List; | |
6 import java.util.Map; | |
7 import java.util.Set; | |
8 | |
9 import org.w3c.dom.Document; | |
10 import org.w3c.dom.Element; | |
11 import org.w3c.dom.Node; | |
12 | |
13 import de.intevation.artifactdatabase.state.Attribute; | |
14 import de.intevation.artifactdatabase.state.Section; | |
15 | |
16 | |
17 /** | |
18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
19 */ | |
20 public class DefaultSection implements Section { | |
21 | |
22 protected String id; | |
23 | |
24 protected List<Section> subsections; | |
25 | |
26 protected Map<String, Attribute> attributes; | |
27 | |
28 | |
29 /** | |
30 * Creates a new DefaultSection instance. <b>Note, that the <i>id</i> is used | |
31 * as Node name of the new Element that is created in toXML().</b> | |
32 */ | |
33 public DefaultSection(String id) { | |
34 this.id = id; | |
35 this.attributes = new HashMap<String, Attribute>(); | |
36 this.subsections = new ArrayList<Section>(); | |
37 } | |
38 | |
39 | |
40 @Override | |
41 public String getId() { | |
42 return id; | |
43 } | |
44 | |
45 | |
46 @Override | |
47 public void addSubsection(Section subsection) { | |
48 if (subsection != null) { | |
49 subsections.add(subsection); | |
50 } | |
51 } | |
52 | |
53 | |
54 @Override | |
55 public int getSubsectionCount() { | |
56 return subsections.size(); | |
57 } | |
58 | |
59 | |
60 @Override | |
61 public Section getSubsection(int pos) { | |
62 if (pos >= 0 && pos < getSubsectionCount()) { | |
63 return subsections.get(pos); | |
64 } | |
65 | |
66 return null; | |
67 } | |
68 | |
69 | |
70 @Override | |
71 public void addAttribute(String key, Attribute attribute) { | |
72 if (key != null && key.length() > 0 && attribute != null) { | |
73 attributes.put(key, attribute); | |
74 } | |
75 } | |
76 | |
77 | |
78 @Override | |
79 public Attribute getAttribute(String key) { | |
80 if (key == null || key.length() == 0) { | |
81 return null; | |
82 } | |
83 | |
84 return attributes.get(key); | |
85 } | |
86 | |
87 | |
88 @Override | |
89 public Set<String> getKeys() { | |
90 return attributes.keySet(); | |
91 } | |
92 | |
93 | |
94 @Override | |
95 public void toXML(Node parent) { | |
96 Document owner = parent.getOwnerDocument(); | |
97 Element sectionEl = owner.createElement(getId()); | |
98 | |
99 parent.appendChild(sectionEl); | |
100 | |
101 for (String key: getKeys()) { | |
102 Attribute attr = getAttribute(key); | |
103 attr.toXML(sectionEl); | |
104 } | |
105 | |
106 for (int i = 0, n = getSubsectionCount(); i < n; i++) { | |
107 Section subsection = getSubsection(i); | |
108 subsection.toXML(sectionEl); | |
109 } | |
110 } | |
111 } | |
112 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |