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

Last change on this file since 8817 was 8817, checked in by mdewsnip, 19 years ago

Temporary fix for broken code.

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