comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 3818:dc18457b1cef

merged flys-artifacts/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents 3e703d134bbe
children e57816cf41d5
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
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 = XMLUtils.xpathString(
104 out, "@name", ArtifactNamespaceContext.INSTANCE);
105
106 if (name == null || name.length() == 0) {
107 logger.warn("No Output name specified. Cancel parsing!");
108 return;
109 }
110
111 Output o = attribute.getOutput(name);
112
113 if (o == null) {
114 logger.debug("Create new output: " + name);
115
116 o = new DefaultOutput(name, null, null);
117 attribute.addOutput(name, o);
118 }
119
120 parseSettings(out, name);
121 parseItems(out, name);
122 }
123
124
125 protected void parseSettings(Node out, String outname) {
126 Node settingsNode = (Node) XMLUtils.xpath(
127 out, "settings",
128 XPathConstants.NODE,
129 null);
130
131 if (settingsNode == null) {
132 logger.debug("No Settings found for Output '" + outname + "'");
133 return;
134 }
135
136 Settings settings = ChartSettings.parse(settingsNode);
137 attribute.setSettings(outname, settings);
138 }
139
140
141 protected void parseItems(Node out, String outname) {
142 NodeList themes = (NodeList) XMLUtils.xpath(
143 out, "art:facet",
144 XPathConstants.NODESET,
145 ArtifactNamespaceContext.INSTANCE);
146
147 int num = themes != null ? themes.getLength() : 0;
148
149 logger.debug("Output has " + num + " themes.");
150
151 String uri = ArtifactNamespaceContext.NAMESPACE_URI;
152
153 for (int i = 0; i < num; i++) {
154 Element theme = (Element) themes.item(i);
155
156 attribute.addFacet(outname, new ManagedDomFacet(theme));
157 }
158 }
159 }
160 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org