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