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
RevLine 
[5164]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 */
[5581]37package org.greenstone.gatherer.collection;
38
[5164]39import java.io.*;
40import java.util.*;
41import javax.swing.*;
42import javax.swing.tree.*;
[8231]43import org.greenstone.gatherer.Configuration;
[8236]44import org.greenstone.gatherer.DebugStream;
[5164]45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.cdm.CollectionDesignManager;
[6051]47import org.greenstone.gatherer.cdm.CollectionMeta;
48import org.greenstone.gatherer.cdm.CollectionMetaManager;
[5721]49import org.greenstone.gatherer.util.StaticStrings;
[5164]50import org.greenstone.gatherer.util.Utility;
[8015]51import org.greenstone.gatherer.util.XMLTools;
[5164]52import org.w3c.dom.*;
[5581]53
[5164]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 {
[6086]59 /** A reference to the Collection Design Manager. */
60 public CollectionDesignManager cdm;
[9136]61 /** A reference to the build ScriptOptions. */
62 public ScriptOptions build_options;
63 /** A reference to the import ScriptOptions. */
64 public ScriptOptions import_options;
[7281]65 /** true if an error has occurred during construction */
66 public boolean error = false;
[6086]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;
[9166]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;
[6086]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";
[6051]90
[6086]91 /** Constructor. */
92 public Collection(File collection_xml) {
93 this.file = collection_xml;
94 // Try to load this collections details.
[9036]95 document = XMLTools.parseXMLFile(collection_xml);
[6086]96 // If that fails load the default settings for a collection.
97 if(document == null) {
[10006]98 document = XMLTools.parseXMLFile(COLLECTION_XML_TEMPLATE, true);
[5164]99 }
[7281]100 if (document == null) {
101 error = true;
102 return;
103 }
[6086]104 // Point the Configuration class at our gatherer config arguments.
[8231]105 Configuration.setCollectionConfiguration(document);
[8253]106 if (Gatherer.g_man != null) {
107 Gatherer.g_man.updateUI();
108 }
[6086]109 // Finally create all of the child managers that are directly dependant on a collection
[9136]110 build_options = new ScriptOptions(getBuildValues(), "buildcol.pl", true);
111 import_options = new ScriptOptions(getImportValues(), "import.pl", true);
[6086]112 }
113
114 /** Destructor.
[6387]115 * @see org.greenstone.gatherer.Configuration
116 * @see org.greenstone.gatherer.Gatherer
117 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
118 */
[6086]119 public void destroy() {
[5164]120 cdm.destroy();
[8231]121 Configuration.setCollectionConfiguration(null);
[8253]122 if (Gatherer.g_man != null) {
123 Gatherer.g_man.updateUI();
124 }
[5164]125 cdm = null;
126 document = null;
[6086]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() {
[8846]138 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), true, true);
[6086]139 }
140
141 /** Calculates the number of documents in this collection. */
142 public int getDocumentCount() {
[8846]143 return getCount((TreeNode)Gatherer.c_man.getCollectionTreeModel().getRoot(), false, true);
[6086]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;
[6051]152 }
[6086]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;
[6051]163 }
[6086]164 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
165 return creator_collectionmeta.getValue(CollectionMeta.TEXT);
166 }
[6204]167
[6086]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 }
[9166]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 }
[6086]188
[9166]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
[6086]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;
[5164]216 }
[6086]217 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
218 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT);
219 }
[6051]220
[6086]221 /** Save this xml document to the given file. */
222 public void save() {
[9036]223 XMLTools.writeXMLFile(file, document);
[8586]224 saved = true;
[6086]225 }
[5237]226
[6086]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) {
[5164]235 set(IMPORTED, value);
236 saved = false;
[6086]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) {
[5164]243 saved = value;
[6086]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);
[6051]253 }
[6086]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) {
[5164]265 boolean result = false;
266 try {
[6086]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++) {
[5164]271 Element argument_element = (Element) arguments.item(i);
272 if(argument_element.getParentNode() == document_element) {
[6086]273 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
[8015]274 String value = XMLTools.getValue(argument_element);
[6086]275 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) {
276 result = true;
[5164]277 }
278 found = true;
279 value = null;
[6086]280 }
[5164]281 }
282 argument_element = null;
[6086]283 }
284 arguments = null;
285 document_element = null;
[5164]286 }
287 catch (Exception error) {
[8236]288 DebugStream.printStackTrace(error);
[5164]289 }
290 return result;
[6086]291 }
292
293 /** Get the value of a collection argument. */
294 private String getString(String name) {
[5721]295 String result = "";
296 try {
[6086]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++) {
[5721]301 Element argument_element = (Element) arguments.item(i);
302 if(argument_element.getParentNode() == document_element) {
[6086]303 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) {
[8015]304 result = XMLTools.getValue(argument_element);
[5721]305 found = true;
[6086]306 }
[5721]307 }
308 argument_element = null;
[6086]309 }
310 arguments = null;
311 document_element = null;
[5721]312 }
313 catch (Exception error) {
[8236]314 DebugStream.printStackTrace(error);
[5721]315 }
316 return result;
[6086]317 }
318
319 /** Method to retrieve the current build options associated with this Collection. */
320 private Element getBuildValues() {
[5164]321 Element build_values_element = null;
322 try {
[6086]323 Element document_element = document.getDocumentElement();
[8015]324 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
325 build_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, BUILD);
[6086]326 build_config_element = null;
327 document_element = null;
[5164]328 }
329 catch (Exception error) {
[8236]330 DebugStream.printStackTrace(error);
[5164]331 }
332 return build_values_element;
[6086]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) {
[5164]337 int count = 0;
[8783]338 File file = ((CollectionTreeNode)node).getFile();
[5164]339 if(file.isFile() && count_files) {
[6086]340 count++;
[5164]341 }
342 else if(file.isDirectory() && count_folders) {
[6086]343 count++;
[5164]344 }
[6325]345 for(int i = 0; !file.getName().equals("CVS") && i < node.getChildCount(); i++) {
[6086]346 count = count + getCount(node.getChildAt(i), count_folders, count_files);
[5164]347 }
348 return count;
[6086]349 }
350
351 /** Method to retrieve the current import options associated with this Collection. */
352 public Element getImportValues() {
[5164]353 Element import_values_element = null;
354 try {
[6086]355 Element document_element = document.getDocumentElement();
[8015]356 Element build_config_element = (Element) XMLTools.getNodeFromNamed(document_element, BUILD_CONFIG);
357 import_values_element = (Element) XMLTools.getNodeFromNamed(build_config_element, IMPORT);
[6086]358 build_config_element = null;
359 document_element = null;
[5164]360 }
361 catch (Exception error) {
[8236]362 DebugStream.printStackTrace(error);
[5164]363 }
364 return import_values_element;
[6086]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++) {
[6051]378 Element argument_element = (Element) arguments.item(i);
379 if(argument_element.getParentNode() == document_element) {
[6086]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 }
[6051]389 }
390 argument_element = null;
[6086]391 }
392 // Append it
393 if(!found) {
[6051]394 Element argument_element = document.createElement(ARGUMENT);
[6086]395 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
[6051]396 argument_element.appendChild(document.createTextNode(value));
397 document_element.appendChild(argument_element);
398 argument_element = null;
[6086]399 }
400 arguments = null;
401 document_element = null;
402 }
403 catch (Exception error) {
[8236]404 DebugStream.printStackTrace(error);
[6086]405 }
406 }
[5164]407}
Note: See TracBrowser for help on using the repository browser.