Mercurial > dive4elements > http-client
comparison src/main/java/de/intevation/artifacts/httpclient/utils/ArtifactProtocolUtils.java @ 0:a1db30b33f43
Moved the experimental branch 'work-on-gwt' of the console-client to an own module 'http-client' and removed the console-client.
http-client/trunk@1323 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 17 Feb 2011 10:31:40 +0000 |
parents | |
children | 73b211b559e2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a1db30b33f43 |
---|---|
1 /* | |
2 * Copyright (c) 2010 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 package de.intevation.artifacts.httpclient.utils; | |
9 | |
10 import java.util.Iterator; | |
11 import java.util.Map; | |
12 import java.util.Set; | |
13 | |
14 import javax.xml.xpath.XPathConstants; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 import org.w3c.dom.Document; | |
19 import org.w3c.dom.Element; | |
20 import org.w3c.dom.Node; | |
21 import org.w3c.dom.NodeList; | |
22 | |
23 import de.intevation.artifacts.httpclient.objects.Artifact; | |
24 import de.intevation.artifacts.httpclient.objects.ArtifactFactory; | |
25 | |
26 public class ArtifactProtocolUtils { | |
27 | |
28 private static final Logger logger = | |
29 Logger.getLogger(ArtifactProtocolUtils.class); | |
30 | |
31 | |
32 public static ArtifactFactory[] extractArtifactFactories(Document doc) { | |
33 NodeList elements = (NodeList) XMLUtils.getXPath( | |
34 doc, | |
35 "/art:result/art:factories/art:factory", | |
36 XPathConstants.NODESET, | |
37 ArtifactNamespaceContext.INSTANCE); | |
38 | |
39 if (elements == null || elements.getLength() == 0) { | |
40 return null; | |
41 } | |
42 | |
43 ArtifactFactory[] facs = new ArtifactFactory[elements.getLength()]; | |
44 | |
45 for (int idx = 0; idx < elements.getLength(); idx++) { | |
46 Node factory = elements.item(idx); | |
47 String desc = XMLUtils.getStringXPath(factory, "@art:description"); | |
48 String name = XMLUtils.getStringXPath(factory, "@art:name"); | |
49 | |
50 if (name != null) { | |
51 facs[idx] = new ArtifactFactory(name, desc); | |
52 } | |
53 } | |
54 | |
55 return facs; | |
56 } | |
57 | |
58 | |
59 public static Document createCreateDocument(String fis) { | |
60 Document document = XMLUtils.newDocument(); | |
61 | |
62 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
63 document, | |
64 ArtifactNamespaceContext.NAMESPACE_URI, | |
65 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
66 ); | |
67 | |
68 Element action = creator.create("action"); | |
69 | |
70 Element type = creator.create("type"); | |
71 type.setAttribute("name", "create"); | |
72 | |
73 Element factory = creator.create("factory"); | |
74 factory.setAttribute("name", fis); | |
75 | |
76 action.appendChild(type); | |
77 action.appendChild(factory); | |
78 document.appendChild(action); | |
79 | |
80 return document; | |
81 } | |
82 | |
83 | |
84 /** | |
85 * Returns a new artifact defined by uuid and hash values of the document | |
86 * returned by the artifact server after creating a new artifact. | |
87 * | |
88 * @param document Contains information about the server-side created | |
89 * artifact. | |
90 * @return a new artifact object. | |
91 */ | |
92 public static Artifact extractArtifact(Document document) { | |
93 String uuid = XMLUtils.getStringXPath( | |
94 document, | |
95 "/art:result/art:uuid/@value"); | |
96 | |
97 String hash = XMLUtils.getStringXPath( | |
98 document, | |
99 "/art:result/art:hash/@value"); | |
100 | |
101 logger.info("NEW Artifact: " + uuid + " / " + hash); | |
102 return new Artifact(uuid, hash); | |
103 } | |
104 | |
105 | |
106 private static Element createArtifactAction( | |
107 XMLUtils.ElementCreator creator, | |
108 Artifact artifact, | |
109 String artifactAction) | |
110 { | |
111 Element action = creator.create("action"); | |
112 | |
113 Element type = creator.create("type"); | |
114 type.setAttribute("name", artifactAction); | |
115 | |
116 Element uuid = creator.create("uuid"); | |
117 uuid.setAttribute("value", artifact.getUuid()); | |
118 | |
119 Element hash = creator.create("hash"); | |
120 hash.setAttribute("value", artifact.getHash()); | |
121 | |
122 action.appendChild(type); | |
123 action.appendChild(uuid); | |
124 action.appendChild(hash); | |
125 | |
126 return action; | |
127 } | |
128 | |
129 | |
130 public static Document createFeedDocument(Artifact artifact, Map attr) { | |
131 Document document = XMLUtils.newDocument(); | |
132 | |
133 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
134 document, | |
135 ArtifactNamespaceContext.NAMESPACE_URI, | |
136 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
137 ); | |
138 | |
139 Element action = createArtifactAction(creator, artifact, "feed"); | |
140 Element data = creator.create("data"); | |
141 | |
142 Set keys = attr.keySet(); | |
143 Iterator iter = keys.iterator(); | |
144 while (iter.hasNext()) { | |
145 String key = (String) iter.next(); | |
146 Object values = attr.get(key); | |
147 | |
148 if (values instanceof Object[]) { | |
149 appendInputNodes(creator, data, key, (Object[]) values); | |
150 } | |
151 else { | |
152 appendInputNodes(creator, data, key, values); | |
153 } | |
154 | |
155 } | |
156 | |
157 action.appendChild(data); | |
158 document.appendChild(action); | |
159 | |
160 return document; | |
161 } | |
162 | |
163 | |
164 private static void appendInputNodes( | |
165 XMLUtils.ElementCreator creator, | |
166 Element root, | |
167 String key, | |
168 Object value) | |
169 { | |
170 Element input = creator.create("input"); | |
171 input.setAttribute("name", key); | |
172 input.setAttribute("value", (String) value); | |
173 root.appendChild(input); | |
174 } | |
175 | |
176 | |
177 private static void appendInputNodes( | |
178 XMLUtils.ElementCreator creator, | |
179 Element root, | |
180 String key, | |
181 Object[] values) | |
182 { | |
183 for (Object value: values) { | |
184 Element input = creator.create("input"); | |
185 input.setAttribute("name", key); | |
186 input.setAttribute("value", (String) value); | |
187 root.appendChild(input); | |
188 } | |
189 } | |
190 | |
191 | |
192 public static Document createDescribeDocument(Artifact art, boolean ui) { | |
193 Document document = XMLUtils.newDocument(); | |
194 | |
195 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
196 document, | |
197 ArtifactNamespaceContext.NAMESPACE_URI, | |
198 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
199 ); | |
200 | |
201 Element action = createArtifactAction(creator, art, "describe"); | |
202 Element includeUi = creator.create("include-ui"); | |
203 includeUi.setTextContent(String.valueOf(ui)); | |
204 | |
205 action.appendChild(includeUi); | |
206 document.appendChild(action); | |
207 | |
208 return document; | |
209 } | |
210 | |
211 | |
212 public static Document createAdvanceDocument(Artifact art, String target) { | |
213 Document document = XMLUtils.newDocument(); | |
214 | |
215 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
216 document, | |
217 ArtifactNamespaceContext.NAMESPACE_URI, | |
218 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
219 ); | |
220 | |
221 Element action = createArtifactAction(creator, art, "advance"); | |
222 Element targetEle = creator.create("target"); | |
223 targetEle.setAttribute("name", target); | |
224 | |
225 action.appendChild(targetEle); | |
226 document.appendChild(action); | |
227 | |
228 return document; | |
229 } | |
230 | |
231 | |
232 public static Document createChartDocument(Artifact artifact, Map opts) { | |
233 Document document = XMLUtils.newDocument(); | |
234 | |
235 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
236 document, | |
237 ArtifactNamespaceContext.NAMESPACE_URI, | |
238 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
239 ); | |
240 | |
241 Element action = createArtifactAction(creator, artifact, "out"); | |
242 Element out = creator.create("out"); | |
243 out.setAttribute("name", "chart"); | |
244 | |
245 Element export = creator.create("export"); | |
246 export.setAttribute("name", "img"); | |
247 | |
248 Element mimetype = creator.create("mime-type"); | |
249 export.setAttribute("value", (String) opts.get("mime-type")); | |
250 | |
251 Element params = creator.create("params"); | |
252 | |
253 Element width = creator.create("input"); | |
254 width.setAttribute("name", "width"); | |
255 width.setAttribute("value", (String) opts.get("width")); | |
256 | |
257 Element height = creator.create("input"); | |
258 height.setAttribute("name", "height"); | |
259 height.setAttribute("value", (String) opts.get("height")); | |
260 | |
261 Element points = creator.create("input"); | |
262 points.setAttribute("name", "points"); | |
263 points.setAttribute("value", (String) opts.get("points")); | |
264 | |
265 params.appendChild(width); | |
266 params.appendChild(height); | |
267 params.appendChild(points); | |
268 | |
269 out.appendChild(export); | |
270 out.appendChild(mimetype); | |
271 out.appendChild(params); | |
272 | |
273 action.appendChild(out); | |
274 document.appendChild(action); | |
275 | |
276 return document; | |
277 } | |
278 } | |
279 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |