Ignore:
Timestamp:
2002-08-12T09:48:42+12:00 (22 years ago)
Author:
kjdon
Message:

some modifications

File:
1 edited

Legend:

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

    r3285 r3341  
    107107    /** add text to a document/subsection  element */
    108108    public static boolean addDocText(Document owner, Element doc, String text) {
     109
    109110    Element content = owner.createElement("content");
    110111    Text t = owner.createTextNode(text);
     
    133134
    134135    }
     136
     137    public static boolean mergeMetadataLists(Node to, Node from) {
     138    Node to_meta = GSXML.getChildByTagName(to, "metadataList");
     139    Node from_meta = getChildByTagName(from, "metadataList");
     140   
     141    if  (from_meta == null) { // nothing to copy
     142        return true;
     143    }
     144    Document to_owner = to.getOwnerDocument();
     145    Node new_from = to_owner.importNode(from_meta, true);
     146
     147    if (to_meta == null) { // just copy the whole list
     148        to.appendChild(new_from);
     149        return true;
     150    }
     151   
     152    // copy individual elements
     153    Node child = new_from.getFirstChild();
     154    while ( child != null) {
     155        to_meta.appendChild(child);
     156        child = child.getNextSibling();
     157    }
     158    return true;
     159    }
     160   
     161    /** takes two nodes - should have the same node type, tag name, etc
     162     * copies the info from 'from' into 'to'
     163     -- not finished haven't thought through the algo yet.*/
     164    /*
     165    public static boolean mergeElements(Element to, Element from) {
     166
     167    if (to.getNodeName()!=from.getNodeName()) {
     168        System.err.println("cant merge two nodes with different names");
     169        return false;
     170    }
     171
     172    // copy any atts over - ignore for now
     173
     174    // copy the children
     175    if (!from.hasChildNodes()) {
     176        return true;
     177    }
     178
     179    // check if they belong to the same document
     180    Document to_doc = to.getOwnerDocument();
     181    Document from_doc = from.getOwnerDocument();
     182    Element newfrom;
     183    if (to_doc != from_doc) {
     184        nfrom = to_doc.importNode(from, true);
     185    } else {
     186        newfrom = from;
     187    }
     188   
     189    if (!to.hasChildNodes()) {
     190        // just copy all the children over
     191        Node child = newfrom.getFirstChild();
     192        while (child !=null) {
     193        to.appendChild(child);
     194        child = child.getNextSibling();
     195        }
     196        return true;
     197    }
     198   
     199    // have to check each one to see if already present or not
     200    HashMap children = getChildrenMap(to);
     201   
     202    Node child = newfrom.getFirstChild();
     203    while (child !=null) {
     204        String name = child.getNodeName();
     205        Node n = map.get(name);
     206        if (n==null) { // there is no elem by that name in to
     207        to.appendChild(child);
     208        }
     209        else {
     210    }
     211    return true;
     212    }
     213    */
     214    /** returns the (first) child element with the given name */
     215    public static Node getChildByTagName(Node n, String name) {
     216
     217    Node child = n.getFirstChild();
     218    while (child!=null) {
     219        if (child.getNodeName().equals(name)) {
     220        return child;
     221        }
     222        child = child.getNextSibling();
     223    }
     224    return null; //not found
     225    }
     226    public static HashMap getChildrenMap(Node n) {
     227   
     228    HashMap map= new HashMap();
     229    Node child = n.getFirstChild();
     230    while (child!=null) {
     231        String name = child.getNodeName();
     232        map.put(name, child);
     233        child = child.getNextSibling();
     234    }
     235    return map;
     236    }
     237       
     238    public static Element createTextElement(Document owner, String elem_name,
     239                        String text) {
     240    Element e = owner.createElement(elem_name);
     241    Text t = owner.createTextNode(text);
     242    e.appendChild(t);
     243    return e;
     244
     245    }
     246
    135247}
Note: See TracChangeset for help on using the changeset viewer.