source: trunk/gli/src/org/greenstone/gatherer/cdm/ArgumentContainer.java@ 12253

Last change on this file since 12253 was 12247, checked in by kjdon, 18 years ago

made ArgumentContainer a base class instead of an interface and moved all the shared code from Plugin and Classifier into it (ie most of these two classes). also removed custom stuff from classifier and plugin - no more custom ones allowed

  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 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.Serializable;
30import java.util.ArrayList;
31import java.util.HashMap;
32import java.util.Iterator;
33import java.util.Map;
34import java.util.Set;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.util.StaticStrings;
37import org.w3c.dom.*;
38
39/**************************************************************************************
40 * Written: 20/05/02
41 * Revised: 03/07/03
42 **************************************************************************************/
43import java.util.ArrayList;
44
45/** This class provides an interface to any class that contains a list of Arguments.
46 * @author John Thompson, Greenstone Digital Library, University of Waikato
47 * @version 2.3
48 */
49abstract public class ArgumentContainer
50 extends ArrayList
51 implements Comparable, DOMProxyListEntry, Serializable {
52
53 // The DOM Element this container is modelled on
54 protected Element element;
55 protected boolean is_abstract = false;
56 protected ArgumentContainer super_container;
57 // should be set by the constructor
58 protected String display_name = "Unknown";
59 protected String description;
60 protected String name;
61
62 /** Constructor used in DOMProxyListModel initializations
63 */
64 public ArgumentContainer() {
65 }
66
67 public ArgumentContainer(Element element, ArgumentContainer base_container) {
68
69 super();
70 this.element = element;
71 this.name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
72
73 // Parse in any argument options for this container, keeping a list of the ones found
74 HashMap known_arguments = new HashMap();
75 NodeList option_elements = element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
76 int option_elements_length = option_elements.getLength();
77 for(int i = 0; i < option_elements_length; i++) {
78 Element option_element = (Element) option_elements.item(i);
79 Argument argument = new Argument(option_element);
80 ///ystem.err.println("Rebuilding existing argument: " + argument.getName());
81 known_arguments.put(argument.getName(), argument);
82 }
83 // If a base classifier was given
84 if(base_container != null) {
85 // Copy the details, and add a reference to whatever the base container's super container is
86 description = base_container.getDescription();
87 // Now search through the 'dummy' arguments belonging to the base container. 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 container, copy them and add them, but without a value.
88 ArrayList all_arguments = base_container.getArguments();
89 int argument_count = all_arguments.size();
90 for(int j = 0; j < argument_count; j++) {
91 Argument base_argument = (Argument) all_arguments.get(j);
92 String base_argument_name = base_argument.getName();
93 ///ystem.err.println("Library indicates this container should have an argument: " + base_argument_name);
94 Argument existing_argument = (Argument) known_arguments.get(base_argument_name);
95 // Found an existing argument. Complete its details
96 if(existing_argument != null) {
97 ///ystem.err.println("Found existing argument. Filling out details.");
98 known_arguments.remove(base_argument_name);
99 existing_argument.setDefaultValue(base_argument.getDefaultValue());
100 existing_argument.setDescription(base_argument.getDescription());
101 existing_argument.setOptions(base_argument.getOptions());
102 existing_argument.setRequired(base_argument.isRequired());
103 existing_argument.setType(base_argument.getType());
104 existing_argument.setMinimum(base_argument.getMinimum());
105 existing_argument.setMaximum(base_argument.getMaximum());
106 existing_argument.setOwner(base_argument.getOwner());
107 existing_argument.setHiddenGLI(base_argument.isHiddenGLI());
108 add(existing_argument);
109 }
110 // No existing argument. Copy base_argument and add it, but set its assigned flag to false
111 else {
112 ///atherer.println("No such argument. Adding new, unassigned, argument.");
113 // The trick thing is that we have to create a new element in the DOM as well.
114 Argument new_argument = base_argument.copy();
115 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
116 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, base_argument_name);
117 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
118 new_argument.setElement(argument_element);
119 // All done. Add it.
120 element.appendChild(argument_element);
121 add(new_argument);
122 }
123 }
124 }
125
126 // The first time we do this, the args come from the config file and
127 // may contain invalid args. Here we remove them from the DOM.
128 if (!known_arguments.isEmpty()) {
129 Set entry_set = known_arguments.entrySet();
130 Iterator entry_iterator = entry_set.iterator();
131 while (entry_iterator.hasNext()) {
132 Argument arg = (Argument)((Map.Entry)entry_iterator.next()).getValue();
133 Element e = arg.getElement();
134 element.removeChild(e);
135 }
136 }
137 }
138
139
140
141
142 /** Constructor used for the plugins that are in the DOMProxyList */
143 /** Method to add an argument to this container. Only adds the argument if it isn't already present.
144 * @param argument The <strong>Argument</strong> to add.
145 */
146 public void addArgument(Argument argument) {
147 if(element == null && !contains(argument)) {
148 add(argument);
149 argument.setOwner(name);
150 }
151 }
152
153 /** Method to compare two containers for ordering.
154 * @param object The container we are comparing to, as an <strong>Object</strong>.
155 * @return An <i>int</i> specifying the container order, using values as set out in String.
156 * @see java.lang.String#compareTo
157 */
158 public int compareTo(Object object) {
159 if(object == null ) {
160 return -1;
161 }
162 return toString().compareTo(object.toString());
163 }
164
165 /** Method to determine if two containers are equal.
166 * @param object The container to test against, as an <strong>Object</strong>.
167 * @return <i>true</i> if they match (based on the equals method), <i>false</i> otherwise.
168 */
169
170 public boolean equals(Object object) {
171 return (compareTo(object) == 0);
172 }
173
174 abstract public DOMProxyListEntry create(Element element);
175 /** Method to retrieve an argument by its name.
176 * @param name The name of the argument as a <strong>String</strong>.
177 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
178 */
179 public Argument getArgument(String name) {
180 // The name given may still include the '-'
181 if(name.startsWith("-")) {
182 name = name.substring(1);
183 }
184 //ArrayList arguments = getArguments(true, true);
185 ArrayList arguments = getArguments();
186 for(int i = 0; i < arguments.size(); i++) {
187 Argument argument = (Argument)arguments.get(i);
188 if(argument.getName().equals(name)) {
189 return argument;
190 }
191 }
192 return null;
193 }
194
195 /** Method to retrieve the list of arguments from this container. Note that this method returns both the containers arguments plus its 'supers' arguments if any, and alphabetically orders them.
196 * @return the arguments within a ArrayList
197 */
198 public ArrayList getArguments() {
199 ArrayList arguments = new ArrayList();
200 arguments.addAll(this);
201 if(super_container != null) {
202 ArrayList remainder = super_container.getArguments();
203 remainder.removeAll(arguments);
204 arguments.addAll(remainder);
205 }
206 return arguments;
207
208 }
209
210 public String getDescription() {
211 return description;
212 }
213
214 public Element getElement() {
215 return element;
216 }
217 /** Method to retrieve the name associated with this argument container.
218 * @return the name as a String
219 */
220 public String getName() {
221 if(name == null && element != null) {
222 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
223 }
224 return name;
225 }
226
227 public boolean isAbstract() {
228 return is_abstract;
229 }
230
231 public boolean isAssigned() {
232 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
233 }
234
235 public void setAssigned(boolean assigned) {
236 if(element != null) {
237 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
238 }
239 }
240
241 public void setDescription(String description) {
242 this.description = description;
243 }
244
245 public void setElement(Element element) {
246 this.element = element;
247 }
248
249 public void setIsAbstract(boolean is_abstract) {
250 this.is_abstract = is_abstract;
251 }
252
253 public void setName(String name) {
254 this.name = name;
255 }
256
257 /** Method to set the value of the super_container.
258 * @param super_container The new value of super_container as a <strong>ArgumentContainer</strong>, or <i>null</i> if this class has no inheritance.
259 */
260 public void setSuper(ArgumentContainer super_container) {
261 this.super_container = super_container;
262 }
263
264 public String toString()
265 {
266 if (element == null) {
267 return name;
268 }
269
270 if (name == null) {
271 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
272 }
273
274 StringBuffer text;
275 if (isAssigned()) {
276 text = new StringBuffer(display_name);
277 } else {
278 text = new StringBuffer("#");
279 text.append(display_name);
280 }
281 text.append(" ");
282 text.append(name);
283
284 ArrayList arguments = getArguments();
285 for (int i = 0; i < arguments.size(); i++) {
286 Argument argument = (Argument) arguments.get(i);
287 if (argument.isAssigned()) {
288 text.append(" ");
289 text.append(argument.toString());
290 }
291 }
292
293 return text.toString();
294 }
295
296}
Note: See TracBrowser for help on using the repository browser.