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

Last change on this file since 12634 was 12466, checked in by shaoqun, 18 years ago

added code for download pane

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