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