Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/collections/AttributeParser.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/collections/AttributeParser.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.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 org.dive4elements.artifacts.ArtifactNamespaceContext; | |
16 | |
17 import org.dive4elements.artifactdatabase.state.DefaultOutput; | |
18 import org.dive4elements.artifactdatabase.state.Facet; | |
19 import org.dive4elements.artifactdatabase.state.Output; | |
20 import org.dive4elements.artifactdatabase.state.Settings; | |
21 | |
22 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
23 | |
24 import org.dive4elements.river.artifacts.model.ManagedDomFacet; | |
25 import org.dive4elements.river.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 private static final Node getChild(Element element, String name) { | |
124 NodeList children = element.getChildNodes(); | |
125 for (int i = 0, N = children.getLength(); i < N; ++i) { | |
126 Node child = children.item(i); | |
127 if (child.getNodeType() == Node.ELEMENT_NODE | |
128 && child.getLocalName().equals(name)) { | |
129 return child; | |
130 } | |
131 } | |
132 return null; | |
133 } | |
134 | |
135 | |
136 protected void parseSettings(Node out, String outname) { | |
137 Node settingsNode = getChild((Element)out, "settings"); | |
138 | |
139 if (settingsNode == null) { | |
140 logger.debug("No Settings found for Output '" + outname + "'"); | |
141 return; | |
142 } | |
143 | |
144 Settings settings = ChartSettings.parse(settingsNode); | |
145 attribute.setSettings(outname, settings); | |
146 } | |
147 | |
148 | |
149 protected void parseItems(Node out, String outname) { | |
150 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
151 Element element = (Element)out; | |
152 | |
153 NodeList themes = element.getElementsByTagNameNS(uri, "facet"); | |
154 | |
155 int num = themes.getLength(); | |
156 | |
157 logger.debug("Output has " + num + " themes."); | |
158 | |
159 for (int i = 0; i < num; i++) { | |
160 Element theme = (Element) themes.item(i); | |
161 if (theme.getParentNode() == out) { | |
162 attribute.addFacet(outname, new ManagedDomFacet(theme)); | |
163 } | |
164 } | |
165 } | |
166 } | |
167 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |