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