source: other-projects/trunk/gs3-webservices-democlient/src/GS3DemoClient/org/greenstone/gs3client/data/QueryFormData.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.0 KB
Line 
1/**
2 *#########################################################################
3 * QueryFormData.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 org.greenstone.gsdl3.util.GSXML;
24import org.w3c.dom.Element;
25import org.w3c.dom.Node;
26import org.w3c.dom.NodeList;
27
28// Currently in this file:
29// Two struct-like classes - QueryFormData and QueryFormParam (subclass of
30// QueryFormData) which is a static inner class of QueryFormData.
31
32/**
33 * Class QueryFormData represents the &lt;param&gt; and &lt;option&gt; elements
34 * that can appear in a &lt;paramList&gt; tag in the xml response returned by
35 * a describe request sent to *Query* Service (e.g. FieldQuery, TextQuery).Its
36 * subclass QueryFormParam more fully represents the &lt;param&gt; elements, but
37 * this class covers the data shared by &lt;param&gt; and &lt;option&gt; elements.
38 * @author ak19
39 */
40public class QueryFormData {
41 /** Constant for the "occurs" attribute name */
42 public static final String OCCURS_ATT = "occurs";
43 /** Name of the parameter or option this QueryFormData represents */
44 public final String name;
45 /** DisplayItem text content of the parameter or option this
46 * QueryFormData represents */
47 public final String displayItem;
48
49 /**
50 * Contructor to create a QueryFormData object from &lt;param&gt;
51 * or &lt;option&gt; elements of a Query response XML message.
52 * @param param the &lt;param&gt; or &lt;option&gt; tag for which a
53 * QueryFormData object is constructed, from the data the element contains.
54 * */
55 public QueryFormData(Element param) {
56 this.name = param.getAttribute(GSXML.NAME_ATT);
57 displayItem = extractDisplayItem(param);
58 }
59
60 /**
61 * Given an element, extracts the text value from the first direct child
62 * element that is a &lt;displayItem&gt; element and returns this.
63 * @param param the &lt;param&gt; or &lt;option&gt; tag from which the data
64 * is extracted.
65 * @return the text content of the displayItem in the &lt;param&gt; tag
66 */
67 protected String extractDisplayItem(Element param) {
68 // Now find the displayItem value for this Param element
69 // by cycling through childnodes of this element
70 NodeList nl = param.getChildNodes();
71 boolean found = false;
72 for(int i = 0; !found && i < nl.getLength(); i++){
73 Node n = nl.item(i);
74 if(n.getNodeType() != Node.ELEMENT_NODE)
75 continue;
76 // else
77 Element e = (Element)n;
78 if(e.getTagName().equals(GSXML.DISPLAY_TEXT_ELEM)) {
79 // get the value in the body of this <displayItem> element
80 if(e.hasChildNodes()) // some <displayItem> contain no text
81 return e.getFirstChild().getNodeValue().trim(); // text node
82 else return "";
83 }
84 }
85 return "";
86 }
87
88 /**
89 * @return this object's displayItem string. This is also what is
90 * displayed when this object is added as a list items into a
91 * JComboBoxes or JList.
92 */
93 public String toString() { return displayItem; }
94
95 /** @return a display String of the member values of this QueryFormData
96 * object for debugging purposes */
97 public String show() {
98 return "name: " + name + " displayItem: " + displayItem;
99 }
100
101 /**
102 * Static inner class QueryFormParam is a subclass of QueryFormData and
103 * uniquely represents the data that can be contained in any &lt;param&gt;
104 * tag of the xml response that's returned when a describe request is sent to
105 * a Query Service. As such, a QueryFormParam can contain further
106 * QueryFormData (&lt;param&gt; or &lt;option&gt; elements) which are stored
107 * as subElements of this QueryFormParam.
108 */
109 public static class QueryFormParam extends QueryFormData {
110 /* Members to store data specific to a &lt;param&gt; tag */
111 /** type attribute of &lt;param&gt; tag */
112 public final String type;
113 /** default attribute of &lt;param&gt; tag */
114 public final String def; // any default value
115
116 // Following fields set only for type=multi (else they're set to ""):
117 /** occurs attribute of &lt;param&gt; tag specifies how often the
118 * widget occurs */
119 public final String occurs;
120 /** ignore attribute of &lt;param&gt; tag specifies whether this param
121 * will be ignored at ignore=pos */
122 public final String ignore;
123
124 /** A &lt;param&gt; element in a Query response XML message can contain
125 * children that are &lt;param&gt; or &lt;option&gt;.
126 * subElements therefore stores an array of QueryFormData to represent
127 * these.
128 * &lt;param&gt; CAN (need not) contain &lt;options&gt;s and/or more
129 * &lt;param&gt;s. If subElements is null, it obviously has no child
130 * &lt;param&gt;s. */
131 protected QueryFormData[] subElements = null;
132
133 /**
134 * Constructor that creates a QueryFormParam object from the data
135 * contained in a &lt;param&gt;element of a Query response XML message.
136 * @param param is an entire &lt;param&gt;element - the one currently being
137 * processed.
138 */
139 public QueryFormParam(Element param) {
140 super(param);
141 this.type = param.getAttribute(GSXML.TYPE_ATT);
142
143 if(param.hasAttribute(GSXML.DEFAULT_ATT))
144 def = param.getAttribute(GSXML.DEFAULT_ATT);
145 else def = "";
146
147 if(param.hasAttribute(OCCURS_ATT)) // no GSXML constant defined for this
148 occurs = param.getAttribute(OCCURS_ATT);
149 else occurs = "";
150
151 if(param.hasAttribute(GSXML.PARAM_IGNORE_POS_ATT))
152 ignore = param.getAttribute(GSXML.PARAM_IGNORE_POS_ATT);
153 else ignore = "";
154 }
155
156 /** Read-only accessor for this QueryFormParam object's subElements
157 * @return an array of this QueryFormParam's children QueryFormData objects
158 * (representing options or params). Null is returned if there are none. */
159 public QueryFormData[] getSubElements() { return subElements; }
160
161 /** @return a display String of the member values of this QueryFormParam
162 * for debugging purposes */
163 public String show() {
164 StringBuffer buf = new StringBuffer(super.show());
165 buf.append(" type: ");
166 buf.append(type);
167 buf.append(" def: ");
168 buf.append(def.equals("") ? "-" : def);
169 buf.append(" occurs: ");
170 buf.append(occurs.equals("")? "-" : occurs);
171
172 if(subElements == null) {
173 buf.append("\n");
174 return buf.toString();
175 }
176 // else
177 for(int i = 0; i < subElements.length; i++) {
178 buf.append("\n ");
179 buf.append(subElements[i].show());
180 }
181 buf.append("\n");
182 return buf.toString();
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.