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

Last change on this file since 3869 was 3869, checked in by kjdon, 21 years ago

some changes to functions

  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import org.w3c.dom.Node;
4import org.w3c.dom.Element;
5import org.w3c.dom.NodeList;
6import org.w3c.dom.Document;
7import org.w3c.dom.Text;
8
9import java.util.HashMap;
10//import java.util.Locale;
11
12/** various functions for extracting info out of GS XML */
13public 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 public static final String STYLESHEET_ELEM = "format";//"stylesheet"; // any additional stylesheet stuff is carried in the message inside this elem
38 public static final String FORMAT_ELEM = "format"; // config files use format - shoudl we use this instead of stylesheet??
39 public static final String TERM_ELEM = "term";
40
41 //config file elems
42 public static final String INDEX_ELEM = "index";
43 public static final String SEARCH_ELEM = "search";
44 public static final String BROWSE_ELEM = "browse";
45 public static final String FIELD_ELEM = "field";
46 public static final String DISPLAYNAME_ELEM = "displayName";
47 public static final String SHORTNAME_ATT = "shortname";
48 // elems for the pages to be processed by xslt
49 public final static String PAGE_ELEM = "page";
50 public final static String TRANSLATION_ELEM = "translate";
51 public final static String CONFIGURATION_ELEM = "config";
52 public final static String DESCRIPTION_ELEM = "description";
53
54 public static final String SITE_NAME_ELEM = "localSiteName";
55
56 // add on to another elem type to get a list of that type
57 public static final String LIST_MODIFIER = "List";
58
59 // greenstone xml attributes
60 public static final String NAME_ATT = "name";
61 public static final String TO_ATT = "to";
62 public static final String FROM_ATT = "from";
63 public static final String LANG_ATT = "lang";
64 public static final String TYPE_ATT = "type";
65 public static final String VALUE_ATT = "value";
66 public static final String DEFAULT_ATT = "default";
67 public static final String INFO_ATT = "info";
68 public static final String ACTION_ATT = "action";
69 public static final String SUBACTION_ATT = "subaction";
70 public static final String OUTPUT_ATT = "output";
71 public static final String ADDRESS_ATT = "address";
72 public static final String STATUS_ERROR_CODE_ATT = "code";
73 public static final String STATUS_PROCESS_ID_ATT = "handle";
74 public static final String PARAM_SHORTNAME_ATT = "shortname";
75 public static final String PARAM_IGNORE_POS_ATT = "ignore";
76 public static final String CLASSIFIER_CONTENT_ATT = "content";
77
78 // document stuff
79 public static final String DOC_NODE_ELEM = "documentNode";
80 public static final String DOC_NODE_CONTENT_ELEM = "documentNodeContent";
81 public static final String DOC_NODE_STRUCTURE_ELEM = "documentNodeStructure";
82 public static final String DOC_NODE_ID_ATT = "documentID";
83 public static final String DOC_NODE_NAME_ATT = "documentNodeName";
84 public static final String DOC_NODE_TYPE_ATT = "documentNodeType";
85
86 public static final String NODE_TYPE_ROOT = "root";
87 public static final String NODE_TYPE_INTERIOR = "interior";
88 public static final String NODE_TYPE_LEAF = "leaf";
89
90 // classifier stuff
91 public static final String CLASS_NODE_ELEM = "classifierNode";
92 public static final String CLASS_NODE_ID_ATT = "classifierID";
93
94 // parameter types
95 public static final String PARAM_TYPE_INTEGER = "integer";
96 public static final String PARAM_TYPE_BOOLEAN = "boolean";
97 public static final String PARAM_TYPE_ENUM_START = "enum";
98 public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
99 public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
100 public static final String PARAM_TYPE_STRING = "string";
101 public static final String PARAM_TYPE_TEXT = "text";
102 public static final String PARAM_TYPE_MULTI = "multi";
103 public static final String PARAM_TYPE_FILE = "file";
104 // stuff for text strings
105 public static final String DISPLAY_ELEM = "display";
106 public static final String DISPLAY_NAME_ELEM = "name";
107 public static final String DISPLAY_SUBMIT_ELEM = "submit";
108
109 // request types - do we need all these? or just
110 // describe, status, and one to say do teh request
111 public static final String REQUEST_TYPE_DESCRIBE = "describe";
112 public static final String REQUEST_TYPE_STATUS = "status";
113 public static final String REQUEST_TYPE_PROCESS = "process";
114
115 public static final String REQUEST_TYPE_CONFIGURE = "configure"; // ?
116 public static final String REQUEST_TYPE_CGI = "cgi"; // ?
117
118 public static final String REQUEST_TYPE_ACTION = "action"; // ??
119 public static final String REQUEST_TYPE_QUERY = "query"; // ??
120 public static final String REQUEST_TYPE_BUILD = "build"; // ??
121
122 // service types
123 public static final String SERVICE_TYPE_QUERY = "query";
124 public static final String SERVICE_TYPE_RETRIEVE = "retrieve";
125 public static final String SERVICE_TYPE_BROWSE = "browse";
126 public static final String SERVICE_TYPE_APPLET = "applet";
127 public static final String SERVICE_TYPE_PROCESS = "process";
128
129 // configure types
130 public static final String CONFIG_ACTION_ACTIVATE = "activate";
131 public static final String CONFIG_ACTION_DEACTIVATE = "deactivate";
132
133 // communicator types
134 public static final String COMM_TYPE_SOAP_JAVA = "soap";
135
136
137 /** takes a list of elements, and returns an array of strings
138 * of the values of attribute att_name */
139 public static String [] getAttributeValuesFromList(Element list,
140 String att_name) {
141
142 NodeList children = list.getChildNodes();
143
144 int num_nodes = children.getLength();
145 String []ids = new String[num_nodes];
146 for (int i=0; i<num_nodes; i++) {
147 Element e = (Element)children.item(i);
148 String id = e.getAttribute(att_name);
149 ids[i] = id;
150 }
151
152 return ids;
153 }
154
155
156 // takes a node with a resource elements inside it and extracts all the
157 // HASH oids - name att for resource
158 // generalise this for any element type? pass in the list, the element name, the att to extract
159// public static String [] getDocumentNameList(Element content) {
160
161// Node n = content.getFirstChild();
162// while (n!=null && !n.getNodeName().equals(DOCUMENT_ELEM+LIST_MODIFIER)) {
163// n = n.getNextSibling();
164// }
165// if (n==null) { // no docs found
166// return null;
167// }
168
169// NodeList docs = n.getChildNodes();
170
171// int numdocs = docs.getLength();
172// String []ids = new String[numdocs];
173// for (int i=0; i<numdocs; i++) {
174// Element e = (Element)docs.item(i);
175// String id = e.getAttribute(NAME_ATT);
176// // check that its a valid id - ie starts with HASH
177// // need to change this if use different ids
178
179// ids[i] = id;
180
181// }
182
183// return ids;
184// }
185
186 // same as above function
187 /** extracts metadata names out of an element */
188// public static String [] getMetaNameList(Element content) {
189// Node n = content.getFirstChild();
190// while (n!=null &&
191// !n.getNodeName().equals(METADATA_ELEM+LIST_MODIFIER)) {
192// n = n.getNextSibling();
193// }
194// if (n==null) { // no metadatas found
195// return null;
196// }
197// NodeList elems = n.getChildNodes();
198
199// int numelems = elems.getLength();
200// String []ids = new String[numelems];
201// for (int i=0; i<numelems; i++) {
202// Element e = (Element)elems.item(i);
203// String id = e.getAttribute(NAME_ATT);
204// ids[i] = id;
205// }
206
207// return ids;
208// }
209
210
211 /** takes a paramList element, and gets a HashMap of name-value pairs
212 * if deep=true, extracts embedded params, otherwise just top level
213 * params*/
214 public static HashMap extractParams(Element xml, boolean deep) {
215
216 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
217 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
218 return null;
219 }
220
221 NodeList params = null;
222 if (deep) { // get all the nested ones
223 params = xml.getElementsByTagName(PARAM_ELEM);
224 } else { // just get the top level ones
225 params = xml.getChildNodes();
226 }
227 HashMap param_map = new HashMap();
228 for (int i=0; i<params.getLength(); i++) {
229 Element param = (Element)params.item(i);
230 String name=param.getAttribute(NAME_ATT);
231 String value=getValue(param); //att or content
232 param_map.put(name, value);
233
234 }
235 return param_map;
236 }
237 /** takes a paramList element, and gets a HashMap of name-value pairs */
238// public static HashMap extractAllParams(Element xml) {
239
240// if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
241// System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
242// return null;
243// }
244// NodeList params = xml.getElementsByTagName(PARAM_ELEM);
245// HashMap param_map = new HashMap();
246// for (int i=0; i<params.getLength(); i++) {
247// Element param = (Element)params.item(i);
248// String name=param.getAttribute(NAME_ATT);
249// String value=param.getAttribute(VALUE_ATT);
250// if (value.equals("")) { // the value is in the content of the param
251// value=getNodeText(param);
252// }
253// param_map.put(name, value);
254
255// }
256// return param_map;
257// }
258
259 /** gets the value att or the text content */
260 public static String getValue(Element e) {
261 String val = e.getAttribute(VALUE_ATT);
262 if (val ==null || val.equals("")) {
263 // have to get it out of the text
264 val=getNodeText(e);
265
266 }
267 return val;
268 }
269
270 /** extracts the text out of a node */
271 public static String getNodeText(Element param) {
272 param.normalize();
273 Node n = param.getFirstChild();
274 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
275 n=n.getNextSibling();
276 }
277 if (n==null) { // no text node
278 return "";
279 }
280 return n.getNodeValue();
281 }
282
283 /** creates a new document Element */
284// public static Element createDocumentElement(Document owner, String oid) {
285// Element e = owner.createElement(DOCUMENT_ELEM);
286// e.setAttribute(NAME_ATT, oid);
287
288// return e;
289// }
290
291 /** add text to a document/subsection element */
292 public static boolean addDocText(Document owner, Element doc, String text) {
293
294 Element content = owner.createElement(DOC_NODE_CONTENT_ELEM);
295 Text t = owner.createTextNode(text);
296 content.appendChild(t);
297 doc.appendChild(content);
298 return true;
299 }
300
301 /** add an error message */
302 public static boolean addError(Document owner, Element doc, String text) {
303
304 Element content = owner.createElement(ERROR_ELEM);
305 Text t = owner.createTextNode(text);
306 content.appendChild(t);
307
308 return true;
309 }
310
311 /** add an error message */
312 public static boolean addError(Document owner, Element doc, Throwable error) {
313 error.printStackTrace();
314 return addError(owner, doc, error.toString());
315 }
316
317 /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
318// public static Element addMetaList(Document owner, Element doc) {
319// Element list = owner.createElement(METADATA_ELEM+LIST_MODIFIER);
320// doc.appendChild(list);
321// return list;
322// }
323 /** adds a metadata elem to a list */
324
325 public static boolean addMetadata(Document owner, Element list,
326 String meta_name, String meta_value) {
327 if (meta_value==null || meta_value.equals("")) {
328 return false;
329 }
330 Element data = owner.createElement(METADATA_ELEM);
331 data.setAttribute(NAME_ATT, meta_name);
332 Text t = owner.createTextNode(meta_value);
333 data.appendChild(t);
334 list.appendChild(data);
335 return true;
336
337 }
338
339 /** copies the metadata out of teh metadataList of 'from' into
340 * the metadataList of 'to' */
341 public static boolean mergeMetadataLists(Node to, Node from) {
342 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
343 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
344
345 if (from_meta == null) { // nothing to copy
346 return true;
347 }
348 Document to_owner = to.getOwnerDocument();
349 Node new_from = to_owner.importNode(from_meta, true);
350
351 if (to_meta == null) { // just copy the whole list
352 to.appendChild(new_from);
353 return true;
354 }
355
356 // copy individual elements
357 Node child = new_from.getFirstChild();
358 while ( child != null) {
359 to_meta.appendChild(child);
360 child = child.getNextSibling();
361 }
362 return true;
363 }
364
365 /** returns the (first) child element with the given name */
366 public static Node getChildByTagName(Node n, String name) {
367
368 Node child = n.getFirstChild();
369 while (child!=null) {
370 if (child.getNodeName().equals(name)) {
371 return child;
372 }
373 child = child.getNextSibling();
374 }
375 return null; //not found
376 }
377
378 /** takes an xpath type expression of the form name/name/...
379 and returns the first node that matches, or null if not found */
380 public static Node getNodeByPath(Node n, String path) {
381
382 String link = GSPath.getFirstLink(path);
383 path = GSPath.removeFirstLink(path);
384 while (!link.equals("")) {
385 n = getChildByTagName(n, link);
386 if (n==null) {
387 return null;
388 }
389 link = GSPath.getFirstLink(path);
390 path = GSPath.removeFirstLink(path);
391 }
392 return n;
393 }
394 public static HashMap getChildrenMap(Node n) {
395
396 HashMap map= new HashMap();
397 Node child = n.getFirstChild();
398 while (child!=null) {
399 String name = child.getNodeName();
400 map.put(name, child);
401 child = child.getNextSibling();
402 }
403 return map;
404 }
405
406 public static Element createTextElement(Document owner, String elem_name,
407 String text) {
408 Element e = owner.createElement(elem_name);
409 Text t = owner.createTextNode(text);
410 e.appendChild(t);
411 return e;
412
413 }
414
415
416 public static Element createParameter(Document owner, String name,
417 String type, String default_value,
418 String []options) {
419
420
421 Element p = owner.createElement(PARAM_ELEM);
422 p.setAttribute(NAME_ATT, name);
423 p.setAttribute(TYPE_ATT, type);
424 if (default_value != null) {
425 p.setAttribute(DEFAULT_ATT, default_value);
426 }
427 if (type.startsWith(PARAM_TYPE_ENUM_START) && options!=null) {
428 for (int i=0; i<options.length; i++) {
429 Element e = owner.createElement(PARAM_OPTION_ELEM);
430 e.setAttribute(NAME_ATT, options[i]);
431 p.appendChild(e);
432 }
433 }
434 return p;
435 }
436
437 /** creates a parm elem containing the text strings
438 * <param><name>xxx<name><option name='x'>yyy</option><option name='y'>zzz</option></param>
439 */
440 public static Element createParameterDisplay(Document owner, String name,
441 String name_text,
442 String []options,
443 String []option_texts) {
444
445
446 Element param = owner.createElement(PARAM_ELEM);
447 param.setAttribute(NAME_ATT, name);
448 param.appendChild(createTextElement(owner, DISPLAY_NAME_ELEM, name_text));
449 if (options != null) {
450 for (int i=0; i<options.length; i++) {
451 Element e = GSXML.createTextElement(owner, PARAM_OPTION_ELEM, option_texts[i]);
452 e.setAttribute(NAME_ATT, options[i]);
453 param.appendChild(e);
454 }
455 }
456
457 return param;
458 }
459 /*
460 public static Element createClassifierDisplay(Document owner, String name,
461 String name_text) {
462 Element classifier = owner.createElement(CLASSIFIER_ELEM);
463 classifier.setAttribute(NAME_ATT, name);
464 classifier.appendChild(createTextElement(owner, DISPLAY_NAME_ELEM, name_text));
465
466 return classifier;
467 }
468
469 *
470 /** returns the element parent/node_name[@attribute_name='attribute_value']
471 */
472 public static Element getNamedElement(Element parent, String node_name,
473 String attribute_name,
474 String attribute_value) {
475
476 NodeList children = parent.getChildNodes();
477 for (int i=0; i<children.getLength(); i++) {
478 Node child = children.item(i);
479 if (child.getNodeName().equals(node_name)) {
480 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
481 return (Element)child;
482 }
483 }
484 // not found
485 return null;
486 }
487
488
489 /** Returns the appropriate language element from a display elem,
490 display is the containing element, name is the name of the element to
491 look for, lang is the preferred language, lang_default is the fall back
492 lang if neither lang is found, will return the first one it finds*/
493 public static String getDisplayText(Element display, String name,
494 String lang, String lang_default) {
495 Element item = getNamedElement(display, name, GSXML.LANG_ATT, lang);
496 if (item==null) {
497 item = getNamedElement(display, name, GSXML.LANG_ATT, lang_default);
498 }
499 if (item ==null) {
500 item = (Element)getChildByTagName(display, name); // just get the first one
501 }
502 if (item==null) {
503 return ""; // should we return an empty string? or null?
504 }
505 return getNodeText(item);
506
507 }
508 // replaces < > " ' & in the original with their entities
509 public static String xmlSafe(String original) {
510
511 StringBuffer filtered = new StringBuffer(original.length());
512 char c;
513 for (int i=0; i<original.length(); i++) {
514 c = original.charAt(i);
515 if (c == '>') {
516 filtered.append("&gt;");
517 } else if (c == '<') {
518 filtered.append("&lt;");
519 } else if (c == '"') {
520 filtered.append("&quot;");
521 } else if (c == '&') {
522 filtered.append("&amp;");
523 } else if (c == '\'') {
524 filtered.append("&apos;");
525 } else {
526 filtered.append(c);
527 }
528 }
529 return filtered.toString();
530 }
531}
Note: See TracBrowser for help on using the repository browser.