Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.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 | 853dceead0f4 |
children | 9ff7e06bcb77 |
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.ArtifactDatabase; | |
16 import de.intevation.artifacts.ArtifactDatabaseException; | |
17 import de.intevation.artifacts.ArtifactNamespaceContext; | |
18 import de.intevation.artifacts.CallMeta; | |
19 | |
20 import de.intevation.artifactdatabase.state.DefaultOutput; | |
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 | |
27 | |
28 public class OutputParser { | |
29 | |
30 /** Constant XPath that points to the outputmodes of an artifact.*/ | |
31 public static final String XPATH_ARTIFACT_OUTPUTMODES = | |
32 "/art:result/art:outputmodes/art:output"; | |
33 | |
34 | |
35 private static Logger logger = Logger.getLogger(OutputParser.class); | |
36 | |
37 protected ArtifactDatabase db; | |
38 protected CallMeta meta; | |
39 | |
40 protected Map<String, Output> outs; | |
41 | |
42 | |
43 public OutputParser(ArtifactDatabase db, CallMeta meta) { | |
44 this.db = db; | |
45 this.meta = meta; | |
46 this.outs = new HashMap<String, Output>(); | |
47 } | |
48 | |
49 | |
50 public void parse(String uuid) | |
51 throws ArtifactDatabaseException | |
52 { | |
53 logger.debug("OutputParser.parse: " + uuid); | |
54 | |
55 // XXX I am not sure if it works well every time with an empty | |
56 // document in the describe operation of an artifact. | |
57 Document description = db.describe(uuid, null, meta); | |
58 | |
59 NodeList outs = (NodeList) XMLUtils.xpath( | |
60 description, | |
61 XPATH_ARTIFACT_OUTPUTMODES, | |
62 XPathConstants.NODESET, | |
63 ArtifactNamespaceContext.INSTANCE); | |
64 | |
65 int num = outs != null ? outs.getLength() : 0; | |
66 | |
67 logger.debug("Artifact has " + num + " outputs."); | |
68 | |
69 for (int i = 0; i < num; i++) { | |
70 Element out = (Element)outs.item(i); | |
71 | |
72 parseOutput(uuid, out); | |
73 } | |
74 } | |
75 | |
76 | |
77 public Map<String, Output> getOuts() { | |
78 return outs; | |
79 } | |
80 | |
81 | |
82 protected void addItem(String out, ManagedFacet item) { | |
83 Output o = outs.get(out); | |
84 | |
85 if (o != null) { | |
86 int num = o.getFacets().size(); | |
87 item.setPosition(num+1); | |
88 | |
89 o.addFacet(item); | |
90 } | |
91 } | |
92 | |
93 | |
94 protected void parseOutput(String uuid, Element out) { | |
95 | |
96 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
97 | |
98 String name = out.getAttributeNS(uri, "name"); | |
99 | |
100 if (outs.get(name) == null) { | |
101 logger.debug("Create new output: " + name); | |
102 newOutput(out, name); | |
103 } | |
104 | |
105 parseItems(uuid, out, name); | |
106 } | |
107 | |
108 | |
109 protected void newOutput(Element out, String name) { | |
110 | |
111 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
112 | |
113 String desc = out.getAttributeNS(uri, "description"); | |
114 String mimetype = out.getAttributeNS(uri, "mime-type"); | |
115 | |
116 Output o = new DefaultOutput(name, desc, mimetype); | |
117 | |
118 outs.put(name, o); | |
119 } | |
120 | |
121 | |
122 protected void parseItems(String uuid, Node out, String outname) { | |
123 NodeList facets = (NodeList) XMLUtils.xpath( | |
124 out, "art:facets/art:facet", | |
125 XPathConstants.NODESET, | |
126 ArtifactNamespaceContext.INSTANCE); | |
127 | |
128 int num = facets != null ? facets.getLength() : 0; | |
129 | |
130 String uri = ArtifactNamespaceContext.NAMESPACE_URI; | |
131 | |
132 logger.debug("Output has " + num + " facets."); | |
133 | |
134 for (int i = 0; i < num; i++) { | |
135 Element facet = (Element) facets.item(i); | |
136 | |
137 String name = facet.getAttributeNS(uri, "name"); | |
138 String desc = facet.getAttributeNS(uri, "description"); | |
139 String index = facet.getAttributeNS(uri, "index"); | |
140 | |
141 ManagedFacet item = new ManagedFacet( | |
142 name, Integer.parseInt(index), desc, uuid, 1, 1); | |
143 | |
144 addItem(outname, item); | |
145 } | |
146 } | |
147 } | |
148 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |