comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 2793:6310b1582f2d

merged flys-artifacts/2.7
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:30 +0200
parents e57816cf41d5
children ef0db530c341
comparison
equal deleted inserted replaced
2548:ada02bbd3b7f 2793:6310b1582f2d
1 package de.intevation.flys.collections;
2
3 import java.util.List;
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.Facet;
19 import de.intevation.artifactdatabase.state.Output;
20 import de.intevation.artifactdatabase.state.Settings;
21
22 import de.intevation.artifacts.common.utils.XMLUtils;
23
24 import de.intevation.flys.artifacts.model.ManagedDomFacet;
25 import de.intevation.flys.exports.ChartSettings;
26
27 /**
28 * Access parts of the Attribute parts of a FLYSCollections description
29 * document.
30 */
31 public class AttributeParser {
32
33 /** Constant XPath that points to the outputmodes of an artifact. */
34 public static final String XPATH_ARTIFACT_OUTPUTMODES =
35 "/art:attribute/art:outputs/art:output";
36
37
38 private static Logger logger = Logger.getLogger(AttributeParser.class);
39
40
41 protected Document attributeDocument;
42
43 protected CollectionAttribute attribute;
44
45
46 public AttributeParser(Document attributeDocument) {
47 this.attributeDocument = attributeDocument;
48 }
49
50
51 public void parse() {
52 logger.debug("AttributeParser.parse");
53
54 attribute = new CollectionAttribute();
55
56 NodeList outs = (NodeList) XMLUtils.xpath(
57 attributeDocument,
58 XPATH_ARTIFACT_OUTPUTMODES,
59 XPathConstants.NODESET,
60 ArtifactNamespaceContext.INSTANCE);
61
62 int num = outs != null ? outs.getLength() : 0;
63
64 logger.debug("Attribute has " + num + " outputs.");
65
66 for (int i = 0; i < num; i++) {
67 Node out = outs.item(i);
68
69 parseOutput(out);
70 }
71 }
72
73
74 public CollectionAttribute getCollectionAttribute() {
75 if (attribute == null) {
76 parse();
77 }
78
79 return attribute;
80 }
81
82
83 public Document getAttributeDocument() {
84 return attributeDocument;
85 }
86
87
88 public Map<String, Output> getOuts() {
89 return attribute.getOutputs();
90 }
91
92
93 /**
94 * Access all facets.
95 * @return list of all facets.
96 */
97 public List<Facet> getFacets() {
98 return attribute.getFacets();
99 }
100
101
102 protected void parseOutput(Node out) {
103 String name = ((Element)out).getAttribute("name");
104
105 if (name.length() == 0) {
106 logger.warn("No Output name specified. Cancel parsing!");
107 return;
108 }
109
110 Output o = attribute.getOutput(name);
111
112 if (o == null) {
113 logger.debug("Create new output: " + name);
114
115 o = new DefaultOutput(name, null, null);
116 attribute.addOutput(name, o);
117 }
118
119 parseSettings(out, name);
120 parseItems(out, name);
121 }
122
123
124 protected void parseSettings(Node out, String outname) {
125 Node settingsNode = (Node) XMLUtils.xpath(
126 out, "settings",
127 XPathConstants.NODE,
128 null);
129
130 if (settingsNode == null) {
131 logger.debug("No Settings found for Output '" + outname + "'");
132 return;
133 }
134
135 Settings settings = ChartSettings.parse(settingsNode);
136 attribute.setSettings(outname, settings);
137 }
138
139
140 protected void parseItems(Node out, String outname) {
141 NodeList themes = (NodeList) XMLUtils.xpath(
142 out, "art:facet",
143 XPathConstants.NODESET,
144 ArtifactNamespaceContext.INSTANCE);
145
146 int num = themes != null ? themes.getLength() : 0;
147
148 logger.debug("Output has " + num + " themes.");
149
150 String uri = ArtifactNamespaceContext.NAMESPACE_URI;
151
152 for (int i = 0; i < num; i++) {
153 Element theme = (Element) themes.item(i);
154
155 attribute.addFacet(outname, new ManagedDomFacet(theme));
156 }
157 }
158 }
159 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org