comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeParser.java @ 3806:881fcd01e056

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

http://dive4elements.wald.intevation.org