source: other-projects/trunk/gs3-webservices-democlient/src/GS3DemoClient/org/greenstone/gs3client/data/ParseUtil.java@ 15222

Last change on this file since 15222 was 15222, checked in by ak19, 16 years ago

Greenstone3 web services demo-clientadded to GS3's other-projects

File size: 7.2 KB
Line 
1/**
2 *#########################################################################
3 * ParseUtil.java - part of the demo-client for Greenstone 3, of the
4 * Greenstone digital library suite from the New Zealand Digital Library
5 * Project at the * University of Waikato, New Zealand.
6 * <BR><BR>
7 * Copyright (C) 2008 New Zealand Digital Library Project
8 * <BR><BR>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 * <BR><BR>
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *########################################################################
19 */
20
21package org.greenstone.gs3client.data;
22
23import java.util.HashMap;
24import java.util.Vector;
25
26import org.w3c.dom.Element;
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29
30
31// Perhaps can use GSXML.extractParams() for at least half of this and
32// GSXML.getChildByTagName() for the other?
33// -> No, extractParams() only extracts <ParamList> child elements <param>s.
34// Then it returns a hashmap of (name, value) pairs.
35// And getChildByTagName returns any Node, not the Element we want.
36/**
37 * Some useful, general response XML message parsing functions
38 * for the JavaClient's data classes.
39 * @author ak19
40*/
41public class ParseUtil {
42 /**
43 * @param tag is the XML element from which the body text value will
44 * be extracted.
45 * @return the bodyText nested within the tag element, if any. Otherwise, this
46 * method returns empty String. */
47 public static String getBodyTextValue(Element tag) {
48 Node textNode = tag.getFirstChild();
49 return (textNode == null) ? "" : textNode.getNodeValue();
50 }
51
52 /**
53 * @param el is the element whose child elements are returned.
54 * @param childrenName is the name of the child elements to be extracted and
55 * returned.
56 * @return a vector containing all child elements of parent that are
57 * called childrenName. Returns null if there are no such child elements.
58 */
59 public static Vector getAllChildElementsCalled(Element el,
60 String childrenName)
61 {
62 Vector v = null;
63 if(el.hasChildNodes()) {
64 NodeList nl = el.getChildNodes();
65 // Get only those of its direct children that are called
66 // <childrenName>
67 v = new Vector(nl.getLength());
68 for(int i = 0; i < nl.getLength(); i++) {
69 Node n = nl.item(i);
70 // For Element Nodes, getNodeName() returns the tag name,
71 // and it *never* returns null for *any* Node type
72 if(n.getNodeName().equals(childrenName)) {
73 v.add((Element)n);
74 }
75 }
76 }
77 return v;
78 }
79
80 /**
81 * Given an xml element ('parent'), it looks through its direct children
82 * to find the &lt;listElementName&gt; tag and extracts each child called
83 * &lt;childElementName&gt; element from it. These are all added into a Vector
84 * which is returned.
85 * @param parent is the XML element from which the specified descendant
86 * elements are identified and returned.
87 * @param listElementName is the name of the child element of parent that we
88 * are looking for, whose child elements will be returned.
89 * @return a vector of Element items, all children &lt;childelementName&gt; of
90 * the <b>first</b> &lt;listElementName&gt; child of parameter called parent.
91 */
92 public static Vector getListElementsAsArray(Element parent,
93 String listElementName, String childElementName)
94 {
95 NodeList nl = parent.getElementsByTagName(listElementName);
96 if(nl.getLength() > 0) {
97 // ensure <listELementName> has children, if so, get its children
98 Element eList = (Element)nl.item(0); // first <listElementName>
99 return getAllChildElementsCalled(eList, childElementName);
100 }
101 return null;
102 }
103
104 /**
105 * Method that returns the first child element of parent whose tagname is
106 * childElementName.
107 * @param parent is the element whose child element (if called
108 * childElementName) is returned.
109 * @param childElementName is the name of the child element to look for in
110 * parent.
111 * @return the first *child* element of &lt;parent&gt; that is
112 * &lt;childElementName&gt;. Null is returned if there are none.
113 */
114 public static Element getFirstChildElementCalled(Element parent,
115 String childElementName)
116 {
117 // find all <childElementName>s, children of parent
118 NodeList nl = parent.getChildNodes();
119 // Find and return first <childElementName>, if any
120 for(int i = 0; i < nl.getLength(); i++) {
121 Node n = nl.item(i);
122 if(n.getNodeName().equals(childElementName))
123 // now we know n is an Element *and* is the one we want
124 return (Element)n;
125 }
126 return null;
127 }
128
129 /**
130 * Method that returns the first descendant element of parent whose tagname
131 * is descElementName.
132 * @param parent is the element whose descendant element (if called
133 * descElementName) is returned.
134 * @param descElementName is the name of the descendant element to look for
135 * in parent.
136 * @return the first <b>descendant</b> element of &lt;parent&gt; that is
137 * &lt;descElementName&gt;. Null is returned if there are none.
138 */
139 public static Element getFirstDescElementCalled(Element parent,
140 String descElementName)
141 {
142 // find all <descElementName>s
143 NodeList nl = parent.getElementsByTagName(descElementName);
144 if(nl.getLength() > 0) // then return first <descElementName>
145 return (Element)nl.item(0); // we know it's an element
146 return null; // else
147 }
148
149 /**
150 * Given an Element &lt;parentEl&gt;, finds all direct child elements called
151 * &lt;elName&gt; that have an attribute named attrName:
152 * &lt;elName attrName=value&gt;bodytext&lt;/elName&gt;
153 * This is particularly useful for extracting info for those cases where
154 * elName=&lt;displayItem&gt; and where the attrName is "name".
155 * @param parentEl is the parent element whose children are searched
156 * @param elName is the name to search for among the child elements of parentEl
157 * @param attrName is the name of the attribute to look for in the child
158 * element (of parentEl) whose tag name is elName.
159 * @return a Hashmap of all the (value, body) instances found.
160 */
161 public static HashMap getElementValuesForAttr(Element parentEl,
162 String elName, String attrName)
163 {
164 HashMap map = new HashMap(10);
165 NodeList nl = parentEl.getChildNodes();
166 if(nl == null || nl.getLength() == 0)
167 return map;
168 for(int i = 0; i < nl.getLength(); i++) {
169 Node n = nl.item(i);
170 // Look for all <elName> children of <parentEl>
171 if(n.getNodeName().equals(elName)){ // processing all <elName>s
172 // Retrieve value of all attributes named attrName in
173 // <elName attrName=value>bodytext</elName>
174 // and the body of the element
175 Element e = (Element)n; // we now know it's an element and the
176 // one we want
177 if(e.hasAttribute(attrName)){
178 // Get the value for the attribute named attrName
179 // and put it as the key in a map entry
180 // The associated map entry's value is the *bodytext*
181 // of the element <elName>bodytext</elName>
182 String bodytext = e.hasChildNodes() ?
183 e.getFirstChild().getNodeValue() : "";
184 map.put(e.getAttribute(attrName), bodytext);
185 }
186 }
187 }
188 return map;
189 }
190}
Note: See TracBrowser for help on using the repository browser.