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

Last change on this file since 28850 was 28850, checked in by kjdon, 10 years ago

moved a couple of methods here from OAIXML as they are just generic xml stuff. With OAIserver I am creating a request with no user context, so modified createBasicRequest to handle that. Should this be allowed? Should oaiserver use user context???

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