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