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