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