source: main/branches/stable_gs3/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java@ 21915

Last change on this file since 21915 was 21915, checked in by xiao, 14 years ago

merge updates from trunk

  • Property svn:keywords set to Author Date Id Revision
File size: 33.9 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 org.w3c.dom.NamedNodeMap;
22import org.w3c.dom.Node;
23import org.w3c.dom.Element;
24import org.w3c.dom.NodeList;
25import org.w3c.dom.Document;
26import org.w3c.dom.Text;
27
28
29import javax.xml.transform.TransformerFactory;
30import javax.xml.transform.Transformer;
31import java.io.StringWriter;
32import javax.xml.transform.stream.StreamResult;
33import javax.xml.transform.dom.DOMSource;
34
35import java.util.Map;
36import java.util.Set;
37import java.util.HashMap;
38import java.util.Vector;
39import java.util.Iterator;
40import java.util.ArrayList;
41
42//import java.util.Locale;
43
44import org.apache.log4j.*;
45
46/** various functions for extracting info out of GS XML */
47public class GSXML {
48
49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName());
50
51 // greenstone xml elements
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??
75 public static final String TERM_ELEM = "term";
76 public static final String SYSTEM_ELEM = "system";
77
78 //config file elems
79 public static final String COLLECTION_CONFIG_ELEM = "collectionConfig";
80 public static final String COLLECTION_BUILD_ELEM = "buildConfig";
81 public static final String COLLECTION_INIT_ELEM = "collectionInit";
82 public static final String RECOGNISE_ELEM = "recognise";
83 public static final String DOC_TYPE_ELEM = "docType";
84 public static final String SEARCH_ELEM = "search";
85 public static final String INDEX_ELEM = "index";
86 public static final String INDEX_STEM_ELEM = "indexStem";
87 public static final String INDEX_OPTION_ELEM = "indexOption";
88 public static final String BROWSE_ELEM = "browse";
89 public static final String DISPLAY_ELEM = "display";
90 public static final String LEVEL_ELEM = "level";
91
92 public static final String DBINFO_ELEM = "dbInfo";
93 public static final String DBNAME_ATT = "dbname";
94 public static final String DBPATH_ATT = "dbpath";
95 public static final String SQLSTATE_ATT = "sqlstate";
96 public static final String DATABASE_TYPE_ELEM = "databaseType";
97 public static final String SHORTNAME_ATT = "shortname";
98 public static final String NOTIFY_ELEM = "notify";
99 public static final String NOTIFY_HOST_ATT = "host";
100
101
102
103 // elems for the pages to be processed by xslt
104 public final static String PAGE_ELEM = "page";
105 public final static String CONFIGURATION_ELEM = "config";
106 public final static String PAGE_REQUEST_ELEM = "pageRequest";
107 public final static String PAGE_RESPONSE_ELEM = "pageResponse";
108 public final static String PAGE_EXTRA_ELEM = "pageExtra";
109
110 //public final static String DESCRIPTION_ELEM = "description";
111
112 public static final String ACTION_ELEM = "action";
113 public static final String SUBACTION_ELEM = "subaction";
114
115 // add on to another elem type to get a list of that type
116 public static final String LIST_MODIFIER = "List";
117
118 // greenstone xml attributes
119 public static final String NAME_ATT = "name";
120 public static final String TO_ATT = "to";
121 public static final String USER_ID_ATT = "uid";
122 public static final String FROM_ATT = "from";
123 public static final String LANG_ATT = "lang";
124 public static final String TYPE_ATT = "type";
125 public static final String VALUE_ATT = "value";
126 public static final String DEFAULT_ATT = "default";
127 public static final String INFO_ATT = "info";
128 public static final String ACTION_ATT = "action";
129 public static final String SUBACTION_ATT = "subaction";
130 public static final String OUTPUT_ATT = "output";
131 public static final String ADDRESS_ATT = "address";
132 public static final String LOCAL_SITE_ATT = "localSite";
133 public static final String LOCAL_SITE_NAME_ATT = "localSiteName";
134 public static final String STATUS_ERROR_CODE_ATT = "code";
135 public static final String STATUS_PROCESS_ID_ATT = "pid";
136 public static final String PARAM_SHORTNAME_ATT = "shortname";
137 public static final String PARAM_IGNORE_POS_ATT = "ignore";
138 public static final String CLASSIFIER_CONTENT_ATT = "content";
139 public static final String ERROR_TYPE_ATT = "type";
140 public static final String COLLECT_TYPE_ATT = "ct";
141 public static final String HIDDEN_ATT = "hidden";
142
143 // document stuff
144 public static final String DOC_TYPE_ATT = "docType";
145 public static final String DOC_NODE_ELEM = "documentNode";
146 public static final String NODE_CONTENT_ELEM = "nodeContent";
147 public static final String NODE_STRUCTURE_ELEM = "nodeStructure";
148 public static final String NODE_ID_ATT = "nodeID";
149 public static final String NODE_NAME_ATT = "nodeName";
150 public static final String NODE_TYPE_ATT = "nodeType";
151 public static final String NODE_RANK_ATT = "rank";
152 public static final String NODE_TYPE_ROOT = "root";
153 public static final String NODE_TYPE_INTERNAL = "internal";
154 public static final String NODE_TYPE_LEAF = "leaf";
155
156 public static final String DOC_TYPE_SIMPLE = "simple";
157 public static final String DOC_TYPE_PAGED = "paged";
158 public static final String DOC_TYPE_HIERARCHY = "hierarchy";
159
160 public static final String SESSION_EXPIRATION = "session_expiration";
161 public static final String USER_SESSION_CACHE_ATT = "user_session_cache";
162
163 // classifier stuff
164 public static final String CLASS_NODE_ELEM = "classifierNode";
165 public static final String CLASS_NODE_ORIENTATION_ATT = "orientation";
166
167 // parameter types
168 public static final String PARAM_TYPE_INTEGER = "integer";
169 public static final String PARAM_TYPE_BOOLEAN = "boolean";
170 public static final String PARAM_TYPE_ENUM_START = "enum";
171 public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
172 public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
173 public static final String PARAM_TYPE_STRING = "string";
174 public static final String PARAM_TYPE_TEXT = "text";
175 public static final String PARAM_TYPE_MULTI = "multi";
176 public static final String PARAM_TYPE_FILE = "file";
177 public static final String PARAM_TYPE_INVISIBLE = "invisible";
178 // stuff for text strings
179 public static final String DISPLAY_TEXT_ELEM = "displayItem";
180 // the following are used for the name attributes
181 public static final String DISPLAY_TEXT_NAME = "name";
182 public static final String DISPLAY_TEXT_SUBMIT = "submit";
183 public static final String DISPLAY_TEXT_DESCRIPTION = "description";
184
185 // request types
186 // get the module description
187 public static final String REQUEST_TYPE_DESCRIBE = "describe";
188 // startup a process
189 public static final String REQUEST_TYPE_PROCESS = "process";
190 // get the status of an ongoing process
191 public static final String REQUEST_TYPE_STATUS = "status";
192 // system type request - eg reload a collection
193 public static final String REQUEST_TYPE_SYSTEM = "system";
194 // page requests to the Receptionist/Actions
195 public static final String REQUEST_TYPE_PAGE = "page"; // used to be cgi
196 // get any format info for a service
197 public static final String REQUEST_TYPE_FORMAT = "format";
198 // modify the requests
199 public static final String REQUEST_TYPE_MESSAGING = "messaging";
200
201 // service types
202 public static final String SERVICE_TYPE_QUERY = "query";
203 public static final String SERVICE_TYPE_RETRIEVE = "retrieve";
204 public static final String SERVICE_TYPE_BROWSE = "browse";
205 public static final String SERVICE_TYPE_APPLET = "applet";
206 public static final String SERVICE_TYPE_PROCESS = "process";
207 public static final String SERVICE_TYPE_ENRICH = "enrich";
208 public static final String SERVICE_TYPE_OAI = "oai";
209 public static final String FLAX_PAGE = "flaxPage";
210 public static final String FLAX_PAGE_GENERATION = "FlaxPageGeneration";
211
212 // system command types and attributes
213 public static final String SYSTEM_TYPE_CONFIGURE = "configure";
214 public static final String SYSTEM_TYPE_ACTIVATE = "activate";
215 public static final String SYSTEM_TYPE_DEACTIVATE = "deactivate";
216
217 public static final String SYSTEM_SUBSET_ATT = "subset";
218 public static final String SYSTEM_MODULE_TYPE_ATT = "moduleType";
219 public static final String SYSTEM_MODULE_NAME_ATT = "moduleName";
220
221 // communicator types
222 public static final String COMM_TYPE_SOAP_JAVA = "soap";
223
224 // error types
225 public static final String ERROR_TYPE_SYNTAX = "syntax";
226 public static final String ERROR_TYPE_SYSTEM = "system";
227 public static final String ERROR_TYPE_INVALID_ID = "invalid_id";
228 public static final String ERROR_TYPE_OTHER = "other";
229
230 // some system wide param names
231 public static final String SUBSET_PARAM = "subset";
232
233 //for plugin
234 public static final String PLUGIN_ELEM = "plugin";
235 public static final String IMPORT_ELEM = "import";
236
237 //for authentication
238 public static final String AUTHEN_NODE_ELEM="authenticationNode";
239 public static final String USER_NODE_ELEM="userNode";
240
241 //for configure action results
242 public static final String SUCCESS = "success";
243 public static final String ERROR = "error";
244
245 /** takes a list of elements, and returns an array of strings
246 * of the values of attribute att_name */
247 public static String [] getAttributeValuesFromList(Element list,
248 String att_name) {
249
250 NodeList children = list.getChildNodes();
251
252 int num_nodes = children.getLength();
253 String []ids = new String[num_nodes];
254 for (int i=0; i<num_nodes; i++) {
255 Element e = (Element)children.item(i);
256 String id = e.getAttribute(att_name);
257 ids[i] = id;
258 }
259
260 return ids;
261 }
262
263 /** takes a paramList element, and gets a HashMap of name-value pairs
264 * if deep=true, extracts embedded params, otherwise just top level
265 * params*/
266 public static HashMap extractParams(Element xml, boolean deep) {
267
268 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
269 logger.error("paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
270 return null;
271 }
272
273 NodeList params = null;
274 if (deep) { // get all the nested ones
275 params = xml.getElementsByTagName(PARAM_ELEM);
276 } else { // just get the top level ones
277 params = xml.getChildNodes();
278 }
279 HashMap param_map = new HashMap();
280 for (int i=0; i<params.getLength(); i++) {
281 if (params.item(i).getNodeName().equals(PARAM_ELEM)) {
282 Element param = (Element)params.item(i);
283 String name=param.getAttribute(NAME_ATT);
284 String value=getValue(param); //att or content
285 int pos = name.indexOf('.');
286 if (pos == -1) { // a base param
287 param_map.put(name, value);
288 } else { // a namespaced param
289
290 String namespace = name.substring(0, pos);
291 name = name.substring(pos+1);
292 HashMap map = (HashMap)param_map.get(namespace);
293 if (map == null) {
294 map = new HashMap();
295 param_map.put(namespace, map);
296 }
297 map.put(name, value);
298 }
299 }
300 }
301 return param_map;
302 }
303
304 /** gets the value att or the text content */
305 public static String getValue(Element e) {
306 String val = e.getAttribute(VALUE_ATT);
307 if (val ==null || val.equals("")) {
308 // have to get it out of the text
309 val=getNodeText(e);
310
311 } else {
312 // unescape the xml stuff
313 val = unXmlSafe(val);
314 }
315 return val;
316 }
317
318 /** extracts the text out of a node */
319 public static Node getNodeTextNode(Element param) {
320 param.normalize();
321 Node n = param.getFirstChild();
322 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
323 n=n.getNextSibling();
324 }
325 return n;
326 }
327
328 /** extracts the text out of a node */
329 public static String getNodeText(Element param) {
330 Node text_node = getNodeTextNode(param);
331 if (text_node == null) {
332 return "";
333 }
334 return text_node.getNodeValue();
335 }
336
337 public static void setNodeText(Element elem, String text) {
338 Node old_text_node = getNodeTextNode(elem);
339 if (old_text_node != null) {
340 elem.removeChild(old_text_node);
341 }
342 Text t = elem.getOwnerDocument().createTextNode(text);
343 elem.appendChild(t);
344 }
345
346 /** add text to a document/subsection element */
347 public static boolean addDocText(Document owner, Element doc, String text) {
348
349 Element content = owner.createElement(NODE_CONTENT_ELEM);
350 Text t = owner.createTextNode(text);
351 content.appendChild(t);
352 doc.appendChild(content);
353 return true;
354 }
355
356 /** add an error message, unknown error type */
357 public static boolean addError(Document owner, Element doc, String text) {
358 return addError(owner, doc, text, ERROR_TYPE_OTHER);
359 }
360 /** add an error message */
361 public static boolean addError(Document owner, Element doc, String text,
362 String error_type) {
363
364 Element content = owner.createElement(ERROR_ELEM);
365 content.setAttribute(ERROR_TYPE_ATT, error_type);
366 Text t = owner.createTextNode(text);
367 content.appendChild(t);
368 doc.appendChild(content);
369 return true;
370 }
371
372 /** add an error message */
373 public static boolean addError(Document owner, Element doc, Throwable error) {
374 return addError(owner, doc, error, ERROR_TYPE_OTHER);
375 }
376
377 /** add an error message */
378 public static boolean addError(Document owner, Element doc,
379 Throwable error, String error_type) {
380 error.printStackTrace();
381 return addError(owner, doc, error.toString(), error_type);
382 }
383
384 public static Element createMetadataParamList(Document owner, Vector meta_values) {
385
386 Element meta_param_list = owner.createElement(PARAM_ELEM+LIST_MODIFIER);
387 Iterator i = meta_values.iterator();
388 while(i.hasNext()) {
389 String next = (String)i.next();
390 Element meta_param = owner.createElement(PARAM_ELEM);
391 meta_param_list.appendChild(meta_param);
392 meta_param.setAttribute(NAME_ATT, "metadata");
393 meta_param.setAttribute(VALUE_ATT, next);
394 }
395 return meta_param_list;
396 }
397
398 /** adds a metadata elem to a list */
399 public static boolean addMetadata(Document owner, Element list,
400 String meta_name, String meta_value) {
401 if (meta_value==null || meta_value.equals("")) {
402 return false;
403 }
404 Element data = owner.createElement(METADATA_ELEM);
405 data.setAttribute(NAME_ATT, meta_name);
406 Text t = owner.createTextNode(meta_value);
407 data.appendChild(t);
408 list.appendChild(data);
409 return true;
410
411 }
412
413 /** copies the metadata out of teh metadataList of 'from' into
414 * the metadataList of 'to' */
415 public static boolean mergeMetadataLists(Node to, Node from) {
416 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
417 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
418
419 if (from_meta == null) { // nothing to copy
420 return true;
421 }
422 Document to_owner = to.getOwnerDocument();
423 Node new_from = to_owner.importNode(from_meta, true);
424
425 if (to_meta == null) { // just copy the whole list
426 to.appendChild(new_from);
427 return true;
428 }
429
430 // copy individual elements
431 Node child = new_from.getFirstChild();
432 while ( child != null) {
433 to_meta.appendChild(child);
434 child = child.getNextSibling();
435 }
436 return true;
437 }
438
439 /** copies all the children from from to to */
440 public static boolean mergeElements(Element to, Element from) {
441
442 Document owner = to.getOwnerDocument();
443 Node child = from.getFirstChild();
444 while (child != null) {
445 to.appendChild(owner.importNode(child, true));
446 child = child.getNextSibling();
447 }
448 return true;
449 }
450 /** returns the (first) element child of the node n */
451 public static Element getFirstElementChild(Node n) {
452
453 Node child = n.getFirstChild();
454 while (child!=null) {
455 if (child.getNodeType() == Node.ELEMENT_NODE) {
456 return (Element)child;
457 }
458 child = child.getNextSibling();
459 }
460 return null; //no element child found
461 }
462 /** returns the (first) child element with the given name */
463 public static Node getChildByTagName(Node n, String name) {
464 if(n != null) { // this line is an attempted solution to the NullPointerException mentioned
465 // in trac bug ticket #225. If n is null can't do n.getFirstChild() below. As per bug #225:
466 // GSXML.getNodeByPath() is called by GS2BrowseAction, which then calls this method.
467 // If n is null, null will be returned which GS2BrowseAction already checks for. It's here
468 // that the NullPointerException was thrown.
469
470 Node child = n.getFirstChild();
471 while (child!=null) {
472 if (child.getNodeName().equals(name)) {
473 return child;
474 }
475 child = child.getNextSibling();
476 }
477 }
478 return null; //not found
479 }
480
481 /** returns the (nth) child element with the given name
482 * index numbers start at 0 */
483 public static Node getChildByTagNameIndexed(Node n, String name, int index) {
484 if (index == -1) {
485 return getChildByTagName(n, name);
486 }
487 int count = 0;
488 Node child = n.getFirstChild();
489 while (child!=null) {
490 if (child.getNodeName().equals(name)) {
491 if (count == index) {
492 return child;
493 } else {
494 count++;
495 }
496 }
497 child = child.getNextSibling();
498 }
499 return null; //not found
500 }
501
502 /** takes an xpath type expression of the form name/name/...
503 * and returns the first node that matches, or null if not found */
504 public static Node getNodeByPath(Node n, String path) {
505
506 String link = GSPath.getFirstLink(path);
507 path = GSPath.removeFirstLink(path);
508 while (!link.equals("")) {
509 n = getChildByTagName(n, link);
510 if (n==null) {
511 return null;
512 }
513 link = GSPath.getFirstLink(path);
514 path = GSPath.removeFirstLink(path);
515 }
516 return n;
517 }
518
519 /** takes an xpath type expression of the form name/name/...
520 * and returns the first node that matches, or null if not found
521 * can include [i] indices. index numbers start at 0 */
522 public static Node getNodeByPathIndexed(Node n, String path) {
523
524 String link = GSPath.getFirstLink(path);
525 int index = GSPath.getIndex(link);
526 if (index != -1) {
527 link = GSPath.removeIndex(link);
528 }
529 path = GSPath.removeFirstLink(path);
530 while (!link.equals("")) {
531 n = getChildByTagNameIndexed(n, link, index);
532 if (n==null) {
533 return null;
534 }
535 link = GSPath.getFirstLink(path);
536 index = GSPath.getIndex(link);
537 if (index != -1) {
538 link = GSPath.removeIndex(link);
539 }
540 path = GSPath.removeFirstLink(path);
541 }
542 return n;
543 }
544
545 public static HashMap getChildrenMap(Node n) {
546
547 HashMap map= new HashMap();
548 Node child = n.getFirstChild();
549 while (child!=null) {
550 String name = child.getNodeName();
551 map.put(name, child);
552 child = child.getNextSibling();
553 }
554 return map;
555 }
556
557 public static NodeList getChildrenByTagName(Node n, String name) {
558 MyNodeList node_list = new MyNodeList();
559 Node child = n.getFirstChild();
560 while (child!=null) {
561 if (child.getNodeName().equals(name)) {
562 node_list.addNode(child);
563 }
564 child = child.getNextSibling();
565 }
566 return node_list;
567 }
568
569
570 /** Duplicates an element, but gives it a new name */
571 public static Element duplicateWithNewName(Document owner, Element element,
572 String element_name, boolean with_attributes) {
573 return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes);
574 }
575
576 /** Duplicates an element, but gives it a new name */
577 public static Element duplicateWithNewNameNS(Document owner,
578 Element element,
579 String element_name,
580 String namespace_uri,
581 boolean with_attributes) {
582 Element duplicate;
583 if (namespace_uri == null) {
584 duplicate = owner.createElement(element_name);
585 } else {
586 duplicate = owner.createElementNS(namespace_uri, element_name);
587 }
588 // Copy element attributes
589 if (with_attributes) {
590 NamedNodeMap attributes = element.getAttributes();
591 for (int i = 0; i < attributes.getLength(); i++) {
592 Node attribute = attributes.item(i);
593 duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
594 }
595 }
596
597 // Copy element children
598 NodeList children = element.getChildNodes();
599 for (int i = 0; i < children.getLength(); i++) {
600 Node child = children.item(i);
601 duplicate.appendChild(owner.importNode(child, true));
602 }
603
604 return duplicate;
605 }
606
607 public static void copyAllChildren(Element to, Element from) {
608
609 Document to_doc = to.getOwnerDocument();
610 Node child = from.getFirstChild();
611 while (child != null) {
612 to.appendChild(to_doc.importNode(child, true));
613 child = child.getNextSibling();
614 }
615 }
616 /** returns a basic request message */
617 public static Element createBasicRequest(Document owner,
618 String request_type, String to,
619 String lang,
620 String uid) {
621 Element request = owner.createElement(REQUEST_ELEM);
622 request.setAttribute(TYPE_ATT, request_type);
623 request.setAttribute(LANG_ATT, lang);
624 request.setAttribute(TO_ATT, to);
625 request.setAttribute(USER_ID_ATT, uid);
626 return request;
627 }
628
629 public static Element createTextElement(Document owner, String elem_name,
630 String text) {
631 Element e = owner.createElement(elem_name);
632 Text t = owner.createTextNode(text);
633 e.appendChild(t);
634 return e;
635
636 }
637
638 public static Element createTextElement(Document owner, String elem_name,
639 String text, String att_name, String att_value) {
640 Element e = owner.createElement(elem_name);
641 e.setAttribute(att_name, att_value);
642 Text t = owner.createTextNode(text);
643 e.appendChild(t);
644 return e;
645
646 }
647
648 public static Element createDisplayTextElement(Document owner,
649 String text_name,
650 String text) {
651 Element e = owner.createElement(DISPLAY_TEXT_ELEM);
652 e.setAttribute(NAME_ATT, text_name);
653 Text t = owner.createTextNode(text);
654 e.appendChild(t);
655 return e;
656
657 }
658
659
660 public static Element createParameter(Document owner, String name,
661 String value) {
662 Element param = owner.createElement(PARAM_ELEM);
663 param.setAttribute(NAME_ATT, name);
664 param.setAttribute(VALUE_ATT, value);
665 return param;
666 }
667
668 public static void addParametersToList(Document owner, Element param_list,
669 HashMap params) {
670 Set items = params.entrySet();
671 Iterator i = items.iterator();
672 while(i.hasNext()) {
673 Map.Entry m = (Map.Entry)i.next();
674 param_list.appendChild(createParameter(owner, (String)m.getKey(), (String)m.getValue()));
675 }
676
677 }
678
679 public static Element createParameterDescription(Document owner,
680 String id,
681 String display_name,
682 String type,
683 String default_value,
684 String []option_ids,
685 String []option_names) {
686
687
688 Element p = owner.createElement(PARAM_ELEM);
689 p.setAttribute(NAME_ATT, id);
690 p.setAttribute(TYPE_ATT, type);
691 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
692
693 if (default_value != null) {
694 p.setAttribute(DEFAULT_ATT, default_value);
695 }
696 if (option_ids!=null && option_names!=null) {
697 for (int i=0; i<option_ids.length; i++) {
698 Element e = owner.createElement(PARAM_OPTION_ELEM);
699 e.setAttribute(NAME_ATT, option_ids[i]);
700 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names[i]));
701 p.appendChild(e);
702 }
703 }
704 return p;
705 }
706 public static Element createParameterDescription2(Document owner,
707 String id,
708 String display_name,
709 String type,
710 String default_value,
711 ArrayList option_ids,
712 ArrayList option_names) {
713
714
715 Element p = owner.createElement(PARAM_ELEM);
716 p.setAttribute(NAME_ATT, id);
717 p.setAttribute(TYPE_ATT, type);
718 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
719 if (default_value != null) {
720 p.setAttribute(DEFAULT_ATT, default_value);
721 }
722 if (option_ids!=null && option_names!=null) {
723 for (int i=0; i<option_ids.size(); i++) {
724 Element e = owner.createElement(PARAM_OPTION_ELEM);
725 e.setAttribute(NAME_ATT, (String)option_ids.get(i));
726 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, (String)option_names.get(i)));
727 p.appendChild(e);
728 }
729 }
730 return p;
731 }
732
733
734 /** returns the element parent/node_name[@attribute_name='attribute_value']
735 */
736 public static Element getNamedElement(Element parent, String node_name,
737 String attribute_name,
738 String attribute_value) {
739
740 NodeList children = parent.getChildNodes();
741 for (int i=0; i<children.getLength(); i++) {
742 Node child = children.item(i);
743 if (child.getNodeName().equals(node_name)) {
744 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
745 return (Element)child;
746 }
747 }
748 // not found
749 return null;
750 }
751 /** returns a NodeList of elements: ancestor/node_name[@attribute_name='attribute_value']
752 */
753 public static NodeList getNamedElements(Element ancestor, String node_name, String attribute_name, String attribute_value) {
754 MyNodeList node_list = new MyNodeList();
755 NodeList children = ancestor.getElementsByTagName(node_name);
756
757 if(children != null && children.getLength() > 0) {
758
759 for (int i=0; i<children.getLength(); i++) {
760 Node child = children.item(i);
761 if (child.getNodeName().equals(node_name)) {
762 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
763 node_list.addNode(child);
764 }
765 }
766 }
767 return node_list;
768 }
769
770 public static int SORT_TYPE_STRING = 0;
771 public static int SORT_TYPE_INT = 1;
772 public static int SORT_TYPE_FLOAT = 2;
773
774 // sort type:
775 public static Element insertIntoOrderedList(Element parent_node,
776 String node_name,
777 Element start_from_elem,
778 Element new_elem, String sort_att,
779 boolean descending) {
780 if (new_elem == null) return null;
781 Element cloned_elem = (Element)parent_node.getOwnerDocument().importNode(new_elem, true);
782 if (start_from_elem == null) {
783 parent_node.appendChild(cloned_elem);
784 return cloned_elem;
785 }
786
787 Node current_node = start_from_elem;
788 String insert_att = cloned_elem.getAttribute(sort_att);
789 String list_att = start_from_elem.getAttribute(sort_att);
790 while ((!descending && list_att.compareTo(insert_att)<0) || (descending && list_att.compareTo(insert_att)>0)) {
791 current_node = current_node.getNextSibling();
792 if (current_node == null) break; // end of the list
793 if (!current_node.getNodeName().equals(node_name)) {
794 continue; // not a valid node
795 }
796 list_att = ((Element)current_node).getAttribute(sort_att);
797 }
798
799 parent_node.insertBefore(cloned_elem, current_node);
800 return cloned_elem;
801 }
802
803
804 /** Returns the appropriate language element from a display elem,
805 * display is the containing element, name is the name of the element to
806 * look for, lang is the preferred language, lang_default is the fall back
807 * lang if neither lang is found, will return the first one it finds*/
808 public static String getDisplayText(Element display, String name,
809 String lang, String lang_default) {
810
811 String def = null;
812 String first = null;
813 NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM);
814 if (elems.getLength() == 0) return "";
815 for (int i=0; i<elems.getLength(); i++) {
816 Element e = (Element)elems.item(i);
817 String n = e.getAttribute(NAME_ATT);
818 if (name.equals(n)) {
819 String l = e.getAttribute(LANG_ATT);
820 if (lang.equals(l)) {
821 return getNodeText(e);
822 } else if (lang_default.equals(l)) {
823 def = getNodeText(e);
824 } else if (first == null) {
825 first = getNodeText(e);
826 }
827 } else {
828 continue;
829 }
830 }
831
832 if (def != null) {
833 return def;
834 }
835 if (first != null) {
836 return first;
837 }
838 return "";
839 }
840
841 // replaces < > " ' & in the original with their entities
842 public static String xmlSafe(String original) {
843
844 StringBuffer filtered = new StringBuffer(original.length());
845 char c;
846 for (int i=0; i<original.length(); i++) {
847 c = original.charAt(i);
848 if (c == '>') {
849 filtered.append("&gt;");
850 } else if (c == '<') {
851 filtered.append("&lt;");
852 } else if (c == '"') {
853 filtered.append("&quot;");
854 } else if (c == '&') {
855 filtered.append("&amp;");
856 } else if (c == '\'') {
857 filtered.append("&apos;");
858 } else {
859 filtered.append(c);
860 }
861 }
862 return filtered.toString();
863 }
864
865
866 // replaces < > " ' & entities with their originals
867 public static String unXmlSafe(String original) {
868
869 StringBuffer filtered = new StringBuffer(original.length());
870 char c;
871 for (int i=0; i<original.length(); i++) {
872 c = original.charAt(i);
873 if (c == '&') {
874 int pos = original.indexOf(";", i);
875 String entity = original.substring(i+1, pos);
876 if (entity.equals("gt")) {
877 filtered.append(">");
878 } else if (entity.equals("lt")) {
879 filtered.append("<");
880 } else if (entity.equals("apos")) {
881 filtered.append("'");
882 } else if (entity.equals("amp")) {
883 filtered.append("&");
884 } else if (entity.equals("quot")) {
885 filtered.append("\"");
886 } else {
887 filtered.append("&"+entity+";");
888 }
889 i = pos;
890 } else {
891 filtered.append(c);
892 }
893 }
894 return filtered.toString();
895 }
896
897 public static void printXMLNode(Node e) {
898 printXMLNode(e, 0) ;
899 }
900
901 public static String xmlNodeToString(Node e){
902 StringBuffer sb = new StringBuffer("");
903 xmlNodeToString(sb,e,0);
904 return sb.toString();
905 }
906
907 private static void xmlNodeToString(StringBuffer sb, Node e, int depth){
908
909 for (int i=0 ; i<depth ; i++)
910 sb.append(' ') ;
911
912 if (e.getNodeType() == Node.TEXT_NODE){
913 sb.append("text") ;
914 return ;
915 }
916
917 sb.append('<');
918 sb.append(e.getNodeName());
919 NamedNodeMap attrs = e.getAttributes();
920 for (int i = 0; i < attrs.getLength(); i++) {
921 Node attr = attrs.item(i);
922 sb.append(' ');
923 sb.append(attr.getNodeName());
924 sb.append("=\"");
925 sb.append(attr.getNodeValue());
926 sb.append('"');
927 }
928
929 NodeList children = e.getChildNodes();
930
931 if (children == null || children.getLength() == 0)
932 sb.append("/>\n") ;
933 else {
934
935 sb.append(">\n") ;
936
937 int len = children.getLength();
938 for (int i = 0; i < len; i++) {
939 xmlNodeToString(sb,children.item(i), depth + 1);
940 }
941
942 for (int i=0 ; i<depth ; i++)
943 sb.append(' ') ;
944
945 sb.append("</" + e.getNodeName() + ">\n");
946 }
947
948
949 }
950
951 public static void printXMLNode(Node e, int depth) { //recursive method call using DOM API...
952
953 for (int i=0 ; i<depth ; i++)
954 System.out.print(' ') ;
955
956 if (e.getNodeType() == Node.TEXT_NODE){
957 // shouldn't we actually print the text here????
958 System.out.println("text") ;
959 return ;
960 }
961
962 System.out.print('<');
963 System.out.print(e.getNodeName());
964 NamedNodeMap attrs = e.getAttributes();
965 for (int i = 0; i < attrs.getLength(); i++) {
966 Node attr = attrs.item(i);
967 System.out.print(' ');
968 System.out.print(attr.getNodeName());
969 System.out.print("=\"");
970 System.out.print(attr.getNodeValue());
971 System.out.print('"');
972 }
973
974 NodeList children = e.getChildNodes();
975
976 if (children == null || children.getLength() == 0)
977 System.out.println("/>") ;
978 else {
979
980 System.out.println('>') ;
981
982 int len = children.getLength();
983 for (int i = 0; i < len; i++) {
984 printXMLNode(children.item(i), depth + 1);
985 }
986
987 for (int i=0 ; i<depth ; i++)
988 System.out.print(' ') ;
989
990 System.out.println("</" + e.getNodeName() + ">");
991 }
992
993 }
994
995 public static void elementToLogAsString(Element e) {
996 try {
997 TransformerFactory tf = TransformerFactory.newInstance();
998 Transformer trans = tf.newTransformer();
999 StringWriter sw = new StringWriter();
1000 trans.transform(new DOMSource(e), new StreamResult(sw));
1001 System.err.println( sw.toString() );
1002 } catch( Exception ex ) {
1003 System.err.println( "couldn't write " + e + " to log" );
1004 }
1005
1006 }
1007}
Note: See TracBrowser for help on using the repository browser.