source: gli/branches/rtl-gli/src/org/greenstone/gatherer/cdm/ArgumentContainer.java@ 18352

Last change on this file since 18352 was 18352, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

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