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

Last change on this file since 8240 was 8240, checked in by mdewsnip, 20 years ago

Removed unnecessary imports of org.greenstone.gatherer.Gatherer.

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