1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | * CollectionData.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.NodeList;
|
---|
26 |
|
---|
27 | import java.util.Vector;
|
---|
28 | import java.util.HashMap;
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * CollectionData represents a Greenstone 3 collection and contains the data
|
---|
33 | * stored in a <collection></collection> element returned from a
|
---|
34 | * MessageRouter's desribe response XML and a Collection describe response XML.
|
---|
35 | * A CollectionData object can therefore contain services (represented by
|
---|
36 | * ServiceData), a list of collection-level metadata (represented by MetaData)
|
---|
37 | * and a list of plugins (represented by PluginData).
|
---|
38 | *
|
---|
39 | * ServiceData and PluginData are static inner classes of CollectionData.
|
---|
40 | * To declare variables of these inner class types in other java files/classes,
|
---|
41 | * import each one: e.g. "import gs3client.CollectionData.ServiceData".
|
---|
42 | * Then they can be used without the outerclass' name for qualification:
|
---|
43 | * i.e. can then use Servicedata sv = new ServiceData(...)
|
---|
44 | * instead of CollectionData.ServiceData sv = new
|
---|
45 | * CollectionData.ServiceData(...).
|
---|
46 | * @author ak19
|
---|
47 | */
|
---|
48 | public class CollectionData {
|
---|
49 | /** Stores the location of images and stuff. Not really used at present */
|
---|
50 | protected String url = "";
|
---|
51 |
|
---|
52 | /** The basic field of a collection: the name of the collection. This is
|
---|
53 | * present for collection elements in all describe response messages,
|
---|
54 | * whether sent from the MessageRouter or from Greenstone collections. */
|
---|
55 | public final String name;
|
---|
56 |
|
---|
57 | /* display items - these fields are set later with setFields() */
|
---|
58 | /** Display name for the Greenstone collection represented by this
|
---|
59 | * CollectionData */
|
---|
60 | protected String displayName;
|
---|
61 | /** Description of the Greenstone collection represented by this
|
---|
62 | * CollectionData */
|
---|
63 | protected String description;
|
---|
64 |
|
---|
65 | protected String smallIcon;
|
---|
66 | protected String icon;
|
---|
67 |
|
---|
68 | /** List of services supported by this collection */
|
---|
69 | protected ServiceData[] serviceList = null;
|
---|
70 | /** Collection level metadata */
|
---|
71 | protected MetaData[] metadataList = null;
|
---|
72 | /** Greenstone plugins used to build this collection with */
|
---|
73 | protected PluginData[] pluginList = null;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Gets a <collection></collection> element as returned by the
|
---|
77 | * <b>MessageRouter's</b> describe response, with only the name field set.
|
---|
78 | * @param mrCollectionTag the <collection></collection> element
|
---|
79 | * as returned by a describe response from the <b>MessageRouter</b>.
|
---|
80 | */
|
---|
81 | public CollectionData(Element mrCollectionTag){
|
---|
82 | // <collection name="NameValue"></collection> - retrieve NameValue first
|
---|
83 | // We'd definitely have a name attribute, but to be on the safe side:
|
---|
84 | this.name = mrCollectionTag.hasAttribute(GSXML.NAME_ATT) ?
|
---|
85 | mrCollectionTag.getAttribute(GSXML.NAME_ATT) : "";
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Gets a <collection></collection> element - as returned by a
|
---|
90 | * describe response from a <b>Collection</b> - and sets the member vars using
|
---|
91 | * the data in there.
|
---|
92 | * @param collectionTag is the <collection></collection> element
|
---|
93 | * as returned by a describe response from a <b>Collection</b>.
|
---|
94 | */
|
---|
95 | public void setFields(Element collectionTag) {
|
---|
96 | // Get the basic attributes: located in <displayItem> children
|
---|
97 | // of collectionTag:
|
---|
98 | HashMap map = ParseUtil.getElementValuesForAttr(collectionTag,
|
---|
99 | GSXML.DISPLAY_TEXT_ELEM, GSXML.DISPLAY_TEXT_NAME);
|
---|
100 | // Variable map now contains all pairs
|
---|
101 | // (name-attribute's value, displayItem's bodytext)
|
---|
102 | // extracted from <displayItem>s of the parent collectionTag element
|
---|
103 | String temp = (String)map.get("name");
|
---|
104 | this.displayName = (temp == null || temp.equals("")) ?
|
---|
105 | this.name : temp;
|
---|
106 | temp = (String)map.get("description");
|
---|
107 | this.description = (temp == null) ? "" : temp;
|
---|
108 | temp = (String)map.get("smallicon");
|
---|
109 | this.smallIcon = (temp == null) ? "" : temp;
|
---|
110 | temp = (String)map.get("icon");
|
---|
111 | this.icon = (temp == null) ? "" : temp;
|
---|
112 | map.clear();
|
---|
113 | map = null;
|
---|
114 |
|
---|
115 | // Now get the serviceList, metadataList, pluginList
|
---|
116 | // (1) Get all the <service></service> in <serviceList>
|
---|
117 | Vector v = ParseUtil.getListElementsAsArray(collectionTag,
|
---|
118 | GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER, GSXML.SERVICE_ELEM);
|
---|
119 | if(v != null) { // v contains individual <service></service> elements
|
---|
120 | serviceList = new ServiceData[v.size()];
|
---|
121 | for(int i = 0; i < serviceList.length; i++)
|
---|
122 | serviceList[i] = new ServiceData((Element)v.get(i));
|
---|
123 | v.clear();
|
---|
124 | }
|
---|
125 |
|
---|
126 | // (2) Get all the <metadata></metadata> elements in <metadataList>
|
---|
127 | v = ParseUtil.getListElementsAsArray(collectionTag,
|
---|
128 | GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER, GSXML.METADATA_ELEM);
|
---|
129 | if(v != null) { // v contains individual <service></service> elements
|
---|
130 | metadataList = new MetaData[v.size()];
|
---|
131 | for(int i = 0; i < metadataList.length; i++)
|
---|
132 | metadataList[i] = new MetaData((Element)v.get(i));
|
---|
133 | v.clear();
|
---|
134 | }
|
---|
135 |
|
---|
136 | // (3) Get all the <plugin></plugin> elements in <metadataList>
|
---|
137 | // Is it necessary for the Client to keep track of the plugins
|
---|
138 | // in any collection?
|
---|
139 |
|
---|
140 | v = ParseUtil.getListElementsAsArray(collectionTag,
|
---|
141 | GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER, GSXML.PLUGIN_ELEM);
|
---|
142 | if(v != null) { // v contains individual <service></service> elements
|
---|
143 | pluginList = new PluginData[v.size()];
|
---|
144 | for(int i = 0; i < pluginList.length; i++)
|
---|
145 | pluginList[i] = new PluginData((Element)v.get(i));
|
---|
146 | v.clear();
|
---|
147 | }
|
---|
148 | v = null;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Useful when adding CollectionData objects to a JList or JCombobox.
|
---|
152 | * @return the name of the collection represented by this CollectionData */
|
---|
153 | public String toString() {
|
---|
154 | // return (displayName==null) ? name : displayName;
|
---|
155 | return name;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** @return a String displaying the contents of this CollectionData object.
|
---|
159 | * Useful for debugging purposes.*/
|
---|
160 | public String show() {
|
---|
161 | StringBuffer buf = new StringBuffer("name: " + name);
|
---|
162 | buf.append(" -displayname: " + displayName);
|
---|
163 | buf.append(" -smallIcon: " + smallIcon);
|
---|
164 | buf.append(" -icon: " + icon);
|
---|
165 | buf.append(" -url: " + url);
|
---|
166 | buf.append("\nColl description: " + description + "\n");
|
---|
167 |
|
---|
168 | if(serviceList != null) {
|
---|
169 | buf.append("\n ServiceList:");
|
---|
170 | for(int i = 0; i < serviceList.length; i++) {
|
---|
171 | buf.append("\n service:");
|
---|
172 | buf.append(" " + serviceList[i].show());
|
---|
173 | }
|
---|
174 | }
|
---|
175 | if(metadataList != null) {
|
---|
176 | buf.append("\n MetadataList:");
|
---|
177 | for(int i = 0; i < metadataList.length; i++) {
|
---|
178 | buf.append("\n metadata:");
|
---|
179 | buf.append(" " + metadataList[i].show());
|
---|
180 | }
|
---|
181 | }
|
---|
182 | if(pluginList != null) {
|
---|
183 | buf.append("\n PluginList:");
|
---|
184 | for(int i = 0; i < pluginList.length; i++) {
|
---|
185 | buf.append("\n plugin:");
|
---|
186 | buf.append(" " + pluginList[i].show());
|
---|
187 | }
|
---|
188 | }
|
---|
189 | return buf.toString();
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** @return the Info string for the collection reprsented by this
|
---|
193 | * CollectionData object. */
|
---|
194 | public String info() { return displayName + "\n" + description; }
|
---|
195 |
|
---|
196 | /* accessor functions - to access the member fields of this CollectionData */
|
---|
197 | public String getName() { return name; }
|
---|
198 | public String getDisplayName() { return displayName; }
|
---|
199 | public String getDescription() { return description; }
|
---|
200 | public String getSmallIcon() { return smallIcon; }
|
---|
201 | public String getIcon() { return icon; }
|
---|
202 | public String getUrl(){ return url; }
|
---|
203 |
|
---|
204 | public MetaData[] getMetadataList() { return metadataList; }
|
---|
205 | public PluginData[] getPluginList() { return pluginList; }
|
---|
206 |
|
---|
207 | /** @return the list of services supported by this collection.
|
---|
208 | * @param executableServicesOnly - if true, only the list of supported query
|
---|
209 | * and browse services are returned. Otherwise, all the services in the
|
---|
210 | * collection are returned. */
|
---|
211 | public ServiceData[] getServiceList(boolean executableServicesOnly) {
|
---|
212 | if(executableServicesOnly) { // return only the query and browse services
|
---|
213 | ServiceData[] executableServices = null;
|
---|
214 | Vector v = new Vector(serviceList.length);
|
---|
215 | for(int i = 0; i < serviceList.length; i++) {
|
---|
216 | if(serviceList[i].type.equals(GSXML.SERVICE_TYPE_QUERY)
|
---|
217 | || serviceList[i].type.equals(GSXML.SERVICE_TYPE_BROWSE))
|
---|
218 | v.add(serviceList[i]);
|
---|
219 | }
|
---|
220 | executableServices = new ServiceData[v.size()];
|
---|
221 | v.toArray(executableServices);
|
---|
222 | return executableServices;
|
---|
223 | } else // all services
|
---|
224 | return serviceList;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Only the name and type of a <service></service> element is
|
---|
229 | * stored, since this is all the service data available inside a collection's
|
---|
230 | * response message to a describe request. And we are here dealing with
|
---|
231 | * just a CollectionData's service information (hence it's an inner class:
|
---|
232 | * CollectionData.ServiceData).
|
---|
233 | */
|
---|
234 | public static class ServiceData {
|
---|
235 | /** name of the Greenstone service represented by this ServiceData object */
|
---|
236 | public final String name;
|
---|
237 | /** type attribute of <service></service> element.
|
---|
238 | * type is optional and therefore can be empty str. */
|
---|
239 | public final String type;
|
---|
240 |
|
---|
241 | /**Gets a <service></service> element and sets its members using
|
---|
242 | * info extracted from it
|
---|
243 | * @param serviceTag is the <service></service> element from
|
---|
244 | * which this ServiceData object is constructed. */
|
---|
245 | public ServiceData(Element serviceTag){
|
---|
246 | // we'll surely have a name attribute, but to be on the safe side:
|
---|
247 | this.name = serviceTag.hasAttribute(GSXML.NAME_ATT) ?
|
---|
248 | serviceTag.getAttribute(GSXML.NAME_ATT) : "";
|
---|
249 | // Look for any lang attributes
|
---|
250 | this.type = serviceTag.hasAttribute(GSXML.TYPE_ATT) ?
|
---|
251 | serviceTag.getAttribute(GSXML.TYPE_ATT) : "";
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** Useful when adding ServiceData objects to a JList or JCombobox
|
---|
255 | * @return the name of this ServiceData, this corresponds to the name of
|
---|
256 | * the Greenstone Service that this object represents. */
|
---|
257 | public String toString() { return name; }
|
---|
258 |
|
---|
259 | /** @return a String displaying the contents of this ServiceData object.
|
---|
260 | * Useful for debugging purposes.*/
|
---|
261 | public String show() {
|
---|
262 | StringBuffer buf = new StringBuffer("name: " + name);
|
---|
263 | if(!type.equals(""))
|
---|
264 | buf.append(" -type: " + type);
|
---|
265 | buf.append("\n");
|
---|
266 | return buf.toString();
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | /** Static inner class MetaData: can import it into other files as
|
---|
271 | * "import gs3client.CollectionData.MetaData;"
|
---|
272 | * In this way, can use it as if it were a regular class (a.o.t. an inner
|
---|
273 | * class). That is, don't need to prepend the outer-classname if imported
|
---|
274 | * like this.
|
---|
275 | * gs3client.MetaData, from which this class inherits, has name, value and
|
---|
276 | * bodyText fields. This class adds an additional language field that is
|
---|
277 | * sometimes present in a Collection's metadata information. So we include
|
---|
278 | * the language as a member variable here in case we ever need it.
|
---|
279 | */
|
---|
280 | public static class MetaData extends org.greenstone.gs3client.data.MetaData{
|
---|
281 | public final String language;
|
---|
282 |
|
---|
283 | /** Given a <metadata></metadata> element, constructs a
|
---|
284 | * CollectionData sets its members using info extracted from it
|
---|
285 | * @param metadataTag is the the <metadata></metadata> element
|
---|
286 | * whose data is used to construct this MetaData object. */
|
---|
287 | public MetaData(Element metadataTag){
|
---|
288 | super(metadataTag);
|
---|
289 | // Look for any lang attributes
|
---|
290 | this.language = metadataTag.hasAttribute("lang") ?
|
---|
291 | metadataTag.getAttribute("lang") : "";
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** @return a String displaying the contents of this MetaData object.
|
---|
295 | * Useful for debugging purposes.*/
|
---|
296 | public String show() {
|
---|
297 | StringBuffer buf = new StringBuffer(super.show());
|
---|
298 | if(!language.equals(""))
|
---|
299 | buf.append(" -lang: " + language);
|
---|
300 | buf.append("\n");
|
---|
301 | return buf.toString();
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | /** PluginData inner class of CollectionData, represents the data stored in
|
---|
306 | * a <plugin></plugin> element. This class just stores the
|
---|
307 | * Plugin's name, as well as option subelements (in variable optionnames). */
|
---|
308 | public static class PluginData {
|
---|
309 | /** name of the plugin represented by this PluginData object */
|
---|
310 | public final String name;
|
---|
311 | /** A plugin's option subelements. Variable optionnames can be empty
|
---|
312 | * (it's an optional variable). */
|
---|
313 | public final String[] optionnames;
|
---|
314 |
|
---|
315 | /** Gets a <plugin></plugin> element and sets its members
|
---|
316 | * using info extracted from it.
|
---|
317 | * @param pluginTag is the the <plugin></plugin> element
|
---|
318 | * whose data is used to construct this PluginData object. */
|
---|
319 | public PluginData(Element pluginTag){
|
---|
320 | this.name = pluginTag.hasAttribute(GSXML.NAME_ATT) ?
|
---|
321 | pluginTag.getAttribute(GSXML.NAME_ATT) : "";
|
---|
322 | // Look for any option subElements
|
---|
323 | NodeList nl = pluginTag.getElementsByTagName("option");
|
---|
324 | if(nl == null || nl.getLength() == 0) optionnames = null;
|
---|
325 | else {
|
---|
326 | optionnames = new String[nl.getLength()];
|
---|
327 | for(int i = 0; i < optionnames.length; i++) {
|
---|
328 | Element option = (Element)nl.item(i);
|
---|
329 | optionnames[i] = option.getAttribute(GSXML.NAME_ATT);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | /** Useful when adding PluginData objects to a JList or JCombobox.
|
---|
335 | * @return the name of the plugin represented by this PluginData object. */
|
---|
336 | public String toString() { return name; }
|
---|
337 |
|
---|
338 | /** @return a String displaying the contents of this PluginData object.
|
---|
339 | * Useful for debugging purposes. */
|
---|
340 | public String show() {
|
---|
341 | StringBuffer buf = new StringBuffer("name: " + name);
|
---|
342 | if(optionnames != null){
|
---|
343 | buf.append("\n optionnames: ");
|
---|
344 | for(int i = 0; i < optionnames.length; i++)
|
---|
345 | buf.append(optionnames[i] + " ");
|
---|
346 | }
|
---|
347 | buf.append("\n");
|
---|
348 | return buf.toString();
|
---|
349 | }
|
---|
350 | }
|
---|
351 | } |
---|