Ignore:
Timestamp:
2003-03-12T16:54:41+13:00 (21 years ago)
Author:
kjdon
Message:

some changes to functions

File:
1 edited

Legend:

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

    r3867 r3869  
    157157    // HASH oids - name att for resource
    158158    // generalise this for any element type? pass in the list, the element name, the att to extract
    159     public static String [] getDocumentNameList(Element content) {
    160    
    161     Node n = content.getFirstChild();
    162     while (n!=null && !n.getNodeName().equals(DOCUMENT_ELEM+LIST_MODIFIER)) {
    163         n = n.getNextSibling();
    164     }
    165     if (n==null) { // no docs found
    166         return null;
    167     }
     159//      public static String [] getDocumentNameList(Element content) {
     160   
     161//      Node n = content.getFirstChild();
     162//      while (n!=null && !n.getNodeName().equals(DOCUMENT_ELEM+LIST_MODIFIER)) {
     163//          n = n.getNextSibling();
     164//      }
     165//      if (n==null) { // no docs found
     166//          return null;
     167//      }
    168168       
    169     NodeList docs = n.getChildNodes();
    170    
    171     int numdocs = docs.getLength();
    172     String []ids = new String[numdocs];
    173     for (int i=0; i<numdocs; i++) {
    174         Element e = (Element)docs.item(i);
    175         String id = e.getAttribute(NAME_ATT);
    176         // check that its a valid id - ie starts with HASH
    177         // need to change this if use different ids
     169//      NodeList docs = n.getChildNodes();
     170   
     171//      int numdocs = docs.getLength();
     172//      String []ids = new String[numdocs];
     173//      for (int i=0; i<numdocs; i++) {
     174//          Element e = (Element)docs.item(i);
     175//          String id = e.getAttribute(NAME_ATT);
     176//          // check that its a valid id - ie starts with HASH
     177//          // need to change this if use different ids
    178178       
    179         ids[i] = id;
     179//          ids[i] = id;
    180180       
    181     }
    182 
    183     return ids;
    184     }
     181//      }
     182
     183//      return ids;
     184//      }
    185185
    186186    // same as above function
    187187    /** extracts metadata names out of an element */
    188     public static String [] getMetaNameList(Element content) {
    189     Node n = content.getFirstChild();
    190     while (n!=null &&
    191            !n.getNodeName().equals(METADATA_ELEM+LIST_MODIFIER)) {
    192         n = n.getNextSibling();
    193     }
    194     if (n==null) { // no metadatas found
    195         return null;
    196     }
    197     NodeList elems = n.getChildNodes();
    198    
    199     int numelems = elems.getLength();
    200     String []ids = new String[numelems];
    201     for (int i=0; i<numelems; i++) {
    202         Element e = (Element)elems.item(i);
    203         String id = e.getAttribute(NAME_ATT);
    204         ids[i] = id;
    205     }
    206 
    207     return ids;
    208     }
    209 
    210     // combine the next two??
    211     /** takes a paramList element, and gets a HashMap of name-value pairs */
    212     public static HashMap extractParams(Element xml) {
     188//      public static String [] getMetaNameList(Element content) {
     189//      Node n = content.getFirstChild();
     190//      while (n!=null &&
     191//             !n.getNodeName().equals(METADATA_ELEM+LIST_MODIFIER)) {
     192//          n = n.getNextSibling();
     193//      }
     194//      if (n==null) { // no metadatas found
     195//          return null;
     196//      }
     197//      NodeList elems = n.getChildNodes();
     198   
     199//      int numelems = elems.getLength();
     200//      String []ids = new String[numelems];
     201//      for (int i=0; i<numelems; i++) {
     202//          Element e = (Element)elems.item(i);
     203//          String id = e.getAttribute(NAME_ATT);
     204//          ids[i] = id;
     205//      }
     206
     207//      return ids;
     208//      }
     209
     210   
     211    /** takes a paramList element, and gets a HashMap of name-value pairs
     212     * if deep=true, extracts embedded params, otherwise just top level
     213     * params*/
     214    public static HashMap extractParams(Element xml, boolean deep) {
    213215
    214216    if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
     
    216218        return null;
    217219    }
    218     NodeList params = xml.getChildNodes();
     220
     221    NodeList params = null;
     222    if (deep) { // get all the nested ones
     223        params = xml.getElementsByTagName(PARAM_ELEM);
     224    } else { // just get the top  level ones
     225        params = xml.getChildNodes();
     226    }
    219227    HashMap param_map = new HashMap();
    220228    for (int i=0; i<params.getLength(); i++) {
    221229        Element param = (Element)params.item(i);
    222230        String name=param.getAttribute(NAME_ATT);
    223         String value=param.getAttribute(VALUE_ATT);
    224         if (value.equals("")) { // the value is in the content of the param
    225         value=getNodeText(param);
    226         }
     231        String value=getValue(param); //att or content
    227232        param_map.put(name, value);
    228233       
     
    231236    }
    232237    /** takes a paramList element, and gets a HashMap of name-value pairs */
    233     public static HashMap extractAllParams(Element xml) {
    234 
    235     if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
    236         System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
    237         return null;
    238     }
    239     NodeList params = xml.getElementsByTagName(PARAM_ELEM);
    240     HashMap param_map = new HashMap();
    241     for (int i=0; i<params.getLength(); i++) {
    242         Element param = (Element)params.item(i);
    243         String name=param.getAttribute(NAME_ATT);
    244         String value=param.getAttribute(VALUE_ATT);
    245         if (value.equals("")) { // the value is in the content of the param
    246         value=getNodeText(param);
    247         }
    248         param_map.put(name, value);
     238//      public static HashMap extractAllParams(Element xml) {
     239
     240//      if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
     241//          System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
     242//          return null;
     243//      }
     244//      NodeList params = xml.getElementsByTagName(PARAM_ELEM);
     245//      HashMap param_map = new HashMap();
     246//      for (int i=0; i<params.getLength(); i++) {
     247//          Element param = (Element)params.item(i);
     248//          String name=param.getAttribute(NAME_ATT);
     249//          String value=param.getAttribute(VALUE_ATT);
     250//          if (value.equals("")) { // the value is in the content of the param
     251//          value=getNodeText(param);
     252//          }
     253//          param_map.put(name, value);
    249254       
    250     }
    251     return param_map;
    252     }
     255//      }
     256//      return param_map;
     257//      }
    253258
    254259    /** gets the value att or the text content */
     
    275280    return n.getNodeValue();
    276281    }
     282
    277283    /** creates a new document Element */
    278     public static Element createDocumentElement(Document owner, String oid) {
    279     Element e = owner.createElement(DOCUMENT_ELEM);
    280     e.setAttribute(NAME_ATT, oid);
    281    
    282     return e;
    283     }
     284//      public static Element createDocumentElement(Document owner, String oid) {
     285//      Element e = owner.createElement(DOCUMENT_ELEM);
     286//      e.setAttribute(NAME_ATT, oid);
     287   
     288//      return e;
     289//      }
    284290   
    285291    /** add text to a document/subsection  element */
     
    316322//      }
    317323    /** adds a metadata elem to a list */
     324
    318325    public static boolean addMetadata(Document owner, Element list,
    319326                      String meta_name, String meta_value) {
     
    356363    }
    357364   
    358     /** takes two nodes - should have the same node type, tag name, etc
    359      * copies the info from 'from' into 'to'
    360      -- not finished haven't thought through the algo yet.*/
    361     /*
    362     public static boolean mergeElements(Element to, Element from) {
    363 
    364     if (to.getNodeName()!=from.getNodeName()) {
    365         System.err.println("cant merge two nodes with different names");
    366         return false;
    367     }
    368 
    369     // copy any atts over - ignore for now
    370 
    371     // copy the children
    372     if (!from.hasChildNodes()) {
    373         return true;
    374     }
    375 
    376     // check if they belong to the same document
    377     Document to_doc = to.getOwnerDocument();
    378     Document from_doc = from.getOwnerDocument();
    379     Element newfrom;
    380     if (to_doc != from_doc) {
    381         nfrom = to_doc.importNode(from, true);
    382     } else {
    383         newfrom = from;
    384     }
    385    
    386     if (!to.hasChildNodes()) {
    387         // just copy all the children over
    388         Node child = newfrom.getFirstChild();
    389         while (child !=null) {
    390         to.appendChild(child);
    391         child = child.getNextSibling();
    392         }
    393         return true;
    394     }
    395    
    396     // have to check each one to see if already present or not
    397     HashMap children = getChildrenMap(to);
    398    
    399     Node child = newfrom.getFirstChild();
    400     while (child !=null) {
    401         String name = child.getNodeName();
    402         Node n = map.get(name);
    403         if (n==null) { // there is no elem by that name in to
    404         to.appendChild(child);
    405         }
    406         else {
    407     }
    408     return true;
    409     }
    410     */
    411365    /** returns the (first) child element with the given name */
    412366    public static Node getChildByTagName(Node n, String name) {
Note: See TracChangeset for help on using the changeset viewer.