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

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

Analyse xsl files in advance to find out what metadata we need

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