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

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

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 25.0 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 SITE_NAME_ELEM = "localSiteName";
75 public static final String SITE_HTTP_ADDRESS_ELEM = "httpAddress";
76 public static final String ACTION_ELEM = "action";
77 public static final String SUBACTION_ELEM = "subaction";
78
79 // add on to another elem type to get a list of that type
80 public static final String LIST_MODIFIER = "List";
81
82 // greenstone xml attributes
83 public static final String NAME_ATT = "name";
84 public static final String TO_ATT = "to";
85 public static final String USER_ID_ATT = "uid";
86 public static final String FROM_ATT = "from";
87 public static final String LANG_ATT = "lang";
88 public static final String TYPE_ATT = "type";
89 public static final String VALUE_ATT = "value";
90 public static final String DEFAULT_ATT = "default";
91 public static final String INFO_ATT = "info";
92 public static final String ACTION_ATT = "action";
93 public static final String SUBACTION_ATT = "subaction";
94 public static final String OUTPUT_ATT = "output";
95 public static final String ADDRESS_ATT = "address";
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
156 // service types
157 public static final String SERVICE_TYPE_QUERY = "query";
158 public static final String SERVICE_TYPE_RETRIEVE = "retrieve";
159 public static final String SERVICE_TYPE_BROWSE = "browse";
160 public static final String SERVICE_TYPE_APPLET = "applet";
161 public static final String SERVICE_TYPE_PROCESS = "process";
162 public static final String SERVICE_TYPE_ENRICH = "enrich";
163
164 // system command types and attributes
165 public static final String SYSTEM_TYPE_CONFIGURE = "configure";
166 public static final String SYSTEM_TYPE_ACTIVATE = "activate";
167 public static final String SYSTEM_TYPE_DEACTIVATE = "deactivate";
168
169 public static final String SYSTEM_SUBSET_ATT = "subset";
170 public static final String SYSTEM_MODULE_TYPE_ATT = "moduleType";
171 public static final String SYSTEM_MODULE_NAME_ATT = "moduleName";
172
173 // communicator types
174 public static final String COMM_TYPE_SOAP_JAVA = "soap";
175
176 // error types
177 public static final String ERROR_TYPE_SYNTAX = "syntax";
178 public static final String ERROR_TYPE_SYSTEM = "system";
179 public static final String ERROR_TYPE_INVALID_ID = "invalid_id";
180 public static final String ERROR_TYPE_OTHER = "other";
181
182 // some system wide param names
183 public static final String SUBSET_PARAM = "subset";
184
185 /** takes a list of elements, and returns an array of strings
186 * of the values of attribute att_name */
187 public static String [] getAttributeValuesFromList(Element list,
188 String att_name) {
189
190 NodeList children = list.getChildNodes();
191
192 int num_nodes = children.getLength();
193 String []ids = new String[num_nodes];
194 for (int i=0; i<num_nodes; i++) {
195 Element e = (Element)children.item(i);
196 String id = e.getAttribute(att_name);
197 ids[i] = id;
198 }
199
200 return ids;
201 }
202
203 /** takes a paramList element, and gets a HashMap of name-value pairs
204 * if deep=true, extracts embedded params, otherwise just top level
205 * params*/
206 public static HashMap extractParams(Element xml, boolean deep) {
207
208 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
209 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
210 return null;
211 }
212
213 NodeList params = null;
214 if (deep) { // get all the nested ones
215 params = xml.getElementsByTagName(PARAM_ELEM);
216 } else { // just get the top level ones
217 params = xml.getChildNodes();
218 }
219 HashMap param_map = new HashMap();
220 for (int i=0; i<params.getLength(); i++) {
221 if (params.item(i).getNodeName().equals(PARAM_ELEM)) {
222 Element param = (Element)params.item(i);
223 String name=param.getAttribute(NAME_ATT);
224 String value=getValue(param); //att or content
225 int pos = name.indexOf('.');
226 if (pos == -1) { // a base param
227 param_map.put(name, value);
228 } else { // a namespaced param
229
230 String namespace = name.substring(0, pos);
231 name = name.substring(pos+1);
232 HashMap map = (HashMap)param_map.get(namespace);
233 if (map == null) {
234 map = new HashMap();
235 param_map.put(namespace, map);
236 }
237 map.put(name, value);
238 }
239 }
240 }
241 return param_map;
242 }
243
244 /** gets the value att or the text content */
245 public static String getValue(Element e) {
246 String val = e.getAttribute(VALUE_ATT);
247 if (val ==null || val.equals("")) {
248 // have to get it out of the text
249 val=getNodeText(e);
250
251 } else {
252 // unescape the xml stuff
253 val = unXmlSafe(val);
254 }
255 return val;
256 }
257
258 /** extracts the text out of a node */
259 public static Node getNodeTextNode(Element param) {
260 param.normalize();
261 Node n = param.getFirstChild();
262 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
263 n=n.getNextSibling();
264 }
265 return n;
266 }
267
268 /** extracts the text out of a node */
269 public static String getNodeText(Element param) {
270 Node text_node = getNodeTextNode(param);
271 if (text_node == null) {
272 return "";
273 }
274 return text_node.getNodeValue();
275 }
276
277
278 /** add text to a document/subsection element */
279 public static boolean addDocText(Document owner, Element doc, String text) {
280
281 Element content = owner.createElement(NODE_CONTENT_ELEM);
282 Text t = owner.createTextNode(text);
283 content.appendChild(t);
284 doc.appendChild(content);
285 return true;
286 }
287
288 /** add an error message, unknown error type */
289 public static boolean addError(Document owner, Element doc, String text) {
290 return addError(owner, doc, text, ERROR_TYPE_OTHER);
291 }
292 /** add an error message */
293 public static boolean addError(Document owner, Element doc, String text,
294 String error_type) {
295
296 Element content = owner.createElement(ERROR_ELEM);
297 content.setAttribute(ERROR_TYPE_ATT, error_type);
298 Text t = owner.createTextNode(text);
299 content.appendChild(t);
300 doc.appendChild(content);
301 return true;
302 }
303
304 /** add an error message */
305 public static boolean addError(Document owner, Element doc, Throwable error) {
306 return addError(owner, doc, error, ERROR_TYPE_OTHER);
307 }
308
309 /** add an error message */
310 public static boolean addError(Document owner, Element doc,
311 Throwable error, String error_type) {
312 error.printStackTrace();
313 return addError(owner, doc, error.toString(), error_type);
314 }
315
316 public static Element createMetadataParamList(Document owner, Vector meta_values) {
317
318 Element meta_param_list = owner.createElement(PARAM_ELEM+LIST_MODIFIER);
319 Iterator i = meta_values.iterator();
320 while(i.hasNext()) {
321 String next = (String)i.next();
322 Element meta_param = owner.createElement(PARAM_ELEM);
323 meta_param_list.appendChild(meta_param);
324 meta_param.setAttribute(NAME_ATT, "metadata");
325 meta_param.setAttribute(VALUE_ATT, next);
326 }
327 return meta_param_list;
328 }
329
330 /** adds a metadata elem to a list */
331 public static boolean addMetadata(Document owner, Element list,
332 String meta_name, String meta_value) {
333 if (meta_value==null || meta_value.equals("")) {
334 return false;
335 }
336 Element data = owner.createElement(METADATA_ELEM);
337 data.setAttribute(NAME_ATT, meta_name);
338 Text t = owner.createTextNode(meta_value);
339 data.appendChild(t);
340 list.appendChild(data);
341 return true;
342
343 }
344
345 /** copies the metadata out of teh metadataList of 'from' into
346 * the metadataList of 'to' */
347 public static boolean mergeMetadataLists(Node to, Node from) {
348 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
349 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
350
351 if (from_meta == null) { // nothing to copy
352 return true;
353 }
354 Document to_owner = to.getOwnerDocument();
355 Node new_from = to_owner.importNode(from_meta, true);
356
357 if (to_meta == null) { // just copy the whole list
358 to.appendChild(new_from);
359 return true;
360 }
361
362 // copy individual elements
363 Node child = new_from.getFirstChild();
364 while ( child != null) {
365 to_meta.appendChild(child);
366 child = child.getNextSibling();
367 }
368 return true;
369 }
370
371 /** copies all the children from from to to */
372 public static boolean mergeElements(Element to, Element from) {
373
374 Document owner = to.getOwnerDocument();
375 Node child = from.getFirstChild();
376 while (child != null) {
377 to.appendChild(owner.importNode(child, true));
378 child = child.getNextSibling();
379 }
380 return true;
381 }
382 /** returns the (first) child element with the given name */
383 public static Node getChildByTagName(Node n, String name) {
384
385 Node child = n.getFirstChild();
386 while (child!=null) {
387 if (child.getNodeName().equals(name)) {
388 return child;
389 }
390 child = child.getNextSibling();
391 }
392 return null; //not found
393 }
394
395 /** takes an xpath type expression of the form name/name/...
396 and returns the first node that matches, or null if not found */
397 public static Node getNodeByPath(Node n, String path) {
398
399 String link = GSPath.getFirstLink(path);
400 path = GSPath.removeFirstLink(path);
401 while (!link.equals("")) {
402 n = getChildByTagName(n, link);
403 if (n==null) {
404 return null;
405 }
406 link = GSPath.getFirstLink(path);
407 path = GSPath.removeFirstLink(path);
408 }
409 return n;
410 }
411 public static HashMap getChildrenMap(Node n) {
412
413 HashMap map= new HashMap();
414 Node child = n.getFirstChild();
415 while (child!=null) {
416 String name = child.getNodeName();
417 map.put(name, child);
418 child = child.getNextSibling();
419 }
420 return map;
421 }
422
423 public static NodeList getChildrenByTagName(Node n, String name) {
424 MyNodeList node_list = new MyNodeList();
425 Node child = n.getFirstChild();
426 while (child!=null) {
427 if (child.getNodeName().equals(name)) {
428 node_list.addNode(child);
429 }
430 child = child.getNextSibling();
431 }
432 return node_list;
433 }
434
435
436 /** Duplicates an element, but gives it a new name */
437 public static Element duplicateWithNewName(Document owner, Element element,
438 String element_name, boolean with_attributes)
439 {
440 return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes);
441 }
442
443 /** Duplicates an element, but gives it a new name */
444 public static Element duplicateWithNewNameNS(Document owner,
445 Element element,
446 String element_name,
447 String namespace_uri,
448 boolean with_attributes)
449 {
450 Element duplicate;
451 if (namespace_uri == null) {
452 duplicate = owner.createElement(element_name);
453 } else {
454 duplicate = owner.createElementNS(namespace_uri, element_name);
455 }
456 // Copy element attributes
457 if (with_attributes) {
458 NamedNodeMap attributes = element.getAttributes();
459 for (int i = 0; i < attributes.getLength(); i++) {
460 Node attribute = attributes.item(i);
461 duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
462 }
463 }
464
465 // Copy element children
466 NodeList children = element.getChildNodes();
467 for (int i = 0; i < children.getLength(); i++) {
468 Node child = children.item(i);
469 duplicate.appendChild(owner.importNode(child, true));
470 }
471
472 return duplicate;
473 }
474
475 public static void copyAllChildren(Element to, Element from) {
476
477 Document to_doc = to.getOwnerDocument();
478 Node child = from.getFirstChild();
479 while (child != null) {
480 to.appendChild(to_doc.importNode(child, true));
481 child = child.getNextSibling();
482 }
483 }
484 /** returns a basic request message */
485 public static Element createBasicRequest(Document owner,
486 String request_type, String to,
487 String lang,
488 String uid) {
489 Element request = owner.createElement(REQUEST_ELEM);
490 request.setAttribute(TYPE_ATT, request_type);
491 request.setAttribute(LANG_ATT, lang);
492 request.setAttribute(TO_ATT, to);
493 request.setAttribute(USER_ID_ATT, uid);
494 return request;
495 }
496
497 public static Element createTextElement(Document owner, String elem_name,
498 String text) {
499 Element e = owner.createElement(elem_name);
500 Text t = owner.createTextNode(text);
501 e.appendChild(t);
502 return e;
503
504 }
505
506 public static Element createDisplayTextElement(Document owner,
507 String text_name,
508 String text) {
509 Element e = owner.createElement(DISPLAY_TEXT_ELEM);
510 e.setAttribute(NAME_ATT, text_name);
511 Text t = owner.createTextNode(text);
512 e.appendChild(t);
513 return e;
514
515 }
516
517
518 public static Element createParameter(Document owner, String name,
519 String value) {
520 Element param = owner.createElement(PARAM_ELEM);
521 param.setAttribute(NAME_ATT, name);
522 param.setAttribute(VALUE_ATT, value);
523 return param;
524 }
525
526 public static void addParametersToList(Document owner, Element param_list,
527 HashMap params) {
528 Set items = params.entrySet();
529 Iterator i = items.iterator();
530 while(i.hasNext()) {
531 Map.Entry m = (Map.Entry)i.next();
532 param_list.appendChild(createParameter(owner, (String)m.getKey(), (String)m.getValue()));
533 }
534
535 }
536
537 public static Element createParameterDescription(Document owner,
538 String id,
539 String display_name,
540 String type,
541 String default_value,
542 String []option_ids,
543 String []option_names) {
544
545
546 Element p = owner.createElement(PARAM_ELEM);
547 p.setAttribute(NAME_ATT, id);
548 p.setAttribute(TYPE_ATT, type);
549 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
550 if (default_value != null) {
551 p.setAttribute(DEFAULT_ATT, default_value);
552 }
553 if (option_ids!=null && option_names!=null) {
554 for (int i=0; i<option_ids.length; i++) {
555 Element e = owner.createElement(PARAM_OPTION_ELEM);
556 e.setAttribute(NAME_ATT, option_ids[i]);
557 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names[i]));
558 p.appendChild(e);
559 }
560 }
561 return p;
562 }
563 public static Element createParameterDescription2(Document owner,
564 String id,
565 String display_name,
566 String type,
567 String default_value,
568 ArrayList option_ids,
569 ArrayList option_names) {
570
571
572 Element p = owner.createElement(PARAM_ELEM);
573 p.setAttribute(NAME_ATT, id);
574 p.setAttribute(TYPE_ATT, type);
575 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
576 if (default_value != null) {
577 p.setAttribute(DEFAULT_ATT, default_value);
578 }
579 if (option_ids!=null && option_names!=null) {
580 for (int i=0; i<option_ids.size(); i++) {
581 Element e = owner.createElement(PARAM_OPTION_ELEM);
582 e.setAttribute(NAME_ATT, (String)option_ids.get(i));
583 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, (String)option_names.get(i)));
584 p.appendChild(e);
585 }
586 }
587 return p;
588 }
589
590
591 /** returns the element parent/node_name[@attribute_name='attribute_value']
592 */
593 public static Element getNamedElement(Element parent, String node_name,
594 String attribute_name,
595 String attribute_value) {
596
597 NodeList children = parent.getChildNodes();
598 for (int i=0; i<children.getLength(); i++) {
599 Node child = children.item(i);
600 ///ystem.out.println("getnamed elem, node nmae="+child.getNodeName());
601 if (child.getNodeName().equals(node_name)) {
602 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
603 return (Element)child;
604 }
605 }
606 // not found
607 return null;
608 }
609
610 public static int SORT_TYPE_STRING = 0;
611 public static int SORT_TYPE_INT = 1;
612 public static int SORT_TYPE_FLOAT = 2;
613
614 // sort type:
615 public static Element insertIntoOrderedList(Element parent_node,
616 String node_name,
617 Element start_from_elem,
618 Element new_elem, String sort_att,
619 boolean descending)
620 {
621 if (new_elem == null) return null;
622 System.err.println("+");
623 Element cloned_elem = (Element)parent_node.getOwnerDocument().importNode(new_elem, true);
624 if (start_from_elem == null) {
625 parent_node.appendChild(cloned_elem);
626 return cloned_elem;
627 }
628 System.err.println("++");
629
630 Node current_node = start_from_elem;
631 String insert_att = cloned_elem.getAttribute(sort_att);
632 String list_att = start_from_elem.getAttribute(sort_att);
633 while ((!descending && list_att.compareTo(insert_att)<0) || (descending && list_att.compareTo(insert_att)>0)) {
634 System.err.println("+++");
635 current_node = current_node.getNextSibling();
636 if (current_node == null) break; // end of the list
637 if (!current_node.getNodeName().equals(node_name)) {
638 continue; // not a valid node
639 }
640 list_att = ((Element)current_node).getAttribute(sort_att);
641 }
642 System.err.println("++++");
643
644 parent_node.insertBefore(cloned_elem, current_node);
645 return cloned_elem;
646 }
647
648
649 /** Returns the appropriate language element from a display elem,
650 display is the containing element, name is the name of the element to
651 look for, lang is the preferred language, lang_default is the fall back
652 lang if neither lang is found, will return the first one it finds*/
653 public static String getDisplayText(Element display, String name,
654 String lang, String lang_default) {
655
656 String def = null;
657 String first = null;
658 NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM);
659 if (elems.getLength() == 0) return "";
660 for (int i=0; i<elems.getLength(); i++) {
661 Element e = (Element)elems.item(i);
662 String n = e.getAttribute(NAME_ATT);
663 if (name.equals(n)) {
664 String l = e.getAttribute(LANG_ATT);
665 if (lang.equals(l)) {
666 return getNodeText(e);
667 } else if (lang_default.equals(l)) {
668 def = getNodeText(e);
669 } else if (first == null) {
670 first = getNodeText(e);
671 }
672 } else {
673 continue;
674 }
675 }
676
677 if (def != null) {
678 return def;
679 }
680 if (first != null) {
681 return first;
682 }
683 return "";
684 }
685
686 // replaces < > " ' & in the original with their entities
687 public static String xmlSafe(String original) {
688
689 StringBuffer filtered = new StringBuffer(original.length());
690 char c;
691 for (int i=0; i<original.length(); i++) {
692 c = original.charAt(i);
693 if (c == '>') {
694 filtered.append("&gt;");
695 } else if (c == '<') {
696 filtered.append("&lt;");
697 } else if (c == '"') {
698 filtered.append("&quot;");
699 } else if (c == '&') {
700 filtered.append("&amp;");
701 } else if (c == '\'') {
702 filtered.append("&apos;");
703 } else {
704 filtered.append(c);
705 }
706 }
707 return filtered.toString();
708 }
709
710
711 // replaces < > " ' & entities with their originals
712 public static String unXmlSafe(String original) {
713
714 StringBuffer filtered = new StringBuffer(original.length());
715 char c;
716 for (int i=0; i<original.length(); i++) {
717 c = original.charAt(i);
718 if (c == '&') {
719 int pos = original.indexOf(";", i);
720 String entity = original.substring(i+1, pos);
721 if (entity.equals("gt")) {
722 filtered.append(">");
723 } else if (entity.equals("lt")) {
724 filtered.append("<");
725 } else if (entity.equals("apos")) {
726 filtered.append("'");
727 } else if (entity.equals("amp")) {
728 filtered.append("&");
729 } else if (entity.equals("quot")) {
730 filtered.append("\"");
731 } else {
732 filtered.append("&"+entity+";");
733 }
734 i = pos;
735 }
736 else {
737 filtered.append(c);
738 }
739 }
740 return filtered.toString();
741 }
742}
Note: See TracBrowser for help on using the repository browser.