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

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

Reformatting and tidying the imports of this file before some changes

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