source: trunk/gli/src/org/greenstone/gatherer/download/Download.java@ 12647

Last change on this file since 12647 was 12641, checked in by mdewsnip, 18 years ago

Changed all access to the static strings through CollectionConfiguration to directly use StaticStrings.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 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.download;
28
29import java.io.*;
30import java.util.*;
31import org.greenstone.gatherer.util.StaticStrings;
32import org.greenstone.gatherer.util.Utility;
33import org.w3c.dom.*;
34import org.greenstone.gatherer.cdm.*;
35
36/** This class is responsible for storing information from a parsed classinfo.pl call in such a way that it allows easy access to parsed details for the purposes of user design and specification of downloads.
37 * @author John Thompson, Greenstone Digital Library, University of Waikato
38 * @version 2.3
39 */
40public class Download extends ArgumentContainer
41 {
42
43 static final public String DOWNLOAD_PREFIX = "CL";
44
45 private boolean is_abstract = false;
46
47 /** A reference to the download that this one inherits from. */
48 private Download super_download = null;
49 /** The element this download is based upon. */
50 private Element element;
51 /** A description of this download. */
52 private String description = null;
53 /** The name of the download as it would appear in the collect.cfg file. */
54 private String name = null;
55 /** This string is filled out the first time this download is created, and remains unchanged there-after. It is used to match up with Format commands that may not yet have been instantiated (and thus only have offline references along the lines of 'CL1' to figure out what Download they want.) */
56 private String old_position_string = null;
57
58 /** Constructor used only in DOMProxyListModel initializations.
59 */
60 public Download() {
61 }
62
63 /** Method to add an argument to this download. Only adds the argument if it isn't already present.
64 * @param argument The <strong>Argument</strong> to add.
65 */
66 public void addArgument(Argument argument) {
67 if(element == null && !contains(argument)) {
68 add(argument);
69 argument.setOwner(name);
70 }
71 }
72
73 /** Method to compare two downloads for ordering.
74 * @param object The download we are comparing to, as an <strong>Object</strong>.
75 * @return An <i>int</i> specifying the download order, using values as set out in String.
76 * @see java.lang.String#compareTo
77 */
78 public int compareTo(Object object) {
79 if(object == null) {
80 return -1;
81 }
82 return toString().compareTo(object.toString());
83 }
84
85 /** The assigned download constructor.
86 * @param element the DOM Element this classifier is based upon
87 */
88 public DOMProxyListEntry create(Element element) {
89// String download_name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
90// // Determine the base classifier from the classifier name
91// Classifier base_classifier = CollectionDesignManager.classifier_manager.getBaseClassifier(classifier_name);
92// Classifier classifier = new Classifier(element, base_classifier);
93// base_classifier = null;
94// classifier_name = null;
95// return classifier;
96 return null;
97 }
98
99 /** Method to determine if two downloads are equal.
100 * @param object The download to test against, as an <strong>Object</strong>.
101 * @return <i>true</i> if the download names match, <i>false</i> otherwise.
102 */
103 public boolean equals(Object object) {
104 return (compareTo(object) == 0);
105 }
106
107 /** Method to retrieve an argument by its name.
108 * @param name The name of the argument as a <strong>String</strong>.
109 * @return The <strong>Argument</strong> requested, or <i>null</i> if no such argument.
110 */
111 public Argument getArgument(String name) {
112 // The name given may still include the '-'
113 if(name.startsWith("-")) {
114 name = name.substring(1);
115 }
116 ArrayList arguments = getArguments(true, true);
117 for(int i = 0; i < arguments.size(); i++) {
118 Argument argument = (Argument)arguments.get(i);
119 if(argument.getName().equals(name)) {
120 return argument;
121 }
122 }
123 return null;
124 }
125
126 /** Retrieve all of the arguments available to this base download, including its super downloads arguments. Some complexity is added by allowing the caller to choose whether they want normal arguments, custom arguments, or both.
127 * @return an ArrayList of all of the arguments, starting with those for this download and ending with the arguments for basplug or similiar root download
128 */
129 public ArrayList getArguments(boolean include_normal, boolean include_custom) {
130 ArrayList arguments = new ArrayList();
131 if(include_normal && include_custom) {
132 arguments.addAll(this);
133 }
134 else {
135 int size = size();
136 for(int i = 0; i < size; i++) {
137 Argument argument = (Argument) get(i);
138 if(argument.isCustomArgument()) {
139 if(include_custom && !arguments.contains(argument)) {
140 arguments.add(argument);
141 }
142 }
143 else {
144 if(include_normal && !arguments.contains(argument)) {
145 arguments.add(argument);
146 }
147 }
148 argument = null;
149 }
150 }
151 if(super_download != null) {
152 ArrayList remainder = super_download.getArguments(include_normal, include_custom);
153 remainder.removeAll(arguments);
154 arguments.addAll(remainder);
155 }
156 return arguments;
157 }
158
159 /** Method to retrieve a download custom argument information. Custom arguments are defined to be those that have not got matching arguments in the base reference download from the library. Of course if there is no base download then all arguments are considered to be custom.
160 * @return the custom arguments as a String
161 */
162 public String getCustom() {
163 StringBuffer custom_text = new StringBuffer();
164 // Retrieve all of the arguments, and append any that are custom into one long string
165 ArrayList arguments = getArguments(false, true);
166 int arguments_size = arguments.size();
167 boolean first = true;
168 for(int i = 0; i < arguments_size; i++) {
169 Argument argument = (Argument) arguments.get(i);
170 if(argument.isAssigned()) {
171 if(!first) {
172 custom_text.append(" ");
173 }
174 custom_text.append(argument.toString());
175 first = false;
176 }
177 }
178 return custom_text.toString();
179 }
180
181 public String getDescription() {
182 return description;
183 }
184
185 public Element getElement() {
186 return element;
187 }
188
189 /** Method to retrieve a download name.
190 * @return A <strong>String</strong> containing the downloads name.
191 */
192 public String getName() {
193 if(name == null && element != null) {
194 name = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
195 }
196 return name;
197 }
198
199 public boolean isAbstract() {
200 return is_abstract;
201 }
202
203 public boolean isAssigned() {
204 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
205 }
206
207 public void setAssigned(boolean assigned) {
208 if(element != null) {
209 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
210 }
211 }
212
213 /** 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!).
214 * @param custom_str the custom arguments all splodged together in one String
215 */
216 public void setCustom(String custom_str) {
217 HashMap raw_arguments = CollectionConfiguration.parseArguments(new CommandTokenizer(custom_str));
218 ArrayList custom_arguments = getArguments(false, true);
219 int size = custom_arguments.size();
220 for(int i = 0; i < size; i++) {
221 Argument argument = (Argument) custom_arguments.get(i);
222 String original_argument_name = StaticStrings.MINUS_CHARACTER + argument.getName();
223 if(raw_arguments.containsKey(original_argument_name)) {
224 // Set as assigned
225 argument.setAssigned(true);
226 String argument_value = (String)raw_arguments.remove(original_argument_name);
227 if(argument_value != null) {
228 argument.setValue(argument_value);
229 argument_value = null;
230 }
231 }
232 // We've removed it from our custom statement, so unassign
233 else {
234 argument.setAssigned(false);
235 }
236 argument = null;
237 }
238 // Any left over, add to the download
239 Iterator argument_names = raw_arguments.keySet().iterator();
240 while(argument_names.hasNext()) {
241 String argument_name = (String) argument_names.next();
242 String argument_value = (String) raw_arguments.get(argument_name);
243 // The tricky thing is that we have to create a new element in the DOM as well.
244 Element argument_element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.OPTION_ELEMENT);
245 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, argument_name.substring(1));
246 argument_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
247 argument_element.setAttribute(StaticStrings.CUSTOM_ATTRIBUTE, StaticStrings.TRUE_STR);
248 Argument argument = new Argument(argument_element);
249 argument_name = null;
250 if(argument_value != null) {
251 argument.setValue(argument_value);
252 argument_value = null;
253 }
254 // All done. Add it.
255 element.appendChild(argument_element);
256 add(argument);
257 argument_element = null;
258 }
259 raw_arguments = null;
260 }
261
262 /** Method to set the value of desc.
263 * @param description The new value of desc as a <strong>String</strong>.
264 */
265 public void setDescription(String description) {
266 this.description = description;
267 }
268
269 public void setElement(Element element) {
270 this.element = element;
271 }
272
273 public void setIsAbstract(boolean is_abstract) {
274 this.is_abstract = is_abstract;
275 }
276
277 /** Method to set the value of name.
278 * @param name The new value of name as a <strong>String</strong>.
279 */
280 public void setName(String name) {
281 this.name = name;
282 }
283
284 /** Method to set the value of the super_download.
285 * @param super_download The new value of super_download as a <strong>Download</strong>, or <i>null</i> if this class has no inheritance.
286 */
287 public void setSuper(Download super_download) {
288 this.super_download = super_download;
289 }
290}
Note: See TracBrowser for help on using the repository browser.