Changeset 9912


Ignore:
Timestamp:
2005-05-19T12:24:02+12:00 (19 years ago)
Author:
kjdon
Message:

added in index handling for paths eg response[1]/docNodeList - so far used for a new request type - messaging, which modifies requests/responses

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/util
Files:
2 edited

Legend:

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

    r9434 r9912  
    122122    return path;
    123123    }
     124
     125    static public int getIndex(String link) {
     126    int i = link.indexOf("[");
     127    if (i==-1) {
     128        return -1;
     129    }
     130    int j = link.indexOf("]");
     131    String num = link.substring(i+1,j);
     132    try {
     133        int index = Integer.parseInt(num);
     134        return index;
     135    } catch (Exception e) {
     136        return -1;
     137    }
     138    }
     139
     140    static public String removeIndex(String link) {
     141    int i = link.indexOf("[");
     142    if (i==-1) {
     143        return link;
     144    }
     145    return link.substring(0, i);
     146    }
     147
    124148}
    125149
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r9874 r9912  
    153153    // get any format info for a service
    154154    public static final String REQUEST_TYPE_FORMAT = "format";
    155    
     155    // modify the requests
     156    public static final String REQUEST_TYPE_MESSAGING = "messaging";
     157
    156158    // service types
    157159    public static final String SERVICE_TYPE_QUERY = "query";
     
    392394    return null; //not found
    393395    }
     396   
     397    /** returns the (nth) child element with the given name
     398     * index numbers start at 0 */
     399    public static Node getChildByTagNameIndexed(Node n, String name, int index) {
     400    if (index == -1) {
     401        return getChildByTagName(n, name);
     402    }
     403    int count = 0;
     404    Node child = n.getFirstChild();
     405    while (child!=null) {
     406        if (child.getNodeName().equals(name)) {
     407        if (count == index) {
     408            return child;
     409        } else {
     410            count++;
     411        }
     412        }
     413        child = child.getNextSibling();
     414    }
     415    return null; //not found
     416    }
    394417
    395418    /** takes an xpath type expression of the form name/name/...
     
    409432    return n;
    410433    }
     434
     435    /** takes an xpath type expression of the form name/name/...
     436     * and returns the first node that matches, or null if not found
     437     * can include [i] indices. index numbers start at 0 */
     438    public static Node getNodeByPathIndexed(Node n, String path) {
     439   
     440    String link = GSPath.getFirstLink(path);
     441    int index = GSPath.getIndex(link);
     442    if (index != -1) {
     443        link = GSPath.removeIndex(link);
     444    }
     445    path = GSPath.removeFirstLink(path);
     446    while (!link.equals("")) {
     447        n = getChildByTagNameIndexed(n, link, index);
     448        if (n==null) {
     449        return null;
     450        }
     451        link = GSPath.getFirstLink(path);
     452        index = GSPath.getIndex(link);
     453        if (index != -1) {
     454        link = GSPath.removeIndex(link);
     455        }
     456        path = GSPath.removeFirstLink(path);
     457    }
     458    return n;
     459    }
     460
    411461    public static HashMap getChildrenMap(Node n) {
    412462   
Note: See TracChangeset for help on using the changeset viewer.