source: trunk/gli/src/org/greenstone/gatherer/cdm/Plugin.java@ 8629

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

First cut at suggesting plugins to use for files dragged into a collection. Seems to work pretty well, although I'm hoping to get all the plugins added to the collection by default which will avoid the need for a non-trivial dialog which hasn't been created yet.

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