Changeset 3576


Ignore:
Timestamp:
2002-11-26T14:05:37+13:00 (21 years ago)
Author:
kjdon
Message:

added some comments, now uses GSXML.static variables instead of strings for xml names

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/CGIArgConverter.java

    r3487 r3576  
    44import org.w3c.dom.Element;
    55
     6/** converts long names for parameters into short names for cgi args, and vice versa */
    67public class CGIArgConverter {
    78
     9    /** long to short mapping */
    810    protected HashMap long_map_=null;
     11    /** short to long mapping */
    912    protected HashMap short_map_=null;
    1013   
     14    /** initialises the maps with known parameters */
    1115    public CGIArgConverter() {
    1216    long_map_ = new HashMap();
     
    1418
    1519    // initialize with the ones we know about
     20    short_map_.put("a", "action");
     21    long_map_.put("action", "a");
     22
     23    short_map_.put("sa", "subaction");
     24    long_map_.put("subaction", "sa");   
     25
     26    short_map_.put("sn", "serviceName");
     27    long_map_.put("serviceName", "sn");
     28
     29    short_map_.put("sc", "serviceCluster");
     30    long_map_.put("serviceCluster", "sc");
     31
     32    short_map_.put("l", "lang");
     33    long_map_.put("lang", "l");
     34
     35    short_map_.put("c", "collection");
     36    long_map_.put("collection", "c");
     37
     38    long_map_.put("resource", "r");
     39    short_map_.put("r", "resource");
     40
     41    // these are service specific - should we have these here??
    1642    long_map_.put("case", "k");
    1743    short_map_.put("k", "case");
     
    3561    short_map_.put("md", "maxDocs");
    3662
    37     short_map_.put("c", "collection");
    38     long_map_.put("collection", "c");
    39 
    40     short_map_.put("a", "action");
    41     long_map_.put("action", "a");
    42 
    43     short_map_.put("sa", "subaction");
    44     long_map_.put("subaction", "sa");   
    45 
    46     short_map_.put("sn", "serviceName");
    47     long_map_.put("serviceName", "sn");
    48 
    49     short_map_.put("sc", "serviceCluster");
    50     long_map_.put("serviceCluster", "sc");
    51 
    52     short_map_.put("l", "lang");
    53     long_map_.put("lang", "l");
    54 
    5563    long_map_.put("query", "q");
    5664    short_map_.put("q", "query");
    5765   
    58     long_map_.put("resource", "r");
    59     short_map_.put("r", "resource");
    60 
    6166    short_map_.put("cl", "classifier");
    6267    long_map_.put("classifier", "cl");
    6368    }
    6469
     70    /** takes a paramList element  - contains <param name="x"> elements
     71     * converts all the short names to long names
     72     * returns true if successful, false otherwise */
    6573    public boolean toLong(Element param_list) {
    6674   
    67     if (!param_list.getNodeName().equals("paramList")) {
    68         System.out.println("wrong element to CGIArgConverter:toLong");
     75    if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
     76        System.err.println("CGIArgConverter Error: wrong element passed to toLong");
    6977        return false;
    7078    }
     
    7280    Element param = (Element)param_list.getFirstChild();
    7381    while (param!=null) {
    74         String name = param.getAttribute("name");
    75         param.setAttribute("name", toLong(name));
     82        String name = param.getAttribute(GSXML.NAME_ATT);
     83        param.setAttribute(GSXML.NAME_ATT, toLong(name));
     84        param = (Element)param.getNextSibling();
     85    }
     86    return true;
     87
     88    }
     89    /** adds shortnames to the params in the list */
     90    public boolean addShortNames(Element param_list) {
     91    if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
     92        System.err.println("CGIArgConverter Error: wrong element passed to addShortNames");
     93        return false;
     94    }
     95   
     96    Element param = (Element)param_list.getFirstChild();
     97    while (param!=null) {
     98        String name = param.getAttribute(GSXML.NAME_ATT);
     99        param.setAttribute(GSXML.PARAM_SHORTNAME_ATT, toShort(name));
    76100        param = (Element)param.getNextSibling();
    77101    }
     
    80104    }
    81105   
    82     public boolean  addShortNames(Element param_list) {
    83     if (!param_list.getNodeName().equals("paramList")) {
    84         System.out.println("wrong element to CGIArgConverter:toShort");
    85         return false;
    86     }
    87    
    88     Element param = (Element)param_list.getFirstChild();
    89     while (param!=null) {
    90         String name = param.getAttribute("name");
    91         param.setAttribute("shortname", toShort(name));
    92         param = (Element)param.getNextSibling();
    93     }
    94     return true;
    95 
    96     }
    97    
    98 
     106    /** returns the long form of a short arg -
     107     * returns the original if it doesn't know about this short arg */
    99108    public String toLong(String short_name) {
    100    
    101109   
    102110    if (short_map_.containsKey(short_name)) {
    103111        return (String)short_map_.get(short_name);
    104112    }
    105     System.err.println("CGIArgConverter: short name '"+short_name+"' not found in map!");
    106113    return short_name; // just return the original
    107114    }
    108 
     115    /** returns the short form of a long arg */
    109116    public String toShort(String long_name) {
    110117
     
    119126    return short_name;
    120127    }
    121 
     128   
     129    /** tries to create a new short form of the long name -
     130     * if it cant, it just returns the original name */
    122131    protected String newShortName(String long_name) {
    123 
     132   
    124133    // for now, try the first letter, then first two etc.
    125134    int i=1;
     
    129138        short_try = long_name.substring(0, i);
    130139    }
    131     if (i==long_name.length()) {
    132         return ""; // couldn't find one
    133     }
    134140    return short_try;
     141   
    135142    }
    136143}
Note: See TracChangeset for help on using the changeset viewer.