comparison flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java @ 944:c256061287d7

Simplified the code to read all provided Outputs of an Artifact. flys-artifacts/trunk@2357 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 18 Jul 2011 17:09:00 +0000
parents 9ff7e06bcb77
children 59ae2a823e73
comparison
equal deleted inserted replaced
943:5de90b0cff8e 944:c256061287d7
1 package de.intevation.flys.collections; 1 package de.intevation.flys.collections;
2 2
3 import java.util.ArrayList;
3 import java.util.HashMap; 4 import java.util.HashMap;
5 import java.util.List;
4 import java.util.Map; 6 import java.util.Map;
5
6 import javax.xml.xpath.XPathConstants;
7 7
8 import org.apache.log4j.Logger; 8 import org.apache.log4j.Logger;
9 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; 10 import de.intevation.artifacts.ArtifactDatabase;
16 import de.intevation.artifacts.ArtifactDatabaseException; 11 import de.intevation.artifacts.ArtifactDatabaseException;
17 import de.intevation.artifacts.ArtifactNamespaceContext; 12 import de.intevation.artifacts.CallContext;
18 import de.intevation.artifacts.CallMeta; 13 import de.intevation.artifacts.CallMeta;
19 14
20 import de.intevation.artifactdatabase.state.DefaultOutput; 15 import de.intevation.artifactdatabase.state.DefaultOutput;
16 import de.intevation.artifactdatabase.state.Facet;
21 import de.intevation.artifactdatabase.state.Output; 17 import de.intevation.artifactdatabase.state.Output;
22 18
23 import de.intevation.artifacts.common.utils.XMLUtils; 19 import de.intevation.flys.artifacts.FLYSArtifact;
24
25 import de.intevation.flys.artifacts.model.ManagedFacet; 20 import de.intevation.flys.artifacts.model.ManagedFacet;
26 21
27 22
28 public class OutputParser { 23 public class OutputParser {
29 24
34 29
35 private static Logger logger = Logger.getLogger(OutputParser.class); 30 private static Logger logger = Logger.getLogger(OutputParser.class);
36 31
37 protected ArtifactDatabase db; 32 protected ArtifactDatabase db;
38 protected CallMeta meta; 33 protected CallMeta meta;
34 protected CallContext context;
39 35
40 protected Map<String, Output> outs; 36 protected Map<String, Output> outs;
41 37
42 38
43 public OutputParser(ArtifactDatabase db, CallMeta meta) { 39 public OutputParser(ArtifactDatabase db, CallContext context) {
44 this.db = db; 40 this.db = db;
45 this.meta = meta; 41 this.meta = context.getMeta();
46 this.outs = new HashMap<String, Output>(); 42 this.context = context;
43 this.outs = new HashMap<String, Output>();
47 } 44 }
48 45
49 46
50 public void parse(String uuid) 47 public void parse(String uuid)
51 throws ArtifactDatabaseException 48 throws ArtifactDatabaseException
52 { 49 {
53 logger.debug("OutputParser.parse: " + uuid); 50 logger.debug("OutputParser.parse: " + uuid);
54 51
55 // XXX I am not sure if it works well every time with an empty 52 FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(uuid);
56 // document in the describe operation of an artifact.
57 Document description = db.describe(uuid, null, meta);
58 53
59 NodeList outs = (NodeList) XMLUtils.xpath( 54 List<Output> outList = flys.getOutputs(context);
60 description,
61 XPATH_ARTIFACT_OUTPUTMODES,
62 XPathConstants.NODESET,
63 ArtifactNamespaceContext.INSTANCE);
64 55
65 int num = outs != null ? outs.getLength() : 0; 56 for (Output out: outList) {
57 String name = out.getName();
66 58
67 logger.debug("Artifact has " + num + " outputs."); 59 Output o = outs.get(name);
60 int pos = 1;
68 61
69 for (int i = 0; i < num; i++) { 62 if (o == null) {
70 Element out = (Element)outs.item(i); 63 o = new DefaultOutput(
64 out.getName(),
65 out.getDescription(),
66 out.getMimeType(),
67 new ArrayList<Facet>(),
68 out.getType());
71 69
72 parseOutput(uuid, out); 70 outs.put(name, o);
71 }
72 else {
73 pos = o.getFacets().size() + 1;
74 }
75
76 List<Facet> facets = facet2ManagedFacet(uuid, out.getFacets(), pos);
77 o.addFacets(facets);
73 } 78 }
74 } 79 }
75 80
76 81
77 public Map<String, Output> getOuts() { 82 public Map<String, Output> getOuts() {
78 return outs; 83 return outs;
79 } 84 }
80 85
81 86
82 protected void addItem(String out, ManagedFacet item) { 87 protected List<Facet> facet2ManagedFacet(
83 Output o = outs.get(out); 88 String uuid,
89 List<Facet> old,
90 int pos)
91 {
92 List<Facet> newFacets = new ArrayList<Facet>(old.size());
84 93
85 if (o != null) { 94 for (Facet f: old) {
86 int num = o.getFacets().size(); 95 newFacets.add(new ManagedFacet(
87 item.setPosition(num+1); 96 f.getName(),
88 97 f.getIndex(),
89 o.addFacet(item); 98 f.getDescription(),
90 } 99 uuid, pos++, 1));
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 } 100 }
104 101
105 parseItems(uuid, out, name); 102 return newFacets;
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 String type = out.getAttributeNS(uri, "type");
116
117 Output o = new DefaultOutput(name, desc, mimetype, type);
118
119 outs.put(name, o);
120 }
121
122
123 protected void parseItems(String uuid, Node out, String outname) {
124 NodeList facets = (NodeList) XMLUtils.xpath(
125 out, "art:facets/art:facet",
126 XPathConstants.NODESET,
127 ArtifactNamespaceContext.INSTANCE);
128
129 int num = facets != null ? facets.getLength() : 0;
130
131 String uri = ArtifactNamespaceContext.NAMESPACE_URI;
132
133 logger.debug("Output has " + num + " facets.");
134
135 for (int i = 0; i < num; i++) {
136 Element facet = (Element) facets.item(i);
137
138 String name = facet.getAttributeNS(uri, "name");
139 String desc = facet.getAttributeNS(uri, "description");
140 String index = facet.getAttributeNS(uri, "index");
141
142 ManagedFacet item = new ManagedFacet(
143 name, Integer.parseInt(index), desc, uuid, 1, 1);
144
145 addItem(outname, item);
146 }
147 } 103 }
148 } 104 }
149 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org