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 | //for plugin
|
---|
193 | public static final String PLUGIN_ELEM = "plugin";
|
---|
194 | public static final String IMPORT_ELEM = "import";
|
---|
195 |
|
---|
196 | //for authentication
|
---|
197 | public static final String AUTHEN_NODE_ELEM="authenticationNode";
|
---|
198 | public static final String USER_NODE_ELEM="userNode";
|
---|
199 |
|
---|
200 | /** takes a list of elements, and returns an array of strings
|
---|
201 | * of the values of attribute att_name */
|
---|
202 | public static String [] getAttributeValuesFromList(Element list,
|
---|
203 | String att_name) {
|
---|
204 |
|
---|
205 | NodeList children = list.getChildNodes();
|
---|
206 |
|
---|
207 | int num_nodes = children.getLength();
|
---|
208 | String []ids = new String[num_nodes];
|
---|
209 | for (int i=0; i<num_nodes; i++) {
|
---|
210 | Element e = (Element)children.item(i);
|
---|
211 | String id = e.getAttribute(att_name);
|
---|
212 | ids[i] = id;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return ids;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** takes a paramList element, and gets a HashMap of name-value pairs
|
---|
219 | * if deep=true, extracts embedded params, otherwise just top level
|
---|
220 | * params*/
|
---|
221 | public static HashMap extractParams(Element xml, boolean deep) {
|
---|
222 |
|
---|
223 | if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
|
---|
224 | logger.error("paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
|
---|
225 | return null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | NodeList params = null;
|
---|
229 | if (deep) { // get all the nested ones
|
---|
230 | params = xml.getElementsByTagName(PARAM_ELEM);
|
---|
231 | } else { // just get the top level ones
|
---|
232 | params = xml.getChildNodes();
|
---|
233 | }
|
---|
234 | HashMap param_map = new HashMap();
|
---|
235 | for (int i=0; i<params.getLength(); i++) {
|
---|
236 | if (params.item(i).getNodeName().equals(PARAM_ELEM)) {
|
---|
237 | Element param = (Element)params.item(i);
|
---|
238 | String name=param.getAttribute(NAME_ATT);
|
---|
239 | String value=getValue(param); //att or content
|
---|
240 | int pos = name.indexOf('.');
|
---|
241 | if (pos == -1) { // a base param
|
---|
242 | param_map.put(name, value);
|
---|
243 | } else { // a namespaced param
|
---|
244 |
|
---|
245 | String namespace = name.substring(0, pos);
|
---|
246 | name = name.substring(pos+1);
|
---|
247 | HashMap map = (HashMap)param_map.get(namespace);
|
---|
248 | if (map == null) {
|
---|
249 | map = new HashMap();
|
---|
250 | param_map.put(namespace, map);
|
---|
251 | }
|
---|
252 | map.put(name, value);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|
256 | return param_map;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /** gets the value att or the text content */
|
---|
260 | public static String getValue(Element e) {
|
---|
261 | String val = e.getAttribute(VALUE_ATT);
|
---|
262 | if (val ==null || val.equals("")) {
|
---|
263 | // have to get it out of the text
|
---|
264 | val=getNodeText(e);
|
---|
265 |
|
---|
266 | } else {
|
---|
267 | // unescape the xml stuff
|
---|
268 | val = unXmlSafe(val);
|
---|
269 | }
|
---|
270 | return val;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /** extracts the text out of a node */
|
---|
274 | public static Node getNodeTextNode(Element param) {
|
---|
275 | param.normalize();
|
---|
276 | Node n = param.getFirstChild();
|
---|
277 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
278 | n=n.getNextSibling();
|
---|
279 | }
|
---|
280 | return n;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /** extracts the text out of a node */
|
---|
284 | public static String getNodeText(Element param) {
|
---|
285 | Node text_node = getNodeTextNode(param);
|
---|
286 | if (text_node == null) {
|
---|
287 | return "";
|
---|
288 | }
|
---|
289 | return text_node.getNodeValue();
|
---|
290 | }
|
---|
291 |
|
---|
292 | public static void setNodeText(Element elem, String text) {
|
---|
293 | Node old_text_node = getNodeTextNode(elem);
|
---|
294 | if (old_text_node != null) {
|
---|
295 | elem.removeChild(old_text_node);
|
---|
296 | }
|
---|
297 | Text t = elem.getOwnerDocument().createTextNode(text);
|
---|
298 | elem.appendChild(t);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /** add text to a document/subsection element */
|
---|
302 | public static boolean addDocText(Document owner, Element doc, String text) {
|
---|
303 |
|
---|
304 | Element content = owner.createElement(NODE_CONTENT_ELEM);
|
---|
305 | Text t = owner.createTextNode(text);
|
---|
306 | content.appendChild(t);
|
---|
307 | doc.appendChild(content);
|
---|
308 | return true;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /** add an error message, unknown error type */
|
---|
312 | public static boolean addError(Document owner, Element doc, String text) {
|
---|
313 | return addError(owner, doc, text, ERROR_TYPE_OTHER);
|
---|
314 | }
|
---|
315 | /** add an error message */
|
---|
316 | public static boolean addError(Document owner, Element doc, String text,
|
---|
317 | String error_type) {
|
---|
318 |
|
---|
319 | Element content = owner.createElement(ERROR_ELEM);
|
---|
320 | content.setAttribute(ERROR_TYPE_ATT, error_type);
|
---|
321 | Text t = owner.createTextNode(text);
|
---|
322 | content.appendChild(t);
|
---|
323 | doc.appendChild(content);
|
---|
324 | return true;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /** add an error message */
|
---|
328 | public static boolean addError(Document owner, Element doc, Throwable error) {
|
---|
329 | return addError(owner, doc, error, ERROR_TYPE_OTHER);
|
---|
330 | }
|
---|
331 |
|
---|
332 | /** add an error message */
|
---|
333 | public static boolean addError(Document owner, Element doc,
|
---|
334 | Throwable error, String error_type) {
|
---|
335 | error.printStackTrace();
|
---|
336 | return addError(owner, doc, error.toString(), error_type);
|
---|
337 | }
|
---|
338 |
|
---|
339 | public static Element createMetadataParamList(Document owner, Vector meta_values) {
|
---|
340 |
|
---|
341 | Element meta_param_list = owner.createElement(PARAM_ELEM+LIST_MODIFIER);
|
---|
342 | Iterator i = meta_values.iterator();
|
---|
343 | while(i.hasNext()) {
|
---|
344 | String next = (String)i.next();
|
---|
345 | Element meta_param = owner.createElement(PARAM_ELEM);
|
---|
346 | meta_param_list.appendChild(meta_param);
|
---|
347 | meta_param.setAttribute(NAME_ATT, "metadata");
|
---|
348 | meta_param.setAttribute(VALUE_ATT, next);
|
---|
349 | }
|
---|
350 | return meta_param_list;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /** adds a metadata elem to a list */
|
---|
354 | public static boolean addMetadata(Document owner, Element list,
|
---|
355 | String meta_name, String meta_value) {
|
---|
356 | if (meta_value==null || meta_value.equals("")) {
|
---|
357 | return false;
|
---|
358 | }
|
---|
359 | Element data = owner.createElement(METADATA_ELEM);
|
---|
360 | data.setAttribute(NAME_ATT, meta_name);
|
---|
361 | Text t = owner.createTextNode(meta_value);
|
---|
362 | data.appendChild(t);
|
---|
363 | list.appendChild(data);
|
---|
364 | return true;
|
---|
365 |
|
---|
366 | }
|
---|
367 |
|
---|
368 | /** copies the metadata out of teh metadataList of 'from' into
|
---|
369 | * the metadataList of 'to' */
|
---|
370 | public static boolean mergeMetadataLists(Node to, Node from) {
|
---|
371 | Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
|
---|
372 | Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
|
---|
373 |
|
---|
374 | if (from_meta == null) { // nothing to copy
|
---|
375 | return true;
|
---|
376 | }
|
---|
377 | Document to_owner = to.getOwnerDocument();
|
---|
378 | Node new_from = to_owner.importNode(from_meta, true);
|
---|
379 |
|
---|
380 | if (to_meta == null) { // just copy the whole list
|
---|
381 | to.appendChild(new_from);
|
---|
382 | return true;
|
---|
383 | }
|
---|
384 |
|
---|
385 | // copy individual elements
|
---|
386 | Node child = new_from.getFirstChild();
|
---|
387 | while ( child != null) {
|
---|
388 | to_meta.appendChild(child);
|
---|
389 | child = child.getNextSibling();
|
---|
390 | }
|
---|
391 | return true;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** copies all the children from from to to */
|
---|
395 | public static boolean mergeElements(Element to, Element from) {
|
---|
396 |
|
---|
397 | Document owner = to.getOwnerDocument();
|
---|
398 | Node child = from.getFirstChild();
|
---|
399 | while (child != null) {
|
---|
400 | to.appendChild(owner.importNode(child, true));
|
---|
401 | child = child.getNextSibling();
|
---|
402 | }
|
---|
403 | return true;
|
---|
404 | }
|
---|
405 | /** returns the (first) element child of the node n */
|
---|
406 | public static Element getFirstElementChild(Node n) {
|
---|
407 |
|
---|
408 | Node child = n.getFirstChild();
|
---|
409 | while (child!=null) {
|
---|
410 | if (child.getNodeType() == Node.ELEMENT_NODE) {
|
---|
411 | return (Element)child;
|
---|
412 | }
|
---|
413 | child = child.getNextSibling();
|
---|
414 | }
|
---|
415 | return null; //no element child found
|
---|
416 | }
|
---|
417 | /** returns the (first) child element with the given name */
|
---|
418 | public static Node getChildByTagName(Node n, String name) {
|
---|
419 |
|
---|
420 | Node child = n.getFirstChild();
|
---|
421 | while (child!=null) {
|
---|
422 | if (child.getNodeName().equals(name)) {
|
---|
423 | return child;
|
---|
424 | }
|
---|
425 | child = child.getNextSibling();
|
---|
426 | }
|
---|
427 | return null; //not found
|
---|
428 | }
|
---|
429 |
|
---|
430 | /** returns the (nth) child element with the given name
|
---|
431 | * index numbers start at 0 */
|
---|
432 | public static Node getChildByTagNameIndexed(Node n, String name, int index) {
|
---|
433 | if (index == -1) {
|
---|
434 | return getChildByTagName(n, name);
|
---|
435 | }
|
---|
436 | int count = 0;
|
---|
437 | Node child = n.getFirstChild();
|
---|
438 | while (child!=null) {
|
---|
439 | if (child.getNodeName().equals(name)) {
|
---|
440 | if (count == index) {
|
---|
441 | return child;
|
---|
442 | } else {
|
---|
443 | count++;
|
---|
444 | }
|
---|
445 | }
|
---|
446 | child = child.getNextSibling();
|
---|
447 | }
|
---|
448 | return null; //not found
|
---|
449 | }
|
---|
450 |
|
---|
451 | /** takes an xpath type expression of the form name/name/...
|
---|
452 | * and returns the first node that matches, or null if not found */
|
---|
453 | public static Node getNodeByPath(Node n, String path) {
|
---|
454 |
|
---|
455 | String link = GSPath.getFirstLink(path);
|
---|
456 | path = GSPath.removeFirstLink(path);
|
---|
457 | while (!link.equals("")) {
|
---|
458 | n = getChildByTagName(n, link);
|
---|
459 | if (n==null) {
|
---|
460 | return null;
|
---|
461 | }
|
---|
462 | link = GSPath.getFirstLink(path);
|
---|
463 | path = GSPath.removeFirstLink(path);
|
---|
464 | }
|
---|
465 | return n;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /** takes an xpath type expression of the form name/name/...
|
---|
469 | * and returns the first node that matches, or null if not found
|
---|
470 | * can include [i] indices. index numbers start at 0 */
|
---|
471 | public static Node getNodeByPathIndexed(Node n, String path) {
|
---|
472 |
|
---|
473 | String link = GSPath.getFirstLink(path);
|
---|
474 | int index = GSPath.getIndex(link);
|
---|
475 | if (index != -1) {
|
---|
476 | link = GSPath.removeIndex(link);
|
---|
477 | }
|
---|
478 | path = GSPath.removeFirstLink(path);
|
---|
479 | while (!link.equals("")) {
|
---|
480 | n = getChildByTagNameIndexed(n, link, index);
|
---|
481 | if (n==null) {
|
---|
482 | return null;
|
---|
483 | }
|
---|
484 | link = GSPath.getFirstLink(path);
|
---|
485 | index = GSPath.getIndex(link);
|
---|
486 | if (index != -1) {
|
---|
487 | link = GSPath.removeIndex(link);
|
---|
488 | }
|
---|
489 | path = GSPath.removeFirstLink(path);
|
---|
490 | }
|
---|
491 | return n;
|
---|
492 | }
|
---|
493 |
|
---|
494 | public static HashMap getChildrenMap(Node n) {
|
---|
495 |
|
---|
496 | HashMap map= new HashMap();
|
---|
497 | Node child = n.getFirstChild();
|
---|
498 | while (child!=null) {
|
---|
499 | String name = child.getNodeName();
|
---|
500 | map.put(name, child);
|
---|
501 | child = child.getNextSibling();
|
---|
502 | }
|
---|
503 | return map;
|
---|
504 | }
|
---|
505 |
|
---|
506 | public static NodeList getChildrenByTagName(Node n, String name) {
|
---|
507 | MyNodeList node_list = new MyNodeList();
|
---|
508 | Node child = n.getFirstChild();
|
---|
509 | while (child!=null) {
|
---|
510 | if (child.getNodeName().equals(name)) {
|
---|
511 | node_list.addNode(child);
|
---|
512 | }
|
---|
513 | child = child.getNextSibling();
|
---|
514 | }
|
---|
515 | return node_list;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | /** Duplicates an element, but gives it a new name */
|
---|
520 | public static Element duplicateWithNewName(Document owner, Element element,
|
---|
521 | String element_name, boolean with_attributes) {
|
---|
522 | return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes);
|
---|
523 | }
|
---|
524 |
|
---|
525 | /** Duplicates an element, but gives it a new name */
|
---|
526 | public static Element duplicateWithNewNameNS(Document owner,
|
---|
527 | Element element,
|
---|
528 | String element_name,
|
---|
529 | String namespace_uri,
|
---|
530 | boolean with_attributes) {
|
---|
531 | Element duplicate;
|
---|
532 | if (namespace_uri == null) {
|
---|
533 | duplicate = owner.createElement(element_name);
|
---|
534 | } else {
|
---|
535 | duplicate = owner.createElementNS(namespace_uri, element_name);
|
---|
536 | }
|
---|
537 | // Copy element attributes
|
---|
538 | if (with_attributes) {
|
---|
539 | NamedNodeMap attributes = element.getAttributes();
|
---|
540 | for (int i = 0; i < attributes.getLength(); i++) {
|
---|
541 | Node attribute = attributes.item(i);
|
---|
542 | duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | // Copy element children
|
---|
547 | NodeList children = element.getChildNodes();
|
---|
548 | for (int i = 0; i < children.getLength(); i++) {
|
---|
549 | Node child = children.item(i);
|
---|
550 | duplicate.appendChild(owner.importNode(child, true));
|
---|
551 | }
|
---|
552 |
|
---|
553 | return duplicate;
|
---|
554 | }
|
---|
555 |
|
---|
556 | public static void copyAllChildren(Element to, Element from) {
|
---|
557 |
|
---|
558 | Document to_doc = to.getOwnerDocument();
|
---|
559 | Node child = from.getFirstChild();
|
---|
560 | while (child != null) {
|
---|
561 | to.appendChild(to_doc.importNode(child, true));
|
---|
562 | child = child.getNextSibling();
|
---|
563 | }
|
---|
564 | }
|
---|
565 | /** returns a basic request message */
|
---|
566 | public static Element createBasicRequest(Document owner,
|
---|
567 | String request_type, String to,
|
---|
568 | String lang,
|
---|
569 | String uid) {
|
---|
570 | Element request = owner.createElement(REQUEST_ELEM);
|
---|
571 | request.setAttribute(TYPE_ATT, request_type);
|
---|
572 | request.setAttribute(LANG_ATT, lang);
|
---|
573 | request.setAttribute(TO_ATT, to);
|
---|
574 | request.setAttribute(USER_ID_ATT, uid);
|
---|
575 | return request;
|
---|
576 | }
|
---|
577 |
|
---|
578 | public static Element createTextElement(Document owner, String elem_name,
|
---|
579 | String text) {
|
---|
580 | Element e = owner.createElement(elem_name);
|
---|
581 | Text t = owner.createTextNode(text);
|
---|
582 | e.appendChild(t);
|
---|
583 | return e;
|
---|
584 |
|
---|
585 | }
|
---|
586 |
|
---|
587 | public static Element createDisplayTextElement(Document owner,
|
---|
588 | String text_name,
|
---|
589 | String text) {
|
---|
590 | Element e = owner.createElement(DISPLAY_TEXT_ELEM);
|
---|
591 | e.setAttribute(NAME_ATT, text_name);
|
---|
592 | Text t = owner.createTextNode(text);
|
---|
593 | e.appendChild(t);
|
---|
594 | return e;
|
---|
595 |
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | public static Element createParameter(Document owner, String name,
|
---|
600 | String value) {
|
---|
601 | Element param = owner.createElement(PARAM_ELEM);
|
---|
602 | param.setAttribute(NAME_ATT, name);
|
---|
603 | param.setAttribute(VALUE_ATT, value);
|
---|
604 | return param;
|
---|
605 | }
|
---|
606 |
|
---|
607 | public static void addParametersToList(Document owner, Element param_list,
|
---|
608 | HashMap params) {
|
---|
609 | Set items = params.entrySet();
|
---|
610 | Iterator i = items.iterator();
|
---|
611 | while(i.hasNext()) {
|
---|
612 | Map.Entry m = (Map.Entry)i.next();
|
---|
613 | param_list.appendChild(createParameter(owner, (String)m.getKey(), (String)m.getValue()));
|
---|
614 | }
|
---|
615 |
|
---|
616 | }
|
---|
617 |
|
---|
618 | public static Element createParameterDescription(Document owner,
|
---|
619 | String id,
|
---|
620 | String display_name,
|
---|
621 | String type,
|
---|
622 | String default_value,
|
---|
623 | String []option_ids,
|
---|
624 | String []option_names) {
|
---|
625 |
|
---|
626 |
|
---|
627 | Element p = owner.createElement(PARAM_ELEM);
|
---|
628 | p.setAttribute(NAME_ATT, id);
|
---|
629 | p.setAttribute(TYPE_ATT, type);
|
---|
630 | p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
|
---|
631 |
|
---|
632 | if (default_value != null) {
|
---|
633 | p.setAttribute(DEFAULT_ATT, default_value);
|
---|
634 | }
|
---|
635 | if (option_ids!=null && option_names!=null) {
|
---|
636 | for (int i=0; i<option_ids.length; i++) {
|
---|
637 | Element e = owner.createElement(PARAM_OPTION_ELEM);
|
---|
638 | e.setAttribute(NAME_ATT, option_ids[i]);
|
---|
639 | e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, option_names[i]));
|
---|
640 | p.appendChild(e);
|
---|
641 | }
|
---|
642 | }
|
---|
643 | return p;
|
---|
644 | }
|
---|
645 | public static Element createParameterDescription2(Document owner,
|
---|
646 | String id,
|
---|
647 | String display_name,
|
---|
648 | String type,
|
---|
649 | String default_value,
|
---|
650 | ArrayList option_ids,
|
---|
651 | ArrayList option_names) {
|
---|
652 |
|
---|
653 |
|
---|
654 | Element p = owner.createElement(PARAM_ELEM);
|
---|
655 | p.setAttribute(NAME_ATT, id);
|
---|
656 | p.setAttribute(TYPE_ATT, type);
|
---|
657 | p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
|
---|
658 | if (default_value != null) {
|
---|
659 | p.setAttribute(DEFAULT_ATT, default_value);
|
---|
660 | }
|
---|
661 | if (option_ids!=null && option_names!=null) {
|
---|
662 | for (int i=0; i<option_ids.size(); i++) {
|
---|
663 | Element e = owner.createElement(PARAM_OPTION_ELEM);
|
---|
664 | e.setAttribute(NAME_ATT, (String)option_ids.get(i));
|
---|
665 | e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, (String)option_names.get(i)));
|
---|
666 | p.appendChild(e);
|
---|
667 | }
|
---|
668 | }
|
---|
669 | return p;
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 | /** returns the element parent/node_name[@attribute_name='attribute_value']
|
---|
674 | */
|
---|
675 | public static Element getNamedElement(Element parent, String node_name,
|
---|
676 | String attribute_name,
|
---|
677 | String attribute_value) {
|
---|
678 |
|
---|
679 | NodeList children = parent.getChildNodes();
|
---|
680 | for (int i=0; i<children.getLength(); i++) {
|
---|
681 | Node child = children.item(i);
|
---|
682 | if (child.getNodeName().equals(node_name)) {
|
---|
683 | if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
|
---|
684 | return (Element)child;
|
---|
685 | }
|
---|
686 | }
|
---|
687 | // not found
|
---|
688 | return null;
|
---|
689 | }
|
---|
690 |
|
---|
691 | public static int SORT_TYPE_STRING = 0;
|
---|
692 | public static int SORT_TYPE_INT = 1;
|
---|
693 | public static int SORT_TYPE_FLOAT = 2;
|
---|
694 |
|
---|
695 | // sort type:
|
---|
696 | public static Element insertIntoOrderedList(Element parent_node,
|
---|
697 | String node_name,
|
---|
698 | Element start_from_elem,
|
---|
699 | Element new_elem, String sort_att,
|
---|
700 | boolean descending) {
|
---|
701 | if (new_elem == null) return null;
|
---|
702 | Element cloned_elem = (Element)parent_node.getOwnerDocument().importNode(new_elem, true);
|
---|
703 | if (start_from_elem == null) {
|
---|
704 | parent_node.appendChild(cloned_elem);
|
---|
705 | return cloned_elem;
|
---|
706 | }
|
---|
707 |
|
---|
708 | Node current_node = start_from_elem;
|
---|
709 | String insert_att = cloned_elem.getAttribute(sort_att);
|
---|
710 | String list_att = start_from_elem.getAttribute(sort_att);
|
---|
711 | while ((!descending && list_att.compareTo(insert_att)<0) || (descending && list_att.compareTo(insert_att)>0)) {
|
---|
712 | current_node = current_node.getNextSibling();
|
---|
713 | if (current_node == null) break; // end of the list
|
---|
714 | if (!current_node.getNodeName().equals(node_name)) {
|
---|
715 | continue; // not a valid node
|
---|
716 | }
|
---|
717 | list_att = ((Element)current_node).getAttribute(sort_att);
|
---|
718 | }
|
---|
719 |
|
---|
720 | parent_node.insertBefore(cloned_elem, current_node);
|
---|
721 | return cloned_elem;
|
---|
722 | }
|
---|
723 |
|
---|
724 |
|
---|
725 | /** Returns the appropriate language element from a display elem,
|
---|
726 | * display is the containing element, name is the name of the element to
|
---|
727 | * look for, lang is the preferred language, lang_default is the fall back
|
---|
728 | * lang if neither lang is found, will return the first one it finds*/
|
---|
729 | public static String getDisplayText(Element display, String name,
|
---|
730 | String lang, String lang_default) {
|
---|
731 |
|
---|
732 | String def = null;
|
---|
733 | String first = null;
|
---|
734 | NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM);
|
---|
735 | if (elems.getLength() == 0) return "";
|
---|
736 | for (int i=0; i<elems.getLength(); i++) {
|
---|
737 | Element e = (Element)elems.item(i);
|
---|
738 | String n = e.getAttribute(NAME_ATT);
|
---|
739 | if (name.equals(n)) {
|
---|
740 | String l = e.getAttribute(LANG_ATT);
|
---|
741 | if (lang.equals(l)) {
|
---|
742 | return getNodeText(e);
|
---|
743 | } else if (lang_default.equals(l)) {
|
---|
744 | def = getNodeText(e);
|
---|
745 | } else if (first == null) {
|
---|
746 | first = getNodeText(e);
|
---|
747 | }
|
---|
748 | } else {
|
---|
749 | continue;
|
---|
750 | }
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (def != null) {
|
---|
754 | return def;
|
---|
755 | }
|
---|
756 | if (first != null) {
|
---|
757 | return first;
|
---|
758 | }
|
---|
759 | return "";
|
---|
760 | }
|
---|
761 |
|
---|
762 | // replaces < > " ' & in the original with their entities
|
---|
763 | public static String xmlSafe(String original) {
|
---|
764 |
|
---|
765 | StringBuffer filtered = new StringBuffer(original.length());
|
---|
766 | char c;
|
---|
767 | for (int i=0; i<original.length(); i++) {
|
---|
768 | c = original.charAt(i);
|
---|
769 | if (c == '>') {
|
---|
770 | filtered.append(">");
|
---|
771 | } else if (c == '<') {
|
---|
772 | filtered.append("<");
|
---|
773 | } else if (c == '"') {
|
---|
774 | filtered.append(""");
|
---|
775 | } else if (c == '&') {
|
---|
776 | filtered.append("&");
|
---|
777 | } else if (c == '\'') {
|
---|
778 | filtered.append("'");
|
---|
779 | } else {
|
---|
780 | filtered.append(c);
|
---|
781 | }
|
---|
782 | }
|
---|
783 | return filtered.toString();
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | // replaces < > " ' & entities with their originals
|
---|
788 | public static String unXmlSafe(String original) {
|
---|
789 |
|
---|
790 | StringBuffer filtered = new StringBuffer(original.length());
|
---|
791 | char c;
|
---|
792 | for (int i=0; i<original.length(); i++) {
|
---|
793 | c = original.charAt(i);
|
---|
794 | if (c == '&') {
|
---|
795 | int pos = original.indexOf(";", i);
|
---|
796 | String entity = original.substring(i+1, pos);
|
---|
797 | if (entity.equals("gt")) {
|
---|
798 | filtered.append(">");
|
---|
799 | } else if (entity.equals("lt")) {
|
---|
800 | filtered.append("<");
|
---|
801 | } else if (entity.equals("apos")) {
|
---|
802 | filtered.append("'");
|
---|
803 | } else if (entity.equals("amp")) {
|
---|
804 | filtered.append("&");
|
---|
805 | } else if (entity.equals("quot")) {
|
---|
806 | filtered.append("\"");
|
---|
807 | } else {
|
---|
808 | filtered.append("&"+entity+";");
|
---|
809 | }
|
---|
810 | i = pos;
|
---|
811 | } else {
|
---|
812 | filtered.append(c);
|
---|
813 | }
|
---|
814 | }
|
---|
815 | return filtered.toString();
|
---|
816 | }
|
---|
817 |
|
---|
818 | public static void printXMLNode(Node e) {
|
---|
819 | printXMLNode(e, 0) ;
|
---|
820 | }
|
---|
821 |
|
---|
822 | public static String xmlNodeToString(Node e){
|
---|
823 | StringBuffer sb = new StringBuffer("");
|
---|
824 | xmlNodeToString(sb,e,0);
|
---|
825 | return sb.toString();
|
---|
826 | }
|
---|
827 |
|
---|
828 | private static void xmlNodeToString(StringBuffer sb, Node e, int depth){
|
---|
829 |
|
---|
830 | for (int i=0 ; i<depth ; i++)
|
---|
831 | sb.append(' ') ;
|
---|
832 |
|
---|
833 | if (e.getNodeType() == Node.TEXT_NODE){
|
---|
834 | sb.append("text") ;
|
---|
835 | return ;
|
---|
836 | }
|
---|
837 |
|
---|
838 | sb.append('<');
|
---|
839 | sb.append(e.getNodeName());
|
---|
840 | NamedNodeMap attrs = e.getAttributes();
|
---|
841 | for (int i = 0; i < attrs.getLength(); i++) {
|
---|
842 | Node attr = attrs.item(i);
|
---|
843 | sb.append(' ');
|
---|
844 | sb.append(attr.getNodeName());
|
---|
845 | sb.append("=\"");
|
---|
846 | sb.append(attr.getNodeValue());
|
---|
847 | sb.append('"');
|
---|
848 | }
|
---|
849 |
|
---|
850 | NodeList children = e.getChildNodes();
|
---|
851 |
|
---|
852 | if (children == null || children.getLength() == 0)
|
---|
853 | sb.append("/>\n") ;
|
---|
854 | else {
|
---|
855 |
|
---|
856 | sb.append(">\n") ;
|
---|
857 |
|
---|
858 | int len = children.getLength();
|
---|
859 | for (int i = 0; i < len; i++) {
|
---|
860 | xmlNodeToString(sb,children.item(i), depth + 1);
|
---|
861 | }
|
---|
862 |
|
---|
863 | for (int i=0 ; i<depth ; i++)
|
---|
864 | sb.append(' ') ;
|
---|
865 |
|
---|
866 | sb.append("</" + e.getNodeName() + ">\n");
|
---|
867 | }
|
---|
868 |
|
---|
869 |
|
---|
870 | }
|
---|
871 |
|
---|
872 | public static void printXMLNode(Node e, int depth) { //recursive method call using DOM API...
|
---|
873 |
|
---|
874 | for (int i=0 ; i<depth ; i++)
|
---|
875 | System.out.print(' ') ;
|
---|
876 |
|
---|
877 | if (e.getNodeType() == Node.TEXT_NODE){
|
---|
878 | // shouldn't we actually print the text here????
|
---|
879 | System.out.println("text") ;
|
---|
880 | return ;
|
---|
881 | }
|
---|
882 |
|
---|
883 | System.out.print('<');
|
---|
884 | System.out.print(e.getNodeName());
|
---|
885 | NamedNodeMap attrs = e.getAttributes();
|
---|
886 | for (int i = 0; i < attrs.getLength(); i++) {
|
---|
887 | Node attr = attrs.item(i);
|
---|
888 | System.out.print(' ');
|
---|
889 | System.out.print(attr.getNodeName());
|
---|
890 | System.out.print("=\"");
|
---|
891 | System.out.print(attr.getNodeValue());
|
---|
892 | System.out.print('"');
|
---|
893 | }
|
---|
894 |
|
---|
895 | NodeList children = e.getChildNodes();
|
---|
896 |
|
---|
897 | if (children == null || children.getLength() == 0)
|
---|
898 | System.out.println("/>") ;
|
---|
899 | else {
|
---|
900 |
|
---|
901 | System.out.println('>') ;
|
---|
902 |
|
---|
903 | int len = children.getLength();
|
---|
904 | for (int i = 0; i < len; i++) {
|
---|
905 | printXMLNode(children.item(i), depth + 1);
|
---|
906 | }
|
---|
907 |
|
---|
908 | for (int i=0 ; i<depth ; i++)
|
---|
909 | System.out.print(' ') ;
|
---|
910 |
|
---|
911 | System.out.println("</" + e.getNodeName() + ">");
|
---|
912 | }
|
---|
913 |
|
---|
914 | }
|
---|
915 | }
|
---|