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

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

Changing the merge method so that it allows for modes

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