1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import org.w3c.dom.Node;
|
---|
4 | import org.w3c.dom.Element;
|
---|
5 | import org.w3c.dom.NodeList;
|
---|
6 | import org.w3c.dom.Document;
|
---|
7 | import org.w3c.dom.Text;
|
---|
8 |
|
---|
9 | import java.util.HashMap;
|
---|
10 | //import java.util.Locale;
|
---|
11 |
|
---|
12 | /** various functions for extracting info out of GS XML */
|
---|
13 | public class GSXML {
|
---|
14 |
|
---|
15 | // greenstone xml elements
|
---|
16 | public static final String MESSAGE_ELEM = "message";
|
---|
17 | public static final String REQUEST_ELEM = "request";
|
---|
18 | public static final String RESPONSE_ELEM = "response";
|
---|
19 | public static final String COLLECTION_ELEM = "collection";
|
---|
20 | public static final String SERVICE_ELEM = "service";
|
---|
21 | public static final String CLUSTER_ELEM = "serviceCluster";
|
---|
22 | public static final String SITE_ELEM = "site";
|
---|
23 | public static final String PARAM_ELEM = "param";
|
---|
24 | public static final String PARAM_OPTION_ELEM = "option";
|
---|
25 | public static final String CONTENT_ELEM = "content";
|
---|
26 | public static final String RESOURCE_ELEM = "resource";
|
---|
27 | public static final String DOCUMENT_ELEM = "document";
|
---|
28 | public static final String METADATA_ELEM = "metadata";
|
---|
29 | public static final String SERVICE_CLASS_ELEM = "serviceRack";
|
---|
30 | public static final String CLASSIFIER_ELEM = "classifier";
|
---|
31 | public static final String APPLET_ELEM = "applet";
|
---|
32 | public static final String APPLET_DATA_ELEM = "appletData";
|
---|
33 | public static final String CONFIGURE_ELEM = "configure";
|
---|
34 | public static final String STATUS_ELEM = "status";
|
---|
35 | public static final String ERROR_ELEM = "error";
|
---|
36 | public static final String DEFAULT_ELEM = "default";
|
---|
37 | // elems for the pages to be processed by xslt
|
---|
38 | public final static String PAGE_ELEM = "page";
|
---|
39 | public final static String TRANSLATION_ELEM = "translate";
|
---|
40 | public final static String CONFIGURATION_ELEM = "config";
|
---|
41 | public final static String DESCRIPTION_ELEM = "description";
|
---|
42 |
|
---|
43 | public static final String SITE_NAME_ELEM = "localSiteName";
|
---|
44 | // add on to another elem type to get a list of that type
|
---|
45 | public static final String LIST_MODIFIER = "List";
|
---|
46 |
|
---|
47 | //public static final String
|
---|
48 | //public static final String
|
---|
49 |
|
---|
50 | // greenstone xml attributes
|
---|
51 | public static final String NAME_ATT = "name";
|
---|
52 | public static final String TO_ATT = "to";
|
---|
53 | public static final String FROM_ATT = "from";
|
---|
54 | public static final String LANG_ATT = "lang";
|
---|
55 | public static final String TYPE_ATT = "type";
|
---|
56 | public static final String VALUE_ATT = "value";
|
---|
57 | public static final String DEFAULT_ATT = "default";
|
---|
58 | public static final String INFO_ATT = "info";
|
---|
59 | public static final String ACTION_ATT = "action";
|
---|
60 | public static final String SUBACTION_ATT = "subaction";
|
---|
61 | public static final String ADDRESS_ATT = "address";
|
---|
62 | public static final String STATUS_ERROR_CODE_ATT = "code";
|
---|
63 | public static final String STATUS_PROCESS_ID_ATT = "handle";
|
---|
64 | public static final String PARAM_SHORTNAME_ATT = "shortname";
|
---|
65 | public static final String PARAM_IGNORE_POS_ATT = "ignore";
|
---|
66 |
|
---|
67 | // parameter types
|
---|
68 | public static final String PARAM_TYPE_INTEGER = "integer";
|
---|
69 | public static final String PARAM_TYPE_BOOLEAN = "boolean";
|
---|
70 | public static final String PARAM_TYPE_ENUM = "enum";
|
---|
71 | public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
|
---|
72 | public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
|
---|
73 | public static final String PARAM_TYPE_STRING = "string";
|
---|
74 | public static final String PARAM_TYPE_MULTI = "multi";
|
---|
75 |
|
---|
76 | // stuff for text strings
|
---|
77 | public static final String DISPLAY_ELEM = "display";
|
---|
78 | public static final String DISPLAY_NAME_ELEM = "name";
|
---|
79 | public static final String DISPLAY_SUBMIT_ELEM = "submit";
|
---|
80 |
|
---|
81 | // request types - do we need all these? or just
|
---|
82 | // describe, status, and one to say do teh request
|
---|
83 | public static final String REQUEST_TYPE_DESCRIBE = "describe";
|
---|
84 | public static final String REQUEST_TYPE_CONFIGURE = "configure";
|
---|
85 | public static final String REQUEST_TYPE_QUERY = "query";
|
---|
86 | public static final String REQUEST_TYPE_ACTION = "action";
|
---|
87 | public static final String REQUEST_TYPE_BUILD = "build";
|
---|
88 | public static final String REQUEST_TYPE_STATUS = "status";
|
---|
89 | public static final String REQUEST_TYPE_PROCESS = "process";
|
---|
90 |
|
---|
91 | // service types
|
---|
92 | public static final String SERVICE_TYPE_QUERY = "query";
|
---|
93 | public static final String SERVICE_TYPE_BUILD = "build";
|
---|
94 |
|
---|
95 | // configure types
|
---|
96 | public static final String CONFIG_ACTION_ACTIVATE = "activate";
|
---|
97 | public static final String CONFIG_ACTION_DEACTIVATE = "deactivate";
|
---|
98 |
|
---|
99 | // communicator types
|
---|
100 | public static final String COMM_TYPE_SOAP_JAVA = "soap";
|
---|
101 |
|
---|
102 | // takes a node with a resource elements inside it and extracts all the
|
---|
103 | // HASh oids - name att for resource
|
---|
104 | public static String [] getDocumentNameList(Element content) {
|
---|
105 |
|
---|
106 | Node n = content.getFirstChild();
|
---|
107 | while (n!=null && !n.getNodeName().equals(DOCUMENT_ELEM+LIST_MODIFIER)) {
|
---|
108 | n = n.getNextSibling();
|
---|
109 | }
|
---|
110 | if (n==null) { // no docs found
|
---|
111 | return null;
|
---|
112 | }
|
---|
113 |
|
---|
114 | NodeList docs = n.getChildNodes();
|
---|
115 |
|
---|
116 | int numdocs = docs.getLength();
|
---|
117 | String []ids = new String[numdocs];
|
---|
118 | for (int i=0; i<numdocs; i++) {
|
---|
119 | Element e = (Element)docs.item(i);
|
---|
120 | String id = e.getAttribute(NAME_ATT);
|
---|
121 | // check that its a valid id - ie starts with HASH
|
---|
122 | // need to change this if use different ids
|
---|
123 |
|
---|
124 | ids[i] = id;
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | return ids;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** extracts metadata names out of an element */
|
---|
132 | public static String [] getMetaNameList(Element content) {
|
---|
133 | Node n = content.getFirstChild();
|
---|
134 | while (n!=null &&
|
---|
135 | !n.getNodeName().equals(METADATA_ELEM+LIST_MODIFIER)) {
|
---|
136 | n = n.getNextSibling();
|
---|
137 | }
|
---|
138 | if (n==null) { // no metadatas found
|
---|
139 | return null;
|
---|
140 | }
|
---|
141 | NodeList elems = n.getChildNodes();
|
---|
142 |
|
---|
143 | int numelems = elems.getLength();
|
---|
144 | String []ids = new String[numelems];
|
---|
145 | for (int i=0; i<numelems; i++) {
|
---|
146 | Element e = (Element)elems.item(i);
|
---|
147 | String id = e.getAttribute(NAME_ATT);
|
---|
148 | ids[i] = id;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return ids;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** takes a paramList element, and gets a HashMap of name-value pairs */
|
---|
155 | public static HashMap extractParams(Element xml) {
|
---|
156 |
|
---|
157 | if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
|
---|
158 | System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
|
---|
159 | return null;
|
---|
160 | }
|
---|
161 | NodeList params = xml.getChildNodes();
|
---|
162 | HashMap param_map = new HashMap();
|
---|
163 | for (int i=0; i<params.getLength(); i++) {
|
---|
164 | Element param = (Element)params.item(i);
|
---|
165 | String name=param.getAttribute(NAME_ATT);
|
---|
166 | String value=param.getAttribute(VALUE_ATT);
|
---|
167 | if (value.equals("")) { // the value is in the content of the param
|
---|
168 | value=getNodeText(param);
|
---|
169 | }
|
---|
170 | param_map.put(name, value);
|
---|
171 |
|
---|
172 | }
|
---|
173 | return param_map;
|
---|
174 | }
|
---|
175 | /** takes a paramList element, and gets a HashMap of name-value pairs */
|
---|
176 | public static HashMap extractAllParams(Element xml) {
|
---|
177 |
|
---|
178 | if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
|
---|
179 | System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
|
---|
180 | return null;
|
---|
181 | }
|
---|
182 | NodeList params = xml.getElementsByTagName(PARAM_ELEM);
|
---|
183 | HashMap param_map = new HashMap();
|
---|
184 | for (int i=0; i<params.getLength(); i++) {
|
---|
185 | Element param = (Element)params.item(i);
|
---|
186 | String name=param.getAttribute(NAME_ATT);
|
---|
187 | String value=param.getAttribute(VALUE_ATT);
|
---|
188 | if (value.equals("")) { // the value is in the content of the param
|
---|
189 | value=getNodeText(param);
|
---|
190 | }
|
---|
191 | param_map.put(name, value);
|
---|
192 |
|
---|
193 | }
|
---|
194 | return param_map;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public static String getValue(Element e) {
|
---|
198 | String val = e.getAttribute(VALUE_ATT);
|
---|
199 | if (val ==null || val.equals("")) {
|
---|
200 | // have to get it out of the text
|
---|
201 | val=getNodeText(e);
|
---|
202 |
|
---|
203 | }
|
---|
204 | return val;
|
---|
205 | }
|
---|
206 | /** extracts the text out of a node */
|
---|
207 | public static String getNodeText(Element param) {
|
---|
208 | param.normalize();
|
---|
209 | Node n = param.getFirstChild();
|
---|
210 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
211 | n=n.getNextSibling();
|
---|
212 | }
|
---|
213 | if (n==null) { // no text node
|
---|
214 | return "";
|
---|
215 | }
|
---|
216 | return n.getNodeValue();
|
---|
217 | }
|
---|
218 | /** creates a new document Element */
|
---|
219 | public static Element createDocumentElement(Document owner, String oid) {
|
---|
220 | Element e = owner.createElement(DOCUMENT_ELEM);
|
---|
221 | e.setAttribute(NAME_ATT, oid);
|
---|
222 |
|
---|
223 | return e;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /** add text to a document/subsection element */
|
---|
227 | public static boolean addDocText(Document owner, Element doc, String text) {
|
---|
228 |
|
---|
229 | Element content = owner.createElement(CONTENT_ELEM);
|
---|
230 | Text t = owner.createTextNode(text);
|
---|
231 | content.appendChild(t);
|
---|
232 | doc.appendChild(content);
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** add an error message */
|
---|
237 | public static boolean addError(Document owner, Element doc, String text) {
|
---|
238 |
|
---|
239 | Element content = owner.createElement(ERROR_ELEM);
|
---|
240 | Text t = owner.createTextNode(text);
|
---|
241 | content.appendChild(t);
|
---|
242 |
|
---|
243 | return true;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** add an error message */
|
---|
247 | public static boolean addError(Document owner, Element doc, Throwable error) {
|
---|
248 | error.printStackTrace();
|
---|
249 | return addError(owner, doc, error.toString());
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
|
---|
253 | public static Element addMetaList(Document owner, Element doc) {
|
---|
254 | Element list = owner.createElement(METADATA_ELEM+LIST_MODIFIER);
|
---|
255 | doc.appendChild(list);
|
---|
256 | return list;
|
---|
257 | }
|
---|
258 | /** adds a metadata elem to a list */
|
---|
259 | public static boolean addMetadata(Document owner, Element list,
|
---|
260 | String meta_name, String meta_value) {
|
---|
261 | if (meta_value==null || meta_value.equals("")) {
|
---|
262 | return false;
|
---|
263 | }
|
---|
264 | Element data = owner.createElement(METADATA_ELEM);
|
---|
265 | data.setAttribute(NAME_ATT, meta_name);
|
---|
266 | Text t = owner.createTextNode(meta_value);
|
---|
267 | data.appendChild(t);
|
---|
268 | list.appendChild(data);
|
---|
269 | return true;
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | public static boolean mergeMetadataLists(Node to, Node from) {
|
---|
274 | Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
|
---|
275 | Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
|
---|
276 |
|
---|
277 | if (from_meta == null) { // nothing to copy
|
---|
278 | return true;
|
---|
279 | }
|
---|
280 | Document to_owner = to.getOwnerDocument();
|
---|
281 | Node new_from = to_owner.importNode(from_meta, true);
|
---|
282 |
|
---|
283 | if (to_meta == null) { // just copy the whole list
|
---|
284 | to.appendChild(new_from);
|
---|
285 | return true;
|
---|
286 | }
|
---|
287 |
|
---|
288 | // copy individual elements
|
---|
289 | Node child = new_from.getFirstChild();
|
---|
290 | while ( child != null) {
|
---|
291 | to_meta.appendChild(child);
|
---|
292 | child = child.getNextSibling();
|
---|
293 | }
|
---|
294 | return true;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** takes two nodes - should have the same node type, tag name, etc
|
---|
298 | * copies the info from 'from' into 'to'
|
---|
299 | -- not finished haven't thought through the algo yet.*/
|
---|
300 | /*
|
---|
301 | public static boolean mergeElements(Element to, Element from) {
|
---|
302 |
|
---|
303 | if (to.getNodeName()!=from.getNodeName()) {
|
---|
304 | System.err.println("cant merge two nodes with different names");
|
---|
305 | return false;
|
---|
306 | }
|
---|
307 |
|
---|
308 | // copy any atts over - ignore for now
|
---|
309 |
|
---|
310 | // copy the children
|
---|
311 | if (!from.hasChildNodes()) {
|
---|
312 | return true;
|
---|
313 | }
|
---|
314 |
|
---|
315 | // check if they belong to the same document
|
---|
316 | Document to_doc = to.getOwnerDocument();
|
---|
317 | Document from_doc = from.getOwnerDocument();
|
---|
318 | Element newfrom;
|
---|
319 | if (to_doc != from_doc) {
|
---|
320 | nfrom = to_doc.importNode(from, true);
|
---|
321 | } else {
|
---|
322 | newfrom = from;
|
---|
323 | }
|
---|
324 |
|
---|
325 | if (!to.hasChildNodes()) {
|
---|
326 | // just copy all the children over
|
---|
327 | Node child = newfrom.getFirstChild();
|
---|
328 | while (child !=null) {
|
---|
329 | to.appendChild(child);
|
---|
330 | child = child.getNextSibling();
|
---|
331 | }
|
---|
332 | return true;
|
---|
333 | }
|
---|
334 |
|
---|
335 | // have to check each one to see if already present or not
|
---|
336 | HashMap children = getChildrenMap(to);
|
---|
337 |
|
---|
338 | Node child = newfrom.getFirstChild();
|
---|
339 | while (child !=null) {
|
---|
340 | String name = child.getNodeName();
|
---|
341 | Node n = map.get(name);
|
---|
342 | if (n==null) { // there is no elem by that name in to
|
---|
343 | to.appendChild(child);
|
---|
344 | }
|
---|
345 | else {
|
---|
346 | }
|
---|
347 | return true;
|
---|
348 | }
|
---|
349 | */
|
---|
350 | /** returns the (first) child element with the given name */
|
---|
351 | public static Node getChildByTagName(Node n, String name) {
|
---|
352 |
|
---|
353 | Node child = n.getFirstChild();
|
---|
354 | while (child!=null) {
|
---|
355 | if (child.getNodeName().equals(name)) {
|
---|
356 | return child;
|
---|
357 | }
|
---|
358 | child = child.getNextSibling();
|
---|
359 | }
|
---|
360 | return null; //not found
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** takes an xpath type expression of the form name/name/...
|
---|
364 | and returns the first node that matches, or null if not found */
|
---|
365 | public static Node getNodeByPath(Node n, String path) {
|
---|
366 |
|
---|
367 | String link = GSPath.getFirstLink(path);
|
---|
368 | path = GSPath.removeFirstLink(path);
|
---|
369 | while (!link.equals("")) {
|
---|
370 | n = getChildByTagName(n, link);
|
---|
371 | if (n==null) {
|
---|
372 | return null;
|
---|
373 | }
|
---|
374 | link = GSPath.getFirstLink(path);
|
---|
375 | path = GSPath.removeFirstLink(path);
|
---|
376 | }
|
---|
377 | return n;
|
---|
378 | }
|
---|
379 | public static HashMap getChildrenMap(Node n) {
|
---|
380 |
|
---|
381 | HashMap map= new HashMap();
|
---|
382 | Node child = n.getFirstChild();
|
---|
383 | while (child!=null) {
|
---|
384 | String name = child.getNodeName();
|
---|
385 | map.put(name, child);
|
---|
386 | child = child.getNextSibling();
|
---|
387 | }
|
---|
388 | return map;
|
---|
389 | }
|
---|
390 |
|
---|
391 | public static Element createTextElement(Document owner, String elem_name,
|
---|
392 | String text) {
|
---|
393 | Element e = owner.createElement(elem_name);
|
---|
394 | Text t = owner.createTextNode(text);
|
---|
395 | e.appendChild(t);
|
---|
396 | return e;
|
---|
397 |
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | public static Element createParameter(Document owner, String name,
|
---|
402 | String type, String default_value,
|
---|
403 | String []options) {
|
---|
404 |
|
---|
405 |
|
---|
406 | Element p = owner.createElement(PARAM_ELEM);
|
---|
407 | p.setAttribute(NAME_ATT, name);
|
---|
408 | p.setAttribute(TYPE_ATT, type);
|
---|
409 | if (default_value != null) {
|
---|
410 | p.setAttribute(DEFAULT_ATT, default_value);
|
---|
411 | }
|
---|
412 | if (type.startsWith(PARAM_TYPE_ENUM) && options!=null) {
|
---|
413 | for (int i=0; i<options.length; i++) {
|
---|
414 | Element e = owner.createElement(PARAM_OPTION_ELEM);
|
---|
415 | e.setAttribute(NAME_ATT, options[i]);
|
---|
416 | p.appendChild(e);
|
---|
417 | }
|
---|
418 | }
|
---|
419 | return p;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** creates a parm elem containing the text strings
|
---|
423 | * <param><name>xxx<name><option name='x'>yyy</option><option name='y'>zzz</option></param>
|
---|
424 | */
|
---|
425 | public static Element createParameterDisplay(Document owner, String name,
|
---|
426 | String name_text,
|
---|
427 | String []options,
|
---|
428 | String []option_texts) {
|
---|
429 |
|
---|
430 |
|
---|
431 | Element param = owner.createElement(PARAM_ELEM);
|
---|
432 | param.setAttribute(NAME_ATT, name);
|
---|
433 | param.appendChild(createTextElement(owner, DISPLAY_NAME_ELEM, name_text));
|
---|
434 | if (options != null) {
|
---|
435 | for (int i=0; i<options.length; i++) {
|
---|
436 | Element e = GSXML.createTextElement(owner, PARAM_OPTION_ELEM, option_texts[i]);
|
---|
437 | e.setAttribute(NAME_ATT, options[i]);
|
---|
438 | param.appendChild(e);
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | return param;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** returns the element parent/node_name[@attribute_name='attribute_value']
|
---|
446 | */
|
---|
447 | public static Element getNamedElement(Element parent, String node_name,
|
---|
448 | String attribute_name,
|
---|
449 | String attribute_value) {
|
---|
450 |
|
---|
451 | NodeList children = parent.getChildNodes();
|
---|
452 | for (int i=0; i<children.getLength(); i++) {
|
---|
453 | Node child = children.item(i);
|
---|
454 | if (child.getNodeName().equals(node_name)) {
|
---|
455 | if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
|
---|
456 | return (Element)child;
|
---|
457 | }
|
---|
458 | }
|
---|
459 | // not found
|
---|
460 | return null;
|
---|
461 | }
|
---|
462 |
|
---|
463 | // replaces < > " ' & in the original with their entities
|
---|
464 | public static String xmlSafe(String original) {
|
---|
465 |
|
---|
466 | StringBuffer filtered = new StringBuffer(original.length());
|
---|
467 | char c;
|
---|
468 | for (int i=0; i<original.length(); i++) {
|
---|
469 | c = original.charAt(i);
|
---|
470 | if (c == '>') {
|
---|
471 | filtered.append(">");
|
---|
472 | } else if (c == '<') {
|
---|
473 | filtered.append("<");
|
---|
474 | } else if (c == '"') {
|
---|
475 | filtered.append(""");
|
---|
476 | } else if (c == '&') {
|
---|
477 | filtered.append("&");
|
---|
478 | } else if (c == '\'') {
|
---|
479 | filtered.append("'");
|
---|
480 | } else {
|
---|
481 | filtered.append(c);
|
---|
482 | }
|
---|
483 | }
|
---|
484 | return filtered.toString();
|
---|
485 | }
|
---|
486 | }
|
---|