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

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

made all the xml elem names etc constants

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