comparison flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java @ 430:7ab81ff32111 2.3

merged flys-artifacts/2.3
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:10 +0200
parents 6167ae622ce0
children 68c6c75a6f7c
comparison
equal deleted inserted replaced
290:a6f56ed9238b 430:7ab81ff32111
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.Node;
12 import org.w3c.dom.NodeList;
13
14 import de.intevation.artifacts.ArtifactDatabase;
15 import de.intevation.artifacts.ArtifactDatabaseException;
16 import de.intevation.artifacts.ArtifactNamespaceContext;
17 import de.intevation.artifacts.CallMeta;
18
19 import de.intevation.artifactdatabase.state.DefaultOutput;
20 import de.intevation.artifactdatabase.state.Output;
21
22 import de.intevation.artifacts.common.utils.XMLUtils;
23
24 import de.intevation.flys.artifacts.model.ManagedFacet;
25
26
27 public class OutputParser {
28
29 /** Constant XPath that points to the outputmodes of an artifact.*/
30 public static final String XPATH_ARTIFACT_OUTPUTMODES =
31 "/art:result/art:outputmodes/art:output";
32
33
34 private static Logger logger = Logger.getLogger(OutputParser.class);
35
36 protected ArtifactDatabase db;
37 protected CallMeta meta;
38
39 protected Map<String, Output> outs;
40
41
42 public OutputParser(ArtifactDatabase db, CallMeta meta) {
43 this.db = db;
44 this.meta = meta;
45 this.outs = new HashMap<String, Output>();
46 }
47
48
49 public void parse(String uuid)
50 throws ArtifactDatabaseException
51 {
52 logger.debug("OutputParser.parse: " + uuid);
53
54 // XXX I am not sure if it works well every time with an empty
55 // document in the describe operation of an artifact.
56 Document description = db.describe(uuid, null, meta);
57
58 NodeList outs = (NodeList) XMLUtils.xpath(
59 description,
60 XPATH_ARTIFACT_OUTPUTMODES,
61 XPathConstants.NODESET,
62 ArtifactNamespaceContext.INSTANCE);
63
64 int num = outs != null ? outs.getLength() : 0;
65
66 logger.debug("Artifact has " + num + " outputs.");
67
68 for (int i = 0; i < num; i++) {
69 Node out = outs.item(i);
70
71 parseOutput(uuid, out);
72 }
73 }
74
75
76 public Map<String, Output> getOuts() {
77 return outs;
78 }
79
80
81 protected void addItem(String out, ManagedFacet item) {
82 Output o = outs.get(out);
83
84 if (o != null) {
85 int num = o.getFacets().size();
86 item.setPosition(num+1);
87
88 o.addFacet(item);
89 }
90 }
91
92
93 protected void parseOutput(String uuid, Node out) {
94 String name = XMLUtils.xpathString(
95 out, "@art:name", ArtifactNamespaceContext.INSTANCE);
96
97 if (outs.get(name) == null) {
98 logger.debug("Create new output: " + name);
99 newOutput(out, name);
100 }
101
102 parseItems(uuid, out, name);
103 }
104
105
106 protected void newOutput(Node out, String name) {
107 String desc = XMLUtils.xpathString(
108 out, "@art:description", ArtifactNamespaceContext.INSTANCE);
109
110 String mimetype = XMLUtils.xpathString(
111 out, "@art:mime-type", ArtifactNamespaceContext.INSTANCE);
112
113 Output o = new DefaultOutput(name, desc, mimetype);
114
115 outs.put(name, o);
116 }
117
118
119 protected void parseItems(String uuid, Node out, String outname) {
120 NodeList facets = (NodeList) XMLUtils.xpath(
121 out, "art:facets/art:facet",
122 XPathConstants.NODESET,
123 ArtifactNamespaceContext.INSTANCE);
124
125 int num = facets != null ? facets.getLength() : 0;
126
127 logger.debug("Output has " + num + " facets.");
128
129 for (int i = 0; i < num; i++) {
130 Node facet = facets.item(i);
131
132 String name = XMLUtils.xpathString(
133 facet, "@art:name", ArtifactNamespaceContext.INSTANCE);
134
135 ManagedFacet item = new ManagedFacet(name, null, uuid, 1, 1);
136
137 addItem(outname, item);
138 }
139 }
140 }
141 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org