1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import org.w3c.dom.Node;
|
---|
4 | import org.w3c.dom.Element;
|
---|
5 | import org.w3c.dom.NodeList;
|
---|
6 | import org.w3c.dom.Document;
|
---|
7 | import org.w3c.dom.Text;
|
---|
8 |
|
---|
9 | import java.util.HashMap;
|
---|
10 |
|
---|
11 | /** various functions for extracting info out of GS XML */
|
---|
12 | public class GSXML {
|
---|
13 |
|
---|
14 | // takes a node with a resource elements inside it and extracts all the
|
---|
15 | // HASh oids - name att for resource
|
---|
16 | public static String [] getResourceNameList(Element content) {
|
---|
17 |
|
---|
18 | Node n = content.getFirstChild();
|
---|
19 | while (n!=null && !n.getNodeName().equals("resourceList")) {
|
---|
20 | n = n.getNextSibling();
|
---|
21 | }
|
---|
22 | if (n==null) { // no docs found
|
---|
23 | return null;
|
---|
24 | }
|
---|
25 |
|
---|
26 | NodeList docs = n.getChildNodes();
|
---|
27 |
|
---|
28 | int numdocs = docs.getLength();
|
---|
29 | String []ids = new String[numdocs];
|
---|
30 | for (int i=0; i<numdocs; i++) {
|
---|
31 | Element e = (Element)docs.item(i);
|
---|
32 | String id = e.getAttribute("name");
|
---|
33 | // check that its a valid id - ie starts with HASH
|
---|
34 | // need to change this if use different ids
|
---|
35 |
|
---|
36 | ids[i] = id;
|
---|
37 |
|
---|
38 | }
|
---|
39 |
|
---|
40 | return ids;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /** extracts metadata names out of an element */
|
---|
44 | public static String [] getMetaNameList(Element content) {
|
---|
45 | Node n = content.getFirstChild();
|
---|
46 | while (n!=null && !n.getNodeName().equals("metadataList")) {
|
---|
47 | n = n.getNextSibling();
|
---|
48 | }
|
---|
49 | if (n==null) { // no metadatas found
|
---|
50 | return null;
|
---|
51 | }
|
---|
52 | NodeList elems = n.getChildNodes();
|
---|
53 |
|
---|
54 | int numelems = elems.getLength();
|
---|
55 | String []ids = new String[numelems];
|
---|
56 | for (int i=0; i<numelems; i++) {
|
---|
57 | Element e = (Element)elems.item(i);
|
---|
58 | String id = e.getAttribute("name");
|
---|
59 | ids[i] = id;
|
---|
60 | }
|
---|
61 |
|
---|
62 | return ids;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** takes a paramList element, and gets a HashMap of name-value pairs */
|
---|
66 | public static HashMap extractParams(Element xml) {
|
---|
67 |
|
---|
68 | if (!xml.getNodeName().equals("paramList")) {
|
---|
69 | System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
|
---|
70 | return null;
|
---|
71 | }
|
---|
72 | NodeList params = xml.getChildNodes();
|
---|
73 | HashMap param_map = new HashMap();
|
---|
74 | for (int i=0; i<params.getLength(); i++) {
|
---|
75 | Element param = (Element)params.item(i);
|
---|
76 | String name=param.getAttribute("name");
|
---|
77 | String value=param.getAttribute("value");
|
---|
78 | if (value.equals("")) { // the value is in the content of the param
|
---|
79 | value=getNodeText(param);
|
---|
80 | }
|
---|
81 | param_map.put(name, value);
|
---|
82 |
|
---|
83 | }
|
---|
84 | return param_map;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** extracts the text out of a node */
|
---|
88 | public static String getNodeText(Element param) {
|
---|
89 | param.normalize();
|
---|
90 | Node n = param.getFirstChild();
|
---|
91 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
92 | n=n.getNextSibling();
|
---|
93 | }
|
---|
94 | if (n==null) { // no text node
|
---|
95 | return "";
|
---|
96 | }
|
---|
97 | return n.getNodeValue();
|
---|
98 | }
|
---|
99 | /** creates a new document Element */
|
---|
100 | public static Element createResourceElement(Document owner, String oid) {
|
---|
101 | Element e = owner.createElement("resource");
|
---|
102 | e.setAttribute("name", oid);
|
---|
103 |
|
---|
104 | return e;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** add text to a document/subsection element */
|
---|
108 | public static boolean addDocText(Document owner, Element doc, String text) {
|
---|
109 | Element content = owner.createElement("content");
|
---|
110 | Text t = owner.createTextNode(text);
|
---|
111 | content.appendChild(t);
|
---|
112 | doc.appendChild(content);
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 | /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
|
---|
116 | public static Element addMetaList(Document owner, Element doc) {
|
---|
117 | Element list = owner.createElement("metadataList");
|
---|
118 | doc.appendChild(list);
|
---|
119 | return list;
|
---|
120 | }
|
---|
121 | /** adds a metadata elem to a list */
|
---|
122 | public static boolean addMetadata(Document owner, Element list,
|
---|
123 | String meta_name, String meta_value) {
|
---|
124 | if (meta_value==null || meta_value.equals("")) {
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 | Element data = owner.createElement("metadata");
|
---|
128 | data.setAttribute("name", meta_name);
|
---|
129 | Text t = owner.createTextNode(meta_value);
|
---|
130 | data.appendChild(t);
|
---|
131 | list.appendChild(data);
|
---|
132 | return true;
|
---|
133 |
|
---|
134 | }
|
---|
135 | }
|
---|