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 |
|
---|
21 | package org.greenstone.gs3client.data;
|
---|
22 |
|
---|
23 | import org.greenstone.gsdl3.util.GSXML;
|
---|
24 | import org.w3c.dom.Element;
|
---|
25 | import org.w3c.dom.Node;
|
---|
26 | import 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 <param> and <option> elements
|
---|
34 | * that can appear in a <paramList> 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 <param> elements, but
|
---|
37 | * this class covers the data shared by <param> and <option> elements.
|
---|
38 | * @author ak19
|
---|
39 | */
|
---|
40 | public 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 <param>
|
---|
51 | * or <option> elements of a Query response XML message.
|
---|
52 | * @param param the <param> or <option> 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 <displayItem> element and returns this.
|
---|
63 | * @param param the <param> or <option> tag from which the data
|
---|
64 | * is extracted.
|
---|
65 | * @return the text content of the displayItem in the <param> 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 <param>
|
---|
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 (<param> or <option> 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 <param> tag */
|
---|
111 | /** type attribute of <param> tag */
|
---|
112 | public final String type;
|
---|
113 | /** default attribute of <param> 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 <param> tag specifies how often the
|
---|
118 | * widget occurs */
|
---|
119 | public final String occurs;
|
---|
120 | /** ignore attribute of <param> tag specifies whether this param
|
---|
121 | * will be ignored at ignore=pos */
|
---|
122 | public final String ignore;
|
---|
123 |
|
---|
124 | /** A <param> element in a Query response XML message can contain
|
---|
125 | * children that are <param> or <option>.
|
---|
126 | * subElements therefore stores an array of QueryFormData to represent
|
---|
127 | * these.
|
---|
128 | * <param> CAN (need not) contain <options>s and/or more
|
---|
129 | * <param>s. If subElements is null, it obviously has no child
|
---|
130 | * <param>s. */
|
---|
131 | protected QueryFormData[] subElements = null;
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Constructor that creates a QueryFormParam object from the data
|
---|
135 | * contained in a <param>element of a Query response XML message.
|
---|
136 | * @param param is an entire <param>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 | } |
---|