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

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

A bit of work in preparation for exploding metadata databases.

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