Index: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java
===================================================================
--- trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java	(revision 3285)
+++ trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java	(revision 3285)
@@ -0,0 +1,135 @@
+package org.greenstone.gsdl3.util;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
+import org.w3c.dom.Text;
+
+import java.util.HashMap;
+
+/** various functions for extracting info out of GS XML */
+public class GSXML {
+
+  // takes a node with a resource elements inside it and extracts all the 
+  // HASh oids - name att for resource
+    public static String [] getResourceNameList(Element content) {
+	
+	Node n = content.getFirstChild();
+	while (n!=null && !n.getNodeName().equals("resourceList")) {
+	    n = n.getNextSibling();
+	}
+	if (n==null) { // no docs found
+	    return null;
+	}
+	    
+	NodeList docs = n.getChildNodes();
+	
+	int numdocs = docs.getLength();
+	String []ids = new String[numdocs];
+	for (int i=0; i<numdocs; i++) {
+	    Element e = (Element)docs.item(i);
+	    String id = e.getAttribute("name");
+	    // check that its a valid id - ie starts with HASH 
+	    // need to change this if use different ids
+	    
+	    ids[i] = id;
+	    
+	}
+
+	return ids;
+    }
+
+    /** extracts metadata names out of an element */
+    public static String [] getMetaNameList(Element content) {
+	Node n = content.getFirstChild();
+	while (n!=null && !n.getNodeName().equals("metadataList")) {
+	    n = n.getNextSibling();
+	}
+	if (n==null) { // no metadatas found
+	    return null;
+	}
+	NodeList elems = n.getChildNodes();
+	
+	int numelems = elems.getLength();
+	String []ids = new String[numelems];
+	for (int i=0; i<numelems; i++) {
+	    Element e = (Element)elems.item(i);
+	    String id = e.getAttribute("name");
+	    ids[i] = id;
+	}
+
+	return ids;
+    }
+
+    /** takes a paramList element, and gets a HashMap of name-value pairs */
+    public static HashMap extractParams(Element xml) {
+
+	if (!xml.getNodeName().equals("paramList")) {
+	    System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
+	    return null;
+	}
+	NodeList params = xml.getChildNodes();
+	HashMap param_map = new HashMap();
+	for (int i=0; i<params.getLength(); i++) {
+	    Element param = (Element)params.item(i);
+	    String name=param.getAttribute("name");
+	    String value=param.getAttribute("value");
+	    if (value.equals("")) { // the value is in the content of the param
+		value=getNodeText(param); 
+	    }
+	    param_map.put(name, value);
+	    
+	}
+	return param_map;
+    }
+
+    /** extracts the text out of a node */
+    public static String getNodeText(Element param) {
+	param.normalize();
+	Node n = param.getFirstChild();
+	while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
+	    n=n.getNextSibling();
+	}
+	if (n==null) { // no text node
+	    return "";
+	}
+	return n.getNodeValue();
+    }
+    /** creates a new document Element */
+    public static Element createResourceElement(Document owner, String oid) { 
+	Element e = owner.createElement("resource");
+	e.setAttribute("name", oid);
+	
+	return e;
+    }
+	
+    /** add text to a document/subsection  element */
+    public static boolean addDocText(Document owner, Element doc, String text) {
+	Element content = owner.createElement("content");
+	Text t = owner.createTextNode(text);
+	content.appendChild(t);
+	doc.appendChild(content);
+	return true;
+    }
+    /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
+    public static Element addMetaList(Document owner, Element doc) {
+	Element list = owner.createElement("metadataList");
+	doc.appendChild(list);
+	return list;
+    }
+    /** adds a metadata elem to a list */
+    public static boolean addMetadata(Document owner, Element list, 
+				      String meta_name, String meta_value) {
+	if (meta_value==null || meta_value.equals("")) {
+	    return false;
+	}
+	Element data = owner.createElement("metadata");
+	data.setAttribute("name", meta_name);
+	Text t = owner.createTextNode(meta_value);
+	data.appendChild(t);
+	list.appendChild(data);
+	return true;
+
+    }
+}
