comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 751:8d5bd3a08dd1

merged flys-artifacts/2.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:14 +0200
parents 68c6c75a6f7c
children 59ae2a823e73
comparison
equal deleted inserted replaced
618:6fd6c223b81e 751:8d5bd3a08dd1
1 package de.intevation.flys.collections;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.xml.xpath.XPathConstants;
7
8 import org.apache.log4j.Logger;
9
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.w3c.dom.Node;
13 import org.w3c.dom.NodeList;
14
15 import de.intevation.artifacts.ArtifactNamespaceContext;
16
17 import de.intevation.artifactdatabase.state.DefaultOutput;
18 import de.intevation.artifactdatabase.state.Output;
19
20 import de.intevation.artifacts.common.utils.XMLUtils;
21
22 import de.intevation.flys.artifacts.model.ManagedFacet;
23
24
25 public class AttributeParser {
26
27 /** Constant XPath that points to the outputmodes of an artifact.*/
28 public static final String XPATH_ARTIFACT_OUTPUTMODES =
29 "/art:attribute/art:outputs/art:output";
30
31
32 private static Logger logger = Logger.getLogger(AttributeParser.class);
33
34
35 protected Map<String, Output> outs;
36
37
38 public AttributeParser() {
39 this.outs = new HashMap<String, Output>();
40 }
41
42
43 public void parse(Document doc) {
44 logger.debug("AttributeParser.parse");
45
46 NodeList outs = (NodeList) XMLUtils.xpath(
47 doc,
48 XPATH_ARTIFACT_OUTPUTMODES,
49 XPathConstants.NODESET,
50 ArtifactNamespaceContext.INSTANCE);
51
52 int num = outs != null ? outs.getLength() : 0;
53
54 logger.debug("Attribute has " + num + " outputs.");
55
56 for (int i = 0; i < num; i++) {
57 Node out = outs.item(i);
58
59 parseOutput(out);
60 }
61 }
62
63
64 public Map<String, Output> getOuts() {
65 return outs;
66 }
67
68
69 protected void addItem(String out, ManagedFacet item) {
70 Output o = outs.get(out);
71
72 if (o != null) {
73 o.addFacet(item);
74 }
75 }
76
77
78 protected void parseOutput(Node out) {
79 String name = XMLUtils.xpathString(
80 out, "@name", ArtifactNamespaceContext.INSTANCE);
81
82 if (outs.get(name) == null) {
83 logger.debug("Create new output: " + name);
84
85 Output o = new DefaultOutput(name, null, null);
86 outs.put(name, o);
87 }
88
89 parseItems(out, name);
90 }
91
92
93 protected void parseItems(Node out, String outname) {
94 NodeList themes = (NodeList) XMLUtils.xpath(
95 out, "art:theme",
96 XPathConstants.NODESET,
97 ArtifactNamespaceContext.INSTANCE);
98
99 int num = themes != null ? themes.getLength() : 0;
100
101 logger.debug("Output has " + num + " themes.");
102
103 String uri = ArtifactNamespaceContext.NAMESPACE_URI;
104
105 for (int i = 0; i < num; i++) {
106 Element theme = (Element) themes.item(i);
107
108 String name = theme.getAttributeNS(uri, "facet");
109 if (name == null || name.length() == 0) {
110 continue;
111 }
112
113 String uuid = theme.getAttributeNS(uri, "artifact");
114 if (uuid == null || uuid.length() == 0) {
115 continue;
116 }
117
118 String pos = theme.getAttributeNS(uri, "pos");
119 if (pos == null || pos.length() == 0) {
120 continue;
121 }
122
123 String index = theme.getAttributeNS(uri, "index");
124 if (index == null || index.length() == 0) {
125 continue;
126 }
127
128 String active = theme.getAttributeNS(uri, "active");
129 if (active == null || active.length() == 0) {
130 continue;
131 }
132
133 String description = theme.getAttributeNS(uri, "description");
134
135 ManagedFacet item = new ManagedFacet(
136 name, Integer.parseInt(index), description, uuid,
137 Integer.parseInt(pos),
138 Integer.parseInt(active));
139
140 addItem(outname, item);
141 }
142 }
143 }
144 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org