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

Last change on this file since 7109 was 6770, checked in by kjdon, 20 years ago

fixed all the javadoc errors. (hope I didn't commit anything I wasn't supposed to)

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