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

Last change on this file since 3571 was 3512, checked in by kjdon, 22 years ago

all modules now talk the same messages; all xml elems and atts have been made constants and moved to GSXML.java; SOAPCommunicator can be used outside a MessageRouter

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 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
11/** various functions for extracting info out of GS XML */
12public class GSXML {
13
14 // greenstone xml elements
15 public static final String MESSAGE_ELEM = "message";
16 public static final String REQUEST_ELEM = "request";
17 public static final String RESPONSE_ELEM = "response";
18 public static final String COLLECTION_ELEM = "collection";
19 public static final String SERVICE_ELEM = "service";
20 public static final String CLUSTER_ELEM = "serviceCluster";
21 public static final String SITE_ELEM = "site";
22 public static final String PARAM_ELEM = "param";
23 public static final String PARAM_OPTION_ELEM = "option";
24 public static final String CONTENT_ELEM = "content";
25 public static final String RESOURCE_ELEM = "resource";
26 public static final String METADATA_ELEM = "metadata";
27 public static final String SERVICE_IMPL_ELEM = "servicesImpl";
28 public static final String CLASSIFIER_ELEM = "classifier";
29 public static final String APPLET_ELEM = "applet";
30 public static final String APPLET_DATA_ELEM = "appletData";
31 public static final String CONFIGURE_ELEM = "configure";
32 public static final String STATUS_ELEM = "status";
33
34 // elems for the pages to be processed by xslt
35 public final static String PAGE_ELEM = "page";
36 public final static String TRANSLATION_ELEM = "translate";
37 public final static String CONFIGURATION_ELEM = "config";
38 public final static String DESCRIPTION_ELEM = "description";
39
40 public static final String SITE_NAME_ELEM = "localSiteName";
41 // add on to another elem type to get a list of that type
42 public static final String LIST_MODIFIER = "List";
43
44 //public static final String
45 //public static final String
46
47 // greenstone xml attributes
48 public static final String NAME_ATT = "name";
49 public static final String TO_ATT = "to";
50 public static final String FROM_ATT = "from";
51 public static final String LANG_ATT = "lang";
52 public static final String TYPE_ATT = "type";
53 public static final String VALUE_ATT = "value";
54 public static final String DEFAULT_ATT = "default";
55 public static final String INFO_ATT = "info";
56 public static final String ACTION_ATT = "action";
57 public static final String SUBACTION_ATT = "subaction";
58 public static final String ADDRESS_ATT = "address";
59
60 // parameter types
61 public static final String PARAM_TYPE_INTEGER = "integer";
62 public static final String PARAM_TYPE_BOOLEAN = "boolean";
63 public static final String PARAM_TYPE_ENUM = "enum";
64 public static final String PARAM_TYPE_INPUT = "input";
65
66 // request types
67 public static final String REQUEST_TYPE_DESCRIBE = "describe";
68 public static final String REQUEST_TYPE_CONFIGURE = "configure";
69 public static final String REQUEST_TYPE_QUERY = "query";
70 public static final String REQUEST_TYPE_ACTION = "action";
71 public static final String REQUEST_TYPE_BUILD = "build";
72
73 // service types
74 public static final String SERVICE_TYPE_QUERY = "query";
75 public static final String SERVICE_TYPE_BUILD = "build";
76
77 // configure types
78 public static final String CONFIG_ACTION_ACTIVATE = "activate";
79 public static final String CONFIG_ACTION_DEACTIVATE = "deactivate";
80
81 // communicator types
82 public static final String COMM_TYPE_SOAP_JAVA = "soap";
83
84 // takes a node with a resource elements inside it and extracts all the
85 // HASh oids - name att for resource
86 public static String [] getResourceNameList(Element content) {
87
88 Node n = content.getFirstChild();
89 while (n!=null && !n.getNodeName().equals(RESOURCE_ELEM+LIST_MODIFIER)) {
90 n = n.getNextSibling();
91 }
92 if (n==null) { // no docs found
93 return null;
94 }
95
96 NodeList docs = n.getChildNodes();
97
98 int numdocs = docs.getLength();
99 String []ids = new String[numdocs];
100 for (int i=0; i<numdocs; i++) {
101 Element e = (Element)docs.item(i);
102 String id = e.getAttribute(NAME_ATT);
103 // check that its a valid id - ie starts with HASH
104 // need to change this if use different ids
105
106 ids[i] = id;
107
108 }
109
110 return ids;
111 }
112
113 /** extracts metadata names out of an element */
114 public static String [] getMetaNameList(Element content) {
115 Node n = content.getFirstChild();
116 while (n!=null &&
117 !n.getNodeName().equals(METADATA_ELEM+LIST_MODIFIER)) {
118 n = n.getNextSibling();
119 }
120 if (n==null) { // no metadatas found
121 return null;
122 }
123 NodeList elems = n.getChildNodes();
124
125 int numelems = elems.getLength();
126 String []ids = new String[numelems];
127 for (int i=0; i<numelems; i++) {
128 Element e = (Element)elems.item(i);
129 String id = e.getAttribute(NAME_ATT);
130 ids[i] = id;
131 }
132
133 return ids;
134 }
135
136 /** takes a paramList element, and gets a HashMap of name-value pairs */
137 public static HashMap extractParams(Element xml) {
138
139 if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) {
140 System.err.println("GSXML:paramList element should have been passed to extractParams, instead it was "+xml.getNodeName());
141 return null;
142 }
143 NodeList params = xml.getChildNodes();
144 HashMap param_map = new HashMap();
145 for (int i=0; i<params.getLength(); i++) {
146 Element param = (Element)params.item(i);
147 String name=param.getAttribute(NAME_ATT);
148 String value=param.getAttribute(VALUE_ATT);
149 if (value.equals("")) { // the value is in the content of the param
150 value=getNodeText(param);
151 }
152 param_map.put(name, value);
153
154 }
155 return param_map;
156 }
157
158 public static String getValue(Element e) {
159 String val = e.getAttribute(VALUE_ATT);
160 if (val ==null || val.equals("")) {
161 // have to get it out of the text
162 val=getNodeText(e);
163
164 }
165 return val;
166 }
167 /** extracts the text out of a node */
168 public static String getNodeText(Element param) {
169 param.normalize();
170 Node n = param.getFirstChild();
171 while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
172 n=n.getNextSibling();
173 }
174 if (n==null) { // no text node
175 return "";
176 }
177 return n.getNodeValue();
178 }
179 /** creates a new document Element */
180 public static Element createResourceElement(Document owner, String oid) {
181 Element e = owner.createElement(RESOURCE_ELEM);
182 e.setAttribute(NAME_ATT, oid);
183
184 return e;
185 }
186
187 /** add text to a document/subsection element */
188 public static boolean addDocText(Document owner, Element doc, String text) {
189
190 Element content = owner.createElement(CONTENT_ELEM);
191 Text t = owner.createTextNode(text);
192 content.appendChild(t);
193 doc.appendChild(content);
194 return true;
195 }
196 /** adds an empty MetadataList elem to a doc, and returns a ref to it*/
197 public static Element addMetaList(Document owner, Element doc) {
198 Element list = owner.createElement(METADATA_ELEM+LIST_MODIFIER);
199 doc.appendChild(list);
200 return list;
201 }
202 /** adds a metadata elem to a list */
203 public static boolean addMetadata(Document owner, Element list,
204 String meta_name, String meta_value) {
205 if (meta_value==null || meta_value.equals("")) {
206 return false;
207 }
208 Element data = owner.createElement(METADATA_ELEM);
209 data.setAttribute(NAME_ATT, meta_name);
210 Text t = owner.createTextNode(meta_value);
211 data.appendChild(t);
212 list.appendChild(data);
213 return true;
214
215 }
216
217 public static boolean mergeMetadataLists(Node to, Node from) {
218 Node to_meta = getChildByTagName(to, METADATA_ELEM+LIST_MODIFIER);
219 Node from_meta = getChildByTagName(from, METADATA_ELEM+LIST_MODIFIER);
220
221 if (from_meta == null) { // nothing to copy
222 return true;
223 }
224 Document to_owner = to.getOwnerDocument();
225 Node new_from = to_owner.importNode(from_meta, true);
226
227 if (to_meta == null) { // just copy the whole list
228 to.appendChild(new_from);
229 return true;
230 }
231
232 // copy individual elements
233 Node child = new_from.getFirstChild();
234 while ( child != null) {
235 to_meta.appendChild(child);
236 child = child.getNextSibling();
237 }
238 return true;
239 }
240
241 /** takes two nodes - should have the same node type, tag name, etc
242 * copies the info from 'from' into 'to'
243 -- not finished haven't thought through the algo yet.*/
244 /*
245 public static boolean mergeElements(Element to, Element from) {
246
247 if (to.getNodeName()!=from.getNodeName()) {
248 System.err.println("cant merge two nodes with different names");
249 return false;
250 }
251
252 // copy any atts over - ignore for now
253
254 // copy the children
255 if (!from.hasChildNodes()) {
256 return true;
257 }
258
259 // check if they belong to the same document
260 Document to_doc = to.getOwnerDocument();
261 Document from_doc = from.getOwnerDocument();
262 Element newfrom;
263 if (to_doc != from_doc) {
264 nfrom = to_doc.importNode(from, true);
265 } else {
266 newfrom = from;
267 }
268
269 if (!to.hasChildNodes()) {
270 // just copy all the children over
271 Node child = newfrom.getFirstChild();
272 while (child !=null) {
273 to.appendChild(child);
274 child = child.getNextSibling();
275 }
276 return true;
277 }
278
279 // have to check each one to see if already present or not
280 HashMap children = getChildrenMap(to);
281
282 Node child = newfrom.getFirstChild();
283 while (child !=null) {
284 String name = child.getNodeName();
285 Node n = map.get(name);
286 if (n==null) { // there is no elem by that name in to
287 to.appendChild(child);
288 }
289 else {
290 }
291 return true;
292 }
293 */
294 /** returns the (first) child element with the given name */
295 public static Node getChildByTagName(Node n, String name) {
296
297 Node child = n.getFirstChild();
298 while (child!=null) {
299 if (child.getNodeName().equals(name)) {
300 return child;
301 }
302 child = child.getNextSibling();
303 }
304 return null; //not found
305 }
306
307 /** takes an xpath type expression of the form name/name/...
308 and returns the first node that matches, or null if not found */
309 public static Node getNodeByPath(Node n, String path) {
310
311 String link = GSPath.getFirstLink(path);
312 path = GSPath.removeFirstLink(path);
313 while (!link.equals("")) {
314 n = getChildByTagName(n, link);
315 if (n==null) {
316 return null;
317 }
318 link = GSPath.getFirstLink(path);
319 path = GSPath.removeFirstLink(path);
320 }
321 return n;
322 }
323 public static HashMap getChildrenMap(Node n) {
324
325 HashMap map= new HashMap();
326 Node child = n.getFirstChild();
327 while (child!=null) {
328 String name = child.getNodeName();
329 map.put(name, child);
330 child = child.getNextSibling();
331 }
332 return map;
333 }
334
335 public static Element createTextElement(Document owner, String elem_name,
336 String text) {
337 Element e = owner.createElement(elem_name);
338 Text t = owner.createTextNode(text);
339 e.appendChild(t);
340 return e;
341
342 }
343
344
345 public static Element createParameter(Document owner, String param_name,
346 String type, String default_value,
347 String []values) {
348
349
350 Element p = owner.createElement(PARAM_ELEM);
351 p.setAttribute(NAME_ATT, param_name);
352 p.setAttribute(TYPE_ATT, type);
353 if (default_value != null) {
354 p.setAttribute(DEFAULT_ATT, default_value);
355 }
356 if (type.equals(PARAM_TYPE_ENUM) && values!=null) {
357 for (int i=0; i<values.length; i++) {
358 Element e = owner.createElement(PARAM_OPTION_ELEM);
359 e.setAttribute(NAME_ATT, values[i]);
360 p.appendChild(e);
361 }
362 }
363 return p;
364 }
365
366 /** returns the element parent/node_name[@attribute_name='attribute_value']
367 */
368 public static Element getNamedElement(Element parent, String node_name,
369 String attribute_name,
370 String attribute_value) {
371
372 NodeList children = parent.getChildNodes();
373 for (int i=0; i<children.getLength(); i++) {
374 Node child = children.item(i);
375 if (child.getNodeName().equals(node_name)) {
376 if (((Element)child).getAttribute(attribute_name).equals(attribute_value))
377 return (Element)child;
378 }
379 }
380 // not found
381 return null;
382 }
383}
Note: See TracBrowser for help on using the repository browser.