source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java@ 25983

Last change on this file since 25983 was 25983, checked in by sjm84, 12 years ago

A minor fix as well as adding in a new element type

  • Property svn:keywords set to Author Date Id Revision
File size: 44.5 KB
Line 
1/*
2 * GSXML.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.io.Serializable;
22import java.io.StringWriter;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.Iterator;
26import java.util.Map;
27import java.util.Set;
28import java.util.Vector;
29
30import javax.xml.transform.OutputKeys;
31import javax.xml.transform.Transformer;
32import javax.xml.transform.TransformerFactory;
33import javax.xml.transform.dom.DOMSource;
34import javax.xml.transform.stream.StreamResult;
35
36import org.apache.log4j.Logger;
37import org.w3c.dom.Document;
38import org.w3c.dom.Element;
39import org.w3c.dom.NamedNodeMap;
40import org.w3c.dom.Node;
41import org.w3c.dom.NodeList;
42import org.w3c.dom.Text;
43
44/** various functions for extracting info out of GS XML */
45public class GSXML
46{
47
48 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName());
49
50 // greenstone message xml elements
51 public static final String MESSAGE_ELEM = "message";
52 public static final String REQUEST_ELEM = "request";
53 public static final String RESPONSE_ELEM = "response";
54 public static final String COLLECTION_ELEM = "collection";
55 public static final String SERVICE_ELEM = "service";
56 public static final String CLUSTER_ELEM = "serviceCluster";
57 public static final String SITE_ELEM = "site";
58 public static final String PARAM_ELEM = "param";
59 public static final String PARAM_OPTION_ELEM = "option";
60 public static final String CONTENT_ELEM = "content";
61 public static final String RESOURCE_ELEM = "resource";
62 public static final String DOCUMENT_ELEM = "document";
63 public static final String METADATA_ELEM = "metadata";
64 public static final String SERVICE_CLASS_ELEM = "serviceRack";
65 public static final String CLASSIFIER_ELEM = "classifier";
66 public static final String APPLET_ELEM = "applet";
67 public static final String APPLET_DATA_ELEM = "appletData";
68 public static final String CONFIGURE_ELEM = "configure";
69 public static final String STATUS_ELEM = "status";
70 public static final String ERROR_ELEM = "error";
71 public static final String DEFAULT_ELEM = "default";
72 public static final String STYLESHEET_ELEM = "format";//"stylesheet"; // any additional stylesheet stuff is carried in the message inside this elem
73 public static final String FORMAT_ELEM = "format"; // config files use format - should we use this instead of stylesheet??
74 public static final String GLOBAL_FORMAT_ELEM = "globalFormat";
75 public static final String TERM_ELEM = "term";
76 public static final String STOPWORD_ELEM = "stopword";
77 public static final String FACET_ELEM = "facet";
78 public static final String COUNT_ELEM = "count";
79 public static final String SYSTEM_ELEM = "system";
80 public static final String FORMAT_STRING_ELEM = "formatString";
81
82 //config file elems
83 public static final String COLLECTION_CONFIG_ELEM = "collectionConfig";
84 public static final String COLLECTION_BUILD_ELEM = "buildConfig";
85 public static final String COLLECTION_INIT_ELEM = "collectionInit";
86 public static final String RECOGNISE_ELEM = "recognise";
87 public static final String DOC_TYPE_ELEM = "docType";
88 public static final String SEARCH_ELEM = "search";
89 public static final String INFODB_ELEM = "infodb";
90 public static final String INDEX_ELEM = "index";
91 public static final String INDEX_STEM_ELEM = "indexStem";
92 public static final String INDEX_OPTION_ELEM = "indexOption";
93 public static final String BROWSE_ELEM = "browse";
94 public static final String DISPLAY_ELEM = "display";
95 public static final String LEVEL_ELEM = "level";
96 public static final String REPLACE_ELEM = "replace";
97
98 public static final String DBINFO_ELEM = "dbInfo";
99 public static final String DBNAME_ATT = "dbname";
100 public static final String DBPATH_ATT = "dbpath";
101 public static final String SQLSTATE_ATT = "sqlstate";
102 public static final String DATABASE_TYPE_ELEM = "databaseType";
103 public static final String SHORTNAME_ATT = "shortname";
104 public static final String NOTIFY_ELEM = "notify";
105 public static final String NOTIFY_HOST_ATT = "host";
106
107 //doc.xml file elems
108 public static final String DOCXML_SECTION_ELEM = "Section";
109 public static final String DOCXML_DESCRIPTION_ELEM = "Description";
110 public static final String DOCXML_METADATA_ELEM = "Metadata";
111 public static final String DOCXML_CONTENT_ELEM = "Content";
112
113 // elems for the pages to be processed by xslt
114 public final static String PAGE_ELEM = "page";
115 public final static String CONFIGURATION_ELEM = "config";
116 public final static String PAGE_REQUEST_ELEM = "pageRequest";
117 public final static String PAGE_RESPONSE_ELEM = "pageResponse";
118 public final static String PAGE_EXTRA_ELEM = "pageExtra";
119
120 //public final static String DESCRIPTION_ELEM = "description";
121
122 public static final String ACTION_ELEM = "action";
123 public static final String SUBACTION_ELEM = "subaction";
124
125 // add on to another elem type to get a list of that type
126 public static final String LIST_MODIFIER = "List";
127 // used to refer back to another element type
128 public static final String REF_MODIFIER = "Ref";
129 // greenstone xml attributes
130 public static final String COLLECTION_ATT = "collection";
131 public static final String NAME_ATT = "name";
132 public static final String TO_ATT = "to";
133 public static final String USER_ID_ATT = "uid";
134 public static final String FROM_ATT = "from";
135 public static final String LANG_ATT = "lang";
136 public static final String TYPE_ATT = "type";
137 public static final String DB_TYPE_ATT = "dbType";
138 public static final String VALUE_ATT = "value";
139 public static final String DEFAULT_ATT = "default";
140 public static final String INFO_ATT = "info";
141 public static final String ACTION_ATT = "action";
142 public static final String SUBACTION_ATT = "subaction";
143 public static final String OUTPUT_ATT = "output";
144 public static final String ADDRESS_ATT = "address";
145 public static final String LOCAL_SITE_ATT = "localSite";
146 public static final String LOCAL_SITE_NAME_ATT = "localSiteName";
147 public static final String STATUS_ERROR_CODE_ATT = "code";
148 public static final String STATUS_PROCESS_ID_ATT = "pid";
149 public static final String PARAM_SHORTNAME_ATT = "shortname";
150 public static final String PARAM_IGNORE_POS_ATT = "ignore";
151 public static final String CLASSIFIER_CONTENT_ATT = "content";
152 public static final String ERROR_TYPE_ATT = "type";
153 public static final String COLLECT_TYPE_ATT = "ct";
154 public static final String HIDDEN_ATT = "hidden";
155 public static final String FACET_ATT = "facet";
156
157 // document stuff
158 public static final String DOC_TYPE_ATT = "docType";
159 public static final String DOC_NODE_ELEM = "documentNode";
160 public static final String NODE_CONTENT_ELEM = "nodeContent";
161 public static final String NODE_STRUCTURE_ELEM = "nodeStructure";
162 public static final String NODE_ID_ATT = "nodeID";
163 public static final String HREF_ID_ATT = "hrefId"; // for ids that need translating
164 public static final String ID_MOD_ATT = "idMod"; // might hold .rt etc for hrefIds
165 public static final String NODE_OID = "oid";
166 public static final String NODE_NAME_ATT = "nodeName";
167 public static final String NODE_TYPE_ATT = "nodeType";
168 public static final String NODE_RANK_ATT = "rank";
169 public static final String NODE_TYPE_ROOT = "root";
170 public static final String NODE_TYPE_INTERNAL = "internal";
171 public static final String NODE_TYPE_LEAF = "leaf";
172
173 public static final String DOC_TYPE_SIMPLE = "simple";
174 public static final String DOC_TYPE_PAGED = "paged";
175 public static final String DOC_TYPE_HIERARCHY = "hierarchy";
176 public static final String DOC_TYPE_PAGED_HIERARCHY = "pagedhierarchy";
177
178 public static final String SESSION_EXPIRATION = "session_expiration";
179 public static final String USER_SESSION_CACHE_ATT = "user_session_cache";
180
181 // classifier stuff
182 public static final String CLASS_NODE_ELEM = "classifierNode";
183 public static final String CLASS_NODE_ORIENTATION_ATT = "orientation";
184
185 // parameter types
186 public static final String PARAM_TYPE_INTEGER = "integer";
187 public static final String PARAM_TYPE_BOOLEAN = "boolean";
188 public static final String PARAM_TYPE_ENUM_START = "enum";
189 public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
190 public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
191 public static final String PARAM_TYPE_STRING = "string";
192 public static final String PARAM_TYPE_TEXT = "text";
193 public static final String PARAM_TYPE_MULTI = "multi";
194 public static final String PARAM_TYPE_FILE = "file";
195 public static final String PARAM_TYPE_INVISIBLE = "invisible";
196 public static final String PARAM_TYPE_CHECKBOX_LIST = "checkbox_list";
197 // stuff for text strings
198 public static final String DISPLAY_TEXT_ELEM = "displayItem";
199 // the following are used for the name attributes
200 public static final String DISPLAY_TEXT_NAME = "name";
201 public static final String DISPLAY_TEXT_SUBMIT = "submit";
202 public static final String DISPLAY_TEXT_DESCRIPTION = "description";
203
204 // request types
205 // get the module description
206 public static final String REQUEST_TYPE_DESCRIBE = "describe";
207 // startup a process
208 public static final String REQUEST_TYPE_PROCESS = "process";
209 // get the status of an ongoing process
210 public static final String REQUEST_TYPE_STATUS = "status";
211 // system type request - eg reload a collection
212 public static final String REQUEST_TYPE_SYSTEM = "system";
213 // page requests to the Receptionist/Actions
214 public static final String REQUEST_TYPE_PAGE = "page"; // used to be cgi
215 // get any format info for a service
216 public static final String REQUEST_TYPE_FORMAT = "format";
217 // modify the requests
218 public static final String REQUEST_TYPE_MESSAGING = "messaging";
219 // save the format string
220 public static final String REQUEST_TYPE_FORMAT_STRING = "formatString";
221 // check credentials
222 public static final String REQUEST_TYPE_SECURITY = "security";
223
224 // service types
225 public static final String SERVICE_TYPE_QUERY = "query";
226 public static final String SERVICE_TYPE_RETRIEVE = "retrieve";
227 public static final String SERVICE_TYPE_BROWSE = "browse";
228 public static final String SERVICE_TYPE_APPLET = "applet";
229 public static final String SERVICE_TYPE_PROCESS = "process";
230 public static final String SERVICE_TYPE_ENRICH = "enrich";
231 public static final String SERVICE_TYPE_OAI = "oai";
232 public static final String FLAX_PAGE = "flaxPage";
233 public static final String FLAX_PAGE_GENERATION = "FlaxPageGeneration";
234
235 // system command types and attributes
236 public static final String SYSTEM_TYPE_CONFIGURE = "configure";
237 public static final String SYSTEM_TYPE_ACTIVATE = "activate";
238 public static final String SYSTEM_TYPE_DEACTIVATE = "deactivate";
239 public static final String SYSTEM_TYPE_PING = "ping";
240 //public static final String SYSTEM_TYPE_ISPERSISTENT = "is-persistent";
241
242 public static final String SYSTEM_SUBSET_ATT = "subset";
243 public static final String SYSTEM_MODULE_TYPE_ATT = "moduleType";
244 public static final String SYSTEM_MODULE_NAME_ATT = "moduleName";
245
246 // communicator types
247 public static final String COMM_TYPE_SOAP_JAVA = "soap";
248
249 // error types
250 public static final String ERROR_TYPE_SYNTAX = "syntax";
251 public static final String ERROR_TYPE_SYSTEM = "system";
252 public static final String ERROR_TYPE_INVALID_ID = "invalid_id";
253 public static final String ERROR_TYPE_OTHER = "other";
254
255 // some system wide param names
256 public static final String SUBSET_PARAM = "subset";
257
258 //for plugin
259 public static final String PLUGIN_ELEM = "plugin";
260 public static final String IMPORT_ELEM = "import";
261
262 //for authentication
263 public static final String AUTHEN_NODE_ELEM = "authenticationNode";
264 public static final String USER_NODE_ELEM = "userNode";
265
266 //for configure action results
267 public static final String SUCCESS = "success";
268 public static final String ERROR = "error";
269
270 //security tags and attributes
271 public static final String SECURITY_ELEM = "security";
272 public static final String SCOPE_ATT = "scope";
273 public static final String DEFAULT_ACCESS_ATT = "default_access";
274 public static final String EXCEPTION_ELEM = "exception";
275 public static final String DOCUMENT_SET_ELEM = "documentSet";
276 public static final String GROUP_ELEM = "group";
277 public static final String MATCH_ELEM = "match";
278 public static final String FIELD_ATT = "field";
279 public static final String USER_INFORMATION_ELEM = "userInformation";
280 public static final String USERNAME_ATT = "username";
281 public static final String GROUPS_ATT = "groups";
282 public static final String BASE_URL = "baseURL";
283
284 //for classifiers
285 public static final String CHILD_TYPE_ATT = "childType";
286 public static final String CLASSIFIER_STYLE_ATT = "classifierStyle";
287 public static final String HLIST = "HList";
288 public static final String VLIST = "VList";
289
290 /**
291 * takes a list of elements, and returns an array of strings of the values
292 * of attribute att_name
293 */
294 public static String[] getAttributeValuesFromList(Element list, String att_name)
295 {
296
297 NodeList children = list.getChildNodes();
298
299 int num_nodes = children.getLength();
300 String[] ids = new String[num_nodes];
301 for (int i = 0; i < num_nodes; i++)
302 {
303 Element e = (Element) children.item(i);
304 String id = e.getAttribute(att_name);
305 ids[i] = id;
306 }
307
308 return ids;
309 }
310
311 public static HashMap<String, Serializable> extractParams(Element xml, boolean deep)
312 {
313 return extractParams(xml, deep, null);
314 }
315
316 /**
317 * takes a paramList element, and gets a HashMap of name-value pairs if
318 * deep=true, extracts embedded params, otherwise just top level params
319 */
320 public static HashMap<String, Serializable> extractParams(Element xml, boolean deep, String toFind)
321 {
322
323 if (!xml.getNodeName().equals(PARAM_ELEM + LIST_MODIFIER))
324 {
325 logger.error("paramList element should have been passed to extractParams, instead it was " + xml.getNodeName());
326 return null;
327 }
328
329 NodeList params = null;
330 if (deep)
331 { // get all the nested ones
332 params = xml.getElementsByTagName(PARAM_ELEM);
333 }
334 else
335 { // just get the top level ones
336 params = xml.getChildNodes();
337 }
338 HashMap<String, Serializable> param_map = new HashMap<String, Serializable>();
339 for (int i = 0; i < params.getLength(); i++)
340 {
341 if (params.item(i).getNodeName().equals(PARAM_ELEM))
342 {
343 Element param = (Element) params.item(i);
344 String name = param.getAttribute(NAME_ATT);
345 String value = getValue(param); //att or content
346
347 // For only one parameter
348 if (toFind != null && name.equals(toFind))
349 {
350 param_map.put(name, value);
351 return param_map;
352 }
353 else if (toFind != null)
354 continue;
355
356 int pos = name.indexOf('.');
357 if (pos == -1)
358 { // a base param
359 param_map.put(name, value);
360 }
361 else
362 { // a namespaced param
363
364 String namespace = name.substring(0, pos);
365 name = name.substring(pos + 1);
366 HashMap<String, String> map = (HashMap<String, String>) param_map.get(namespace);
367 if (map == null)
368 {
369 map = new HashMap<String, String>();
370 param_map.put(namespace, map);
371 }
372 map.put(name, value);
373 }
374 }
375 }
376 return param_map;
377 }
378
379 /** gets the value att or the text content */
380 public static String getValue(Element e)
381 {
382 String val = e.getAttribute(VALUE_ATT);
383 if (val == null || val.equals(""))
384 {
385 // have to get it out of the text
386 val = getNodeText(e);
387
388 }
389 else
390 {
391 // unescape the xml stuff
392 val = unXmlSafe(val);
393 }
394 return val;
395 }
396
397 /** extracts the text out of a node */
398 public static Node getNodeTextNode(Element param)
399 {
400 param.normalize();
401 Node n = param.getFirstChild();
402 while (n != null && n.getNodeType() != Node.TEXT_NODE)
403 {
404 n = n.getNextSibling();
405 }
406 return n;
407 }
408
409 /** extracts the text out of a node */
410 public static String getNodeText(Element param)
411 {
412 Node text_node = getNodeTextNode(param);
413 if (text_node == null)
414 {
415 return "";
416 }
417 return text_node.getNodeValue();
418 }
419
420 public static void setNodeText(Element elem, String text)
421 {
422 Node old_text_node = getNodeTextNode(elem);
423 if (old_text_node != null)
424 {
425 elem.removeChild(old_text_node);
426 }
427 Text t = elem.getOwnerDocument().createTextNode(text);
428 elem.appendChild(t);
429 }
430
431 /** add text to a document/subsection element */
432 public static boolean addDocText(Document owner, Element doc, String text)
433 {
434
435 Element content = owner.createElement(NODE_CONTENT_ELEM);
436 Text t = owner.createTextNode(text);
437 content.appendChild(t);
438 doc.appendChild(content);
439 return true;
440 }
441
442 /** add an error message, unknown error type */
443 public static boolean addError(Document owner, Element doc, String text)
444 {
445 return addError(owner, doc, text, ERROR_TYPE_OTHER);
446 }
447
448 /** add an error message */
449 public static boolean addError(Document owner, Element doc, String text, String error_type)
450 {
451
452 Element content = owner.createElement(ERROR_ELEM);
453 content.setAttribute(ERROR_TYPE_ATT, error_type);
454 Text t = owner.createTextNode(text);
455 content.appendChild(t);
456 doc.appendChild(content);
457 return true;
458 }
459
460 /** add an error message */
461 public static boolean addError(Document owner, Element doc, Throwable error)
462 {
463 return addError(owner, doc, error, ERROR_TYPE_OTHER);
464 }
465
466 /** add an error message */
467 public static boolean addError(Document owner, Element doc, Throwable error, String error_type)
468 {
469 error.printStackTrace();
470 return addError(owner, doc, error.toString(), error_type);
471 }
472
473 public static Element createMetadataParamList(Document owner, Vector meta_values)
474 {
475
476 Element meta_param_list = owner.createElement(PARAM_ELEM + LIST_MODIFIER);
477 Iterator i = meta_values.iterator();
478 while (i.hasNext())
479 {
480 String next = (String) i.next();
481 Element meta_param = owner.createElement(PARAM_ELEM);
482 meta_param_list.appendChild(meta_param);
483 meta_param.setAttribute(NAME_ATT, "metadata");
484 meta_param.setAttribute(VALUE_ATT, next);
485 }
486 return meta_param_list;
487 }
488
489 /** adds a metadata elem to a list */
490 public static boolean addMetadata(Document owner, Element list, String meta_name, String meta_value)
491 {
492 if (meta_value == null || meta_value.equals(""))
493 {
494 return false;
495 }
496 Element data = owner.createElement(METADATA_ELEM);
497 data.setAttribute(NAME_ATT, meta_name);
498 Text t = owner.createTextNode(meta_value);
499 data.appendChild(t);
500 list.appendChild(data);
501 return true;
502
503 }
504
505 /**
506 * copies the metadata out of the metadataList of 'from' into the
507 * metadataList of 'to'
508 */
509 public static boolean mergeMetadataLists(Node to, Node from)
510 {
511 Node from_meta = getChildByTagName(from, METADATA_ELEM + LIST_MODIFIER);
512 if (from_meta == null)
513 { // nothing to copy
514 return true;
515 }
516 return mergeMetadataFromList(to, from_meta);
517 }
518
519 /**
520 * copies the metadata out of the meta_list metadataList into the
521 * metadataList of 'to'
522 */
523 public static boolean mergeMetadataFromList(Node to, Node meta_list)
524 {
525 if (meta_list == null)
526 return false;
527 Node to_meta = getChildByTagName(to, METADATA_ELEM + LIST_MODIFIER);
528 Document to_owner = to.getOwnerDocument();
529 if (to_meta == null)
530 {
531 to.appendChild(to_owner.importNode(meta_list, true));
532 return true;
533 }
534 // copy individual metadata elements
535 NodeList meta_items = ((Element) meta_list).getElementsByTagName(METADATA_ELEM);
536 for (int i = 0; i < meta_items.getLength(); i++)
537 {
538 to_meta.appendChild(to_owner.importNode(meta_items.item(i), true));
539 }
540 return true;
541 }
542
543 /** copies all the children from from to to */
544 public static boolean mergeElements(Element to, Element from)
545 {
546
547 Document owner = to.getOwnerDocument();
548 Node child = from.getFirstChild();
549 while (child != null)
550 {
551 to.appendChild(owner.importNode(child, true));
552 child = child.getNextSibling();
553 }
554 return true;
555 }
556
557 /** returns the (first) element child of the node n */
558 public static Element getFirstElementChild(Node n)
559 {
560
561 Node child = n.getFirstChild();
562 while (child != null)
563 {
564 if (child.getNodeType() == Node.ELEMENT_NODE)
565 {
566 return (Element) child;
567 }
568 child = child.getNextSibling();
569 }
570 return null; //no element child found
571 }
572
573 /** returns the (first) child element with the given name */
574 public static Node getChildByTagName(Node n, String name)
575 {
576 if (n != null)
577 { // this line is an attempted solution to the NullPointerException mentioned
578 // in trac bug ticket #225. If n is null can't do n.getFirstChild() below. As per bug #225:
579 // GSXML.getNodeByPath() is called by GS2BrowseAction, which then calls this method.
580 // If n is null, null will be returned which GS2BrowseAction already checks for. It's here
581 // that the NullPointerException was thrown.
582
583 Node child = n.getFirstChild();
584 while (child != null)
585 {
586 if (child.getNodeName().equals(name))
587 {
588 return child;
589 }
590 child = child.getNextSibling();
591 }
592 }
593 return null; //not found
594 }
595
596 /**
597 * returns the (nth) child element with the given name index numbers start
598 * at 0
599 */
600 public static Node getChildByTagNameIndexed(Node n, String name, int index)
601 {
602 if (index == -1)
603 {
604 return getChildByTagName(n, name);
605 }
606 int count = 0;
607 Node child = n.getFirstChild();
608 while (child != null)
609 {
610 if (child.getNodeName().equals(name))
611 {
612 if (count == index)
613 {
614 return child;
615 }
616 else
617 {
618 count++;
619 }
620 }
621 child = child.getNextSibling();
622 }
623 return null; //not found
624 }
625
626 /**
627 * takes an xpath type expression of the form name/name/... and returns the
628 * first node that matches, or null if not found
629 */
630 public static Node getNodeByPath(Node n, String path)
631 {
632
633 String link = GSPath.getFirstLink(path);
634 path = GSPath.removeFirstLink(path);
635 while (!link.equals(""))
636 {
637 n = getChildByTagName(n, link);
638 if (n == null)
639 {
640 return null;
641 }
642 link = GSPath.getFirstLink(path);
643 path = GSPath.removeFirstLink(path);
644 }
645 return n;
646 }
647
648 /**
649 * takes an xpath type expression of the form name/name/... and returns the
650 * first node that matches, or null if not found can include [i] indices.
651 * index numbers start at 0
652 */
653 public static Node getNodeByPathIndexed(Node n, String path)
654 {
655
656 String link = GSPath.getFirstLink(path);
657 int index = GSPath.getIndex(link);
658 if (index != -1)
659 {
660 link = GSPath.removeIndex(link);
661 }
662 path = GSPath.removeFirstLink(path);
663 while (!link.equals(""))
664 {
665 n = getChildByTagNameIndexed(n, link, index);
666 if (n == null)
667 {
668 return null;
669 }
670 link = GSPath.getFirstLink(path);
671 index = GSPath.getIndex(link);
672 if (index != -1)
673 {
674 link = GSPath.removeIndex(link);
675 }
676 path = GSPath.removeFirstLink(path);
677 }
678 return n;
679 }
680
681 public static HashMap<String, Node> getChildrenMap(Node n)
682 {
683
684 HashMap<String, Node> map = new HashMap<String, Node>();
685 Node child = n.getFirstChild();
686 while (child != null)
687 {
688 String name = child.getNodeName();
689 map.put(name, child);
690 child = child.getNextSibling();
691 }
692 return map;
693 }
694
695 public static NodeList getChildrenByTagName(Node n, String name)
696 {
697 MyNodeList node_list = new MyNodeList();
698 Node child = n.getFirstChild();
699 while (child != null)
700 {
701 if (child.getNodeName().equals(name))
702 {
703 node_list.addNode(child);
704 }
705 child = child.getNextSibling();
706 }
707 return node_list;
708 }
709
710 public static NodeList getChildrenByTagNameNS(Node n, String namespace, String local_name)
711 {
712 MyNodeList node_list = new MyNodeList();
713 Node child = n.getFirstChild();
714 while (child != null)
715 {
716 if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI() != null && child.getNamespaceURI().equals(namespace) && child.getLocalName() != null && child.getLocalName().equals(local_name))
717 {
718 node_list.addNode(child);
719 }
720 child = child.getNextSibling();
721 }
722 return node_list;
723 }
724
725 /** Duplicates an element, but gives it a new name */
726 public static Element duplicateWithNewName(Document owner, Element element, String element_name, boolean with_attributes)
727 {
728 return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes);
729 }
730
731 /** Duplicates an element, but gives it a new name */
732 public static Element duplicateWithNewNameNS(Document owner, Element element, String element_name, String namespace_uri, boolean with_attributes)
733 {
734 Element duplicate;
735 if (namespace_uri == null)
736 {
737 duplicate = owner.createElement(element_name);
738 }
739 else
740 {
741 duplicate = owner.createElementNS(namespace_uri, element_name);
742 }
743 // Copy element attributes
744 if (with_attributes)
745 {
746 NamedNodeMap attributes = element.getAttributes();
747 for (int i = 0; i < attributes.getLength(); i++)
748 {
749 Node attribute = attributes.item(i);
750 duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
751 }
752 }
753
754 // Copy element children
755 NodeList children = element.getChildNodes();
756 for (int i = 0; i < children.getLength(); i++)
757 {
758 Node child = children.item(i);
759 duplicate.appendChild(owner.importNode(child, true));
760 }
761
762 return duplicate;
763 }
764
765 public static void copyAllChildren(Element to, Element from)
766 {
767
768 Document to_doc = to.getOwnerDocument();
769 Node child = from.getFirstChild();
770 while (child != null)
771 {
772 to.appendChild(to_doc.importNode(child, true));
773 child = child.getNextSibling();
774 }
775 }
776
777 /** returns a basic request message */
778 public static Element createBasicRequest(Document owner, String request_type, String to, UserContext userContext)
779 {
780 Element request = owner.createElement(REQUEST_ELEM);
781 request.setAttribute(TYPE_ATT, request_type);
782 request.setAttribute(LANG_ATT, userContext._lang);
783 request.setAttribute(TO_ATT, to);
784 request.setAttribute(USER_ID_ATT, userContext._userID);
785 return request;
786 }
787
788 public static Element createBasicResponse(Document owner, String from)
789 {
790 Element response = owner.createElement(GSXML.RESPONSE_ELEM);
791 response.setAttribute(GSXML.FROM_ATT, from);
792 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
793 return response;
794 }
795
796 public static Element createMetadataElement(Document owner, String name, String value)
797 {
798 Element metaElem = owner.createElement(GSXML.METADATA_ELEM);
799 metaElem.setAttribute(GSXML.NAME_ATT, name);
800 metaElem.setAttribute(GSXML.VALUE_ATT, value);
801 return metaElem;
802 }
803
804 public static Element createTextElement(Document owner, String elem_name, String text)
805 {
806 Element e = owner.createElement(elem_name);
807 Text t = owner.createTextNode(text);
808 e.appendChild(t);
809 return e;
810
811 }
812
813 public static Element createTextElement(Document owner, String elem_name, String text, String att_name, String att_value)
814 {
815 Element e = owner.createElement(elem_name);
816 e.setAttribute(att_name, att_value);
817 Text t = owner.createTextNode(text);
818 e.appendChild(t);
819 return e;
820
821 }
822
823 public static Element createDisplayTextElement(Document owner, String text_name, String text)
824 {
825 Element e = owner.createElement(DISPLAY_TEXT_ELEM);
826 e.setAttribute(NAME_ATT, text_name);
827 Text t = owner.createTextNode(text);
828 e.appendChild(t);
829 return e;
830
831 }
832
833 public static Element createParameter(Document owner, String name, String value)
834 {
835 Element param = owner.createElement(PARAM_ELEM);
836 param.setAttribute(NAME_ATT, name);
837 param.setAttribute(VALUE_ATT, value);
838 return param;
839 }
840
841 public static void addParametersToList(Document owner, Element param_list, HashMap params)
842 {
843 if (params == null)
844 {
845 return;
846 }
847
848 Set items = params.entrySet();
849 Iterator i = items.iterator();
850 while (i.hasNext())
851 {
852 Map.Entry m = (Map.Entry) i.next();
853 param_list.appendChild(createParameter(owner, (String) m.getKey(), (String) m.getValue()));
854 }
855
856 }
857
858 public static Element createParameterDescription(Document owner, String id, String display_name, String type, String default_value, String[] option_ids, String[] option_names)
859 {
860
861 Element p = owner.createElement(PARAM_ELEM);
862 p.setAttribute(NAME_ATT, id);
863 p.setAttribute(TYPE_ATT, type);
864 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
865
866 if (default_value != null)
867 {
868 p.setAttribute(DEFAULT_ATT, default_value);
869 }
870 if (option_ids != null && option_names != null)
871 {
872 for (int i = 0; i < option_ids.length; i++)
873 {
874 Element e = owner.createElement(PARAM_OPTION_ELEM);
875 e.setAttribute(NAME_ATT, option_ids[i]);
876 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names[i]));
877 p.appendChild(e);
878 }
879 }
880 return p;
881 }
882
883 public static Element createParameterDescription2(Document owner, String id, String display_name, String type, String default_value, ArrayList<String> option_ids, ArrayList<String> option_names)
884 {
885
886 Element p = owner.createElement(PARAM_ELEM);
887 p.setAttribute(NAME_ATT, id);
888 p.setAttribute(TYPE_ATT, type);
889 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
890 if (default_value != null)
891 {
892 p.setAttribute(DEFAULT_ATT, default_value);
893 }
894 if (option_ids != null && option_names != null)
895 {
896 for (int i = 0; i < option_ids.size(); i++)
897 {
898 Element e = owner.createElement(PARAM_OPTION_ELEM);
899 e.setAttribute(NAME_ATT, option_ids.get(i));
900 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names.get(i)));
901 p.appendChild(e);
902 }
903 }
904 return p;
905 }
906
907 /** returns the element parent/node_name[@attribute_name='attribute_value'] */
908 public static Element getNamedElement(Element parent, String node_name, String attribute_name, String attribute_value)
909 {
910
911 NodeList children = parent.getChildNodes();
912 for (int i = 0; i < children.getLength(); i++)
913 {
914 Node child = children.item(i);
915 if (child.getNodeName().equals(node_name))
916 {
917 if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
918 return (Element) child;
919 }
920 }
921 // not found
922 return null;
923 }
924
925 public static Element getNamedElementNS(Element parent, String namespace_uri, String node_local_name, String attribute_name, String attribute_value)
926 {
927 NodeList children = parent.getChildNodes();
928 for (int i = 0; i < children.getLength(); i++)
929 {
930 Node child = children.item(i);
931 if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI().equals(namespace_uri) && child.getLocalName().equals(node_local_name))
932 {
933 if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
934 return (Element) child;
935 }
936 }
937 // not found
938 return null;
939 }
940
941 public static NodeList getNamedElementsNS(Element parent, String namespace_uri, String node_local_name, String attribute_name, String attribute_value)
942 {
943 MyNodeList result = new MyNodeList();
944 NodeList children = parent.getChildNodes();
945 for (int i = 0; i < children.getLength(); i++)
946 {
947 Node child = children.item(i);
948 if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI().equals(namespace_uri) && child.getLocalName().equals(node_local_name))
949 {
950 if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
951 result.addNode(child);
952 }
953 }
954 return result;
955 }
956
957 public static NodeList getElementsWithAttributesNS(Element parent, String namespace_uri, String node_local_name, String[] attribute_names, String[] attribute_values)
958 {
959 if (attribute_names.length == 0 || attribute_names.length != attribute_values.length)
960 {
961 return new MyNodeList();
962 }
963
964 MyNodeList result = new MyNodeList();
965
966 NodeList matchingNodes = GSXML.getNamedElementsNS(parent, namespace_uri, node_local_name, attribute_names[0], attribute_values[0]);
967 for (int i = 0; i < matchingNodes.getLength(); i++)
968 {
969 Element current = (Element) matchingNodes.item(i);
970 boolean nodeMatches = true;
971 for (int j = 1; j < attribute_names.length; j++)
972 {
973 String currentName = attribute_names[j];
974 String currentValue = attribute_values[j];
975 if (!current.getAttribute(currentName).equals(currentValue))
976 {
977 nodeMatches = false;
978 break;
979 }
980 }
981
982 if (nodeMatches)
983 {
984 result.addNode(current);
985 }
986 }
987
988 return result;
989 }
990
991 public static void removeElementsWithAttributesNS(Element parent, String namespace_uri, String node_local_name, String[] attribute_names, String[] attribute_values)
992 {
993 NodeList matchingNodes = GSXML.getElementsWithAttributesNS(parent, namespace_uri, node_local_name, attribute_names, attribute_values);
994 for (int i = 0; i < matchingNodes.getLength(); i++)
995 {
996 parent.removeChild(matchingNodes.item(i));
997 }
998 }
999
1000 // In element main, tries to find any previous occurrence of elements with xsl-template-name=templateName,
1001 // and whose named attribute (attributeName) has the same value as the same attribute in node.
1002 // If this is the case, such a previous occurrence is removed from element main, since
1003 // the new node will contain a more specific redefinition of this element.
1004 public static void removeNamedElementNS(Element parent, String namespace_uri, String node_local_name, String attribute_name, String attribute_value)
1005 {
1006 if (attribute_value.equals(""))
1007 {
1008 // it has no identifying attribute, so we can't find any matches
1009 return;
1010 }
1011
1012 Element old_elem = GSXML.getNamedElementNS(parent, namespace_uri, node_local_name, attribute_name, attribute_value);
1013 if (old_elem != null)
1014 {
1015 parent.removeChild(old_elem);
1016 }
1017 }
1018
1019 public static void removeNamedElementsNS(Element parent, String namespace, String node_local_name, String attribute_name, String attribute_value)
1020 {
1021 if (attribute_value.equals(""))
1022 {
1023 // it has no identifying attribute, so we can't find any matches
1024 return;
1025 }
1026
1027 NodeList children = parent.getChildNodes();
1028 for (int i = children.getLength() - 1; i >= 0; i--)
1029 {
1030 Node child = children.item(i);
1031 if (child.getNodeType() == Node.ELEMENT_NODE && child.getNamespaceURI().equals(namespace) && child.getLocalName() != null && child.getLocalName().equals(node_local_name))
1032 {
1033 if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
1034 parent.removeChild(child);
1035 }
1036 }
1037 }
1038
1039 /**
1040 * returns a NodeList of elements:
1041 * ancestor/node_name[@attribute_name='attribute_value']
1042 */
1043 public static NodeList getNamedElements(Element ancestor, String node_name, String attribute_name, String attribute_value)
1044 {
1045 MyNodeList node_list = new MyNodeList();
1046 NodeList children = ancestor.getElementsByTagName(node_name);
1047
1048 if (children != null && children.getLength() > 0)
1049 {
1050
1051 for (int i = 0; i < children.getLength(); i++)
1052 {
1053 Node child = children.item(i);
1054 if (child.getNodeName().equals(node_name))
1055 {
1056 if (((Element) child).getAttribute(attribute_name).equals(attribute_value))
1057 node_list.addNode(child);
1058 }
1059 }
1060 }
1061 return node_list;
1062 }
1063
1064 public static Element getLastElementByTagNameNS(Element main, String namespace, String node_name)
1065 {
1066 NodeList nodes = main.getElementsByTagNameNS(namespace, node_name);
1067 int len = nodes.getLength();
1068 if (len == 0)
1069 {
1070 return null;
1071 }
1072 return (Element) nodes.item(len - 1);
1073 }
1074
1075 public static int SORT_TYPE_STRING = 0;
1076 public static int SORT_TYPE_INT = 1;
1077 public static int SORT_TYPE_FLOAT = 2;
1078
1079 // sort type:
1080 public static Element insertIntoOrderedList(Element parent_node, String node_name, Element start_from_elem, Element new_elem, String sort_att, boolean descending)
1081 {
1082 if (new_elem == null)
1083 return null;
1084 Element cloned_elem = (Element) parent_node.getOwnerDocument().importNode(new_elem, true);
1085 if (start_from_elem == null)
1086 {
1087 parent_node.appendChild(cloned_elem);
1088 return cloned_elem;
1089 }
1090
1091 Node current_node = start_from_elem;
1092 String insert_att = cloned_elem.getAttribute(sort_att);
1093 String list_att = start_from_elem.getAttribute(sort_att);
1094 while ((!descending && list_att.compareTo(insert_att) < 0) || (descending && list_att.compareTo(insert_att) > 0))
1095 {
1096 current_node = current_node.getNextSibling();
1097 if (current_node == null)
1098 break; // end of the list
1099 if (!current_node.getNodeName().equals(node_name))
1100 {
1101 continue; // not a valid node
1102 }
1103 list_att = ((Element) current_node).getAttribute(sort_att);
1104 }
1105
1106 parent_node.insertBefore(cloned_elem, current_node);
1107 return cloned_elem;
1108 }
1109
1110 /**
1111 * Returns the appropriate language element from a display elem, display is
1112 * the containing element, name is the name of the element to look for, lang
1113 * is the preferred language, lang_default is the fall back lang if neither
1114 * lang is found, will return the first one it finds
1115 */
1116 public static String getDisplayText(Element display, String name, String lang, String lang_default)
1117 {
1118
1119 String def = null;
1120 String first = null;
1121 NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM);
1122 if (elems.getLength() == 0)
1123 return "";
1124 for (int i = 0; i < elems.getLength(); i++)
1125 {
1126 Element e = (Element) elems.item(i);
1127 String n = e.getAttribute(NAME_ATT);
1128 if (name.equals(n))
1129 {
1130 String l = e.getAttribute(LANG_ATT);
1131 if (lang.equals(l))
1132 {
1133 return getNodeText(e);
1134 }
1135 else if (lang_default.equals(l))
1136 {
1137 def = getNodeText(e);
1138 }
1139 else if (first == null)
1140 {
1141 first = getNodeText(e);
1142 }
1143 }
1144 else
1145 {
1146 continue;
1147 }
1148 }
1149
1150 if (def != null)
1151 {
1152 return def;
1153 }
1154 if (first != null)
1155 {
1156 return first;
1157 }
1158 return "";
1159 }
1160
1161 // replaces < > " ' & in the original with their entities
1162 public static String xmlSafe(String original)
1163 {
1164
1165 StringBuffer filtered = new StringBuffer(original.length());
1166 char c;
1167 for (int i = 0; i < original.length(); i++)
1168 {
1169 c = original.charAt(i);
1170 if (c == '>')
1171 {
1172 filtered.append("&gt;");
1173 }
1174 else if (c == '<')
1175 {
1176 filtered.append("&lt;");
1177 }
1178 else if (c == '"')
1179 {
1180 filtered.append("&quot;");
1181 }
1182 else if (c == '&')
1183 {
1184 filtered.append("&amp;");
1185 }
1186 else if (c == '\'')
1187 {
1188 filtered.append("&apos;");
1189 }
1190 else
1191 {
1192 filtered.append(c);
1193 }
1194 }
1195 return filtered.toString();
1196 }
1197
1198 // replaces < > " ' & entities with their originals
1199 public static String unXmlSafe(String original)
1200 {
1201
1202 StringBuffer filtered = new StringBuffer(original.length());
1203 char c;
1204 for (int i = 0; i < original.length(); i++)
1205 {
1206 c = original.charAt(i);
1207 if (c == '&')
1208 {
1209 int pos = original.indexOf(";", i);
1210 String entity = original.substring(i + 1, pos);
1211 if (entity.equals("gt"))
1212 {
1213 filtered.append(">");
1214 }
1215 else if (entity.equals("lt"))
1216 {
1217 filtered.append("<");
1218 }
1219 else if (entity.equals("apos"))
1220 {
1221 filtered.append("'");
1222 }
1223 else if (entity.equals("amp"))
1224 {
1225 filtered.append("&");
1226 }
1227 else if (entity.equals("quot"))
1228 {
1229 filtered.append("\"");
1230 }
1231 else
1232 {
1233 filtered.append("&" + entity + ";");
1234 }
1235 i = pos;
1236 }
1237 else
1238 {
1239 filtered.append(c);
1240 }
1241 }
1242 return filtered.toString();
1243 }
1244
1245 public static void printXMLNode(Node e, boolean printText)
1246 {
1247 printXMLNode(e, 0, printText);
1248 }
1249
1250 public static String xmlNodeToString(Node e)
1251 {
1252 StringBuffer sb = new StringBuffer("");
1253 xmlNodeToString(sb, e, true, "\t", 0);
1254 return sb.toString();
1255 }
1256
1257 public static void xmlNodeToString(StringBuffer sb, Node e, boolean indent, String indentString, int depth)
1258 {
1259 if (e.getNodeType() == Node.TEXT_NODE)
1260 {
1261 if (e.getNodeValue() != "")
1262 {
1263 String text = e.getNodeValue();
1264 text = text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("^[\\n\\r\\t\\s]*", "").replaceAll("[\\n\\r\\t\\s]*$", "");
1265 sb.append(text);
1266 }
1267 return;
1268 }
1269
1270 if (e.getNodeType() == Node.COMMENT_NODE)
1271 {
1272 if (e.getNodeValue() != "")
1273 {
1274 sb.append("<!--" + e.getNodeValue() + "-->");
1275 }
1276 return;
1277 }
1278
1279 if (indent)
1280 {
1281 for (int i = 0; i < depth; i++)
1282 {
1283 sb.append(indentString);
1284 }
1285 }
1286
1287 sb.append('<');
1288 sb.append(e.getNodeName());
1289 NamedNodeMap attrs = e.getAttributes();
1290 if (attrs != null)
1291 {
1292 for (int i = 0; i < attrs.getLength(); i++)
1293 {
1294 Node attr = attrs.item(i);
1295 sb.append(' ');
1296 sb.append(attr.getNodeName());
1297 sb.append("=\"");
1298 sb.append(attr.getNodeValue().replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
1299 sb.append('"');
1300 }
1301 }
1302 NodeList children = e.getChildNodes();
1303
1304 boolean hasElements = false;
1305 boolean indentSwapped = false;
1306 for (int i = 0; i < children.getLength(); i++)
1307 {
1308 if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
1309 {
1310 hasElements = true;
1311 }
1312 if (children.item(i).getNodeType() == Node.TEXT_NODE && indent)
1313 {
1314 if (children.item(i).getNodeValue().trim().length() > 0)
1315 {
1316 indentSwapped = true;
1317 indent = false;
1318 }
1319 }
1320 }
1321
1322 if (children == null || children.getLength() == 0)
1323 {
1324 sb.append("/>");
1325
1326 if (indent)
1327 {
1328 sb.append("\n");
1329 }
1330 }
1331 else
1332 {
1333 sb.append(">");
1334 if (hasElements && indent)
1335 {
1336 sb.append("\n");
1337 }
1338
1339 int len = children.getLength();
1340 for (int i = 0; i < len; i++)
1341 {
1342 xmlNodeToString(sb, children.item(i), indent, indentString, depth + 1);
1343 }
1344
1345 if (indent)
1346 {
1347 for (int i = 0; i < depth; i++)
1348 {
1349 sb.append(indentString);
1350 }
1351 }
1352
1353 sb.append("</" + e.getNodeName() + ">");
1354
1355 if ((hasElements && indent) || indentSwapped)
1356 {
1357 sb.append("\n");
1358 }
1359 }
1360 }
1361
1362 public static void printXMLNode(Node e, int depth, boolean printText)
1363 { //recursive method call using DOM API...
1364
1365 if (e == null)
1366 {
1367 return;
1368 }
1369
1370 for (int i = 0; i < depth; i++)
1371 System.out.print(' ');
1372
1373 if (e.getNodeType() == Node.TEXT_NODE)
1374 {
1375 if (printText)
1376 {
1377 System.out.println(e.getNodeValue());
1378 }
1379 else
1380 {
1381 System.out.println("text");
1382 }
1383 return;
1384 }
1385
1386 System.out.print('<');
1387 System.out.print(e.getNodeName());
1388 NamedNodeMap attrs = e.getAttributes();
1389
1390 if (attrs != null)
1391 {
1392 for (int i = 0; i < attrs.getLength(); i++)
1393 {
1394 Node attr = attrs.item(i);
1395 System.out.print(' ');
1396 System.out.print(attr.getNodeName());
1397 System.out.print("=\"");
1398 System.out.print(attr.getNodeValue());
1399 System.out.print('"');
1400 }
1401 }
1402
1403 NodeList children = e.getChildNodes();
1404
1405 if (children == null || children.getLength() == 0)
1406 System.out.println("/>");
1407 else
1408 {
1409
1410 System.out.println('>');
1411
1412 int len = children.getLength();
1413 for (int i = 0; i < len; i++)
1414 {
1415 printXMLNode(children.item(i), depth + 1, printText);
1416 }
1417
1418 for (int i = 0; i < depth; i++)
1419 System.out.print(' ');
1420
1421 System.out.println("</" + e.getNodeName() + ">");
1422 }
1423 }
1424
1425 public static void elementToLogAsString(Element e, boolean indent)
1426 {
1427 String str = elementToString(e, indent);
1428 System.err.println(str);
1429 logger.error(str);
1430 }
1431
1432 public static String elementToString(Element e, boolean indent)
1433 {
1434 String str = "";
1435 try
1436 {
1437 TransformerFactory tf = TransformerFactory.newInstance();
1438 Transformer trans = tf.newTransformer();
1439 StringWriter sw = new StringWriter();
1440 if (indent)
1441 {
1442 trans.setOutputProperty(OutputKeys.INDENT, "yes");
1443 }
1444 else
1445 {
1446 trans.setOutputProperty(OutputKeys.INDENT, "no");
1447 }
1448 trans.transform(new DOMSource(e), new StreamResult(sw));
1449 str += sw.toString();
1450 }
1451 catch (Exception ex)
1452 {
1453 str += "Exception: couldn't write " + e + " to log";
1454 }
1455 finally
1456 {
1457 return str;
1458 }
1459 }
1460
1461 public static ArrayList<String> getGroupsFromSecurityResponse(Element securityResponse)
1462 {
1463 ArrayList<String> groups = new ArrayList<String>();
1464
1465 Element groupList = (Element) GSXML.getChildByTagName(securityResponse, GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
1466 if (groupList == null)
1467 {
1468 return groups;
1469 }
1470
1471 NodeList groupElems = GSXML.getChildrenByTagName(groupList, GSXML.GROUP_ELEM);
1472
1473 for (int i = 0; i < groupElems.getLength(); i++)
1474 {
1475 Element groupElem = (Element) groupElems.item(i);
1476 groups.add(groupElem.getAttribute(GSXML.NAME_ATT));
1477 }
1478
1479 return groups;
1480 }
1481
1482 public static NodeList getHTMLStructureElements(Document doc)
1483 {
1484 MyNodeList elems = new MyNodeList();
1485
1486 String[] structureTagNames = new String[] { "html", "div", "td", "li" };
1487
1488 for (String tagName : structureTagNames)
1489 {
1490 NodeList htmlElems = doc.getElementsByTagName(tagName);
1491 elems.addNodeList(htmlElems);
1492 }
1493
1494 return elems;
1495 }
1496
1497 public static void addDebugSpanTags(Document doc)
1498 {
1499 NodeList allElements = doc.getElementsByTagName("*");
1500
1501 for (int i = 0; i < allElements.getLength(); i++)
1502 {
1503 Element current = (Element) allElements.item(i);
1504 String debugString = null;
1505 if ((debugString = (String) current.getUserData("GSDEBUGFILENAME")) != null)
1506 {
1507 System.err.println("DEBUGSTRING = " + debugString);
1508
1509 Element xmlSpan = doc.createElement("span");
1510 xmlSpan.setAttribute("style", "display:none;");
1511 xmlSpan.setAttribute("class", "debugSpan");
1512 xmlSpan.appendChild(doc.createTextNode("\"filename\":\"" + debugString + "\", \"xml\":\"" + GSXML.xmlNodeToString((Element) current.getUserData("GSDEBUGXML")) + "\""));
1513
1514 if (current.hasChildNodes())
1515 {
1516 current.insertBefore(xmlSpan, current.getFirstChild());
1517 }
1518 else
1519 {
1520 current.appendChild(xmlSpan);
1521 }
1522 }
1523 }
1524 }
1525}
Note: See TracBrowser for help on using the repository browser.