source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java@ 3646

Last change on this file since 3646 was 3646, checked in by kjdon, 21 years ago

aadded a few more constants

  • Property svn:keywords set to Author Date Id Revision
File size: 15.4 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import org.w3c.dom.Node;
4import org.w3c.dom.Element;
5import org.w3c.dom.NodeList;
6import org.w3c.dom.Document;
7import org.w3c.dom.Text;
8
9import java.util.HashMap;
10//import java.util.Locale;
11
12/** various functions for extracting info out of GS XML */
13public class GSXML {
14
15 // greenstone xml elements
16 public static final String MESSAGE_ELEM = "message";
17 public static final String REQUEST_ELEM = "request";
18 public static final String RESPONSE_ELEM = "response";
19 public static final String COLLECTION_ELEM = "collection";
20 public static final String SERVICE_ELEM = "service";
21 public static final String CLUSTER_ELEM = "serviceCluster";
22 public static final String SITE_ELEM = "site";
23 public static final String PARAM_ELEM = "param";
24 public static final String PARAM_OPTION_ELEM = "option";
25 public static final String CONTENT_ELEM = "content";
26 public static final String RESOURCE_ELEM = "resource";
27 public static final String DOCUMENT_ELEM = "document";
28 public static final String METADATA_ELEM = "metadata";
29 public static final String SERVICE_CLASS_ELEM = "serviceRack";
30 public static final String CLASSIFIER_ELEM = "classifier";
31 public static final String APPLET_ELEM = "applet";
32 public static final String APPLET_DATA_ELEM = "appletData";
33 public static final String CONFIGURE_ELEM = "configure";
34 public static final String STATUS_ELEM = "status";
35 public static final String ERROR_ELEM = "error";
36 public static final String DEFAULT_ELEM = "default";
37 // elems for the pages to be processed by xslt
38 public final static String PAGE_ELEM = "page";
39 public final static String TRANSLATION_ELEM = "translate";
40 public final static String CONFIGURATION_ELEM = "config";
41 public final static String DESCRIPTION_ELEM = "description";
42
43 public static final String SITE_NAME_ELEM = "localSiteName";
44 // add on to another elem type to get a list of that type
45 public static final String LIST_MODIFIER = "List";
46
47 //public static final String
48 //public static final String
49
50 // greenstone xml attributes
51 public static final String NAME_ATT = "name";
52 public static final String TO_ATT = "to";
53 public static final String FROM_ATT = "from";
54 public static final String LANG_ATT = "lang";
55 public static final String TYPE_ATT = "type";
56 public static final String VALUE_ATT = "value";
57 public static final String DEFAULT_ATT = "default";
58 public static final String INFO_ATT = "info";
59 public static final String ACTION_ATT = "action";
60 public static final String SUBACTION_ATT = "subaction";
61 public static final String OUTPUT_ATT = "output";
62 public static final String ADDRESS_ATT = "address";
63 public static final String STATUS_ERROR_CODE_ATT = "code";
64 public static final String STATUS_PROCESS_ID_ATT = "handle";
65 public static final String PARAM_SHORTNAME_ATT = "shortname";
66 public static final String PARAM_IGNORE_POS_ATT = "ignore";
67
68 // parameter types
69 public static final String PARAM_TYPE_INTEGER = "integer";
70 public static final String PARAM_TYPE_BOOLEAN = "boolean";
71 public static final String PARAM_TYPE_ENUM = "enum";
72 public static final String PARAM_TYPE_ENUM_SINGLE = "enum_single";
73 public static final String PARAM_TYPE_ENUM_MULTI = "enum_multi";
74 public static final String PARAM_TYPE_STRING = "string";
75 public static final String PARAM_TYPE_MULTI = "multi";
76
77 // stuff for text strings
78 public static final String DISPLAY_ELEM = "display";
79 public static final String DISPLAY_NAME_ELEM = "name";
80 public static final String DISPLAY_SUBMIT_ELEM = "submit";
81
82 // request types - do we need all these? or just
83 // describe, status, and one to say do teh request
84 public static final String REQUEST_TYPE_DESCRIBE = "describe";
85 public static final String REQUEST_TYPE_CONFIGURE = "configure";
86 public static final String REQUEST_TYPE_QUERY = "query";
87 public static final String REQUEST_TYPE_ACTION = "action";
88 public static final String REQUEST_TYPE_BUILD = "build";
89 public static final String REQUEST_TYPE_STATUS = "status";
90 public static final String REQUEST_TYPE_PROCESS = "process";
91 public static final String REQUEST_TYPE_CGI = "cgi";
92
93 // service types
94 public static final String SERVICE_TYPE_QUERY = "query";
95 public static final String SERVICE_TYPE_BUILD = "build";
96
97 // configure types
98 public static final String CONFIG_ACTION_ACTIVATE = "activate";
99 public static final String CONFIG_ACTION_DEACTIVATE = "deactivate";
100
101 // communicator types
102 public static final String COMM_TYPE_SOAP_JAVA = "soap";
103
104 // takes a node with a resource elements inside it and extracts all the
105 // HASh oids - name att for resource
106 public static String [] getDocumentNameList(Element content) {
107
108 Node n = content.getFirstChild();
109 while (n!=null && !n.getNodeName().equals(DOCUMENT_ELEM+LIST_MODIFIER)) {
110 n = n.getNextSibling();
111 }
112 if (n==null) { // no docs found
113 return null;
114 }
115
116 NodeList docs = n.getChildNodes();
117
118 int numdocs = docs.getLength();
119 String []ids = new String[numdocs];
120 for (int i=0; i<numdocs; i++) {
121 Element e = (Element)docs.item(i);
122 String id = e.getAttribute(NAME_ATT);
123 // check that its a valid id - ie starts with HASH
124 // need to change this if use different ids
125
126 ids[i] = id;
127
128 }
129
130 return ids;
131 }
132
133 /** extracts metadata names out of an element */
134 public static String [] getMetaNameList(Element content) {
135 Node n = content.getFirstChild();
136 while (n!=null &&
137 !n.getNodeName().equals(METADATA_ELEM+LIST_MODIFIER)) {
138 n = n.getNextSibling();
139 }
140 if (n==null) { // no metadatas found
141 return null;
142 }
143 NodeList elems = n.getChildNodes();
144
145 int numelems = elems.getLength();
146 String []ids = new String[numelems];
147 for (int i=0; i<numelems; i++) {
148 Element e = (Element)elems.item(i);
149 String id = e.getAttribute(NAME_ATT);
150 ids[i] = id;
151 }
152
153 return ids;
154 }
155
156 /** takes a paramList element, and gets a HashMap of name-value pairs */
157 public static HashMap extractParams(Element xml) {
158
159 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
160 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
161 return null;
162 }
163 NodeList params = xml.getChildNodes();
164 HashMap param_map = new HashMap();
165 for (int i=0; i<params.getLength(); i++) {
166 Element param = (Element)params.item(i);
167 String name=param.getAttribute(NAME_ATT);
168 String value=param.getAttribute(VALUE_ATT);
169 if (value.equals("")) { // the value is in the content of the param
170 value=getNodeText(param);
171 }
172 param_map.put(name, value);
173
174 }
175 return param_map;
176 }
177 /** takes a paramList element, and gets a HashMap of name-value pairs */
178 public static HashMap extractAllParams(Element xml) {
179
180 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
181 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
182 return null;
183 }
184 NodeList params = xml.getElementsByTagName(PARAM_ELEM);
185 HashMap param_map = new HashMap();
186 for (int i=0; i<params.getLength(); i++) {
187 Element param = (Element)params.item(i);
188 String name=param.getAttribute(NAME_ATT);
189 String value=param.getAttribute(VALUE_ATT);
190 if (value.equals("")) { // the value is in the content of the param
191 value=getNodeText(param);
192 }
193 param_map.put(name, value);
194
195 }
196 return param_map;
197 }
198
199 public static String getValue(Element e) {
200 String val = e.getAttribute(VALUE_ATT);
201 if (val ==null || val.equals("")) {
202 // have to get it out of the text
203 val=getNodeText(e);
204
205 }
206 return val;
207 }
208 /** extracts the text out of a node */
209 public static String getNodeText(Element param) {
210 param.normalize();
211 Node n = param.getFirstChild();
212 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
213 n=n.getNextSibling();
214 }
215 if (n==null) { // no text node
216 return "";
217 }
218 return n.getNodeValue();
219 }
220 /** creates a new document Element */
221 public static Element createDocumentElement(Document owner, String oid) {
222 Element e = owner.createElement(DOCUMENT_ELEM);
223 e.setAttribute(NAME_ATT, oid);
224
225 return e;
226 }
227
228 /** add text to a document/subsection element */
229 public static boolean addDocText(Document owner, Element doc, String text) {
230
231 Element content = owner.createElement(CONTENT_ELEM);
232 Text t = owner.createTextNode(text);
233 content.appendChild(t);
234 doc.appendChild(content);
235 return true;
236 }
237
238 /** add an error message */
239 public static boolean addError(Document owner, Element doc, String text) {
240
241 Element content = owner.createElement(ERROR_ELEM);
242 Text t = owner.createTextNode(text);
243 content.appendChild(t);
244
245 return true;
246 }
247
248 /** add an error message */
249 public static boolean addError(Document owner, Element doc, Throwable error) {
250 error.printStackTrace();
251 return addError(owner, doc, error.toString());
252 }
253
254 /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
255 public static Element addMetaList(Document owner, Element doc) {
256 Element list = owner.createElement(METADATA_ELEM+LIST_MODIFIER);
257 doc.appendChild(list);
258 return list;
259 }
260 /** adds a metadata elem to a list */
261 public static boolean addMetadata(Document owner, Element list,
262 String meta_name, String meta_value) {
263 if (meta_value==null || meta_value.equals("")) {
264 return false;
265 }
266 Element data = owner.createElement(METADATA_ELEM);
267 data.setAttribute(NAME_ATT, meta_name);
268 Text t = owner.createTextNode(meta_value);
269 data.appendChild(t);
270 list.appendChild(data);
271 return true;
272
273 }
274
275 public static boolean mergeMetadataLists(Node to, Node from) {
276 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
277 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
278
279 if (from_meta == null) { // nothing to copy
280 return true;
281 }
282 Document to_owner = to.getOwnerDocument();
283 Node new_from = to_owner.importNode(from_meta, true);
284
285 if (to_meta == null) { // just copy the whole list
286 to.appendChild(new_from);
287 return true;
288 }
289
290 // copy individual elements
291 Node child = new_from.getFirstChild();
292 while ( child != null) {
293 to_meta.appendChild(child);
294 child = child.getNextSibling();
295 }
296 return true;
297 }
298
299 /** takes two nodes - should have the same node type, tag name, etc
300 * copies the info from 'from' into 'to'
301 -- not finished haven't thought through the algo yet.*/
302 /*
303 public static boolean mergeElements(Element to, Element from) {
304
305 if (to.getNodeName()!=from.getNodeName()) {
306 System.err.println("cant merge two nodes with different names");
307 return false;
308 }
309
310 // copy any atts over - ignore for now
311
312 // copy the children
313 if (!from.hasChildNodes()) {
314 return true;
315 }
316
317 // check if they belong to the same document
318 Document to_doc = to.getOwnerDocument();
319 Document from_doc = from.getOwnerDocument();
320 Element newfrom;
321 if (to_doc != from_doc) {
322 nfrom = to_doc.importNode(from, true);
323 } else {
324 newfrom = from;
325 }
326
327 if (!to.hasChildNodes()) {
328 // just copy all the children over
329 Node child = newfrom.getFirstChild();
330 while (child !=null) {
331 to.appendChild(child);
332 child = child.getNextSibling();
333 }
334 return true;
335 }
336
337 // have to check each one to see if already present or not
338 HashMap children = getChildrenMap(to);
339
340 Node child = newfrom.getFirstChild();
341 while (child !=null) {
342 String name = child.getNodeName();
343 Node n = map.get(name);
344 if (n==null) { // there is no elem by that name in to
345 to.appendChild(child);
346 }
347 else {
348 }
349 return true;
350 }
351 */
352 /** returns the (first) child element with the given name */
353 public static Node getChildByTagName(Node n, String name) {
354
355 Node child = n.getFirstChild();
356 while (child!=null) {
357 if (child.getNodeName().equals(name)) {
358 return child;
359 }
360 child = child.getNextSibling();
361 }
362 return null; //not found
363 }
364
365 /** takes an xpath type expression of the form name/name/...
366 and returns the first node that matches, or null if not found */
367 public static Node getNodeByPath(Node n, String path) {
368
369 String link = GSPath.getFirstLink(path);
370 path = GSPath.removeFirstLink(path);
371 while (!link.equals("")) {
372 n = getChildByTagName(n, link);
373 if (n==null) {
374 return null;
375 }
376 link = GSPath.getFirstLink(path);
377 path = GSPath.removeFirstLink(path);
378 }
379 return n;
380 }
381 public static HashMap getChildrenMap(Node n) {
382
383 HashMap map= new HashMap();
384 Node child = n.getFirstChild();
385 while (child!=null) {
386 String name = child.getNodeName();
387 map.put(name, child);
388 child = child.getNextSibling();
389 }
390 return map;
391 }
392
393 public static Element createTextElement(Document owner, String elem_name,
394 String text) {
395 Element e = owner.createElement(elem_name);
396 Text t = owner.createTextNode(text);
397 e.appendChild(t);
398 return e;
399
400 }
401
402
403 public static Element createParameter(Document owner, String name,
404 String type, String default_value,
405 String []options) {
406
407
408 Element p = owner.createElement(PARAM_ELEM);
409 p.setAttribute(NAME_ATT, name);
410 p.setAttribute(TYPE_ATT, type);
411 if (default_value != null) {
412 p.setAttribute(DEFAULT_ATT, default_value);
413 }
414 if (type.startsWith(PARAM_TYPE_ENUM) && options!=null) {
415 for (int i=0; i<options.length; i++) {
416 Element e = owner.createElement(PARAM_OPTION_ELEM);
417 e.setAttribute(NAME_ATT, options[i]);
418 p.appendChild(e);
419 }
420 }
421 return p;
422 }
423
424 /** creates a parm elem containing the text strings
425 * <param><name>xxx<name><option name='x'>yyy</option><option name='y'>zzz</option></param>
426 */
427 public static Element createParameterDisplay(Document owner, String name,
428 String name_text,
429 String []options,
430 String []option_texts) {
431
432
433 Element param = owner.createElement(PARAM_ELEM);
434 param.setAttribute(NAME_ATT, name);
435 param.appendChild(createTextElement(owner, DISPLAY_NAME_ELEM, name_text));
436 if (options != null) {
437 for (int i=0; i<options.length; i++) {
438 Element e = GSXML.createTextElement(owner, PARAM_OPTION_ELEM, option_texts[i]);
439 e.setAttribute(NAME_ATT, options[i]);
440 param.appendChild(e);
441 }
442 }
443
444 return param;
445 }
446
447 /** returns the element parent/node_name[@attribute_name='attribute_value']
448 */
449 public static Element getNamedElement(Element parent, String node_name,
450 String attribute_name,
451 String attribute_value) {
452
453 NodeList children = parent.getChildNodes();
454 for (int i=0; i<children.getLength(); i++) {
455 Node child = children.item(i);
456 if (child.getNodeName().equals(node_name)) {
457 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
458 return (Element)child;
459 }
460 }
461 // not found
462 return null;
463 }
464
465 // replaces < > " ' & in the original with their entities
466 public static String xmlSafe(String original) {
467
468 StringBuffer filtered = new StringBuffer(original.length());
469 char c;
470 for (int i=0; i<original.length(); i++) {
471 c = original.charAt(i);
472 if (c == '>') {
473 filtered.append("&gt;");
474 } else if (c == '<') {
475 filtered.append("&lt;");
476 } else if (c == '"') {
477 filtered.append("&quot;");
478 } else if (c == '&') {
479 filtered.append("&amp;");
480 } else if (c == '\'') {
481 filtered.append("&apos;");
482 } else {
483 filtered.append(c);
484 }
485 }
486 return filtered.toString();
487 }
488}
Note: See TracBrowser for help on using the repository browser.