Changeset 24984 for main


Ignore:
Timestamp:
2012-01-26T11:22:27+13:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r24609 r24984  
    11/*
    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 */
     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 */
    1919package org.greenstone.gsdl3.util;
    2020
     
    4444
    4545/** various functions for extracting info out of GS XML */
    46 public class GSXML {
     46public class GSXML
     47{
    4748
    4849    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName());
     
    9394
    9495    public static final String DBINFO_ELEM = "dbInfo";
    95     public static final String DBNAME_ATT = "dbname"; 
     96    public static final String DBNAME_ATT = "dbname";
    9697    public static final String DBPATH_ATT = "dbpath";
    9798    public static final String SQLSTATE_ATT = "sqlstate";
     
    100101    public static final String NOTIFY_ELEM = "notify";
    101102    public static final String NOTIFY_HOST_ATT = "host";
    102    
     103
    103104    //doc.xml file elems
    104105    public static final String DOCXML_SECTION_ELEM = "Section";
     
    116117    //public final static String DESCRIPTION_ELEM = "description";
    117118
    118     public static final String  ACTION_ELEM = "action";
    119     public static final String  SUBACTION_ELEM = "subaction";
     119    public static final String ACTION_ELEM = "action";
     120    public static final String SUBACTION_ELEM = "subaction";
    120121
    121122    // add on to another elem type to get a list of that type
     
    218219    public static final String SERVICE_TYPE_OAI = "oai";
    219220    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";
    221222
    222223    // system command types and attributes
     
    246247
    247248    //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";
    250251
    251252    //for configure action results
     
    253254    public static final String ERROR = "error";
    254255
    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
    260263        NodeList children = list.getChildNodes();
    261        
     264
    262265        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);
    266270            String id = e.getAttribute(att_name);
    267271            ids[i] = id;
    268272        }
    269        
     273
    270274        return ids;
    271275    }
    272276
    273     public static HashMap extractParams(Element xml, boolean deep) {
     277    public static HashMap extractParams(Element xml, boolean deep)
     278    {
    274279        return extractParams(xml, deep, null);
    275280    }
    276281
    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());
    284292            return null;
    285293        }
    286        
     294
    287295        NodeList params = null;
    288         if (deep) { // get all the nested ones
     296        if (deep)
     297        { // get all the nested ones
    289298            params = xml.getElementsByTagName(PARAM_ELEM);
    290         } else { // just get the top  level ones
     299        }
     300        else
     301        { // just get the top  level ones
    291302            params = xml.getChildNodes();
    292303        }
    293304        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
    299312
    300313                // For only one parameter
    301                 if(toFind != null && name.equals(toFind)) {
     314                if (toFind != null && name.equals(toFind))
     315                {
    302316                    param_map.put(name, value);
    303317                    return param_map;
    304                 }       
    305                 else if(toFind != null)
    306                 continue;
     318                }
     319                else if (toFind != null)
     320                    continue;
    307321
    308322                int pos = name.indexOf('.');
    309                 if (pos == -1) { // a base param
     323                if (pos == -1)
     324                { // a base param
    310325                    param_map.put(name, value);
    311                 } else { // a namespaced param
    312                    
     326                }
     327                else
     328                { // a namespaced param
     329
    313330                    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                    {
    317335                        map = new HashMap();
    318336                        param_map.put(namespace, map);
     
    326344
    327345    /** gets the value att or the text content */
    328     public static String getValue(Element e) {
     346    public static String getValue(Element e)
     347    {
    329348        String val = e.getAttribute(VALUE_ATT);
    330         if (val ==null || val.equals("")) {
     349        if (val == null || val.equals(""))
     350        {
    331351            // have to get it out of the text
    332             val=getNodeText(e);
    333            
    334         } else {
     352            val = getNodeText(e);
     353
     354        }
     355        else
     356        {
    335357            // unescape the xml stuff
    336358            val = unXmlSafe(val);
     
    340362
    341363    /** extracts the text out of a node */
    342     public static Node getNodeTextNode(Element param) {
     364    public static Node getNodeTextNode(Element param)
     365    {
    343366        param.normalize();
    344367        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();
    347371        }
    348372        return n;
     
    350374
    351375    /** extracts the text out of a node */
    352     public static String getNodeText(Element param) {
     376    public static String getNodeText(Element param)
     377    {
    353378        Node text_node = getNodeTextNode(param);
    354         if (text_node == null) {
     379        if (text_node == null)
     380        {
    355381            return "";
    356382        }
     
    358384    }
    359385
    360     public static void setNodeText(Element elem, String text) {
     386    public static void setNodeText(Element elem, String text)
     387    {
    361388        Node old_text_node = getNodeTextNode(elem);
    362         if (old_text_node != null) {
     389        if (old_text_node != null)
     390        {
    363391            elem.removeChild(old_text_node);
    364392        }
     
    367395    }
    368396
    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
    372401        Element content = owner.createElement(NODE_CONTENT_ELEM);
    373402        Text t = owner.createTextNode(text);
     
    378407
    379408    /** 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    {
    381411        return addError(owner, doc, text, ERROR_TYPE_OTHER);
    382412    }
     413
    383414    /** 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
    387418        Element content = owner.createElement(ERROR_ELEM);
    388419        content.setAttribute(ERROR_TYPE_ATT, error_type);
     
    394425
    395426    /** 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    {
    397429        return addError(owner, doc, error, ERROR_TYPE_OTHER);
    398430    }
    399431
    400432    /** 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    {
    403435        error.printStackTrace();
    404436        return addError(owner, doc, error.toString(), error_type);
    405437    }
    406438
    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);
    410443        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();
    413447            Element meta_param = owner.createElement(PARAM_ELEM);
    414448            meta_param_list.appendChild(meta_param);
     
    420454
    421455    /** 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        {
    425460            return false;
    426461        }
     
    431466        list.appendChild(data);
    432467        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
    441480            return true;
    442481        }
     
    444483    }
    445484
    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);
    452494        Document to_owner = to.getOwnerDocument();
    453         if (to_meta == null) {
     495        if (to_meta == null)
     496        {
    454497            to.appendChild(to_owner.importNode(meta_list, true));
    455498            return true;
    456499        }
    457500        // 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));
    461505        }
    462506        return true;
     
    464508
    465509    /** 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
    468513        Document owner = to.getOwnerDocument();
    469514        Node child = from.getFirstChild();
    470         while (child != null) {
     515        while (child != null)
     516        {
    471517            to.appendChild(owner.importNode(child, true));
    472518            child = child.getNextSibling();
     
    474520        return true;
    475521    }
     522
    476523    /** 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
    479527        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;
    483533            }
    484534            child = child.getNextSibling();
     
    486536        return null; //no element child found
    487537    }
     538
    488539    /** 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
    491544            // in trac bug ticket #225. If n is null can't do n.getFirstChild() below. As per bug #225:
    492545            // GSXML.getNodeByPath() is called by GS2BrowseAction, which then calls this method.
     
    495548
    496549            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                {
    499554                    return child;
    500555                }
     
    505560    }
    506561
    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        {
    511570            return getChildByTagName(n, name);
    512571        }
    513572        int count = 0;
    514573        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                {
    518580                    return child;
    519                 } else {
     581                }
     582                else
     583                {
    520584                    count++;
    521585                }
     
    526590    }
    527591
    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
    532599        String link = GSPath.getFirstLink(path);
    533600        path = GSPath.removeFirstLink(path);
    534         while (!link.equals("")) {
     601        while (!link.equals(""))
     602        {
    535603            n = getChildByTagName(n, link);
    536             if (n==null) {
     604            if (n == null)
     605            {
    537606                return null;
    538607            }
     
    543612    }
    544613
    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
    550622        String link = GSPath.getFirstLink(path);
    551623        int index = GSPath.getIndex(link);
    552         if (index != -1) {
     624        if (index != -1)
     625        {
    553626            link = GSPath.removeIndex(link);
    554627        }
    555628        path = GSPath.removeFirstLink(path);
    556         while (!link.equals("")) {
     629        while (!link.equals(""))
     630        {
    557631            n = getChildByTagNameIndexed(n, link, index);
    558             if (n==null) {
     632            if (n == null)
     633            {
    559634                return null;
    560635            }
    561636            link = GSPath.getFirstLink(path);
    562637            index = GSPath.getIndex(link);
    563             if (index != -1) {
     638            if (index != -1)
     639            {
    564640                link = GSPath.removeIndex(link);
    565641            }
     
    569645    }
    570646
    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();
    574651        Node child = n.getFirstChild();
    575         while (child!=null) {
     652        while (child != null)
     653        {
    576654            String name = child.getNodeName();
    577655            map.put(name, child);
     
    581659    }
    582660
    583     public static NodeList getChildrenByTagName(Node n, String name) {
     661    public static NodeList getChildrenByTagName(Node n, String name)
     662    {
    584663        MyNodeList node_list = new MyNodeList();
    585664        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            {
    588669                node_list.addNode(child);
    589670            }
     
    593674    }
    594675
    595 
    596676    /** 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    {
    599679        return duplicateWithNewNameNS(owner, element, element_name, null, with_attributes);
    600680    }
    601681
    602682    /** 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    {
    608685        Element duplicate;
    609         if (namespace_uri == null) {
     686        if (namespace_uri == null)
     687        {
    610688            duplicate = owner.createElement(element_name);
    611         } else {
     689        }
     690        else
     691        {
    612692            duplicate = owner.createElementNS(namespace_uri, element_name);
    613693        }
    614694        // Copy element attributes
    615         if (with_attributes) {
     695        if (with_attributes)
     696        {
    616697            NamedNodeMap attributes = element.getAttributes();
    617             for (int i = 0; i < attributes.getLength(); i++) {
     698            for (int i = 0; i < attributes.getLength(); i++)
     699            {
    618700                Node attribute = attributes.item(i);
    619701                duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
    620702            }
    621703        }
    622        
     704
    623705        // Copy element children
    624706        NodeList children = element.getChildNodes();
    625         for (int i = 0; i < children.getLength(); i++) {
     707        for (int i = 0; i < children.getLength(); i++)
     708        {
    626709            Node child = children.item(i);
    627710            duplicate.appendChild(owner.importNode(child, true));
    628711        }
    629        
     712
    630713        return duplicate;
    631714    }
    632715
    633     public static void copyAllChildren(Element to, Element from) {
    634        
     716    public static void copyAllChildren(Element to, Element from)
     717    {
     718
    635719        Document to_doc = to.getOwnerDocument();
    636720        Node child = from.getFirstChild();
    637         while (child != null) {
     721        while (child != null)
     722        {
    638723            to.appendChild(to_doc.importNode(child, true));
    639724            child = child.getNextSibling();
    640725        }
    641726    }
     727
    642728    /** 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    {
    647731        Element request = owner.createElement(REQUEST_ELEM);
    648732        request.setAttribute(TYPE_ATT, request_type);
     
    652736        return request;
    653737    }
    654    
     738
    655739    public static Element createBasicResponse(Document owner, String from)
    656740    {
     
    660744        return response;
    661745    }
    662    
     746
    663747    public static Element createMetadataElement(Document owner, String name, String value)
    664748    {
     
    669753    }
    670754
    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    {
    673757        Element e = owner.createElement(elem_name);
    674758        Text t = owner.createTextNode(text);
    675759        e.appendChild(t);
    676760        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    {
    682766        Element e = owner.createElement(elem_name);
    683767        e.setAttribute(att_name, att_value);
     
    685769        e.appendChild(t);
    686770        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    {
    693776        Element e = owner.createElement(DISPLAY_TEXT_ELEM);
    694777        e.setAttribute(NAME_ATT, text_name);
     
    696779        e.appendChild(t);
    697780        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    {
    704786        Element param = owner.createElement(PARAM_ELEM);
    705787        param.setAttribute(NAME_ATT, name);
     
    708790    }
    709791
    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    {
    712794        if (params == null)
    713795        {
    714796            return;
    715797        }
    716        
     798
    717799        Set items = params.entrySet();
    718800        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
    735812        Element p = owner.createElement(PARAM_ELEM);
    736813        p.setAttribute(NAME_ATT, id);
    737814        p.setAttribute(TYPE_ATT, type);
    738815        p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
    739        
    740         if (default_value != null) {
     816
     817        if (default_value != null)
     818        {
    741819            p.setAttribute(DEFAULT_ATT, default_value);
    742820        }
    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            {
    745825                Element e = owner.createElement(PARAM_OPTION_ELEM);
    746826                e.setAttribute(NAME_ATT, option_ids[i]);
     
    751831        return p;
    752832    }
    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
    762837        Element p = owner.createElement(PARAM_ELEM);
    763838        p.setAttribute(NAME_ATT, id);
    764839        p.setAttribute(TYPE_ATT, type);
    765840        p.appendChild(createDisplayTextElement(owner, GSXML.DISPLAY_TEXT_NAME, display_name));
    766         if (default_value != null) {
     841        if (default_value != null)
     842        {
    767843            p.setAttribute(DEFAULT_ATT, default_value);
    768844        }
    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            {
    771849                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)));
    774852                p.appendChild(e);
    775853            }
     
    778856    }
    779857
    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
    787862        NodeList children = parent.getChildNodes();
    788         for (int i=0; i<children.getLength(); i++) {
     863        for (int i = 0; i < children.getLength(); i++)
     864        {
    789865            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;
    793870            }
    794871        }
     
    796873        return null;
    797874    }
    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    {
    801882        MyNodeList node_list = new MyNodeList();
    802883        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            {
    807890                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            }
    813897        }
    814898        return node_list;
     
    820904
    821905    // 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        {
    830913            parent_node.appendChild(cloned_elem);
    831914            return cloned_elem;
    832915        }
    833        
     916
    834917        Node current_node = start_from_elem;
    835918        String insert_att = cloned_elem.getAttribute(sort_att);
    836919        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        {
    838922            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            {
    841927                continue; // not a valid node
    842928            }
    843             list_att = ((Element)current_node).getAttribute(sort_att);
    844         }
    845        
     929            list_att = ((Element) current_node).getAttribute(sort_att);
     930        }
     931
    846932        parent_node.insertBefore(cloned_elem, current_node);
    847933        return cloned_elem;
    848934    }
    849935
    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
    858945        String def = null;
    859946        String first = null;
    860947        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);
    864953            String n = e.getAttribute(NAME_ATT);
    865             if (name.equals(n)) {
     954            if (name.equals(n))
     955            {
    866956                String l = e.getAttribute(LANG_ATT);
    867                 if (lang.equals(l)) {
     957                if (lang.equals(l))
     958                {
    868959                    return getNodeText(e);
    869                 } else if (lang_default.equals(l)) {
     960                }
     961                else if (lang_default.equals(l))
     962                {
    870963                    def = getNodeText(e);
    871                 } else if (first == null) {
     964                }
     965                else if (first == null)
     966                {
    872967                    first = getNodeText(e);
    873968                }
    874             } else {
     969            }
     970            else
     971            {
    875972                continue;
    876973            }
    877974        }
    878        
    879         if (def != null) {
     975
     976        if (def != null)
     977        {
    880978            return def;
    881979        }
    882         if (first != null) {
     980        if (first != null)
     981        {
    883982            return first;
    884983        }
     
    887986
    888987    // replaces < > " ' & in the original with their entities
    889     public static String xmlSafe(String original) {
    890        
     988    public static String xmlSafe(String original)
     989    {
     990
    891991        StringBuffer filtered = new StringBuffer(original.length());
    892992        char c;
    893         for (int i=0; i<original.length(); i++) {
     993        for (int i = 0; i < original.length(); i++)
     994        {
    894995            c = original.charAt(i);
    895             if (c == '>') {
     996            if (c == '>')
     997            {
    896998                filtered.append("&gt;");
    897             } else if (c == '<') {
     999            }
     1000            else if (c == '<')
     1001            {
    8981002                filtered.append("&lt;");
    899             } else if (c == '"') {
     1003            }
     1004            else if (c == '"')
     1005            {
    9001006                filtered.append("&quot;");
    901             } else if (c == '&') {
     1007            }
     1008            else if (c == '&')
     1009            {
    9021010                filtered.append("&amp;");
    903             } else if (c == '\'') {
     1011            }
     1012            else if (c == '\'')
     1013            {
    9041014                filtered.append("&apos;");
    905             } else {
     1015            }
     1016            else
     1017            {
    9061018                filtered.append(c);
    9071019            }
     
    9101022    }
    9111023
    912 
    9131024    // replaces < > " ' & entities with their originals
    914     public static String unXmlSafe(String original) {
    915        
     1025    public static String unXmlSafe(String original)
     1026    {
     1027
    9161028        StringBuffer filtered = new StringBuffer(original.length());
    9171029        char c;
    918         for (int i=0; i<original.length(); i++) {
     1030        for (int i = 0; i < original.length(); i++)
     1031        {
    9191032            c = original.charAt(i);
    920             if (c == '&') {
     1033            if (c == '&')
     1034            {
    9211035                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                {
    9241039                    filtered.append(">");
    925                 } else if (entity.equals("lt")) {
     1040                }
     1041                else if (entity.equals("lt"))
     1042                {
    9261043                    filtered.append("<");
    927                 } else if (entity.equals("apos")) {
     1044                }
     1045                else if (entity.equals("apos"))
     1046                {
    9281047                    filtered.append("'");
    929                 } else if (entity.equals("amp")) {
     1048                }
     1049                else if (entity.equals("amp"))
     1050                {
    9301051                    filtered.append("&");
    931                 } else if (entity.equals("quot")) {
     1052                }
     1053                else if (entity.equals("quot"))
     1054                {
    9321055                    filtered.append("\"");
    933                 } else {
    934                     filtered.append("&"+entity+";");
     1056                }
     1057                else
     1058                {
     1059                    filtered.append("&" + entity + ";");
    9351060                }
    9361061                i = pos;
    937             } else {
     1062            }
     1063            else
     1064            {
    9381065                filtered.append(c);
    9391066            }
     
    9421069    }
    9431070
    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    {
    9491078        StringBuffer sb = new StringBuffer("");
    950         xmlNodeToString(sb,e,0,true);
     1079        xmlNodeToString(sb, e, 0, true);
    9511080        return sb.toString();
    9521081    }
    9531082
    954     public static String xmlNodeToString(Node e, boolean printText){
     1083    public static String xmlNodeToString(Node e, boolean printText)
     1084    {
    9551085        StringBuffer sb = new StringBuffer("");
    956         xmlNodeToString(sb,e,0,printText);
     1086        xmlNodeToString(sb, e, 0, printText);
    9571087        return sb.toString();
    9581088    }
    9591089
    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)
    9671099            {
    9681100                sb.append(e.getNodeValue());
     
    9721104                sb.append("text");
    9731105            }
    974             return ;
    975         }
    976        
     1106            return;
     1107        }
     1108
    9771109        sb.append('<');
    9781110        sb.append(e.getNodeName());
    9791111        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            {
    9831116                Node attr = attrs.item(i);
    9841117                sb.append(' ');
     
    9891122            }
    9901123        }
    991        
     1124
    9921125        NodeList children = e.getChildNodes();
    993        
     1126
    9941127        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
    10001134            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
    10081143            sb.append("</" + e.getNodeName() + ">\n");
    10091144        }
    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            {
    10231163                System.out.println(e.getNodeValue());
    10241164            }
    1025             else {
     1165            else
     1166            {
    10261167                System.out.println("text");
    10271168            }
    1028             return ;
    1029         }
    1030        
     1169            return;
     1170        }
     1171
    10311172        System.out.print('<');
    10321173        System.out.print(e.getNodeName());
    10331174        NamedNodeMap attrs = e.getAttributes();
    1034        
     1175
    10351176        if (attrs != null)
    10361177        {
    1037             for (int i = 0; i < attrs.getLength(); i++) {
     1178            for (int i = 0; i < attrs.getLength(); i++)
     1179            {
    10381180                Node attr = attrs.item(i);
    10391181                System.out.print(' ');
     
    10441186            }
    10451187        }
    1046        
     1188
    10471189        NodeList children = e.getChildNodes();
    1048        
     1190
    10491191        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
    10551198            int len = children.getLength();
    1056             for (int i = 0; i < len; i++) {
     1199            for (int i = 0; i < len; i++)
     1200            {
    10571201                printXMLNode(children.item(i), depth + 1, printText);
    10581202            }
    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
    10631207            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        {
    10691215            TransformerFactory tf = TransformerFactory.newInstance();
    10701216            Transformer trans = tf.newTransformer();
    10711217            StringWriter sw = new StringWriter();
    10721218            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");
    10761224        }
    10771225    }
Note: See TracChangeset for help on using the changeset viewer.