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

Last change on this file since 7111 was 7111, checked in by kjdon, 20 years ago

modified how the arguments get added to the list when creating a new plugin based on an old one. also arguments keep their original owner now unless they are assigned, so that the configure plugin display looks the same after list regeneration

  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 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. Used for Base plugins (those in the list of available plugins, not ones that are in the DOMProxyList)
51 */
52 public PlugIn() {
53 }
54
55 /** Constructor used for the plugins that are in the DOMProxyList */
56 // Every time the list of plugins in the assigned plugins box changes, (eg plugin added or removed, not when plugin configured), the plugins seem to be regenerated, using the element from the old plugin. All known args get added to the element the first time this happens - we need to add them to the arguments list in the order they are found in the base plugins though, not this order.
57 public PlugIn(Element element, PlugIn base_plugin) {
58 super();
59 this.element = element;
60 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
61 //Gatherer.println("Establishing Plugin: " + name);
62 // Parse in any argument options for this plugin, keeping a list of the ones found
63 HashMap known_arguments = new HashMap();
64 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
65 int option_elements_length = option_elements.getLength();
66 for(int i = 0; i < option_elements_length; i++) {
67 Element option_element = (Element) option_elements.item(i);
68 Argument argument = new Argument(option_element);
69 //Gatherer.println("Rebuilding existing argument: " + argument.getName());
70 known_arguments.put(argument.getName(), argument);
71 if (argument.isAssigned() || base_plugin == null) {
72 // if the arg is assigned, the current plugin now becomes its owner, and we add it to the front of the ArrayList of args. But otherwise, it will retain its old owner, and we will add it in the correct place from the base plugin
73 argument.setOwner(name);
74 add(argument);
75 }
76 }
77 // If a base plugin was given
78 if(base_plugin != null) {
79 //Gatherer.println("Based on previous plugin.");
80 // Copy the details, and add a reference to whatever base_plugins super plugin is. ??
81 description = base_plugin.getDescription();
82 // 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.
83 ArrayList all_arguments = base_plugin.getArguments(true, true);
84 int argument_count = all_arguments.size();
85 for(int j = 0; j < argument_count; j++) {
86 Argument base_argument = (Argument) all_arguments.get(j);
87 String base_argument_name = base_argument.getName();
88 //Gatherer.println("Library indicates this plugin should have an argument: " + base_argument_name);
89 Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
90 // Found an existing argument. Complete its details
91 if(existing_argument != null) {
92 //Gatherer.println("Found existing argument. Filling out details.");
93 existing_argument.setCustomArgument(false);
94 existing_argument.setDefaultValue(base_argument.getDefaultValue());
95 existing_argument.setDescription(base_argument.getDescription());
96 existing_argument.setOptions(base_argument.getOptions());
97 existing_argument.setRequired(base_argument.isRequired());
98 existing_argument.setType(base_argument.getType());
99 existing_argument.setMinimum(base_argument.getMinimum());
100 existing_argument.setMaximum(base_argument.getMaximum());
101 if (!existing_argument.isAssigned()) {
102 // here we give it back its original owner and add it to the list. If it is assigned, these two things were done already
103 existing_argument.setOwner(base_argument.getOwner());
104 add(existing_argument);
105 }
106
107 }
108 // 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.
109 else {
110 //Gatherer.println("No such argument. Adding new, unassigned, argument.");
111 // The trick thing is that we have to create a new element in the DOM as well.
112 Argument new_argument = base_argument.copy();
113 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
114 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
115 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
116 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.FALSE_STR);
117 new_argument.setElement(argument_element);
118 // All done. Add it.
119 element.appendChild(argument_element);
120 add(new_argument);
121 }
122 }
123 }
124 }
125
126 /** 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).
127 * @param argument the Argument to add
128 */
129 public void addArgument(Argument argument) {
130 if(element == null && !contains(argument)) {
131 add(argument);
132 argument.setOwner(name);
133 }
134 }
135
136 /** Method to compare two plugins for ordering.
137 * @param object The plugin we are comparing to, as an <strong>Object</strong>.
138 * @return An <i>int</i> specifying the plugin order, using values as set out in <strong>String</strong>.
139 * @see java.lang.String#compareTo
140 */
141 public int compareTo(Object object) {
142 if(object instanceof PlugIn) {
143 return name.compareTo(((PlugIn)object).getName());
144 }
145 return -1;
146 }
147
148 /** The assigned plugin constructor.
149 * @param element the DOM Element this plugin is based upon
150 */
151 public DOMProxyListEntry create(Element element) {
152 String plugin_name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
153 // Determine the base plugin from the plugin name
154 PlugIn base_plugin = CollectionDesignManager.plugin_manager.getBasePlugIn(plugin_name);
155 PlugIn plugin = new PlugIn(element, base_plugin);
156 base_plugin = null;
157 plugin_name = null;
158 return plugin;
159 }
160
161 /** Method to determine if two plugins are equal.
162 * @param object The plugin to test against, as an <strong>Object</strong>.
163 * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
164 */
165 public boolean equals(Object object) {
166 return (compareTo(object) == 0);
167 }
168
169 /** Method to retrieve an argument by its name.
170 * @param name The name of the argument as a <strong>String</strong>.
171 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
172 */
173 public Argument getArgument(String name) {
174 // The name given may still include the '-'
175 if(name.startsWith("-")) {
176 name = name.substring(1);
177 }
178 ArrayList arguments = getArguments(true, true);
179 for(int i = 0; i < arguments.size(); i++) {
180 Argument argument = (Argument)arguments.get(i);
181 if(argument.getName().equals(name)) {
182 return argument;
183 }
184 }
185 return null;
186 }
187
188 /** 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.
189 * @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
190 */
191 public ArrayList getArguments(boolean include_normal, boolean include_custom) {
192 ArrayList arguments = new ArrayList();
193 if(include_normal && include_custom) {
194 arguments.addAll(this);
195 }
196 else {
197 int size = size();
198 for(int i = 0; i < size; i++) {
199 Argument argument = (Argument) get(i);
200 if(argument.isCustomArgument()) {
201 if(include_custom && !arguments.contains(argument)) {
202 arguments.add(argument);
203 }
204 }
205 else {
206 if(include_normal && !arguments.contains(argument)) {
207 arguments.add(argument);
208 }
209 }
210 argument = null;
211 }
212 }
213 if(super_plugin != null) {
214 ArrayList remainder = super_plugin.getArguments(include_normal, include_custom);
215 remainder.removeAll(arguments);
216 arguments.addAll(remainder);
217 }
218 return arguments;
219 }
220
221 /** 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.
222 * @return the custom arguments as a String
223 */
224 public String getCustom() {
225 StringBuffer custom_text = new StringBuffer();
226 // Retrieve all of the arguments, and append any that are custom into one long string
227 ArrayList arguments = getArguments(false, true);
228 int arguments_size = arguments.size();
229 boolean first = true;
230 for(int i = 0; i < arguments_size; i++) {
231 Argument argument = (Argument) arguments.get(i);
232 if(argument.isAssigned()) {
233 if(!first) {
234 custom_text.append(" ");
235 }
236 custom_text.append(argument.toString());
237 first = false;
238 }
239 }
240 return custom_text.toString();
241 }
242
243 public String getDescription() {
244 return description;
245 }
246
247 public Element getElement() {
248 return element;
249 }
250
251 /** Method to retrieve a plugins name.
252 * @return A <strong>String</strong> containing the plugins name.
253 */
254 public String getName() {
255 if(name == null && element != null) {
256 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
257 }
258 return name;
259 }
260
261 public boolean isAbstract() {
262 return is_abstract;
263 }
264
265 public boolean isAssigned() {
266 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
267 }
268
269 public boolean isSeparator() {
270 return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
271 }
272
273 public void setAssigned(boolean assigned) {
274 if(element != null) {
275 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
276 }
277 }
278
279 /** 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!).
280 * @param custom_str the custom arguments all splodged together in one String
281 */
282 public void setCustom(String custom_str) {
283 HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
284 ArrayList custom_arguments = getArguments(false, true);
285 int size = custom_arguments.size();
286 for(int i = 0; i < size; i++) {
287 Argument argument = (Argument) custom_arguments.get(i);
288 String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
289 if(raw_arguments.containsKey(original_argument_name)) {
290 // Set as assigned
291 argument.setAssigned(true);
292 String argument_value = (String)raw_arguments.remove(original_argument_name);
293 if(argument_value != null) {
294 argument.setValue(argument_value);
295 argument_value = null;
296 }
297 }
298 // We've removed it from our custom statement, so unassign
299 else {
300 argument.setAssigned(false);
301 }
302 argument = null;
303 }
304 // Any left over, add to the plugin
305 Iterator argument_names = raw_arguments.keySet().iterator();
306 while(argument_names.hasNext()) {
307 String argument_name = (String) argument_names.next();
308 String argument_value = (String) raw_arguments.get(argument_name);
309 // The tricky thing is that we have to create a new element in the DOM as well.
310 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
311 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
312 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
313 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
314 Argument argument = new Argument(argument_element);
315 argument_name = null;
316 if(argument_value != null) {
317 argument.setValue(argument_value);
318 argument_value = null;
319 }
320 // All done. Add it.
321 element.appendChild(argument_element);
322 add(argument);
323 argument_element = null;
324 }
325 raw_arguments = null;
326 }
327
328 /** Method to set the value of desc.
329 * @param description The new value of desc as a <strong>String</strong>.
330 */
331 public void setDescription(String description) {
332 this.description = description;
333 }
334
335 public void setElement(Element element) {
336 this.element = element;
337 }
338
339 public void setIsAbstract(boolean is_abstract) {
340 this.is_abstract = is_abstract;
341 }
342
343 /** Method to set the value of name.
344 * @param name The new value of name as a <strong>String</strong>.
345 */
346 public void setName(String name) {
347 this.name = name;
348 }
349
350 /** Method to set the value of the super_plugin.
351 * @param super_plugin The new value of super_plugin as a <strong>PlugIn</strong>, or <i>null</i> if this class has no inheritance.
352 */
353 public void setSuper(PlugIn super_plugin) {
354 this.super_plugin = super_plugin;
355 }
356
357 /** Method to print out this plugin as it would appear as a command within the collection configuration file.
358 * @return A <strong>String</strong> containing a single plugin command.
359 */
360 public String toString() {
361 if(element != null) {
362 if(name == null) {
363 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
364 }
365 StringBuffer text = new StringBuffer(StaticStrings.PLUGIN_STR);
366 text.append(" ");
367 text.append(name);
368 text.append(" ");
369 ArrayList arguments = getArguments(true, true);
370 int arguments_size = arguments.size();
371 for(int i = 0; i < arguments_size; i++) {
372 Argument argument = (Argument)arguments.get(i);
373 if(argument.isAssigned()) {
374 text.append(argument.toString());
375 text.append(" ");
376 }
377 argument = null;
378 }
379 return text.substring(0, text.length() - 1);
380 }
381 // Basic Plugin
382 else {
383 return name;
384 }
385 }
386}
Note: See TracBrowser for help on using the repository browser.