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

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

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.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 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 /** Method to determine if two plugins are equal.
158 * @param object The plugin to test against, as an <strong>Object</strong>.
159 * @return <i>true</i> if the plugin names match, <i>false</i> otherwise.
160 */
161 public boolean equals(Object object) {
162 return (compareTo(object) == 0);
163 }
164
165 /** Method to retrieve an argument by its name.
166 * @param name The name of the argument as a <strong>String</strong>.
167 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
168 */
169 public Argument getArgument(String name) {
170 // The name given may still include the '-'
171 if(name.startsWith("-")) {
172 name = name.substring(1);
173 }
174 ArrayList arguments = getArguments(true, true);
175 for(int i = 0; i < arguments.size(); i++) {
176 Argument argument = (Argument)arguments.get(i);
177 if(argument.getName().equals(name)) {
178 return argument;
179 }
180 }
181 return null;
182 }
183
184 /** 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.
185 * @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
186 */
187 public ArrayList getArguments(boolean include_normal, boolean include_custom) {
188 ArrayList arguments = new ArrayList();
189 if(include_normal && include_custom) {
190 arguments.addAll(this);
191 }
192 else {
193 int size = size();
194 for(int i = 0; i < size; i++) {
195 Argument argument = (Argument) get(i);
196 if(argument.isCustomArgument()) {
197 if(include_custom && !arguments.contains(argument)) {
198 arguments.add(argument);
199 }
200 }
201 else {
202 if(include_normal && !arguments.contains(argument)) {
203 arguments.add(argument);
204 }
205 }
206 argument = null;
207 }
208 }
209 if(super_plugin != null) {
210 ArrayList remainder = super_plugin.getArguments(include_normal, include_custom);
211 remainder.removeAll(arguments);
212 arguments.addAll(remainder);
213 }
214 return arguments;
215 }
216
217 /** 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.
218 * @return the custom arguments as a String
219 */
220 public String getCustom() {
221 StringBuffer custom_text = new StringBuffer();
222 // Retrieve all of the arguments, and append any that are custom into one long string
223 ArrayList arguments = getArguments(false, true);
224 int arguments_size = arguments.size();
225 boolean first = true;
226 for(int i = 0; i < arguments_size; i++) {
227 Argument argument = (Argument) arguments.get(i);
228 if(argument.isAssigned()) {
229 if(!first) {
230 custom_text.append(" ");
231 }
232 custom_text.append(argument.toString());
233 first = false;
234 }
235 }
236 return custom_text.toString();
237 }
238
239 public String getDescription() {
240 return description;
241 }
242
243 public Element getElement() {
244 return element;
245 }
246
247 /** Method to retrieve a plugins name.
248 * @return A <strong>String</strong> containing the plugins name.
249 */
250 public String getName() {
251 if(name == null && element != null) {
252 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
253 }
254 return name;
255 }
256
257 public boolean isAbstract() {
258 return is_abstract;
259 }
260
261 public boolean isAssigned() {
262 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
263 }
264
265 public boolean isSeparator() {
266 return (element != null && element.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
267 }
268
269 public void setAssigned(boolean assigned) {
270 if(element != null) {
271 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
272 }
273 }
274
275 /** 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!).
276 * @param custom_str the custom arguments all splodged together in one String
277 */
278 public void setCustom(String custom_str) {
279 HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
280 ArrayList custom_arguments = getArguments(false, true);
281 int size = custom_arguments.size();
282 for(int i = 0; i < size; i++) {
283 Argument argument = (Argument) custom_arguments.get(i);
284 String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
285 if(raw_arguments.containsKey(original_argument_name)) {
286 // Set as assigned
287 argument.setAssigned(true);
288 String argument_value = (String)raw_arguments.remove(original_argument_name);
289 if(argument_value != null) {
290 argument.setValue(argument_value);
291 argument_value = null;
292 }
293 }
294 // We've removed it from our custom statement, so unassign
295 else {
296 argument.setAssigned(false);
297 }
298 argument = null;
299 }
300 // Any left over, add to the plugin
301 Iterator argument_names = raw_arguments.keySet().iterator();
302 while(argument_names.hasNext()) {
303 String argument_name = (String) argument_names.next();
304 String argument_value = (String) raw_arguments.get(argument_name);
305 // The tricky thing is that we have to create a new element in the DOM as well.
306 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
307 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
308 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
309 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
310 Argument argument = new Argument(argument_element);
311 argument_name = null;
312 if(argument_value != null) {
313 argument.setValue(argument_value);
314 argument_value = null;
315 }
316 // All done. Add it.
317 element.appendChild(argument_element);
318 add(argument);
319 argument_element = null;
320 }
321 raw_arguments = null;
322 }
323
324 /** Method to set the value of desc.
325 * @param description The new value of desc as a <strong>String</strong>.
326 */
327 public void setDescription(String description) {
328 this.description = description;
329 }
330
331 public void setElement(Element element) {
332 this.element = element;
333 }
334
335 public void setIsAbstract(boolean is_abstract) {
336 this.is_abstract = is_abstract;
337 }
338
339 /** Method to set the value of name.
340 * @param name The new value of name as a <strong>String</strong>.
341 */
342 public void setName(String name) {
343 this.name = name;
344 }
345
346 /** Method to set the value of the super_plugin.
347 * @param super_plugin The new value of super_plugin as a <strong>PlugIn</strong>, or <i>null</i> if this class has no inheritance.
348 */
349 public void setSuper(PlugIn super_plugin) {
350 this.super_plugin = super_plugin;
351 }
352
353 /** Method to print out this plugin as it would appear as a command within the collection configuration file.
354 * @return A <strong>String</strong> containing a single plugin command.
355 */
356 public String toString() {
357 if(element != null) {
358 if(name == null) {
359 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
360 }
361 StringBuffer text = new StringBuffer(StaticStrings.PLUGIN_STR);
362 text.append(" ");
363 text.append(name);
364 text.append(" ");
365 ArrayList arguments = getArguments(true, true);
366 int arguments_size = arguments.size();
367 for(int i = 0; i < arguments_size; i++) {
368 Argument argument = (Argument)arguments.get(i);
369 if(argument.isAssigned()) {
370 text.append(argument.toString());
371 text.append(" ");
372 }
373 argument = null;
374 }
375 return text.substring(0, text.length() - 1);
376 }
377 // Basic Plugin
378 else {
379 return name;
380 }
381 }
382}
Note: See TracBrowser for help on using the repository browser.