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

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

added classes for download pane

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