source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java@ 6864

Last change on this file since 6864 was 6864, checked in by kjdon, 20 years ago

added some new element names

  • Property svn:keywords set to Author Date Id Revision
File size: 20.7 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import org.w3c.dom.NamedNodeMap;
4import org.w3c.dom.Node;
5import org.w3c.dom.Element;
6import org.w3c.dom.NodeList;
7import org.w3c.dom.Document;
8import org.w3c.dom.Text;
9
10import java.util.Map;
11import java.util.Set;
12import java.util.HashMap;
13import java.util.Vector;
14import java.util.Iterator;
15//import java.util.Locale;
16
17/** various functions for extracting info out of GS XML */
18public class GSXML {
19
20 // greenstone xml elements
21 public static final String MESSAGE_ELEM = "message";
22 public static final String REQUEST_ELEM = "request";
23 public static final String RESPONSE_ELEM = "response";
24 public static final String COLLECTION_ELEM = "collection";
25 public static final String SERVICE_ELEM = "service";
26 public static final String CLUSTER_ELEM = "serviceCluster";
27 public static final String SITE_ELEM = "site";
28 public static final String PARAM_ELEM = "param";
29 public static final String PARAM_OPTION_ELEM = "option";
30 public static final String CONTENT_ELEM = "content";
31 public static final String RESOURCE_ELEM = "resource";
32 public static final String DOCUMENT_ELEM = "document";
33 public static final String METADATA_ELEM = "metadata";
34 public static final String SERVICE_CLASS_ELEM = "serviceRack";
35 public static final String CLASSIFIER_ELEM = "classifier";
36 public static final String APPLET_ELEM = "applet";
37 public static final String APPLET_DATA_ELEM = "appletData";
38 public static final String CONFIGURE_ELEM = "configure";
39 public static final String STATUS_ELEM = "status";
40 public static final String ERROR_ELEM = "error";
41 public static final String DEFAULT_ELEM = "default";
42 public static final String STYLESHEET_ELEM = "format";//"stylesheet"; // any additional stylesheet stuff is carried in the message inside this elem
43 public static final String FORMAT_ELEM = "format"; // config files use format - should we use this instead of stylesheet??
44 public static final String TERM_ELEM = "term";
45 public static final String SYSTEM_ELEM = "system";
46
47 //config file elems
48 public static final String COLLECTION_CONFIG_ELEM = "collectionConfig";
49 public static final String COLLECTION_BUILD_ELEM = "buildConfig";
50 public static final String COLLECTION_INIT_ELEM = "collectionInit";
51 public static final String INDEX_ELEM = "index";
52 public static final String SEARCH_ELEM = "search";
53 public static final String BROWSE_ELEM = "browse";
54 public static final String DISPLAY_ELEM = "display";
55 public static final String FIELD_ELEM = "field";
56 public static final String LEVEL_ELEM = "level";
57 public static final String SHORTNAME_ATT = "shortname";
58
59 // elems for the pages to be processed by xslt
60 public final static String PAGE_ELEM = "page";
61 public final static String CONFIGURATION_ELEM = "config";
62 public final static String PAGE_REQUEST_ELEM = "pageRequest";
63 public final static String PAGE_RESPONSE_ELEM = "pageResponse";
64 public final static String PAGE_EXTRA_ELEM = "pageExtra";
65
66 //public final static String DESCRIPTION_ELEM = "description";
67
68 public static final String SITE_NAME_ELEM = "localSiteName";
69 public static final String SITE_HTTP_ADDRESS_ELEM = "httpAddress";
70 public static final String ACTION_ELEM = "action";
71 public static final String SUBACTION_ELEM = "subaction";
72
73 // add on to another elem type to get a list of that type
74 public static final String LIST_MODIFIER = "List";
75
76 // greenstone xml attributes
77 public static final String NAME_ATT = "name";
78 public static final String TO_ATT = "to";
79 public static final String USER_ID_ATT = "uid";
80 public static final String FROM_ATT = "from";
81 public static final String LANG_ATT = "lang";
82 public static final String TYPE_ATT = "type";
83 public static final String VALUE_ATT = "value";
84 public static final String DEFAULT_ATT = "default";
85 public static final String INFO_ATT = "info";
86 public static final String ACTION_ATT = "action";
87 public static final String SUBACTION_ATT = "subaction";
88 public static final String OUTPUT_ATT = "output";
89 public static final String ADDRESS_ATT = "address";
90 public static final String STATUS_ERROR_CODE_ATT = "code";
91 public static final String STATUS_PROCESS_ID_ATT = "pid";
92 public static final String PARAM_SHORTNAME_ATT = "shortname";
93 public static final String PARAM_IGNORE_POS_ATT = "ignore";
94 public static final String CLASSIFIER_CONTENT_ATT = "content";
95
96 // document stuff
97 public static final String DOC_TYPE_ATT = "docType";
98 public static final String DOC_NODE_ELEM = "documentNode";
99 public static final String NODE_CONTENT_ELEM = "nodeContent";
100 public static final String NODE_STRUCTURE_ELEM = "nodeStructure";
101 public static final String NODE_ID_ATT = "nodeID";
102 public static final String NODE_NAME_ATT = "nodeName";
103 public static final String NODE_TYPE_ATT = "nodeType";
104
105 public static final String NODE_TYPE_ROOT = "root";
106 public static final String NODE_TYPE_INTERIOR = "interior";
107 public static final String NODE_TYPE_LEAF = "leaf";
108
109 // classifier stuff
110 public static final String CLASS_NODE_ELEM = "classifierNode";
111 public static final String CLASS_NODE_ORIENTATION_ATT = "orientation";
112
113 // parameter types
114 public static final String PARAM_TYPE_INTEGER = "integer";
115 public static final String PARAM_TYPE_BOOLEAN = "boolean";
116 public static final String PARAM_TYPE_ENUM_START = "enum";
117 public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
118 public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
119 public static final String PARAM_TYPE_STRING = "string";
120 public static final String PARAM_TYPE_TEXT = "text";
121 public static final String PARAM_TYPE_MULTI = "multi";
122 public static final String PARAM_TYPE_FILE = "file";
123 public static final String PARAM_TYPE_INVISIBLE = "invisible";
124 // stuff for text strings
125 public static final String DISPLAY_TEXT_ELEM = "displayItem";
126 // the following are used for the name attributes
127 public static final String DISPLAY_TEXT_NAME = "name";
128 public static final String DISPLAY_TEXT_SUBMIT = "submit";
129 public static final String DISPLAY_TEXT_DESCRIPTION = "description";
130
131 // request types
132 // get the module description
133 public static final String REQUEST_TYPE_DESCRIBE = "describe";
134 // startup a process
135 public static final String REQUEST_TYPE_PROCESS = "process";
136 // get the status of an ongoing process
137 public static final String REQUEST_TYPE_STATUS = "status";
138 // system type request - eg reload a collection
139 public static final String REQUEST_TYPE_SYSTEM = "system";
140 // page requests to the Receptionist/Actions
141 public static final String REQUEST_TYPE_PAGE = "page"; // used to be cgi
142 // get any format info for a service
143 public static final String REQUEST_TYPE_FORMAT = "format";
144
145 // service types
146 public static final String SERVICE_TYPE_QUERY = "query";
147 public static final String SERVICE_TYPE_RETRIEVE = "retrieve";
148 public static final String SERVICE_TYPE_BROWSE = "browse";
149 public static final String SERVICE_TYPE_APPLET = "applet";
150 public static final String SERVICE_TYPE_PROCESS = "process";
151 public static final String SERVICE_TYPE_ENRICH = "enrich";
152
153 // system command types and attributes
154 public static final String SYSTEM_TYPE_CONFIGURE = "configure";
155 public static final String SYSTEM_TYPE_ACTIVATE = "activate";
156 public static final String SYSTEM_TYPE_DEACTIVATE = "deactivate";
157
158 public static final String SYSTEM_SUBSET_ATT = "subset";
159 public static final String SYSTEM_MODULE_TYPE_ATT = "moduleType";
160 public static final String SYSTEM_MODULE_NAME_ATT = "moduleName";
161
162 // communicator types
163 public static final String COMM_TYPE_SOAP_JAVA = "soap";
164
165 // some system wide param names
166 public static final String SUBSET_PARAM = "subset";
167
168 /** takes a list of elements, and returns an array of strings
169 * of the values of attribute att_name */
170 public static String [] getAttributeValuesFromList(Element list,
171 String att_name) {
172
173 NodeList children = list.getChildNodes();
174
175 int num_nodes = children.getLength();
176 String []ids = new String[num_nodes];
177 for (int i=0; i<num_nodes; i++) {
178 Element e = (Element)children.item(i);
179 String id = e.getAttribute(att_name);
180 ids[i] = id;
181 }
182
183 return ids;
184 }
185
186 /** takes a paramList element, and gets a HashMap of name-value pairs
187 * if deep=true, extracts embedded params, otherwise just top level
188 * params*/
189 public static HashMap extractParams(Element xml, boolean deep) {
190
191 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
192 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
193 return null;
194 }
195
196 NodeList params = null;
197 if (deep) { // get all the nested ones
198 params = xml.getElementsByTagName(PARAM_ELEM);
199 } else { // just get the top level ones
200 params = xml.getChildNodes();
201 }
202 HashMap param_map = new HashMap();
203 for (int i=0; i<params.getLength(); i++) {
204 if (params.item(i).getNodeName().equals(PARAM_ELEM)) {
205 Element param = (Element)params.item(i);
206 String name=param.getAttribute(NAME_ATT);
207 String value=getValue(param); //att or content
208 int pos = name.indexOf('.');
209 if (pos == -1) { // a base param
210 param_map.put(name, value);
211 } else { // a namespaced param
212
213 String namespace = name.substring(0, pos);
214 name = name.substring(pos+1);
215 HashMap map = (HashMap)param_map.get(namespace);
216 if (map == null) {
217 map = new HashMap();
218 param_map.put(namespace, map);
219 }
220 map.put(name, value);
221 }
222 }
223 }
224 return param_map;
225 }
226
227 /** gets the value att or the text content */
228 public static String getValue(Element e) {
229 String val = e.getAttribute(VALUE_ATT);
230 if (val ==null || val.equals("")) {
231 // have to get it out of the text
232 val=getNodeText(e);
233
234 }
235 return val;
236 }
237
238 /** extracts the text out of a node */
239 public static Node getNodeTextNode(Element param) {
240 param.normalize();
241 Node n = param.getFirstChild();
242 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
243 n=n.getNextSibling();
244 }
245 return n;
246 }
247
248 /** extracts the text out of a node */
249 public static String getNodeText(Element param) {
250 Node text_node = getNodeTextNode(param);
251 if (text_node == null) {
252 return "";
253 }
254 return text_node.getNodeValue();
255 }
256
257
258 /** add text to a document/subsection element */
259 public static boolean addDocText(Document owner, Element doc, String text) {
260
261 Element content = owner.createElement(NODE_CONTENT_ELEM);
262 Text t = owner.createTextNode(text);
263 content.appendChild(t);
264 doc.appendChild(content);
265 return true;
266 }
267
268 /** add an error message */
269 public static boolean addError(Document owner, Element doc, String text) {
270
271 Element content = owner.createElement(ERROR_ELEM);
272 Text t = owner.createTextNode(text);
273 content.appendChild(t);
274
275 return true;
276 }
277
278 /** add an error message */
279 public static boolean addError(Document owner, Element doc, Throwable error) {
280 error.printStackTrace();
281 return addError(owner, doc, error.toString());
282 }
283
284 public static Element createMetadataParamList(Document owner, Vector meta_values) {
285
286 Element meta_param_list = owner.createElement(PARAM_ELEM+LIST_MODIFIER);
287 Iterator i = meta_values.iterator();
288 while(i.hasNext()) {
289 String next = (String)i.next();
290 Element meta_param = owner.createElement(PARAM_ELEM);
291 meta_param_list.appendChild(meta_param);
292 meta_param.setAttribute(NAME_ATT, "metadata");
293 meta_param.setAttribute(VALUE_ATT, next);
294 }
295 return meta_param_list;
296 }
297
298 /** adds a metadata elem to a list */
299 public static boolean addMetadata(Document owner, Element list,
300 String meta_name, String meta_value) {
301 if (meta_value==null || meta_value.equals("")) {
302 return false;
303 }
304 Element data = owner.createElement(METADATA_ELEM);
305 data.setAttribute(NAME_ATT, meta_name);
306 Text t = owner.createTextNode(meta_value);
307 data.appendChild(t);
308 list.appendChild(data);
309 return true;
310
311 }
312
313 /** copies the metadata out of teh metadataList of 'from' into
314 * the metadataList of 'to' */
315 public static boolean mergeMetadataLists(Node to, Node from) {
316 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
317 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
318
319 if (from_meta == null) { // nothing to copy
320 return true;
321 }
322 Document to_owner = to.getOwnerDocument();
323 Node new_from = to_owner.importNode(from_meta, true);
324
325 if (to_meta == null) { // just copy the whole list
326 to.appendChild(new_from);
327 return true;
328 }
329
330 // copy individual elements
331 Node child = new_from.getFirstChild();
332 while ( child != null) {
333 to_meta.appendChild(child);
334 child = child.getNextSibling();
335 }
336 return true;
337 }
338
339 /** copies all the children from from to to */
340 public static boolean mergeElements(Element to, Element from) {
341
342 Document owner = to.getOwnerDocument();
343 Node child = from.getFirstChild();
344 while (child != null) {
345 to.appendChild(owner.importNode(child, true));
346 child = child.getNextSibling();
347 }
348 return true;
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
392 /** Duplicates an element, but gives it a new name */
393 public static Element duplicateWithNewName(Document owner, Element element,
394 String element_name, boolean with_attributes)
395 {
396 Element duplicate = owner.createElement(element_name);
397
398 // Copy element attributes
399 if (with_attributes) {
400 NamedNodeMap attributes = element.getAttributes();
401 for (int i = 0; i < attributes.getLength(); i++) {
402 Node attribute = attributes.item(i);
403 duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
404 }
405 }
406
407 // Copy element children
408 NodeList children = element.getChildNodes();
409 for (int i = 0; i < children.getLength(); i++) {
410 Node child = children.item(i);
411 duplicate.appendChild(owner.importNode(child, true));
412 }
413
414 return duplicate;
415 }
416
417 public static void copyAllChildren(Element to, Element from) {
418
419 Document to_doc = to.getOwnerDocument();
420 Node child = from.getFirstChild();
421 while (child != null) {
422 to.appendChild(to_doc.importNode(child, true));
423 child = child.getNextSibling();
424 }
425 }
426 /** returns a basic request message */
427 public static Element createBasicRequest(Document owner,
428 String request_type, String to,
429 String lang,
430 String uid) {
431 Element request = owner.createElement(REQUEST_ELEM);
432 request.setAttribute(TYPE_ATT, request_type);
433 request.setAttribute(LANG_ATT, lang);
434 request.setAttribute(TO_ATT, to);
435 request.setAttribute(USER_ID_ATT, uid);
436 return request;
437 }
438
439 public static Element createTextElement(Document owner, String elem_name,
440 String text) {
441 Element e = owner.createElement(elem_name);
442 Text t = owner.createTextNode(text);
443 e.appendChild(t);
444 return e;
445
446 }
447
448 public static Element createDisplayTextElement(Document owner,
449 String text_name,
450 String text) {
451 Element e = owner.createElement(DISPLAY_TEXT_ELEM);
452 e.setAttribute(NAME_ATT, text_name);
453 Text t = owner.createTextNode(text);
454 e.appendChild(t);
455 return e;
456
457 }
458
459
460 public static Element createParameter(Document owner, String name,
461 String value) {
462 Element param = owner.createElement(PARAM_ELEM);
463 param.setAttribute(NAME_ATT, name);
464 param.setAttribute(VALUE_ATT, value);
465 return param;
466 }
467
468 public static Element createParameterList(Document owner,
469 HashMap params) {
470
471 Element list = owner.createElement(PARAM_ELEM+LIST_MODIFIER);
472 Set items = params.entrySet();
473 Iterator i = items.iterator();
474 while(i.hasNext()) {
475 Map.Entry m = (Map.Entry)i.next();
476 list.appendChild(createParameter(owner, (String)m.getKey(), (String)m.getValue()));
477 }
478 return list;
479 }
480
481 public static Element createParameterDescription(Document owner,
482 String id,
483 String display_name,
484 String type,
485 String default_value,
486 String []option_ids,
487 String []option_names) {
488
489
490 Element p = owner.createElement(PARAM_ELEM);
491 p.setAttribute(NAME_ATT, id);
492 p.setAttribute(TYPE_ATT, type);
493 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
494 if (default_value != null) {
495 p.setAttribute(DEFAULT_ATT, default_value);
496 }
497 if (option_ids!=null && option_names!=null) {
498 for (int i=0; i<option_ids.length; i++) {
499 Element e = owner.createElement(PARAM_OPTION_ELEM);
500 e.setAttribute(NAME_ATT, option_ids[i]);
501 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names[i]));
502 p.appendChild(e);
503 }
504 }
505 return p;
506 }
507
508
509 /** returns the element parent/node_name[@attribute_name='attribute_value']
510 */
511 public static Element getNamedElement(Element parent, String node_name,
512 String attribute_name,
513 String attribute_value) {
514
515 NodeList children = parent.getChildNodes();
516 for (int i=0; i<children.getLength(); i++) {
517 Node child = children.item(i);
518 ///ystem.out.println("getnamed elem, node nmae="+child.getNodeName());
519 if (child.getNodeName().equals(node_name)) {
520 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
521 return (Element)child;
522 }
523 }
524 // not found
525 return null;
526 }
527
528
529
530 /** Returns the appropriate language element from a display elem,
531 display is the containing element, name is the name of the element to
532 look for, lang is the preferred language, lang_default is the fall back
533 lang if neither lang is found, will return the first one it finds*/
534 public static String getDisplayText(Element display, String name,
535 String lang, String lang_default) {
536
537 String def = null;
538 String first = null;
539 NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM);
540 if (elems.getLength() == 0) return "";
541 for (int i=0; i<elems.getLength(); i++) {
542 Element e = (Element)elems.item(i);
543 String n = e.getAttribute(NAME_ATT);
544 if (name.equals(n)) {
545 String l = e.getAttribute(LANG_ATT);
546 if (lang.equals(l)) {
547 return getNodeText(e);
548 } else if (lang_default.equals(l)) {
549 def = getNodeText(e);
550 } else if (first == null) {
551 first = getNodeText(e);
552 }
553 } else {
554 continue;
555 }
556 }
557
558 if (def != null) {
559 return def;
560 }
561 if (first != null) {
562 return first;
563 }
564 return "";
565 }
566
567 // replaces < > " ' & in the original with their entities
568 public static String xmlSafe(String original) {
569
570 StringBuffer filtered = new StringBuffer(original.length());
571 char c;
572 for (int i=0; i<original.length(); i++) {
573 c = original.charAt(i);
574 if (c == '>') {
575 filtered.append("&gt;");
576 } else if (c == '<') {
577 filtered.append("&lt;");
578 } else if (c == '"') {
579 filtered.append("&quot;");
580 } else if (c == '&') {
581 filtered.append("&amp;");
582 } else if (c == '\'') {
583 filtered.append("&apos;");
584 } else {
585 filtered.append(c);
586 }
587 }
588 return filtered.toString();
589 }
590
591
592// // replaces < > " ' & in the original with their entities
593// public static String unXmlSafe(String original) {
594
595// StringBuffer filtered = new StringBuffer(original.length());
596// char c;
597// for (int i=0; i<original.length(); i++) {
598// c = original.charAt(i);
599// if (c == '&') {
600// int pos = original.indexOf(";"), i);
601// String entity = original.substring(i+1, pos);
602// if (entity.equals("gt")) {
603// filtered.append(">");
604// } else if (entity.equals("lt")) {
605// filtered.append("<");
606// } else if (entity.equals("apos")) {
607// filtered.append(
608// while (c != ';') {
609
610// // process the entity
611// } else {
612// filtered.append(c);
613// }
614// }
615// return filtered.toString();
616// }
617}
Note: See TracBrowser for help on using the repository browser.