Changeset 24984
- Timestamp:
- 2012-01-26T11:22:27+13:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java
r24609 r24984 1 1 /* 2 * GSXML.java3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org4 *5 * This program is free software; you can redistribute it and/or modify6 * it under the terms of the GNU General Public License as published by7 * the Free Software Foundation; either version 2 of the License, or8 * (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 of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU General Public License16 * along with this program; if not, write to the Free Software17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18 */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 19 package org.greenstone.gsdl3.util; 20 20 … … 44 44 45 45 /** various functions for extracting info out of GS XML */ 46 public class GSXML { 46 public class GSXML 47 { 47 48 48 49 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName()); … … 93 94 94 95 public static final String DBINFO_ELEM = "dbInfo"; 95 public static final String DBNAME_ATT = "dbname"; 96 public static final String DBNAME_ATT = "dbname"; 96 97 public static final String DBPATH_ATT = "dbpath"; 97 98 public static final String SQLSTATE_ATT = "sqlstate"; … … 100 101 public static final String NOTIFY_ELEM = "notify"; 101 102 public static final String NOTIFY_HOST_ATT = "host"; 102 103 103 104 //doc.xml file elems 104 105 public static final String DOCXML_SECTION_ELEM = "Section"; … … 116 117 //public final static String DESCRIPTION_ELEM = "description"; 117 118 118 public static final String 119 public static final String 119 public static final String ACTION_ELEM = "action"; 120 public static final String SUBACTION_ELEM = "subaction"; 120 121 121 122 // add on to another elem type to get a list of that type … … 218 219 public static final String SERVICE_TYPE_OAI = "oai"; 219 220 public static final String FLAX_PAGE = "flaxPage"; 220 public static final String FLAX_PAGE_GENERATION = "FlaxPageGeneration"; 221 public static final String FLAX_PAGE_GENERATION = "FlaxPageGeneration"; 221 222 222 223 // system command types and attributes … … 246 247 247 248 //for authentication 248 public static final String AUTHEN_NODE_ELEM ="authenticationNode";249 public static final String USER_NODE_ELEM ="userNode";249 public static final String AUTHEN_NODE_ELEM = "authenticationNode"; 250 public static final String USER_NODE_ELEM = "userNode"; 250 251 251 252 //for configure action results … … 253 254 public static final String ERROR = "error"; 254 255 255 /** takes a list of elements, and returns an array of strings 256 * of the values of attribute att_name */ 257 public static String [] getAttributeValuesFromList(Element list, 258 String att_name) { 259 256 /** 257 * takes a list of elements, and returns an array of strings of the values 258 * of attribute att_name 259 */ 260 public static String[] getAttributeValuesFromList(Element list, String att_name) 261 { 262 260 263 NodeList children = list.getChildNodes(); 261 264 262 265 int num_nodes = children.getLength(); 263 String []ids = new String[num_nodes]; 264 for (int i=0; i<num_nodes; i++) { 265 Element e = (Element)children.item(i); 266 String[] ids = new String[num_nodes]; 267 for (int i = 0; i < num_nodes; i++) 268 { 269 Element e = (Element) children.item(i); 266 270 String id = e.getAttribute(att_name); 267 271 ids[i] = id; 268 272 } 269 273 270 274 return ids; 271 275 } 272 276 273 public static HashMap extractParams(Element xml, boolean deep) { 277 public static HashMap extractParams(Element xml, boolean deep) 278 { 274 279 return extractParams(xml, deep, null); 275 280 } 276 281 277 /** takes a paramList element, and gets a HashMap of name-value pairs 278 * if deep=true, extracts embedded params, otherwise just top level 279 * params*/ 280 public static HashMap extractParams(Element xml, boolean deep, String toFind) { 281 282 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) { 283 logger.error("paramList element should have been passed to extractParams, instead it was "+xml.getNodeName()); 282 /** 283 * takes a paramList element, and gets a HashMap of name-value pairs if 284 * deep=true, extracts embedded params, otherwise just top level params 285 */ 286 public static HashMap extractParams(Element xml, boolean deep, String toFind) 287 { 288 289 if (!xml.getNodeName().equals(PARAM_ELEM + LIST_MODIFIER)) 290 { 291 logger.error("paramList element should have been passed to extractParams, instead it was " + xml.getNodeName()); 284 292 return null; 285 293 } 286 294 287 295 NodeList params = null; 288 if (deep) { // get all the nested ones 296 if (deep) 297 { // get all the nested ones 289 298 params = xml.getElementsByTagName(PARAM_ELEM); 290 } else { // just get the top level ones 299 } 300 else 301 { // just get the top level ones 291 302 params = xml.getChildNodes(); 292 303 } 293 304 HashMap param_map = new HashMap(); 294 for (int i=0; i<params.getLength(); i++) { 295 if (params.item(i).getNodeName().equals(PARAM_ELEM)) { 296 Element param = (Element)params.item(i); 297 String name=param.getAttribute(NAME_ATT); 298 String value=getValue(param); //att or content 305 for (int i = 0; i < params.getLength(); i++) 306 { 307 if (params.item(i).getNodeName().equals(PARAM_ELEM)) 308 { 309 Element param = (Element) params.item(i); 310 String name = param.getAttribute(NAME_ATT); 311 String value = getValue(param); //att or content 299 312 300 313 // For only one parameter 301 if(toFind != null && name.equals(toFind)) { 314 if (toFind != null && name.equals(toFind)) 315 { 302 316 param_map.put(name, value); 303 317 return param_map; 304 } 305 else if (toFind != null)306 continue;318 } 319 else if (toFind != null) 320 continue; 307 321 308 322 int pos = name.indexOf('.'); 309 if (pos == -1) { // a base param 323 if (pos == -1) 324 { // a base param 310 325 param_map.put(name, value); 311 } else { // a namespaced param 312 326 } 327 else 328 { // a namespaced param 329 313 330 String namespace = name.substring(0, pos); 314 name = name.substring(pos+1); 315 HashMap map = (HashMap)param_map.get(namespace); 316 if (map == null) { 331 name = name.substring(pos + 1); 332 HashMap map = (HashMap) param_map.get(namespace); 333 if (map == null) 334 { 317 335 map = new HashMap(); 318 336 param_map.put(namespace, map); … … 326 344 327 345 /** gets the value att or the text content */ 328 public static String getValue(Element e) { 346 public static String getValue(Element e) 347 { 329 348 String val = e.getAttribute(VALUE_ATT); 330 if (val ==null || val.equals("")) { 349 if (val == null || val.equals("")) 350 { 331 351 // have to get it out of the text 332 val=getNodeText(e); 333 334 } else { 352 val = getNodeText(e); 353 354 } 355 else 356 { 335 357 // unescape the xml stuff 336 358 val = unXmlSafe(val); … … 340 362 341 363 /** extracts the text out of a node */ 342 public static Node getNodeTextNode(Element param) { 364 public static Node getNodeTextNode(Element param) 365 { 343 366 param.normalize(); 344 367 Node n = param.getFirstChild(); 345 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) { 346 n=n.getNextSibling(); 368 while (n != null && n.getNodeType() != Node.TEXT_NODE) 369 { 370 n = n.getNextSibling(); 347 371 } 348 372 return n; … … 350 374 351 375 /** extracts the text out of a node */ 352 public static String getNodeText(Element param) { 376 public static String getNodeText(Element param) 377 { 353 378 Node text_node = getNodeTextNode(param); 354 if (text_node == null) { 379 if (text_node == null) 380 { 355 381 return ""; 356 382 } … … 358 384 } 359 385 360 public static void setNodeText(Element elem, String text) { 386 public static void setNodeText(Element elem, String text) 387 { 361 388 Node old_text_node = getNodeTextNode(elem); 362 if (old_text_node != null) { 389 if (old_text_node != null) 390 { 363 391 elem.removeChild(old_text_node); 364 392 } … … 367 395 } 368 396 369 /** add text to a document/subsection element */ 370 public static boolean addDocText(Document owner, Element doc, String text) { 371 397 /** add text to a document/subsection element */ 398 public static boolean addDocText(Document owner, Element doc, String text) 399 { 400 372 401 Element content = owner.createElement(NODE_CONTENT_ELEM); 373 402 Text t = owner.createTextNode(text); … … 378 407 379 408 /** add an error message, unknown error type */ 380 public static boolean addError(Document owner, Element doc, String text) { 409 public static boolean addError(Document owner, Element doc, String text) 410 { 381 411 return addError(owner, doc, text, ERROR_TYPE_OTHER); 382 412 } 413 383 414 /** add an error message */ 384 public static boolean addError(Document owner, Element doc, String text, 385 String error_type){386 415 public static boolean addError(Document owner, Element doc, String text, String error_type) 416 { 417 387 418 Element content = owner.createElement(ERROR_ELEM); 388 419 content.setAttribute(ERROR_TYPE_ATT, error_type); … … 394 425 395 426 /** add an error message */ 396 public static boolean addError(Document owner, Element doc, Throwable error) { 427 public static boolean addError(Document owner, Element doc, Throwable error) 428 { 397 429 return addError(owner, doc, error, ERROR_TYPE_OTHER); 398 430 } 399 431 400 432 /** add an error message */ 401 public static boolean addError(Document owner, Element doc, 402 Throwable error, String error_type){433 public static boolean addError(Document owner, Element doc, Throwable error, String error_type) 434 { 403 435 error.printStackTrace(); 404 436 return addError(owner, doc, error.toString(), error_type); 405 437 } 406 438 407 public static Element createMetadataParamList(Document owner, Vector meta_values) { 408 409 Element meta_param_list = owner.createElement(PARAM_ELEM+LIST_MODIFIER); 439 public static Element createMetadataParamList(Document owner, Vector meta_values) 440 { 441 442 Element meta_param_list = owner.createElement(PARAM_ELEM + LIST_MODIFIER); 410 443 Iterator i = meta_values.iterator(); 411 while(i.hasNext()) { 412 String next = (String)i.next(); 444 while (i.hasNext()) 445 { 446 String next = (String) i.next(); 413 447 Element meta_param = owner.createElement(PARAM_ELEM); 414 448 meta_param_list.appendChild(meta_param); … … 420 454 421 455 /** adds a metadata elem to a list */ 422 public static boolean addMetadata(Document owner, Element list, 423 String meta_name, String meta_value) { 424 if (meta_value==null || meta_value.equals("")) { 456 public static boolean addMetadata(Document owner, Element list, String meta_name, String meta_value) 457 { 458 if (meta_value == null || meta_value.equals("")) 459 { 425 460 return false; 426 461 } … … 431 466 list.appendChild(data); 432 467 return true; 433 434 } 435 436 /** copies the metadata out of the metadataList of 'from' into 437 * the metadataList of 'to' */ 438 public static boolean mergeMetadataLists(Node to, Node from) { 439 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER); 440 if (from_meta == null) { // nothing to copy 468 469 } 470 471 /** 472 * copies the metadata out of the metadataList of 'from' into the 473 * metadataList of 'to' 474 */ 475 public static boolean mergeMetadataLists(Node to, Node from) 476 { 477 Node from_meta = getChildByTagName(from, METADATA_ELEM + LIST_MODIFIER); 478 if (from_meta == null) 479 { // nothing to copy 441 480 return true; 442 481 } … … 444 483 } 445 484 446 447 /** copies the metadata out of the meta_list metadataList into 448 * the metadataList of 'to' */ 449 public static boolean mergeMetadataFromList(Node to, Node meta_list) { 450 if (meta_list == null) return false; 451 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER); 485 /** 486 * copies the metadata out of the meta_list metadataList into the 487 * metadataList of 'to' 488 */ 489 public static boolean mergeMetadataFromList(Node to, Node meta_list) 490 { 491 if (meta_list == null) 492 return false; 493 Node to_meta = getChildByTagName(to, METADATA_ELEM + LIST_MODIFIER); 452 494 Document to_owner = to.getOwnerDocument(); 453 if (to_meta == null) { 495 if (to_meta == null) 496 { 454 497 to.appendChild(to_owner.importNode(meta_list, true)); 455 498 return true; 456 499 } 457 500 // copy individual metadata elements 458 NodeList meta_items = ((Element)meta_list).getElementsByTagName(METADATA_ELEM); 459 for (int i=0; i<meta_items.getLength(); i++) { 460 to_meta.appendChild(to_owner.importNode(meta_items.item(i),true)); 501 NodeList meta_items = ((Element) meta_list).getElementsByTagName(METADATA_ELEM); 502 for (int i = 0; i < meta_items.getLength(); i++) 503 { 504 to_meta.appendChild(to_owner.importNode(meta_items.item(i), true)); 461 505 } 462 506 return true; … … 464 508 465 509 /** copies all the children from from to to */ 466 public static boolean mergeElements(Element to, Element from) { 467 510 public static boolean mergeElements(Element to, Element from) 511 { 512 468 513 Document owner = to.getOwnerDocument(); 469 514 Node child = from.getFirstChild(); 470 while (child != null) { 515 while (child != null) 516 { 471 517 to.appendChild(owner.importNode(child, true)); 472 518 child = child.getNextSibling(); … … 474 520 return true; 475 521 } 522 476 523 /** returns the (first) element child of the node n */ 477 public static Element getFirstElementChild(Node n) { 478 524 public static Element getFirstElementChild(Node n) 525 { 526 479 527 Node child = n.getFirstChild(); 480 while (child!=null) { 481 if (child.getNodeType() == Node.ELEMENT_NODE) { 482 return (Element)child; 528 while (child != null) 529 { 530 if (child.getNodeType() == Node.ELEMENT_NODE) 531 { 532 return (Element) child; 483 533 } 484 534 child = child.getNextSibling(); … … 486 536 return null; //no element child found 487 537 } 538 488 539 /** returns the (first) child element with the given name */ 489 public static Node getChildByTagName(Node n, String name) { 490 if(n != null) { // this line is an attempted solution to the NullPointerException mentioned 540 public static Node getChildByTagName(Node n, String name) 541 { 542 if (n != null) 543 { // this line is an attempted solution to the NullPointerException mentioned 491 544 // in trac bug ticket #225. If n is null can't do n.getFirstChild() below. As per bug #225: 492 545 // GSXML.getNodeByPath() is called by GS2BrowseAction, which then calls this method. … … 495 548 496 549 Node child = n.getFirstChild(); 497 while (child!=null) { 498 if (child.getNodeName().equals(name)) { 550 while (child != null) 551 { 552 if (child.getNodeName().equals(name)) 553 { 499 554 return child; 500 555 } … … 505 560 } 506 561 507 /** returns the (nth) child element with the given name 508 * index numbers start at 0 */ 509 public static Node getChildByTagNameIndexed(Node n, String name, int index) { 510 if (index == -1) { 562 /** 563 * returns the (nth) child element with the given name index numbers start 564 * at 0 565 */ 566 public static Node getChildByTagNameIndexed(Node n, String name, int index) 567 { 568 if (index == -1) 569 { 511 570 return getChildByTagName(n, name); 512 571 } 513 572 int count = 0; 514 573 Node child = n.getFirstChild(); 515 while (child!=null) { 516 if (child.getNodeName().equals(name)) { 517 if (count == index) { 574 while (child != null) 575 { 576 if (child.getNodeName().equals(name)) 577 { 578 if (count == index) 579 { 518 580 return child; 519 } else { 581 } 582 else 583 { 520 584 count++; 521 585 } … … 526 590 } 527 591 528 /** takes an xpath type expression of the form name/name/... 529 * and returns the first node that matches, or null if not found */ 530 public static Node getNodeByPath(Node n, String path) { 531 592 /** 593 * takes an xpath type expression of the form name/name/... and returns the 594 * first node that matches, or null if not found 595 */ 596 public static Node getNodeByPath(Node n, String path) 597 { 598 532 599 String link = GSPath.getFirstLink(path); 533 600 path = GSPath.removeFirstLink(path); 534 while (!link.equals("")) { 601 while (!link.equals("")) 602 { 535 603 n = getChildByTagName(n, link); 536 if (n==null) { 604 if (n == null) 605 { 537 606 return null; 538 607 } … … 543 612 } 544 613 545 /** takes an xpath type expression of the form name/name/... 546 * and returns the first node that matches, or null if not found 547 * can include [i] indices. index numbers start at 0 */ 548 public static Node getNodeByPathIndexed(Node n, String path) { 549 614 /** 615 * takes an xpath type expression of the form name/name/... and returns the 616 * first node that matches, or null if not found can include [i] indices. 617 * index numbers start at 0 618 */ 619 public static Node getNodeByPathIndexed(Node n, String path) 620 { 621 550 622 String link = GSPath.getFirstLink(path); 551 623 int index = GSPath.getIndex(link); 552 if (index != -1) { 624 if (index != -1) 625 { 553 626 link = GSPath.removeIndex(link); 554 627 } 555 628 path = GSPath.removeFirstLink(path); 556 while (!link.equals("")) { 629 while (!link.equals("")) 630 { 557 631 n = getChildByTagNameIndexed(n, link, index); 558 if (n==null) { 632 if (n == null) 633 { 559 634 return null; 560 635 } 561 636 link = GSPath.getFirstLink(path); 562 637 index = GSPath.getIndex(link); 563 if (index != -1) { 638 if (index != -1) 639 { 564 640 link = GSPath.removeIndex(link); 565 641 } … … 569 645 } 570 646 571 public static HashMap getChildrenMap(Node n) { 572 573 HashMap map= new HashMap(); 647 public static HashMap getChildrenMap(Node n) 648 { 649 650 HashMap map = new HashMap(); 574 651 Node child = n.getFirstChild(); 575 while (child!=null) { 652 while (child != null) 653 { 576 654 String name = child.getNodeName(); 577 655 map.put(name, child); … … 581 659 } 582 660 583 public static NodeList getChildrenByTagName(Node n, String name) { 661 public static NodeList getChildrenByTagName(Node n, String name) 662 { 584 663 MyNodeList node_list = new MyNodeList(); 585 664 Node child = n.getFirstChild(); 586 while (child!=null) { 587 if (child.getNodeName().equals(name)) { 665 while (child != null) 666 { 667 if (child.getNodeName().equals(name)) 668 { 588 669 node_list.addNode(child); 589 670 } … … 593 674 } 594 675 595 596 676 /** Duplicates an element, but gives it a new name */ 597 public static Element duplicateWithNewName(Document owner, Element element, 598 String element_name, boolean with_attributes){677 public static Element duplicateWithNewName(Document owner, Element element, String element_name, boolean with_attributes) 678 { 599 679 return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes); 600 680 } 601 681 602 682 /** Duplicates an element, but gives it a new name */ 603 public static Element duplicateWithNewNameNS(Document owner, 604 Element element, 605 String element_name, 606 String namespace_uri, 607 boolean with_attributes) { 683 public static Element duplicateWithNewNameNS(Document owner, Element element, String element_name, String namespace_uri, boolean with_attributes) 684 { 608 685 Element duplicate; 609 if (namespace_uri == null) { 686 if (namespace_uri == null) 687 { 610 688 duplicate = owner.createElement(element_name); 611 } else { 689 } 690 else 691 { 612 692 duplicate = owner.createElementNS(namespace_uri, element_name); 613 693 } 614 694 // Copy element attributes 615 if (with_attributes) { 695 if (with_attributes) 696 { 616 697 NamedNodeMap attributes = element.getAttributes(); 617 for (int i = 0; i < attributes.getLength(); i++) { 698 for (int i = 0; i < attributes.getLength(); i++) 699 { 618 700 Node attribute = attributes.item(i); 619 701 duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue()); 620 702 } 621 703 } 622 704 623 705 // Copy element children 624 706 NodeList children = element.getChildNodes(); 625 for (int i = 0; i < children.getLength(); i++) { 707 for (int i = 0; i < children.getLength(); i++) 708 { 626 709 Node child = children.item(i); 627 710 duplicate.appendChild(owner.importNode(child, true)); 628 711 } 629 712 630 713 return duplicate; 631 714 } 632 715 633 public static void copyAllChildren(Element to, Element from) { 634 716 public static void copyAllChildren(Element to, Element from) 717 { 718 635 719 Document to_doc = to.getOwnerDocument(); 636 720 Node child = from.getFirstChild(); 637 while (child != null) { 721 while (child != null) 722 { 638 723 to.appendChild(to_doc.importNode(child, true)); 639 724 child = child.getNextSibling(); 640 725 } 641 726 } 727 642 728 /** returns a basic request message */ 643 public static Element createBasicRequest(Document owner, 644 String request_type, String to, 645 String lang, 646 String uid) { 729 public static Element createBasicRequest(Document owner, String request_type, String to, String lang, String uid) 730 { 647 731 Element request = owner.createElement(REQUEST_ELEM); 648 732 request.setAttribute(TYPE_ATT, request_type); … … 652 736 return request; 653 737 } 654 738 655 739 public static Element createBasicResponse(Document owner, String from) 656 740 { … … 660 744 return response; 661 745 } 662 746 663 747 public static Element createMetadataElement(Document owner, String name, String value) 664 748 { … … 669 753 } 670 754 671 public static Element createTextElement(Document owner, String elem_name, 672 String text){755 public static Element createTextElement(Document owner, String elem_name, String text) 756 { 673 757 Element e = owner.createElement(elem_name); 674 758 Text t = owner.createTextNode(text); 675 759 e.appendChild(t); 676 760 return e; 677 678 } 679 680 public static Element createTextElement(Document owner, String elem_name, 681 String text, String att_name, String att_value){761 762 } 763 764 public static Element createTextElement(Document owner, String elem_name, String text, String att_name, String att_value) 765 { 682 766 Element e = owner.createElement(elem_name); 683 767 e.setAttribute(att_name, att_value); … … 685 769 e.appendChild(t); 686 770 return e; 687 688 } 689 690 public static Element createDisplayTextElement(Document owner, 691 String text_name, 692 String text) { 771 772 } 773 774 public static Element createDisplayTextElement(Document owner, String text_name, String text) 775 { 693 776 Element e = owner.createElement(DISPLAY_TEXT_ELEM); 694 777 e.setAttribute(NAME_ATT, text_name); … … 696 779 e.appendChild(t); 697 780 return e; 698 699 } 700 701 702 public static Element createParameter(Document owner, String name, 703 String value) { 781 782 } 783 784 public static Element createParameter(Document owner, String name, String value) 785 { 704 786 Element param = owner.createElement(PARAM_ELEM); 705 787 param.setAttribute(NAME_ATT, name); … … 708 790 } 709 791 710 public static void addParametersToList(Document owner, Element param_list, 711 HashMap params){792 public static void addParametersToList(Document owner, Element param_list, HashMap params) 793 { 712 794 if (params == null) 713 795 { 714 796 return; 715 797 } 716 798 717 799 Set items = params.entrySet(); 718 800 Iterator i = items.iterator(); 719 while(i.hasNext()) { 720 Map.Entry m = (Map.Entry)i.next(); 721 param_list.appendChild(createParameter(owner, (String)m.getKey(), (String)m.getValue())); 722 } 723 724 } 725 726 public static Element createParameterDescription(Document owner, 727 String id, 728 String display_name, 729 String type, 730 String default_value, 731 String []option_ids, 732 String []option_names) { 733 734 801 while (i.hasNext()) 802 { 803 Map.Entry m = (Map.Entry) i.next(); 804 param_list.appendChild(createParameter(owner, (String) m.getKey(), (String) m.getValue())); 805 } 806 807 } 808 809 public static Element createParameterDescription(Document owner, String id, String display_name, String type, String default_value, String[] option_ids, String[] option_names) 810 { 811 735 812 Element p = owner.createElement(PARAM_ELEM); 736 813 p.setAttribute(NAME_ATT, id); 737 814 p.setAttribute(TYPE_ATT, type); 738 815 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name)); 739 740 if (default_value != null) { 816 817 if (default_value != null) 818 { 741 819 p.setAttribute(DEFAULT_ATT, default_value); 742 820 } 743 if (option_ids!=null && option_names!=null) { 744 for (int i=0; i<option_ids.length; i++) { 821 if (option_ids != null && option_names != null) 822 { 823 for (int i = 0; i < option_ids.length; i++) 824 { 745 825 Element e = owner.createElement(PARAM_OPTION_ELEM); 746 826 e.setAttribute(NAME_ATT, option_ids[i]); … … 751 831 return p; 752 832 } 753 public static Element createParameterDescription2(Document owner, 754 String id, 755 String display_name, 756 String type, 757 String default_value, 758 ArrayList option_ids, 759 ArrayList option_names) { 760 761 833 834 public static Element createParameterDescription2(Document owner, String id, String display_name, String type, String default_value, ArrayList option_ids, ArrayList option_names) 835 { 836 762 837 Element p = owner.createElement(PARAM_ELEM); 763 838 p.setAttribute(NAME_ATT, id); 764 839 p.setAttribute(TYPE_ATT, type); 765 840 p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name)); 766 if (default_value != null) { 841 if (default_value != null) 842 { 767 843 p.setAttribute(DEFAULT_ATT, default_value); 768 844 } 769 if (option_ids!=null && option_names!=null) { 770 for (int i=0; i<option_ids.size(); i++) { 845 if (option_ids != null && option_names != null) 846 { 847 for (int i = 0; i < option_ids.size(); i++) 848 { 771 849 Element e = owner.createElement(PARAM_OPTION_ELEM); 772 e.setAttribute(NAME_ATT, (String) option_ids.get(i));773 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, (String) option_names.get(i)));850 e.setAttribute(NAME_ATT, (String) option_ids.get(i)); 851 e.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, (String) option_names.get(i))); 774 852 p.appendChild(e); 775 853 } … … 778 856 } 779 857 780 781 /** returns the element parent/node_name[@attribute_name='attribute_value'] 782 */ 783 public static Element getNamedElement(Element parent, String node_name, 784 String attribute_name, 785 String attribute_value) { 786 858 /** returns the element parent/node_name[@attribute_name='attribute_value'] */ 859 public static Element getNamedElement(Element parent, String node_name, String attribute_name, String attribute_value) 860 { 861 787 862 NodeList children = parent.getChildNodes(); 788 for (int i=0; i<children.getLength(); i++) { 863 for (int i = 0; i < children.getLength(); i++) 864 { 789 865 Node child = children.item(i); 790 if (child.getNodeName().equals(node_name)) { 791 if (((Element)child).getAttribute(attribute_name).equals(attribute_value)) 792 return (Element)child; 866 if (child.getNodeName().equals(node_name)) 867 { 868 if (((Element) child).getAttribute(attribute_name).equals(attribute_value)) 869 return (Element) child; 793 870 } 794 871 } … … 796 873 return null; 797 874 } 798 /** returns a NodeList of elements: ancestor/node_name[@attribute_name='attribute_value'] 799 */ 800 public static NodeList getNamedElements(Element ancestor, String node_name, String attribute_name, String attribute_value) { 875 876 /** 877 * returns a NodeList of elements: 878 * ancestor/node_name[@attribute_name='attribute_value'] 879 */ 880 public static NodeList getNamedElements(Element ancestor, String node_name, String attribute_name, String attribute_value) 881 { 801 882 MyNodeList node_list = new MyNodeList(); 802 883 NodeList children = ancestor.getElementsByTagName(node_name); 803 804 if(children != null && children.getLength() > 0) { 805 806 for (int i=0; i<children.getLength(); i++) { 884 885 if (children != null && children.getLength() > 0) 886 { 887 888 for (int i = 0; i < children.getLength(); i++) 889 { 807 890 Node child = children.item(i); 808 if (child.getNodeName().equals(node_name)) { 809 if (((Element)child).getAttribute(attribute_name).equals(attribute_value)) 810 node_list.addNode(child); 811 } 812 } 891 if (child.getNodeName().equals(node_name)) 892 { 893 if (((Element) child).getAttribute(attribute_name).equals(attribute_value)) 894 node_list.addNode(child); 895 } 896 } 813 897 } 814 898 return node_list; … … 820 904 821 905 // sort type: 822 public static Element insertIntoOrderedList(Element parent_node, 823 String node_name, 824 Element start_from_elem, 825 Element new_elem, String sort_att, 826 boolean descending) { 827 if (new_elem == null) return null; 828 Element cloned_elem = (Element)parent_node.getOwnerDocument().importNode(new_elem, true); 829 if (start_from_elem == null) { 906 public static Element insertIntoOrderedList(Element parent_node, String node_name, Element start_from_elem, Element new_elem, String sort_att, boolean descending) 907 { 908 if (new_elem == null) 909 return null; 910 Element cloned_elem = (Element) parent_node.getOwnerDocument().importNode(new_elem, true); 911 if (start_from_elem == null) 912 { 830 913 parent_node.appendChild(cloned_elem); 831 914 return cloned_elem; 832 915 } 833 916 834 917 Node current_node = start_from_elem; 835 918 String insert_att = cloned_elem.getAttribute(sort_att); 836 919 String list_att = start_from_elem.getAttribute(sort_att); 837 while ((!descending && list_att.compareTo(insert_att)<0) || (descending && list_att.compareTo(insert_att)>0)) { 920 while ((!descending && list_att.compareTo(insert_att) < 0) || (descending && list_att.compareTo(insert_att) > 0)) 921 { 838 922 current_node = current_node.getNextSibling(); 839 if (current_node == null) break; // end of the list 840 if (!current_node.getNodeName().equals(node_name)) { 923 if (current_node == null) 924 break; // end of the list 925 if (!current_node.getNodeName().equals(node_name)) 926 { 841 927 continue; // not a valid node 842 928 } 843 list_att = ((Element) current_node).getAttribute(sort_att);844 } 845 929 list_att = ((Element) current_node).getAttribute(sort_att); 930 } 931 846 932 parent_node.insertBefore(cloned_elem, current_node); 847 933 return cloned_elem; 848 934 } 849 935 850 851 /** Returns the appropriate language element from a display elem, 852 * display is the containing element, name is the name of the element to 853 * look for, lang is the preferred language, lang_default is the fall back 854 * lang if neither lang is found, will return the first one it finds*/ 855 public static String getDisplayText(Element display, String name, 856 String lang, String lang_default) { 857 936 /** 937 * Returns the appropriate language element from a display elem, display is 938 * the containing element, name is the name of the element to look for, lang 939 * is the preferred language, lang_default is the fall back lang if neither 940 * lang is found, will return the first one it finds 941 */ 942 public static String getDisplayText(Element display, String name, String lang, String lang_default) 943 { 944 858 945 String def = null; 859 946 String first = null; 860 947 NodeList elems = display.getElementsByTagName(DISPLAY_TEXT_ELEM); 861 if (elems.getLength() == 0) return ""; 862 for (int i=0; i<elems.getLength(); i++) { 863 Element e = (Element)elems.item(i); 948 if (elems.getLength() == 0) 949 return ""; 950 for (int i = 0; i < elems.getLength(); i++) 951 { 952 Element e = (Element) elems.item(i); 864 953 String n = e.getAttribute(NAME_ATT); 865 if (name.equals(n)) { 954 if (name.equals(n)) 955 { 866 956 String l = e.getAttribute(LANG_ATT); 867 if (lang.equals(l)) { 957 if (lang.equals(l)) 958 { 868 959 return getNodeText(e); 869 } else if (lang_default.equals(l)) { 960 } 961 else if (lang_default.equals(l)) 962 { 870 963 def = getNodeText(e); 871 } else if (first == null) { 964 } 965 else if (first == null) 966 { 872 967 first = getNodeText(e); 873 968 } 874 } else { 969 } 970 else 971 { 875 972 continue; 876 973 } 877 974 } 878 879 if (def != null) { 975 976 if (def != null) 977 { 880 978 return def; 881 979 } 882 if (first != null) { 980 if (first != null) 981 { 883 982 return first; 884 983 } … … 887 986 888 987 // replaces < > " ' & in the original with their entities 889 public static String xmlSafe(String original) { 890 988 public static String xmlSafe(String original) 989 { 990 891 991 StringBuffer filtered = new StringBuffer(original.length()); 892 992 char c; 893 for (int i=0; i<original.length(); i++) { 993 for (int i = 0; i < original.length(); i++) 994 { 894 995 c = original.charAt(i); 895 if (c == '>') { 996 if (c == '>') 997 { 896 998 filtered.append(">"); 897 } else if (c == '<') { 999 } 1000 else if (c == '<') 1001 { 898 1002 filtered.append("<"); 899 } else if (c == '"') { 1003 } 1004 else if (c == '"') 1005 { 900 1006 filtered.append("""); 901 } else if (c == '&') { 1007 } 1008 else if (c == '&') 1009 { 902 1010 filtered.append("&"); 903 } else if (c == '\'') { 1011 } 1012 else if (c == '\'') 1013 { 904 1014 filtered.append("'"); 905 } else { 1015 } 1016 else 1017 { 906 1018 filtered.append(c); 907 1019 } … … 910 1022 } 911 1023 912 913 1024 // replaces < > " ' & entities with their originals 914 public static String unXmlSafe(String original) { 915 1025 public static String unXmlSafe(String original) 1026 { 1027 916 1028 StringBuffer filtered = new StringBuffer(original.length()); 917 1029 char c; 918 for (int i=0; i<original.length(); i++) { 1030 for (int i = 0; i < original.length(); i++) 1031 { 919 1032 c = original.charAt(i); 920 if (c == '&') { 1033 if (c == '&') 1034 { 921 1035 int pos = original.indexOf(";", i); 922 String entity = original.substring(i+1, pos); 923 if (entity.equals("gt")) { 1036 String entity = original.substring(i + 1, pos); 1037 if (entity.equals("gt")) 1038 { 924 1039 filtered.append(">"); 925 } else if (entity.equals("lt")) { 1040 } 1041 else if (entity.equals("lt")) 1042 { 926 1043 filtered.append("<"); 927 } else if (entity.equals("apos")) { 1044 } 1045 else if (entity.equals("apos")) 1046 { 928 1047 filtered.append("'"); 929 } else if (entity.equals("amp")) { 1048 } 1049 else if (entity.equals("amp")) 1050 { 930 1051 filtered.append("&"); 931 } else if (entity.equals("quot")) { 1052 } 1053 else if (entity.equals("quot")) 1054 { 932 1055 filtered.append("\""); 933 } else { 934 filtered.append("&"+entity+";"); 1056 } 1057 else 1058 { 1059 filtered.append("&" + entity + ";"); 935 1060 } 936 1061 i = pos; 937 } else { 1062 } 1063 else 1064 { 938 1065 filtered.append(c); 939 1066 } … … 942 1069 } 943 1070 944 public static void printXMLNode(Node e, boolean printText) { 945 printXMLNode(e, 0, printText) ; 946 } 947 948 public static String xmlNodeToString(Node e){ 1071 public static void printXMLNode(Node e, boolean printText) 1072 { 1073 printXMLNode(e, 0, printText); 1074 } 1075 1076 public static String xmlNodeToString(Node e) 1077 { 949 1078 StringBuffer sb = new StringBuffer(""); 950 xmlNodeToString(sb, e,0,true);1079 xmlNodeToString(sb, e, 0, true); 951 1080 return sb.toString(); 952 1081 } 953 1082 954 public static String xmlNodeToString(Node e, boolean printText){ 1083 public static String xmlNodeToString(Node e, boolean printText) 1084 { 955 1085 StringBuffer sb = new StringBuffer(""); 956 xmlNodeToString(sb, e,0,printText);1086 xmlNodeToString(sb, e, 0, printText); 957 1087 return sb.toString(); 958 1088 } 959 1089 960 private static void xmlNodeToString(StringBuffer sb, Node e, int depth, boolean printText){ 961 962 for (int i=0 ; i<depth ; i++) 963 sb.append(' ') ; 964 965 if (e.getNodeType() == Node.TEXT_NODE){ 966 if(printText) 1090 private static void xmlNodeToString(StringBuffer sb, Node e, int depth, boolean printText) 1091 { 1092 1093 for (int i = 0; i < depth; i++) 1094 sb.append(' '); 1095 1096 if (e.getNodeType() == Node.TEXT_NODE) 1097 { 1098 if (printText) 967 1099 { 968 1100 sb.append(e.getNodeValue()); … … 972 1104 sb.append("text"); 973 1105 } 974 return 975 } 976 1106 return; 1107 } 1108 977 1109 sb.append('<'); 978 1110 sb.append(e.getNodeName()); 979 1111 NamedNodeMap attrs = e.getAttributes(); 980 if(attrs != null) 981 { 982 for (int i = 0; i < attrs.getLength(); i++) { 1112 if (attrs != null) 1113 { 1114 for (int i = 0; i < attrs.getLength(); i++) 1115 { 983 1116 Node attr = attrs.item(i); 984 1117 sb.append(' '); … … 989 1122 } 990 1123 } 991 1124 992 1125 NodeList children = e.getChildNodes(); 993 1126 994 1127 if (children == null || children.getLength() == 0) 995 sb.append("/>\n") ; 996 else { 997 998 sb.append(">\n") ; 999 1128 sb.append("/>\n"); 1129 else 1130 { 1131 1132 sb.append(">\n"); 1133 1000 1134 int len = children.getLength(); 1001 for (int i = 0; i < len; i++) { 1002 xmlNodeToString(sb,children.item(i), depth + 1, printText); 1003 } 1004 1005 for (int i=0 ; i<depth ; i++) 1006 sb.append(' ') ; 1007 1135 for (int i = 0; i < len; i++) 1136 { 1137 xmlNodeToString(sb, children.item(i), depth + 1, printText); 1138 } 1139 1140 for (int i = 0; i < depth; i++) 1141 sb.append(' '); 1142 1008 1143 sb.append("</" + e.getNodeName() + ">\n"); 1009 1144 } 1010 1011 1012 } 1013 1014 public static void printXMLNode(Node e, int depth, boolean printText) { //recursive method call using DOM API... 1015 1016 if(e == null){return;} 1017 1018 for (int i=0 ; i<depth ; i++) 1019 System.out.print(' ') ; 1020 1021 if (e.getNodeType() == Node.TEXT_NODE){ 1022 if(printText){ 1145 1146 } 1147 1148 public static void printXMLNode(Node e, int depth, boolean printText) 1149 { //recursive method call using DOM API... 1150 1151 if (e == null) 1152 { 1153 return; 1154 } 1155 1156 for (int i = 0; i < depth; i++) 1157 System.out.print(' '); 1158 1159 if (e.getNodeType() == Node.TEXT_NODE) 1160 { 1161 if (printText) 1162 { 1023 1163 System.out.println(e.getNodeValue()); 1024 1164 } 1025 else { 1165 else 1166 { 1026 1167 System.out.println("text"); 1027 1168 } 1028 return 1029 } 1030 1169 return; 1170 } 1171 1031 1172 System.out.print('<'); 1032 1173 System.out.print(e.getNodeName()); 1033 1174 NamedNodeMap attrs = e.getAttributes(); 1034 1175 1035 1176 if (attrs != null) 1036 1177 { 1037 for (int i = 0; i < attrs.getLength(); i++) { 1178 for (int i = 0; i < attrs.getLength(); i++) 1179 { 1038 1180 Node attr = attrs.item(i); 1039 1181 System.out.print(' '); … … 1044 1186 } 1045 1187 } 1046 1188 1047 1189 NodeList children = e.getChildNodes(); 1048 1190 1049 1191 if (children == null || children.getLength() == 0) 1050 System.out.println("/>") ; 1051 else { 1052 1053 System.out.println('>') ; 1054 1192 System.out.println("/>"); 1193 else 1194 { 1195 1196 System.out.println('>'); 1197 1055 1198 int len = children.getLength(); 1056 for (int i = 0; i < len; i++) { 1199 for (int i = 0; i < len; i++) 1200 { 1057 1201 printXMLNode(children.item(i), depth + 1, printText); 1058 1202 } 1059 1060 for (int i =0 ; i<depth; i++)1061 System.out.print(' ');1062 1203 1204 for (int i = 0; i < depth; i++) 1205 System.out.print(' '); 1206 1063 1207 System.out.println("</" + e.getNodeName() + ">"); 1064 } 1065 } 1066 1067 public static void elementToLogAsString(Element e) { 1068 try { 1208 } 1209 } 1210 1211 public static void elementToLogAsString(Element e) 1212 { 1213 try 1214 { 1069 1215 TransformerFactory tf = TransformerFactory.newInstance(); 1070 1216 Transformer trans = tf.newTransformer(); 1071 1217 StringWriter sw = new StringWriter(); 1072 1218 trans.transform(new DOMSource(e), new StreamResult(sw)); 1073 System.err.println( sw.toString() ); 1074 } catch( Exception ex ) { 1075 System.err.println( "couldn't write " + e + " to log" ); 1219 System.err.println(sw.toString()); 1220 } 1221 catch (Exception ex) 1222 { 1223 System.err.println("couldn't write " + e + " to log"); 1076 1224 } 1077 1225 }
Note:
See TracChangeset
for help on using the changeset viewer.