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

Last change on this file since 4932 was 4932, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

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