comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/state/DefaultSection.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultSection.java@92b51af2d02d
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 package de.intevation.artifactdatabase.state;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
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 /**
14 * Attributes keep the order in which they were inserted.
15 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
16 */
17 public class DefaultSection implements Section {
18
19 protected String id;
20
21 protected List<Section> subsections;
22
23 /** Attribute-map. */
24 protected Map<String, Attribute> attributes;
25
26
27 /**
28 * Creates a new DefaultSection instance. <b>Note, that the <i>id</i> is used
29 * as Node name of the new Element that is created in toXML().</b>
30 */
31 public DefaultSection(String id) {
32 this.id = id;
33 // Use LinkedHashMap to keep insertion order.
34 this.attributes = new LinkedHashMap<String, Attribute>();
35 this.subsections = new ArrayList<Section>();
36 }
37
38
39 @Override
40 public String getId() {
41 return id;
42 }
43
44
45 @Override
46 public void addSubsection(Section subsection) {
47 if (subsection != null) {
48 subsections.add(subsection);
49 }
50 }
51
52
53 @Override
54 public int getSubsectionCount() {
55 return subsections.size();
56 }
57
58
59 @Override
60 public Section getSubsection(int pos) {
61 if (pos >= 0 && pos < getSubsectionCount()) {
62 return subsections.get(pos);
63 }
64
65 return null;
66 }
67
68
69 /** Adding attribute to end of list. */
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 :

http://dive4elements.wald.intevation.org