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

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

removed an unnecessary comment

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