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

Last change on this file since 10289 was 10289, checked in by kjdon, 19 years ago

changed the xml for siteConfig files a bit - removed SITE_NAME_ELEM and SITE_HTTP_ADDRESS_ELEM and added LOCAL_SITE_ATT and LOCAL_SITE_NAME_ATT

  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 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;
15import java.util.ArrayList;
16
17//import java.util.Locale;
18
19/** various functions for extracting info out of GS XML */
20public class GSXML {
21
22 // greenstone xml elements
23 public static final String MESSAGE_ELEM = "message";
24 public static final String REQUEST_ELEM = "request";
25 public static final String RESPONSE_ELEM = "response";
26 public static final String COLLECTION_ELEM = "collection";
27 public static final String SERVICE_ELEM = "service";
28 public static final String CLUSTER_ELEM = "serviceCluster";
29 public static final String SITE_ELEM = "site";
30 public static final String PARAM_ELEM = "param";
31 public static final String PARAM_OPTION_ELEM = "option";
32 public static final String CONTENT_ELEM = "content";
33 public static final String RESOURCE_ELEM = "resource";
34 public static final String DOCUMENT_ELEM = "document";
35 public static final String METADATA_ELEM = "metadata";
36 public static final String SERVICE_CLASS_ELEM = "serviceRack";
37 public static final String CLASSIFIER_ELEM = "classifier";
38 public static final String APPLET_ELEM = "applet";
39 public static final String APPLET_DATA_ELEM = "appletData";
40 public static final String CONFIGURE_ELEM = "configure";
41 public static final String STATUS_ELEM = "status";
42 public static final String ERROR_ELEM = "error";
43 public static final String DEFAULT_ELEM = "default";
44 public static final String STYLESHEET_ELEM = "format";//"stylesheet"; // any additional stylesheet stuff is carried in the message inside this elem
45 public static final String FORMAT_ELEM = "format"; // config files use format - should we use this instead of stylesheet??
46 public static final String TERM_ELEM = "term";
47 public static final String SYSTEM_ELEM = "system";
48
49 //config file elems
50 public static final String COLLECTION_CONFIG_ELEM = "collectionConfig";
51 public static final String COLLECTION_BUILD_ELEM = "buildConfig";
52 public static final String COLLECTION_INIT_ELEM = "collectionInit";
53 public static final String RECOGNISE_ELEM = "recognise";
54 public static final String DOC_TYPE_ELEM = "docType";
55 public static final String SEARCH_ELEM = "search";
56 public static final String INDEX_ELEM = "index";
57 public static final String BROWSE_ELEM = "browse";
58 public static final String DISPLAY_ELEM = "display";
59 public static final String FIELD_ELEM = "field";
60 public static final String LEVEL_ELEM = "level";
61 public static final String SHORTNAME_ATT = "shortname";
62 public static final String NOTIFY_ELEM = "notify";
63 public static final String NOTIFY_HOST_ATT = "host";
64
65 // elems for the pages to be processed by xslt
66 public final static String PAGE_ELEM = "page";
67 public final static String CONFIGURATION_ELEM = "config";
68 public final static String PAGE_REQUEST_ELEM = "pageRequest";
69 public final static String PAGE_RESPONSE_ELEM = "pageResponse";
70 public final static String PAGE_EXTRA_ELEM = "pageExtra";
71
72 //public final static String DESCRIPTION_ELEM = "description";
73
74 public static final String ACTION_ELEM = "action";
75 public static final String SUBACTION_ELEM = "subaction";
76
77 // add on to another elem type to get a list of that type
78 public static final String LIST_MODIFIER = "List";
79
80 // greenstone xml attributes
81 public static final String NAME_ATT = "name";
82 public static final String TO_ATT = "to";
83 public static final String USER_ID_ATT = "uid";
84 public static final String FROM_ATT = "from";
85 public static final String LANG_ATT = "lang";
86 public static final String TYPE_ATT = "type";
87 public static final String VALUE_ATT = "value";
88 public static final String DEFAULT_ATT = "default";
89 public static final String INFO_ATT = "info";
90 public static final String ACTION_ATT = "action";
91 public static final String SUBACTION_ATT = "subaction";
92 public static final String OUTPUT_ATT = "output";
93 public static final String ADDRESS_ATT = "address";
94 public static final String LOCAL_SITE_ATT = "localSite";
95 public static final String LOCAL_SITE_NAME_ATT = "localSiteName";
96 public static final String STATUS_ERROR_CODE_ATT = "code";
97 public static final String STATUS_PROCESS_ID_ATT = "pid";
98 public static final String PARAM_SHORTNAME_ATT = "shortname";
99 public static final String PARAM_IGNORE_POS_ATT = "ignore";
100 public static final String CLASSIFIER_CONTENT_ATT = "content";
101 public static final String ERROR_TYPE_ATT = "type";
102
103 // document stuff
104 public static final String DOC_TYPE_ATT = "docType";
105 public static final String DOC_NODE_ELEM = "documentNode";
106 public static final String NODE_CONTENT_ELEM = "nodeContent";
107 public static final String NODE_STRUCTURE_ELEM = "nodeStructure";
108 public static final String NODE_ID_ATT = "nodeID";
109 public static final String NODE_NAME_ATT = "nodeName";
110 public static final String NODE_TYPE_ATT = "nodeType";
111 public static final String NODE_RANK_ATT = "rank";
112 public static final String NODE_TYPE_ROOT = "root";
113 public static final String NODE_TYPE_INTERNAL = "internal";
114 public static final String NODE_TYPE_LEAF = "leaf";
115
116 public static final String DOC_TYPE_SIMPLE = "simple";
117 public static final String DOC_TYPE_PAGED = "paged";
118 public static final String DOC_TYPE_HIERARCHY = "hierarchy";
119
120 // classifier stuff
121 public static final String CLASS_NODE_ELEM = "classifierNode";
122 public static final String CLASS_NODE_ORIENTATION_ATT = "orientation";
123
124 // parameter types
125 public static final String PARAM_TYPE_INTEGER = "integer";
126 public static final String PARAM_TYPE_BOOLEAN = "boolean";
127 public static final String PARAM_TYPE_ENUM_START = "enum";
128 public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
129 public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
130 public static final String PARAM_TYPE_STRING = "string";
131 public static final String PARAM_TYPE_TEXT = "text";
132 public static final String PARAM_TYPE_MULTI = "multi";
133 public static final String PARAM_TYPE_FILE = "file";
134 public static final String PARAM_TYPE_INVISIBLE = "invisible";
135 // stuff for text strings
136 public static final String DISPLAY_TEXT_ELEM = "displayItem";
137 // the following are used for the name attributes
138 public static final String DISPLAY_TEXT_NAME = "name";
139 public static final String DISPLAY_TEXT_SUBMIT = "submit";
140 public static final String DISPLAY_TEXT_DESCRIPTION = "description";
141
142 // request types
143 // get the module description
144 public static final String REQUEST_TYPE_DESCRIBE = "describe";
145 // startup a process
146 public static final String REQUEST_TYPE_PROCESS = "process";
147 // get the status of an ongoing process
148 public static final String REQUEST_TYPE_STATUS = "status";
149 // system type request - eg reload a collection
150 public static final String REQUEST_TYPE_SYSTEM = "system";
151 // page requests to the Receptionist/Actions
152 public static final String REQUEST_TYPE_PAGE = "page"; // used to be cgi
153 // get any format info for a service
154 public static final String REQUEST_TYPE_FORMAT = "format";
155 // modify the requests
156 public static final String REQUEST_TYPE_MESSAGING = "messaging";
157
158 // service types
159 public static final String SERVICE_TYPE_QUERY = "query";
160 public static final String SERVICE_TYPE_RETRIEVE = "retrieve";
161 public static final String SERVICE_TYPE_BROWSE = "browse";
162 public static final String SERVICE_TYPE_APPLET = "applet";
163 public static final String SERVICE_TYPE_PROCESS = "process";
164 public static final String SERVICE_TYPE_ENRICH = "enrich";
165
166 // system command types and attributes
167 public static final String SYSTEM_TYPE_CONFIGURE = "configure";
168 public static final String SYSTEM_TYPE_ACTIVATE = "activate";
169 public static final String SYSTEM_TYPE_DEACTIVATE = "deactivate";
170
171 public static final String SYSTEM_SUBSET_ATT = "subset";
172 public static final String SYSTEM_MODULE_TYPE_ATT = "moduleType";
173 public static final String SYSTEM_MODULE_NAME_ATT = "moduleName";
174
175 // communicator types
176 public static final String COMM_TYPE_SOAP_JAVA = "soap";
177
178 // error types
179 public static final String ERROR_TYPE_SYNTAX = "syntax";
180 public static final String ERROR_TYPE_SYSTEM = "system";
181 public static final String ERROR_TYPE_INVALID_ID = "invalid_id";
182 public static final String ERROR_TYPE_OTHER = "other";
183
184 // some system wide param names
185 public static final String SUBSET_PARAM = "subset";
186
187 /** takes a list of elements, and returns an array of strings
188 * of the values of attribute att_name */
189 public static String [] getAttributeValuesFromList(Element list,
190 String att_name) {
191
192 NodeList children = list.getChildNodes();
193
194 int num_nodes = children.getLength();
195 String []ids = new String[num_nodes];
196 for (int i=0; i<num_nodes; i++) {
197 Element e = (Element)children.item(i);
198 String id = e.getAttribute(att_name);
199 ids[i] = id;
200 }
201
202 return ids;
203 }
204
205 /** takes a paramList element, and gets a HashMap of name-value pairs
206 * if deep=true, extracts embedded params, otherwise just top level
207 * params*/
208 public static HashMap extractParams(Element xml, boolean deep) {
209
210 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
211 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
212 return null;
213 }
214
215 NodeList params = null;
216 if (deep) { // get all the nested ones
217 params = xml.getElementsByTagName(PARAM_ELEM);
218 } else { // just get the top level ones
219 params = xml.getChildNodes();
220 }
221 HashMap param_map = new HashMap();
222 for (int i=0; i<params.getLength(); i++) {
223 if (params.item(i).getNodeName().equals(PARAM_ELEM)) {
224 Element param = (Element)params.item(i);
225 String name=param.getAttribute(NAME_ATT);
226 String value=getValue(param); //att or content
227 int pos = name.indexOf('.');
228 if (pos == -1) { // a base param
229 param_map.put(name, value);
230 } else { // a namespaced param
231
232 String namespace = name.substring(0, pos);
233 name = name.substring(pos+1);
234 HashMap map = (HashMap)param_map.get(namespace);
235 if (map == null) {
236 map = new HashMap();
237 param_map.put(namespace, map);
238 }
239 map.put(name, value);
240 }
241 }
242 }
243 return param_map;
244 }
245
246 /** gets the value att or the text content */
247 public static String getValue(Element e) {
248 String val = e.getAttribute(VALUE_ATT);
249 if (val ==null || val.equals("")) {
250 // have to get it out of the text
251 val=getNodeText(e);
252
253 } else {
254 // unescape the xml stuff
255 val = unXmlSafe(val);
256 }
257 return val;
258 }
259
260 /** extracts the text out of a node */
261 public static Node getNodeTextNode(Element param) {
262 param.normalize();
263 Node n = param.getFirstChild();
264 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
265 n=n.getNextSibling();
266 }
267 return n;
268 }
269
270 /** extracts the text out of a node */
271 public static String getNodeText(Element param) {
272 Node text_node = getNodeTextNode(param);
273 if (text_node == null) {
274 return "";
275 }
276 return text_node.getNodeValue();
277 }
278
279
280 /** add text to a document/subsection element */
281 public static boolean addDocText(Document owner, Element doc, String text) {
282
283 Element content = owner.createElement(NODE_CONTENT_ELEM);
284 Text t = owner.createTextNode(text);
285 content.appendChild(t);
286 doc.appendChild(content);
287 return true;
288 }
289
290 /** add an error message, unknown error type */
291 public static boolean addError(Document owner, Element doc, String text) {
292 return addError(owner, doc, text, ERROR_TYPE_OTHER);
293 }
294 /** add an error message */
295 public static boolean addError(Document owner, Element doc, String text,
296 String error_type) {
297
298 Element content = owner.createElement(ERROR_ELEM);
299 content.setAttribute(ERROR_TYPE_ATT, error_type);
300 Text t = owner.createTextNode(text);
301 content.appendChild(t);
302 doc.appendChild(content);
303 return true;
304 }
305
306 /** add an error message */
307 public static boolean addError(Document owner, Element doc, Throwable error) {
308 return addError(owner, doc, error, ERROR_TYPE_OTHER);
309 }
310
311 /** add an error message */
312 public static boolean addError(Document owner, Element doc,
313 Throwable error, String error_type) {
314 error.printStackTrace();
315 return addError(owner, doc, error.toString(), error_type);
316 }
317
318 public static Element createMetadataParamList(Document owner, Vector meta_values) {
319
320 Element meta_param_list = owner.createElement(PARAM_ELEM+LIST_MODIFIER);
321 Iterator i = meta_values.iterator();
322 while(i.hasNext()) {
323 String next = (String)i.next();
324 Element meta_param = owner.createElement(PARAM_ELEM);
325 meta_param_list.appendChild(meta_param);
326 meta_param.setAttribute(NAME_ATT, "metadata");
327 meta_param.setAttribute(VALUE_ATT, next);
328 }
329 return meta_param_list;
330 }
331
332 /** adds a metadata elem to a list */
333 public static boolean addMetadata(Document owner, Element list,
334 String meta_name, String meta_value) {
335 if (meta_value==null || meta_value.equals("")) {
336 return false;
337 }
338 Element data = owner.createElement(METADATA_ELEM);
339 data.setAttribute(NAME_ATT, meta_name);
340 Text t = owner.createTextNode(meta_value);
341 data.appendChild(t);
342 list.appendChild(data);
343 return true;
344
345 }
346
347 /** copies the metadata out of teh metadataList of 'from' into
348 * the metadataList of 'to' */
349 public static boolean mergeMetadataLists(Node to, Node from) {
350 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
351 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
352
353 if (from_meta == null) { // nothing to copy
354 return true;
355 }
356 Document to_owner = to.getOwnerDocument();
357 Node new_from = to_owner.importNode(from_meta, true);
358
359 if (to_meta == null) { // just copy the whole list
360 to.appendChild(new_from);
361 return true;
362 }
363
364 // copy individual elements
365 Node child = new_from.getFirstChild();
366 while ( child != null) {
367 to_meta.appendChild(child);
368 child = child.getNextSibling();
369 }
370 return true;
371 }
372
373 /** copies all the children from from to to */
374 public static boolean mergeElements(Element to, Element from) {
375
376 Document owner = to.getOwnerDocument();
377 Node child = from.getFirstChild();
378 while (child != null) {
379 to.appendChild(owner.importNode(child, true));
380 child = child.getNextSibling();
381 }
382 return true;
383 }
384 /** returns the (first) child element with the given name */
385 public static Node getChildByTagName(Node n, String name) {
386
387 Node child = n.getFirstChild();
388 while (child!=null) {
389 if (child.getNodeName().equals(name)) {
390 return child;
391 }
392 child = child.getNextSibling();
393 }
394 return null; //not found
395 }
396
397 /** returns the (nth) child element with the given name
398 * index numbers start at 0 */
399 public static Node getChildByTagNameIndexed(Node n, String name, int index) {
400 if (index == -1) {
401 return getChildByTagName(n, name);
402 }
403 int count = 0;
404 Node child = n.getFirstChild();
405 while (child!=null) {
406 if (child.getNodeName().equals(name)) {
407 if (count == index) {
408 return child;
409 } else {
410 count++;
411 }
412 }
413 child = child.getNextSibling();
414 }
415 return null; //not found
416 }
417
418 /** takes an xpath type expression of the form name/name/...
419 and returns the first node that matches, or null if not found */
420 public static Node getNodeByPath(Node n, String path) {
421
422 String link = GSPath.getFirstLink(path);
423 path = GSPath.removeFirstLink(path);
424 while (!link.equals("")) {
425 n = getChildByTagName(n, link);
426 if (n==null) {
427 return null;
428 }
429 link = GSPath.getFirstLink(path);
430 path = GSPath.removeFirstLink(path);
431 }
432 return n;
433 }
434
435 /** takes an xpath type expression of the form name/name/...
436 * and returns the first node that matches, or null if not found
437 * can include [i] indices. index numbers start at 0 */
438 public static Node getNodeByPathIndexed(Node n, String path) {
439
440 String link = GSPath.getFirstLink(path);
441 int index = GSPath.getIndex(link);
442 if (index != -1) {
443 link = GSPath.removeIndex(link);
444 }
445 path = GSPath.removeFirstLink(path);
446 while (!link.equals("")) {
447 n = getChildByTagNameIndexed(n, link, index);
448 if (n==null) {
449 return null;
450 }
451 link = GSPath.getFirstLink(path);
452 index = GSPath.getIndex(link);
453 if (index != -1) {
454 link = GSPath.removeIndex(link);
455 }
456 path = GSPath.removeFirstLink(path);
457 }
458 return n;
459 }
460
461 public static HashMap getChildrenMap(Node n) {
462
463 HashMap map= new HashMap();
464 Node child = n.getFirstChild();
465 while (child!=null) {
466 String name = child.getNodeName();
467 map.put(name, child);
468 child = child.getNextSibling();
469 }
470 return map;
471 }
472
473 public static NodeList getChildrenByTagName(Node n, String name) {
474 MyNodeList node_list = new MyNodeList();
475 Node child = n.getFirstChild();
476 while (child!=null) {
477 if (child.getNodeName().equals(name)) {
478 node_list.addNode(child);
479 }
480 child = child.getNextSibling();
481 }
482 return node_list;
483 }
484
485
486 /** Duplicates an element, but gives it a new name */
487 public static Element duplicateWithNewName(Document owner, Element element,
488 String element_name, boolean with_attributes)
489 {
490 return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes);
491 }
492
493 /** Duplicates an element, but gives it a new name */
494 public static Element duplicateWithNewNameNS(Document owner,
495 Element element,
496 String element_name,
497 String namespace_uri,
498 boolean with_attributes)
499 {
500 Element duplicate;
501 if (namespace_uri == null) {
502 duplicate = owner.createElement(element_name);
503 } else {
504 duplicate = owner.createElementNS(namespace_uri, element_name);
505 }
506 // Copy element attributes
507 if (with_attributes) {
508 NamedNodeMap attributes = element.getAttributes();
509 for (int i = 0; i < attributes.getLength(); i++) {
510 Node attribute = attributes.item(i);
511 duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
512 }
513 }
514
515 // Copy element children
516 NodeList children = element.getChildNodes();
517 for (int i = 0; i < children.getLength(); i++) {
518 Node child = children.item(i);
519 duplicate.appendChild(owner.importNode(child, true));
520 }
521
522 return duplicate;
523 }
524
525 public static void copyAllChildren(Element to, Element from) {
526
527 Document to_doc = to.getOwnerDocument();
528 Node child = from.getFirstChild();
529 while (child != null) {
530 to.appendChild(to_doc.importNode(child, true));
531 child = child.getNextSibling();
532 }
533 }
534 /** returns a basic request message */
535 public static Element createBasicRequest(Document owner,
536 String request_type, String to,
537 String lang,
538 String uid) {
539 Element request = owner.createElement(REQUEST_ELEM);
540 request.setAttribute(TYPE_ATT, request_type);
541 request.setAttribute(LANG_ATT, lang);
542 request.setAttribute(TO_ATT, to);
543 request.setAttribute(USER_ID_ATT, uid);
544 return request;
545 }
546
547 public static Element createTextElement(Document owner, String elem_name,
548 String text) {
549 Element e = owner.createElement(elem_name);
550 Text t = owner.createTextNode(text);
551 e.appendChild(t);
552 return e;
553
554 }
555
556 public static Element createDisplayTextElement(Document owner,
557 String text_name,
558 String text) {
559 Element e = owner.createElement(DISPLAY_TEXT_ELEM);
560 e.setAttribute(NAME_ATT, text_name);
561 Text t = owner.createTextNode(text);
562 e.appendChild(t);
563 return e;
564
565 }
566
567
568 public static Element createParameter(Document owner, String name,
569 String value) {
570 Element param = owner.createElement(PARAM_ELEM);
571 param.setAttribute(NAME_ATT, name);
572 param.setAttribute(VALUE_ATT, value);
573 return param;
574 }
575
576 public static void addParametersToList(Document owner, Element param_list,
577 HashMap params) {
578 Set items = params.entrySet();
579 Iterator i = items.iterator();
580 while(i.hasNext()) {
581 Map.Entry m = (Map.Entry)i.next();
582 param_list.appendChild(createParameter(owner, (String)m.getKey(), (String)m.getValue()));
583 }
584
585 }
586
587 public static Element createParameterDescription(Document owner,
588 String id,
589 String display_name,
590 String type,
591 String default_value,
592 String []option_ids,
593 String []option_names) {
594
595
596 Element p = owner.createElement(PARAM_ELEM);
597 p.setAttribute(NAME_ATT, id);
598 p.setAttribute(TYPE_ATT, type);
599 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
600 if (default_value != null) {
601 p.setAttribute(DEFAULT_ATT, default_value);
602 }
603 if (option_ids!=null && option_names!=null) {
604 for (int i=0; i<option_ids.length; i++) {
605 Element e = owner.createElement(PARAM_OPTION_ELEM);
606 e.setAttribute(NAME_ATT, option_ids[i]);
607 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names[i]));
608 p.appendChild(e);
609 }
610 }
611 return p;
612 }
613 public static Element createParameterDescription2(Document owner,
614 String id,
615 String display_name,
616 String type,
617 String default_value,
618 ArrayList option_ids,
619 ArrayList option_names) {
620
621
622 Element p = owner.createElement(PARAM_ELEM);
623 p.setAttribute(NAME_ATT, id);
624 p.setAttribute(TYPE_ATT, type);
625 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
626 if (default_value != null) {
627 p.setAttribute(DEFAULT_ATT, default_value);
628 }
629 if (option_ids!=null && option_names!=null) {
630 for (int i=0; i<option_ids.size(); i++) {
631 Element e = owner.createElement(PARAM_OPTION_ELEM);
632 e.setAttribute(NAME_ATT, (String)option_ids.get(i));
633 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, (String)option_names.get(i)));
634 p.appendChild(e);
635 }
636 }
637 return p;
638 }
639
640
641 /** returns the element parent/node_name[@attribute_name='attribute_value']
642 */
643 public static Element getNamedElement(Element parent, String node_name,
644 String attribute_name,
645 String attribute_value) {
646
647 NodeList children = parent.getChildNodes();
648 for (int i=0; i<children.getLength(); i++) {
649 Node child = children.item(i);
650 ///ystem.out.println("getnamed elem, node nmae="+child.getNodeName());
651 if (child.getNodeName().equals(node_name)) {
652 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
653 return (Element)child;
654 }
655 }
656 // not found
657 return null;
658 }
659
660 public static int SORT_TYPE_STRING = 0;
661 public static int SORT_TYPE_INT = 1;
662 public static int SORT_TYPE_FLOAT = 2;
663
664 // sort type:
665 public static Element insertIntoOrderedList(Element parent_node,
666 String node_name,
667 Element start_from_elem,
668 Element new_elem, String sort_att,
669 boolean descending)
670 {
671 if (new_elem == null) return null;
672 System.err.println("+");
673 Element cloned_elem = (Element)parent_node.getOwnerDocument().importNode(new_elem, true);
674 if (start_from_elem == null) {
675 parent_node.appendChild(cloned_elem);
676 return cloned_elem;
677 }
678 System.err.println("++");
679
680 Node current_node = start_from_elem;
681 String insert_att = cloned_elem.getAttribute(sort_att);
682 String list_att = start_from_elem.getAttribute(sort_att);
683 while ((!descending && list_att.compareTo(insert_att)<0) || (descending && list_att.compareTo(insert_att)>0)) {
684 System.err.println("+++");
685 current_node = current_node.getNextSibling();
686 if (current_node == null) break; // end of the list
687 if (!current_node.getNodeName().equals(node_name)) {
688 continue; // not a valid node
689 }
690 list_att = ((Element)current_node).getAttribute(sort_att);
691 }
692 System.err.println("++++");
693
694 parent_node.insertBefore(cloned_elem, current_node);
695 return cloned_elem;
696 }
697
698
699 /** Returns the appropriate language element from a display elem,
700 display is the containing element, name is the name of the element to
701 look for, lang is the preferred language, lang_default is the fall back
702 lang if neither lang is found, will return the first one it finds*/
703 public static String getDisplayText(Element display, String name,
704 String lang, String lang_default) {
705
706 String def = null;
707 String first = null;
708 NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM);
709 if (elems.getLength() == 0) return "";
710 for (int i=0; i<elems.getLength(); i++) {
711 Element e = (Element)elems.item(i);
712 String n = e.getAttribute(NAME_ATT);
713 if (name.equals(n)) {
714 String l = e.getAttribute(LANG_ATT);
715 if (lang.equals(l)) {
716 return getNodeText(e);
717 } else if (lang_default.equals(l)) {
718 def = getNodeText(e);
719 } else if (first == null) {
720 first = getNodeText(e);
721 }
722 } else {
723 continue;
724 }
725 }
726
727 if (def != null) {
728 return def;
729 }
730 if (first != null) {
731 return first;
732 }
733 return "";
734 }
735
736 // replaces < > " ' & in the original with their entities
737 public static String xmlSafe(String original) {
738
739 StringBuffer filtered = new StringBuffer(original.length());
740 char c;
741 for (int i=0; i<original.length(); i++) {
742 c = original.charAt(i);
743 if (c == '>') {
744 filtered.append("&gt;");
745 } else if (c == '<') {
746 filtered.append("&lt;");
747 } else if (c == '"') {
748 filtered.append("&quot;");
749 } else if (c == '&') {
750 filtered.append("&amp;");
751 } else if (c == '\'') {
752 filtered.append("&apos;");
753 } else {
754 filtered.append(c);
755 }
756 }
757 return filtered.toString();
758 }
759
760
761 // replaces < > " ' & entities with their originals
762 public static String unXmlSafe(String original) {
763
764 StringBuffer filtered = new StringBuffer(original.length());
765 char c;
766 for (int i=0; i<original.length(); i++) {
767 c = original.charAt(i);
768 if (c == '&') {
769 int pos = original.indexOf(";", i);
770 String entity = original.substring(i+1, pos);
771 if (entity.equals("gt")) {
772 filtered.append(">");
773 } else if (entity.equals("lt")) {
774 filtered.append("<");
775 } else if (entity.equals("apos")) {
776 filtered.append("'");
777 } else if (entity.equals("amp")) {
778 filtered.append("&");
779 } else if (entity.equals("quot")) {
780 filtered.append("\"");
781 } else {
782 filtered.append("&"+entity+";");
783 }
784 i = pos;
785 }
786 else {
787 filtered.append(c);
788 }
789 }
790 return filtered.toString();
791 }
792}
Note: See TracBrowser for help on using the repository browser.