source: trunk/gli/src/org/greenstone/gatherer/cdm/PlugIn.java@ 6540

Last change on this file since 6540 was 5863, checked in by jmt12, 21 years ago

The lists for selecting PlugIns and Classifiers now show the description of the aforementioned items if you hover over the combobox. If no description is a available it defaults to the appropriate message from the dictionary.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.cdm;
28
29import java.io.*;
30import java.util.*;
31import org.greenstone.gatherer.Gatherer;
32import org.greenstone.gatherer.cdm.Argument;
33import org.greenstone.gatherer.cdm.ArgumentContainer;
34import org.greenstone.gatherer.cdm.DOMProxyListEntry;
35import org.greenstone.gatherer.util.StaticStrings;
36import org.w3c.dom.*;
37
38/** This class is responsible for storing information from a parsed pluginfo call in such a way that it allows easy access to parsed details for the purposes of user design and specification of plugins. */
39public class PlugIn
40 extends ArrayList
41 implements ArgumentContainer, Comparable, DOMProxyListEntry, Serializable {
42 private boolean is_abstract = false; // Only for Base
43 /** The DOM Element this assigned PlugIn is modelled on. */
44 private Element element;
45 /** The parent PlugIn this one inherits from, if any. */
46 private PlugIn super_plugin; // Only for Base
47 private String description; // Only for Base
48 private String name; // Only for Base
49
50 /** Constructor used in DOMProxyListModel initializations, and Library Level.
51 */
52 public PlugIn() {
53 }
54
55 public PlugIn(Element element, PlugIn base_plugin) {
56 super();
57 this.element = element;
58 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
59 //Gatherer.println("Establishing Plugin: " + name);
60 // Parse in any argument options for this plugin, keeping a list of the ones found
61 HashMap known_arguments = new HashMap();
62 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
63 int option_elements_length = option_elements.getLength();
64 for(int i = 0; i < option_elements_length; i++) {
65 Element option_element = (Element) option_elements.item(i);
66 Argument argument = new Argument(option_element);
67 //Gatherer.println("Rebuilding existing argument: " + argument.getName());
68 argument.setOwner(name);
69 add(argument);
70 known_arguments.put(argument.getName(), argument);
71 }
72 // If a base plugin was given
73 if(base_plugin != null) {
74 //Gatherer.println("Based on previous plugin.");
75 // Copy the details, and add a reference to whatever base_plugins super plugin is.
76 description = base_plugin.getDescription();
77 // Now search through the 'dummy' arguments belonging to the base plugin. For each found, if it is already assigned, fill out further details such as type. If any are found that are not already assigned for this plugin, copy them and add them, but without a value.
78 ArrayList all_arguments = base_plugin.getArguments(true, true);
79 int argument_count = all_arguments.size();
80 for(int j = 0; j < argument_count; j++) {
81 Argument base_argument = (Argument) all_arguments.get(j);
82 String base_argument_name = base_argument.getName();
83 //Gatherer.println("Library indicates this plugin should have an argument: " + base_argument_name);
84 Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
85 // Found an existing argument. Complete its details
86 if(existing_argument != null) {
87 //Gatherer.println("Found existing argument. Filling out details.");
88 existing_argument.setCustomArgument(false);
89 existing_argument.setDefaultValue(base_argument.getDefaultValue());
90 existing_argument.setDescription(base_argument.getDescription());
91 existing_argument.setOptions(base_argument.getOptions());
92 existing_argument.setRequired(base_argument.isRequired());
93 existing_argument.setType(base_argument.getType());
94 }
95 // No existing argument. Copy base_argument and add it, but do not set its assigned flag. That should be set the first time its changed by the user.
96 else {
97 //Gatherer.println("No such argument. Adding new, unassigned, argument.");
98 // The trick thing is that we have to create a new element in the DOM as well.
99 Argument new_argument = base_argument.copy();
100 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
101 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
102 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
103 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.FALSE_STR);
104 new_argument.setElement(argument_element);
105 // All done. Add it.
106 element.appendChild(argument_element);
107 add(new_argument);
108 }
109 }
110 }
111 }
112
113 /** Method to add an argument to this base plugin. Only adds the argument if it isn't already present, and only if this is a base plugin (ie not based on DOM).
114 * @param argument the Argument to add
115 */
116 public void addArgument(Argument argument) {
117 if(element == null && !contains(argument)) {
118 add(argument);
119 argument.setOwner(name);
120 }
121 }
122
123 /** Method to compare two plugins for ordering.
124 * @param object The plugin we are comparing to, as an <strong>Object</strong>.
125 * @return An <i>int</i> specifying the plugin order, using values as set out in <strong>String</strong>.
126 * @see java.lang.String#compareTo
127 */
128 public int compareTo(Object object) {
129 if(object instanceof PlugIn) {
130 return name.compareTo(((PlugIn)object).getName());
131 }
132 return -1;
133 }
134
135 /** The assigned plugin constructor.
136 * @param element the DOM Element this plugin is based upon
137 * @param base_plugin the PlugIn from the stored library showing details about this plugin, may be null
138 */
139 public DOMProxyListEntry create(Element element) {
140 String plugin_name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
141 // Determine the base plugin from the plugin name
142 PlugIn base_plugin = CollectionDesignManager.plugin_manager.getBasePlugIn(plugin_name);
143 PlugIn plugin = new PlugIn(element, base_plugin);
144 base_plugin = null;
145 plugin_name = null;
146 return plugin;
147 }
148
149 /** Method to determine if two plugins are equal.
150 * @param object The plugin to test against, as an <strong>Object</strong>.
151 * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
152 */
153 public boolean equals(Object object) {
154 return (compareTo(object) == 0);
155 }
156
157 /** Method to retrieve an argument by its name.
158 * @param name The name of the argument as a <strong>String</strong>.
159 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
160 */
161 public Argument getArgument(String name) {
162 // The name given may still include the '-'
163 if(name.startsWith("-")) {
164 name = name.substring(1);
165 }
166 ArrayList arguments = getArguments(true, true);
167 for(int i = 0; i < arguments.size(); i++) {
168 Argument argument = (Argument)arguments.get(i);
169 if(argument.getName().equals(name)) {
170 return argument;
171 }
172 }
173 return null;
174 }
175
176 /** Retrieve all of the arguments available to this base plugin, including its super plugins arguments. Some complexity is added by allowing the caller to choose whether they want normal arguments, custom arguments, or both.
177 * @return an ArrayList of all of the arguments, starting with those for this plugin and ending with the arguments for basplug or similiar root plugin
178 */
179 public ArrayList getArguments(boolean include_normal, boolean include_custom) {
180 ArrayList arguments = new ArrayList();
181 if(include_normal && include_custom) {
182 arguments.addAll(this);
183 }
184 else {
185 int size = size();
186 for(int i = 0; i < size; i++) {
187 Argument argument = (Argument) get(i);
188 if(argument.isCustomArgument()) {
189 if(include_custom && !arguments.contains(argument)) {
190 arguments.add(argument);
191 }
192 }
193 else {
194 if(include_normal && !arguments.contains(argument)) {
195 arguments.add(argument);
196 }
197 }
198 argument = null;
199 }
200 }
201 if(super_plugin != null) {
202 ArrayList remainder = super_plugin.getArguments(include_normal, include_custom);
203 remainder.removeAll(arguments);
204 arguments.addAll(remainder);
205 }
206 return arguments;
207 }
208
209 /** Method to retrieve a plugins custom argument information. Custom arguments are defined to be those that have not got matching arguments in the base reference plugin from the library. Of course if there is no base plugin then all arguments are considered to be custom.
210 * @return the custom arguments as a String
211 */
212 public String getCustom() {
213 StringBuffer custom_text = new StringBuffer();
214 // Retrieve all of the arguments, and append any that are custom into one long string
215 ArrayList arguments = getArguments(false, true);
216 int arguments_size = arguments.size();
217 boolean first = true;
218 for(int i = 0; i < arguments_size; i++) {
219 Argument argument = (Argument) arguments.get(i);
220 if(argument.isAssigned()) {
221 if(!first) {
222 custom_text.append(" ");
223 }
224 custom_text.append(argument.toString());
225 first = false;
226 }
227 }
228 return custom_text.toString();
229 }
230
231 public String getDescription() {
232 return description;
233 }
234
235 public Element getElement() {
236 return element;
237 }
238
239 /** Method to retrieve a plugins name.
240 * @return A <strong>String</strong> containing the plugins name.
241 */
242 public String getName() {
243 if(name == null && element != null) {
244 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
245 }
246 return name;
247 }
248
249 public boolean isAbstract() {
250 return is_abstract;
251 }
252
253 public boolean isAssigned() {
254 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
255 }
256
257 public boolean isSeparator() {
258 return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
259 }
260
261 public void setAssigned(boolean assigned) {
262 if(element != null) {
263 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
264 }
265 }
266
267 /** Set the custom arguments. This turns out to be quite tricky. We must parse in the string, searching for arguments (for that we use a handy method in CollectionConfiguration). Next, for each argument, we check if we already know about it. If so we update its value, otherwise we create a new argument and assign it (must assign!).
268 * @param custom_str the custom arguments all splodged together in one String
269 */
270 public void setCustom(String custom_str) {
271 HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
272 ArrayList custom_arguments = getArguments(false, true);
273 int size = custom_arguments.size();
274 for(int i = 0; i < size; i++) {
275 Argument argument = (Argument) custom_arguments.get(i);
276 String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
277 if(raw_arguments.containsKey(original_argument_name)) {
278 // Set as assigned
279 argument.setAssigned(true);
280 String argument_value = (String)raw_arguments.remove(original_argument_name);
281 if(argument_value != null) {
282 argument.setValue(argument_value);
283 argument_value = null;
284 }
285 }
286 // We've removed it from our custom statement, so unassign
287 else {
288 argument.setAssigned(false);
289 }
290 argument = null;
291 }
292 // Any left over, add to the plugin
293 Iterator argument_names = raw_arguments.keySet().iterator();
294 while(argument_names.hasNext()) {
295 String argument_name = (String) argument_names.next();
296 String argument_value = (String) raw_arguments.get(argument_name);
297 // The tricky thing is that we have to create a new element in the DOM as well.
298 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
299 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
300 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
301 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
302 Argument argument = new Argument(argument_element);
303 argument_name = null;
304 if(argument_value != null) {
305 argument.setValue(argument_value);
306 argument_value = null;
307 }
308 // All done. Add it.
309 element.appendChild(argument_element);
310 add(argument);
311 argument_element = null;
312 }
313 raw_arguments = null;
314 }
315
316 /** Method to set the value of desc.
317 * @param desc The new value of desc as a <strong>String</strong>.
318 */
319 public void setDescription(String description) {
320 this.description = description;
321 }
322
323 public void setElement(Element element) {
324 this.element = element;
325 }
326
327 public void setIsAbstract(boolean is_abstract) {
328 this.is_abstract = is_abstract;
329 }
330
331 /** Method to set the value of name.
332 * @param name The new value of name as a <strong>String</strong>.
333 */
334 public void setName(String name) {
335 this.name = name;
336 }
337
338 /** Method to set the value of the super_plugin.
339 * @param super_plugin The new value of super_plugin as a <strong>PlugIn</strong>, or <i>null</i> if this class has no inheritance.
340 */
341 public void setSuper(PlugIn super_plugin) {
342 this.super_plugin = super_plugin;
343 }
344
345 /** Method to print out this plugin as it would appear as a command within the collection configuration file.
346 * @return A <strong>String</strong> containing a single plugin command.
347 */
348 public String toString() {
349 if(element != null) {
350 if(name == null) {
351 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
352 }
353 StringBuffer text = new StringBuffer(StaticStrings.PLUGIN_STR);
354 text.append(" ");
355 text.append(name);
356 text.append(" ");
357 ArrayList arguments = getArguments(true, true);
358 int arguments_size = arguments.size();
359 for(int i = 0; i < arguments_size; i++) {
360 Argument argument = (Argument)arguments.get(i);
361 if(argument.isAssigned()) {
362 text.append(argument.toString());
363 text.append(" ");
364 }
365 argument = null;
366 }
367 return text.substring(0, text.length() - 1);
368 }
369 // Basic Plugin
370 else {
371 return name;
372 }
373 }
374}
Note: See TracBrowser for help on using the repository browser.