source: trunk/gli/src/org/greenstone/gatherer/collection/Collection.java@ 10009

Last change on this file since 10009 was 10006, checked in by mdewsnip, 19 years ago

Moved Utility.parse to XMLTools.parseXMLFile, as part of tidying up the Utility class.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.collection;
38
39import java.io.*;
40import java.util.*;
41import javax.swing.*;
42import javax.swing.tree.*;
43import org.greenstone.gatherer.Configuration;
44import org.greenstone.gatherer.DebugStream;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.cdm.CollectionDesignManager;
47import org.greenstone.gatherer.cdm.CollectionMeta;
48import org.greenstone.gatherer.cdm.CollectionMetaManager;
49import org.greenstone.gatherer.util.StaticStrings;
50import org.greenstone.gatherer.util.Utility;
51import org.greenstone.gatherer.util.XMLTools;
52import org.w3c.dom.*;
53
54/** Collection provides a common collection point for all the information about a certain collection build session. It keeps a record of several other managers that actually handle the content of the collection, such as metadata sets and metadata itself.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.3c
57 */
58public class Collection {
59 /** A reference to the Collection Design Manager. */
60 public CollectionDesignManager cdm;
61 /** A reference to the build ScriptOptions. */
62 public ScriptOptions build_options;
63 /** A reference to the import ScriptOptions. */
64 public ScriptOptions import_options;
65 /** true if an error has occurred during construction */
66 public boolean error = false;
67 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */
68 private boolean saved = false;
69 /*<i>true</i> if the currently loaded collection has had files added since its last build */
70 private boolean filesChanged = true;
71 /*<i>true</i> if the currently loaded collection has had metadata added since its last build */
72 private boolean metadataChanged = true;
73 /** The document around which this collection class is based. */
74 private Document document;
75 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */
76 private File file;
77 /** The name of the argument element. */
78 static final private String ARGUMENT = "Argument";
79 static final private String BASE_COLLECTION = "base_collection";
80 /** The name of the build element. */
81 static final private String BUILD = "Build";
82 /** The name of the build config element. */
83 static final private String BUILD_CONFIG = "BuildConfig";
84 /** The name of the collection xml template. */
85 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col";
86 /** The name of the import element. */
87 static final private String IMPORT = "Import";
88 /** The name of the imported attribute. */
89 static final private String IMPORTED = "imported";
90
91 /** Constructor. */
92 public Collection(File collection_xml) {
93 this.file = collection_xml;
94 // Try to load this collections details.
95 document = XMLTools.parseXMLFile(collection_xml);
96 // If that fails load the default settings for a collection.
97 if(document == null) {
98 document = XMLTools.parseXMLFile(COLLECTION_XML_TEMPLATE, true);
99 }
100 if (document == null) {
101 error = true;
102 return;
103 }
104 // Point the Configuration class at our gatherer config arguments.
105 Configuration.setCollectionConfiguration(document);
106 if (Gatherer.g_man != null) {
107 Gatherer.g_man.updateUI();
108 }
109 // Finally create all of the child managers that are directly dependant on a collection
110 build_options = new ScriptOptions(getBuildValues(), "buildcol.pl", true);
111 import_options = new ScriptOptions(getImportValues(), "import.pl", true);
112 }
113
114 /** Destructor.
115 * @see org.greenstone.gatherer.Configuration
116 * @see org.greenstone.gatherer.Gatherer
117 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
118 */
119 public void destroy() {
120 cdm.destroy();
121 Configuration.setCollectionConfiguration(null);
122 if (Gatherer.g_man != null) {
123 Gatherer.g_man.updateUI();
124 }
125 cdm = null;
126 document = null;
127 }
128
129 /** Determine the path to the base collection.
130 * @return the path as a String
131 */
132 public String getBaseCollection() {
133 return getString(BASE_COLLECTION);
134 }
135
136 /** Determine the number of documents and folders in this collection. */
137 public int getCount() {
138 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), true, true);
139 }
140
141 /** Calculates the number of documents in this collection. */
142 public int getDocumentCount() {
143 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), false, true);
144 }
145
146 /** Retrieve the description of this collection.
147 * @return a String
148 */
149 public String getDescription() {
150 if(cdm == null) {
151 return StaticStrings.EMPTY_STR;
152 }
153 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
154 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT);
155 }
156
157 /** Retrieve the authors email for this collection.
158 * @return The email as a <strong>String</strong>.
159 */
160 public String getEmail() {
161 if(cdm == null) {
162 return StaticStrings.EMPTY_STR;
163 }
164 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
165 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
166 }
167
168 /** Retrieve the short name for this collection.
169 * @return The name as a <strong>String</strong>.
170 */
171 public String getName() {
172 return file.getParentFile().getName();
173 }
174
175 /** Determine if this collection has been saved since the last major change.
176 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise.
177 */
178 public boolean getSaved() {
179 return saved;
180 }
181
182 /** Determine if this collection has had its collection files changed since the last build.
183 * @return <i>true</i> if they have been saved, <i>false</i> otherwise.
184 */
185 public boolean getFilesChanged() {
186 return filesChanged;
187 }
188
189 /** Set the flag that marks weather or not the files has been changed since the last build.
190 @param changed if files has been added/removed, as a <strong>boolean</strong>
191 */
192 public void setFilesChanged(boolean changed) {
193 filesChanged = changed;
194 }
195
196 /** Determine if this collection has had metadata changed since the last build.
197 * @return <i>true</i> if it has been changed, <i>false</i> otherwise.
198 */
199 public boolean getMetadataChanged() {
200 return metadataChanged;
201 }
202
203 /** Set the flag that marks weather or not the metadata has been changed since the last build.
204 @param changed if metadata has been added/changed/removed, as a <strong>boolean</strong>
205 */
206 public void setMetadataChanged(boolean changed) {
207 metadataChanged = changed;
208 }
209
210 /** Retrieve the title of this collection.
211 * @return The title as a <strong>String</strong>.
212 */
213 public String getTitle() {
214 if(cdm == null) {
215 return StaticStrings.EMPTY_STR;
216 }
217 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
218 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
219 }
220
221 /** Save this xml document to the given file. */
222 public void save() {
223 XMLTools.writeXMLFile(file, document);
224 saved = true;
225 }
226
227 public void setBaseCollection(String base_collection) {
228 set(BASE_COLLECTION, base_collection);
229 }
230
231 /** Set the value of imported to the given value.
232 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise.
233 */
234 public void setImported(boolean value) {
235 set(IMPORTED, value);
236 saved = false;
237 }
238
239 /** Set the value of saved to the given value.
240 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise.
241 */
242 public void setSaved(boolean value) {
243 saved = value;
244 }
245
246 /** Set the value of title to the given value.
247 * @param title The new <strong>String</strong> title.
248 */
249 public void setTitle(String title) {
250 if(cdm != null) {
251 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
252 collection_name_collectionmeta.setValue(title);
253 }
254 }
255
256 /** Method called to return a textual representation of a class, which in this case is the collections title.
257 * @return A <strong>String</strong> containing the collections title.
258 */
259 public String toString() {
260 return getTitle();
261 }
262
263 /** Get the value of a collection argument. */
264 private boolean get(String name) {
265 boolean result = false;
266 try {
267 Element document_element = document.getDocumentElement();
268 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
269 boolean found = false;
270 for(int i = 0; !found && i < arguments.getLength(); i++) {
271 Element argument_element = (Element) arguments.item(i);
272 if(argument_element.getParentNode() == document_element) {
273 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
274 String value = XMLTools.getValue(argument_element);
275 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
276 result = true;
277 }
278 found = true;
279 value = null;
280 }
281 }
282 argument_element = null;
283 }
284 arguments = null;
285 document_element = null;
286 }
287 catch (Exception error) {
288 DebugStream.printStackTrace(error);
289 }
290 return result;
291 }
292
293 /** Get the value of a collection argument. */
294 private String getString(String name) {
295 String result = "";
296 try {
297 Element document_element = document.getDocumentElement();
298 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
299 boolean found = false;
300 for(int i = 0; !found && i < arguments.getLength(); i++) {
301 Element argument_element = (Element) arguments.item(i);
302 if(argument_element.getParentNode() == document_element) {
303 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
304 result = XMLTools.getValue(argument_element);
305 found = true;
306 }
307 }
308 argument_element = null;
309 }
310 arguments = null;
311 document_element = null;
312 }
313 catch (Exception error) {
314 DebugStream.printStackTrace(error);
315 }
316 return result;
317 }
318
319 /** Method to retrieve the current build options associated with this Collection. */
320 private Element getBuildValues() {
321 Element build_values_element = null;
322 try {
323 Element document_element = document.getDocumentElement();
324 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
325 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
326 build_config_element = null;
327 document_element = null;
328 }
329 catch (Exception error) {
330 DebugStream.printStackTrace(error);
331 }
332 return build_values_element;
333 }
334
335 /** Count either documents or folders, depending on the state of the given boolean. */
336 private int getCount(TreeNode node, boolean count_folders, boolean count_files) {
337 int count = 0;
338 File file = ((CollectionTreeNode)node).getFile();
339 if(file.isFile() && count_files) {
340 count++;
341 }
342 else if(file.isDirectory() && count_folders) {
343 count++;
344 }
345 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
346 count = count + getCount(node.getChildAt(i), count_folders, count_files);
347 }
348 return count;
349 }
350
351 /** Method to retrieve the current import options associated with this Collection. */
352 public Element getImportValues() {
353 Element import_values_element = null;
354 try {
355 Element document_element = document.getDocumentElement();
356 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
357 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
358 build_config_element = null;
359 document_element = null;
360 }
361 catch (Exception error) {
362 DebugStream.printStackTrace(error);
363 }
364 return import_values_element;
365 }
366
367 /** Set the value of a collection argument. */
368 private void set(String name, boolean value) {
369 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
370 }
371
372 private void set(String name, String value) {
373 try {
374 Element document_element = document.getDocumentElement();
375 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);
376 boolean found = false;
377 for(int i = 0; !found && i < arguments.getLength(); i++) {
378 Element argument_element = (Element) arguments.item(i);
379 if(argument_element.getParentNode() == document_element) {
380 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
381 // Strip any current value nodes.
382 while(argument_element.hasChildNodes()) {
383 argument_element.removeChild(argument_element.getFirstChild());
384 }
385 // Append new value
386 argument_element.appendChild(document.createTextNode(value));
387 found = true;
388 }
389 }
390 argument_element = null;
391 }
392 // Append it
393 if(!found) {
394 Element argument_element = document.createElement(ARGUMENT);
395 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
396 argument_element.appendChild(document.createTextNode(value));
397 document_element.appendChild(argument_element);
398 argument_element = null;
399 }
400 arguments = null;
401 document_element = null;
402 }
403 catch (Exception error) {
404 DebugStream.printStackTrace(error);
405 }
406 }
407}
Note: See TracBrowser for help on using the repository browser.