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

Last change on this file since 25969 was 25969, checked in by kjdon, 12 years ago

added new pagedhierarchy doctype - for structured paged image documents

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