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 | public static final String PARAM_TYPE_INTEGER = "integer";
|
---|
15 | public static final String PARAM_TYPE_BOOLEAN = "boolean";
|
---|
16 | public static final String PARAM_TYPE_ENUM = "enum";
|
---|
17 | // takes a node with a resource elements inside it and extracts all the
|
---|
18 | // HASh oids - name att for resource
|
---|
19 | public static String [] getResourceNameList(Element content) {
|
---|
20 |
|
---|
21 | Node n = content.getFirstChild();
|
---|
22 | while (n!=null && !n.getNodeName().equals("resourceList")) {
|
---|
23 | n = n.getNextSibling();
|
---|
24 | }
|
---|
25 | if (n==null) { // no docs found
|
---|
26 | return null;
|
---|
27 | }
|
---|
28 |
|
---|
29 | NodeList docs = n.getChildNodes();
|
---|
30 |
|
---|
31 | int numdocs = docs.getLength();
|
---|
32 | String []ids = new String[numdocs];
|
---|
33 | for (int i=0; i<numdocs; i++) {
|
---|
34 | Element e = (Element)docs.item(i);
|
---|
35 | String id = e.getAttribute("name");
|
---|
36 | // check that its a valid id - ie starts with HASH
|
---|
37 | // need to change this if use different ids
|
---|
38 |
|
---|
39 | ids[i] = id;
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | return ids;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /** extracts metadata names out of an element */
|
---|
47 | public static String [] getMetaNameList(Element content) {
|
---|
48 | Node n = content.getFirstChild();
|
---|
49 | while (n!=null && !n.getNodeName().equals("metadataList")) {
|
---|
50 | n = n.getNextSibling();
|
---|
51 | }
|
---|
52 | if (n==null) { // no metadatas found
|
---|
53 | return null;
|
---|
54 | }
|
---|
55 | NodeList elems = n.getChildNodes();
|
---|
56 |
|
---|
57 | int numelems = elems.getLength();
|
---|
58 | String []ids = new String[numelems];
|
---|
59 | for (int i=0; i<numelems; i++) {
|
---|
60 | Element e = (Element)elems.item(i);
|
---|
61 | String id = e.getAttribute("name");
|
---|
62 | ids[i] = id;
|
---|
63 | }
|
---|
64 |
|
---|
65 | return ids;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** takes a paramList element, and gets a HashMap of name-value pairs */
|
---|
69 | public static HashMap extractParams(Element xml) {
|
---|
70 |
|
---|
71 | if (!xml.getNodeName().equals("paramList")) {
|
---|
72 | System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 | NodeList params = xml.getChildNodes();
|
---|
76 | HashMap param_map = new HashMap();
|
---|
77 | for (int i=0; i<params.getLength(); i++) {
|
---|
78 | Element param = (Element)params.item(i);
|
---|
79 | String name=param.getAttribute("name");
|
---|
80 | String value=param.getAttribute("value");
|
---|
81 | if (value.equals("")) { // the value is in the content of the param
|
---|
82 | value=getNodeText(param);
|
---|
83 | }
|
---|
84 | param_map.put(name, value);
|
---|
85 |
|
---|
86 | }
|
---|
87 | return param_map;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public static String getValue(Element e) {
|
---|
91 | String val = e.getAttribute("value");
|
---|
92 | if (val ==null || val.equals("")) {
|
---|
93 | // have to get it out of the text
|
---|
94 | val=getNodeText(e);
|
---|
95 |
|
---|
96 | }
|
---|
97 | return val;
|
---|
98 | }
|
---|
99 | /** extracts the text out of a node */
|
---|
100 | public static String getNodeText(Element param) {
|
---|
101 | param.normalize();
|
---|
102 | Node n = param.getFirstChild();
|
---|
103 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
104 | n=n.getNextSibling();
|
---|
105 | }
|
---|
106 | if (n==null) { // no text node
|
---|
107 | return "";
|
---|
108 | }
|
---|
109 | return n.getNodeValue();
|
---|
110 | }
|
---|
111 | /** creates a new document Element */
|
---|
112 | public static Element createResourceElement(Document owner, String oid) {
|
---|
113 | Element e = owner.createElement("resource");
|
---|
114 | e.setAttribute("name", oid);
|
---|
115 |
|
---|
116 | return e;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** add text to a document/subsection element */
|
---|
120 | public static boolean addDocText(Document owner, Element doc, String text) {
|
---|
121 |
|
---|
122 | Element content = owner.createElement("content");
|
---|
123 | Text t = owner.createTextNode(text);
|
---|
124 | content.appendChild(t);
|
---|
125 | doc.appendChild(content);
|
---|
126 | return true;
|
---|
127 | }
|
---|
128 | /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
|
---|
129 | public static Element addMetaList(Document owner, Element doc) {
|
---|
130 | Element list = owner.createElement("metadataList");
|
---|
131 | doc.appendChild(list);
|
---|
132 | return list;
|
---|
133 | }
|
---|
134 | /** adds a metadata elem to a list */
|
---|
135 | public static boolean addMetadata(Document owner, Element list,
|
---|
136 | String meta_name, String meta_value) {
|
---|
137 | if (meta_value==null || meta_value.equals("")) {
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 | Element data = owner.createElement("metadata");
|
---|
141 | data.setAttribute("name", meta_name);
|
---|
142 | Text t = owner.createTextNode(meta_value);
|
---|
143 | data.appendChild(t);
|
---|
144 | list.appendChild(data);
|
---|
145 | return true;
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|
149 | public static boolean mergeMetadataLists(Node to, Node from) {
|
---|
150 | Node to_meta = GSXML.getChildByTagName(to, "metadataList");
|
---|
151 | Node from_meta = getChildByTagName(from, "metadataList");
|
---|
152 |
|
---|
153 | if (from_meta == null) { // nothing to copy
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 | Document to_owner = to.getOwnerDocument();
|
---|
157 | Node new_from = to_owner.importNode(from_meta, true);
|
---|
158 |
|
---|
159 | if (to_meta == null) { // just copy the whole list
|
---|
160 | to.appendChild(new_from);
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // copy individual elements
|
---|
165 | Node child = new_from.getFirstChild();
|
---|
166 | while ( child != null) {
|
---|
167 | to_meta.appendChild(child);
|
---|
168 | child = child.getNextSibling();
|
---|
169 | }
|
---|
170 | return true;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** takes two nodes - should have the same node type, tag name, etc
|
---|
174 | * copies the info from 'from' into 'to'
|
---|
175 | -- not finished haven't thought through the algo yet.*/
|
---|
176 | /*
|
---|
177 | public static boolean mergeElements(Element to, Element from) {
|
---|
178 |
|
---|
179 | if (to.getNodeName()!=from.getNodeName()) {
|
---|
180 | System.err.println("cant merge two nodes with different names");
|
---|
181 | return false;
|
---|
182 | }
|
---|
183 |
|
---|
184 | // copy any atts over - ignore for now
|
---|
185 |
|
---|
186 | // copy the children
|
---|
187 | if (!from.hasChildNodes()) {
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 | // check if they belong to the same document
|
---|
192 | Document to_doc = to.getOwnerDocument();
|
---|
193 | Document from_doc = from.getOwnerDocument();
|
---|
194 | Element newfrom;
|
---|
195 | if (to_doc != from_doc) {
|
---|
196 | nfrom = to_doc.importNode(from, true);
|
---|
197 | } else {
|
---|
198 | newfrom = from;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (!to.hasChildNodes()) {
|
---|
202 | // just copy all the children over
|
---|
203 | Node child = newfrom.getFirstChild();
|
---|
204 | while (child !=null) {
|
---|
205 | to.appendChild(child);
|
---|
206 | child = child.getNextSibling();
|
---|
207 | }
|
---|
208 | return true;
|
---|
209 | }
|
---|
210 |
|
---|
211 | // have to check each one to see if already present or not
|
---|
212 | HashMap children = getChildrenMap(to);
|
---|
213 |
|
---|
214 | Node child = newfrom.getFirstChild();
|
---|
215 | while (child !=null) {
|
---|
216 | String name = child.getNodeName();
|
---|
217 | Node n = map.get(name);
|
---|
218 | if (n==null) { // there is no elem by that name in to
|
---|
219 | to.appendChild(child);
|
---|
220 | }
|
---|
221 | else {
|
---|
222 | }
|
---|
223 | return true;
|
---|
224 | }
|
---|
225 | */
|
---|
226 | /** returns the (first) child element with the given name */
|
---|
227 | public static Node getChildByTagName(Node n, String name) {
|
---|
228 |
|
---|
229 | Node child = n.getFirstChild();
|
---|
230 | while (child!=null) {
|
---|
231 | if (child.getNodeName().equals(name)) {
|
---|
232 | return child;
|
---|
233 | }
|
---|
234 | child = child.getNextSibling();
|
---|
235 | }
|
---|
236 | return null; //not found
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** takes an xpath type expression of the form name/name/...
|
---|
240 | and returns the first node that matches, or null if not found */
|
---|
241 | public static Node getNodeByPath(Node n, String path) {
|
---|
242 |
|
---|
243 | String link = GSPath.getFirstLink(path);
|
---|
244 | path = GSPath.removeFirstLink(path);
|
---|
245 | while (!link.equals("")) {
|
---|
246 | n = getChildByTagName(n, link);
|
---|
247 | if (n==null) {
|
---|
248 | return null;
|
---|
249 | }
|
---|
250 | link = GSPath.getFirstLink(path);
|
---|
251 | path = GSPath.removeFirstLink(path);
|
---|
252 | }
|
---|
253 | return n;
|
---|
254 | }
|
---|
255 | public static HashMap getChildrenMap(Node n) {
|
---|
256 |
|
---|
257 | HashMap map= new HashMap();
|
---|
258 | Node child = n.getFirstChild();
|
---|
259 | while (child!=null) {
|
---|
260 | String name = child.getNodeName();
|
---|
261 | map.put(name, child);
|
---|
262 | child = child.getNextSibling();
|
---|
263 | }
|
---|
264 | return map;
|
---|
265 | }
|
---|
266 |
|
---|
267 | public static Element createTextElement(Document owner, String elem_name,
|
---|
268 | String text) {
|
---|
269 | Element e = owner.createElement(elem_name);
|
---|
270 | Text t = owner.createTextNode(text);
|
---|
271 | e.appendChild(t);
|
---|
272 | return e;
|
---|
273 |
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | public static Element createParameter(Document owner, String param_name,
|
---|
278 | String type, String default_value,
|
---|
279 | String []values) {
|
---|
280 |
|
---|
281 |
|
---|
282 | Element p = owner.createElement("param");
|
---|
283 | p.setAttribute("name", param_name);
|
---|
284 | p.setAttribute("type", type);
|
---|
285 | p.setAttribute("default", default_value);
|
---|
286 |
|
---|
287 | if (type.equals(PARAM_TYPE_ENUM)) {
|
---|
288 | for (int i=0; i<values.length; i++) {
|
---|
289 | Element e = owner.createElement("element");
|
---|
290 | e.setAttribute("name", values[i]);
|
---|
291 | p.appendChild(e);
|
---|
292 | }
|
---|
293 | }
|
---|
294 | return p;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** returns the element parent/node_name[@attribute_name='attribute_value']
|
---|
298 | */
|
---|
299 | public static Element getNamedElement(Element parent, String node_name,
|
---|
300 | String attribute_name,
|
---|
301 | String attribute_value) {
|
---|
302 |
|
---|
303 | NodeList children = parent.getChildNodes();
|
---|
304 | for (int i=0; i<children.getLength(); i++) {
|
---|
305 | Node child = children.item(i);
|
---|
306 | if (child.getNodeName().equals(node_name)) {
|
---|
307 | if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
|
---|
308 | return (Element)child;
|
---|
309 | }
|
---|
310 | }
|
---|
311 | // not found
|
---|
312 | return null;
|
---|
313 | }
|
---|
314 | }
|
---|