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

Last change on this file since 9019 was 9014, checked in by kjdon, 19 years ago

adding a new element to plugins list not in the first collection caused a wrong document exception. now we import the node. but has this fixed the problem or just put a plaster on it? should the list be rebuilt for the new collection?

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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.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 // if we are not in the first collection opened, then this
117 /// will belong to a different document, so need to import
118 // it.
119 element.appendChild(element.getOwnerDocument().importNode(argument_element, true));
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
162 /** Checks whether the plugin this instance is based on processes metadata databases that can be exploded. */
163 public boolean doesExplodeMetadataDatabases()
164 {
165 return false;
166 // Plugin base_plugin = CollectionDesignManager.plugin_manager.getBasePlugin(getName());
167 // return base_plugin.does_explode_metadata_databases;
168 }
169
170
171 /** Checks whether this plugin instance will process the specified file (given its process_exp and block_exp). */
172 public boolean doesProcessFile(File file)
173 {
174 // Check the filename against the plugin's process_exp and block_exp values
175 ArrayList arguments = getArguments(true, true);
176 for (int i = 0; i < arguments.size(); i++) {
177 Argument argument = (Argument) arguments.get(i);
178 if (argument.getName().equals("process_exp") || argument.getName().equals("block_exp")) {
179 // Try the assigned value first, for when the user has manually set the value
180 String regular_expression = argument.getValue();
181 if (regular_expression == null || regular_expression.equals("")) {
182 // Not set, so use the default value
183 regular_expression = argument.getDefaultValue();
184 if (regular_expression.equals("")) {
185 continue;
186 }
187 }
188
189 // The $ at the end doesn't seem to work in Java, so need to add ".*" at the start
190 if (regular_expression.startsWith("(?i)")) {
191 // Don't mess up case-insensitive matching though
192 regular_expression = "(?i)" + ".*" + regular_expression.substring("(?i)".length());
193 }
194 else {
195 regular_expression = ".*" + regular_expression;
196 }
197
198 // If the filename matches the regular expression, this plugin will deal with the file in some way
199 if (file.getName().matches(regular_expression)) {
200 return true;
201 }
202 }
203 }
204
205 // This plugin will (probably) not deal with the specified file
206 return false;
207 }
208
209
210 /** Method to determine if two plugins are equal.
211 * @param object The plugin to test against, as an <strong>Object</strong>.
212 * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
213 */
214 public boolean equals(Object object) {
215 return (compareTo(object) == 0);
216 }
217
218 /** Method to retrieve an argument by its name.
219 * @param name The name of the argument as a <strong>String</strong>.
220 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
221 */
222 public Argument getArgument(String name) {
223 // The name given may still include the '-'
224 if(name.startsWith("-")) {
225 name = name.substring(1);
226 }
227 ArrayList arguments = getArguments(true, true);
228 for(int i = 0; i < arguments.size(); i++) {
229 Argument argument = (Argument)arguments.get(i);
230 if(argument.getName().equals(name)) {
231 return argument;
232 }
233 }
234 return null;
235 }
236
237 /** 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.
238 * @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
239 */
240 public ArrayList getArguments(boolean include_normal, boolean include_custom) {
241 ArrayList arguments = new ArrayList();
242 if(include_normal && include_custom) {
243 arguments.addAll(this);
244 }
245 else {
246 int size = size();
247 for(int i = 0; i < size; i++) {
248 Argument argument = (Argument) get(i);
249 if(argument.isCustomArgument()) {
250 if(include_custom && !arguments.contains(argument)) {
251 arguments.add(argument);
252 }
253 }
254 else {
255 if(include_normal && !arguments.contains(argument)) {
256 arguments.add(argument);
257 }
258 }
259 argument = null;
260 }
261 }
262 if(super_plugin != null) {
263 ArrayList remainder = super_plugin.getArguments(include_normal, include_custom);
264 remainder.removeAll(arguments);
265 arguments.addAll(remainder);
266 }
267 return arguments;
268 }
269
270 /** 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.
271 * @return the custom arguments as a String
272 */
273 public String getCustom() {
274 StringBuffer custom_text = new StringBuffer();
275 // Retrieve all of the arguments, and append any that are custom into one long string
276 ArrayList arguments = getArguments(false, true);
277 int arguments_size = arguments.size();
278 boolean first = true;
279 for(int i = 0; i < arguments_size; i++) {
280 Argument argument = (Argument) arguments.get(i);
281 if(argument.isAssigned()) {
282 if(!first) {
283 custom_text.append(" ");
284 }
285 custom_text.append(argument.toString());
286 first = false;
287 }
288 }
289 return custom_text.toString();
290 }
291
292 public String getDescription() {
293 return description;
294 }
295
296 public Element getElement() {
297 return element;
298 }
299
300 /** Method to retrieve a plugins name.
301 * @return A <strong>String</strong> containing the plugins name.
302 */
303 public String getName() {
304 if(name == null && element != null) {
305 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
306 }
307 return name;
308 }
309
310 public boolean isAbstract() {
311 return is_abstract;
312 }
313
314 public boolean isAssigned() {
315 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
316 }
317
318 public boolean isSeparator() {
319 return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
320 }
321
322 public void setAssigned(boolean assigned) {
323 if(element != null) {
324 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
325 }
326 }
327
328 /** 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!).
329 * @param custom_str the custom arguments all splodged together in one String
330 */
331 public void setCustom(String custom_str) {
332 HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
333 ArrayList custom_arguments = getArguments(false, true);
334 int size = custom_arguments.size();
335 for(int i = 0; i < size; i++) {
336 Argument argument = (Argument) custom_arguments.get(i);
337 String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
338 if(raw_arguments.containsKey(original_argument_name)) {
339 // Set as assigned
340 argument.setAssigned(true);
341 String argument_value = (String)raw_arguments.remove(original_argument_name);
342 if(argument_value != null) {
343 argument.setValue(argument_value);
344 argument_value = null;
345 }
346 }
347 // We've removed it from our custom statement, so unassign
348 else {
349 argument.setAssigned(false);
350 }
351 argument = null;
352 }
353 // Any left over, add to the plugin
354 Iterator argument_names = raw_arguments.keySet().iterator();
355 while(argument_names.hasNext()) {
356 String argument_name = (String) argument_names.next();
357 String argument_value = (String) raw_arguments.get(argument_name);
358 // The tricky thing is that we have to create a new element in the DOM as well.
359 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
360 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
361 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
362 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
363 Argument argument = new Argument(argument_element);
364 argument_name = null;
365 if(argument_value != null) {
366 argument.setValue(argument_value);
367 argument_value = null;
368 }
369 // All done. Add it.
370 element.appendChild(argument_element);
371 add(argument);
372 argument_element = null;
373 }
374 raw_arguments = null;
375 }
376
377 /** Method to set the value of desc.
378 * @param description The new value of desc as a <strong>String</strong>.
379 */
380 public void setDescription(String description) {
381 this.description = description;
382 }
383
384 public void setDoesExplodeMetadataDatabases(boolean does_explode_metadata_databases) {
385 this.does_explode_metadata_databases = does_explode_metadata_databases;
386 }
387
388 public void setElement(Element element) {
389 this.element = element;
390 }
391
392 public void setIsAbstract(boolean is_abstract) {
393 this.is_abstract = is_abstract;
394 }
395
396 /** Method to set the value of name.
397 * @param name The new value of name as a <strong>String</strong>.
398 */
399 public void setName(String name) {
400 this.name = name;
401 }
402
403 /** Method to set the value of the super_plugin.
404 * @param super_plugin The new value of super_plugin as a <strong>Plugin</strong>, or <i>null</i> if this class has no inheritance.
405 */
406 public void setSuper(Plugin super_plugin) {
407 this.super_plugin = super_plugin;
408 }
409
410 /** Method to print out this plugin as it would appear as a command within the collection configuration file.
411 * @return A <strong>String</strong> containing a single plugin command.
412 */
413 public String toString() {
414 if(element != null) {
415 if(name == null) {
416 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
417 }
418 StringBuffer text = new StringBuffer(StaticStrings.PLUGIN_STR);
419 text.append(" ");
420 text.append(name);
421 text.append(" ");
422 ArrayList arguments = getArguments(true, true);
423 int arguments_size = arguments.size();
424 for(int i = 0; i < arguments_size; i++) {
425 Argument argument = (Argument)arguments.get(i);
426 if(argument.isAssigned()) {
427 text.append(argument.toString());
428 text.append(" ");
429 }
430 argument = null;
431 }
432 return text.substring(0, text.length() - 1);
433 }
434 // Basic Plugin
435 else {
436 return name;
437 }
438 }
439}
Note: See TracBrowser for help on using the repository browser.