[6051] | 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 | */
|
---|
| 37 | package org.greenstone.gatherer.collection;
|
---|
| 38 |
|
---|
| 39 | import java.io.*;
|
---|
[19945] | 40 | import java.util.ArrayList;
|
---|
[25666] | 41 | import org.greenstone.gatherer.Configuration;
|
---|
[8236] | 42 | import org.greenstone.gatherer.DebugStream;
|
---|
[8247] | 43 | import org.greenstone.gatherer.Dictionary;
|
---|
[19220] | 44 | import org.greenstone.gatherer.Gatherer;
|
---|
[6051] | 45 | import org.greenstone.gatherer.cdm.CommandTokenizer;
|
---|
| 46 | import org.greenstone.gatherer.util.Codec;
|
---|
| 47 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
[14049] | 48 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[6051] | 49 | import org.w3c.dom.*;
|
---|
| 50 |
|
---|
| 51 | /** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
|
---|
| 52 | public class BasicCollectionConfiguration
|
---|
[6147] | 53 | implements Comparable {
|
---|
| 54 |
|
---|
| 55 | private File file;
|
---|
[7603] | 56 | private String creator = "";
|
---|
| 57 | private String description = "";
|
---|
| 58 | private String maintainer = "";
|
---|
| 59 | private String name = "";
|
---|
[14049] | 60 | private String is_public = "";
|
---|
[16267] | 61 | private String collectgroup = "false";
|
---|
[6147] | 62 |
|
---|
[19220] | 63 | private String short_name = "";
|
---|
| 64 |
|
---|
[7603] | 65 | private String site = null; // used for gs3 colls
|
---|
[7340] | 66 |
|
---|
[6147] | 67 | public BasicCollectionConfiguration(File file) {
|
---|
| 68 | this.file = file;
|
---|
| 69 | try {
|
---|
| 70 | String filename = file.getName().toLowerCase();
|
---|
[6224] | 71 | if(!file.exists()) {
|
---|
[6198] | 72 | return;
|
---|
| 73 | }
|
---|
[7603] | 74 | if(filename.endsWith(".cfg")) {
|
---|
[6850] | 75 | FileInputStream fis = new FileInputStream(file);
|
---|
| 76 | InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
|
---|
| 77 | BufferedReader br = new BufferedReader(isr);
|
---|
[6147] | 78 | String command = null;
|
---|
| 79 | while((command = br.readLine()) != null) {
|
---|
[19796] | 80 | if (command.startsWith("#")) {
|
---|
| 81 | // a comment
|
---|
| 82 | continue;
|
---|
| 83 | }
|
---|
[6147] | 84 | if(command.length() > 0) {
|
---|
| 85 | // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
|
---|
| 86 | while(command.trim().endsWith("\\")) {
|
---|
| 87 | command = command.substring(0, command.lastIndexOf("\\"));
|
---|
| 88 | String next_line = br.readLine();
|
---|
[7591] | 89 | if (next_line != null) {
|
---|
| 90 | command = command + "\n" + next_line;
|
---|
[6147] | 91 | }
|
---|
| 92 | next_line = null;
|
---|
[6051] | 93 | }
|
---|
[6147] | 94 | CommandTokenizer tokenizer = new CommandTokenizer(command, br);
|
---|
[8146] | 95 | String command_type_str = tokenizer.nextToken();
|
---|
[8147] | 96 | if (command_type_str == null) {
|
---|
| 97 | // Bad command. Do nothing
|
---|
| 98 | continue;
|
---|
[8146] | 99 | }
|
---|
[19796] | 100 |
|
---|
[8147] | 101 | command_type_str = command_type_str.toLowerCase();
|
---|
| 102 | if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
|
---|
[6147] | 103 | creator = tokenizer.nextToken();
|
---|
| 104 | }
|
---|
| 105 | else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
|
---|
| 106 | maintainer = tokenizer.nextToken();
|
---|
| 107 | }
|
---|
[16267] | 108 | else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTGROUP_STR)) {
|
---|
| 109 | collectgroup = tokenizer.nextToken();
|
---|
| 110 | }
|
---|
[6147] | 111 | else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
|
---|
| 112 | String meta_type_str = tokenizer.nextToken();
|
---|
| 113 | String value_str = tokenizer.nextToken();
|
---|
| 114 | // check for language
|
---|
| 115 | String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
|
---|
| 116 | if(meta_type_str != null && value_str != null) {
|
---|
| 117 | meta_type_str = meta_type_str.toLowerCase();
|
---|
| 118 | if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
|
---|
| 119 | language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
|
---|
| 120 | language_str = language_str.toLowerCase();
|
---|
| 121 | value_str = tokenizer.nextToken();
|
---|
| 122 | }
|
---|
| 123 | // now we can work out which coll meta we are dealing with
|
---|
| 124 | if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
|
---|
| 125 | // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
|
---|
[8247] | 126 | if (name == null || language_str.equals(Dictionary.getLanguage())) {
|
---|
[6147] | 127 | name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
|
---|
| 131 | // Again we are either looking for the first description, then after that a language specific one
|
---|
[8247] | 132 | if (description == null || language_str.equals(Dictionary.getLanguage())) {
|
---|
[6147] | 133 | description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | language_str = null;
|
---|
| 138 | value_str = null;
|
---|
| 139 | meta_type_str = null;
|
---|
[7603] | 140 | } else {
|
---|
| 141 | // we want to process all the tokens to make sure we get rid of multi line commands before trying to find the next one
|
---|
| 142 | while (tokenizer.hasMoreTokens()) {
|
---|
| 143 | tokenizer.nextToken();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[6147] | 146 | } // end of coll meta bit
|
---|
| 147 | command_type_str = null;
|
---|
| 148 | tokenizer = null;
|
---|
| 149 | } // if command.length > 0
|
---|
| 150 | } // while
|
---|
| 151 | command = null;
|
---|
| 152 | br.close();
|
---|
[6850] | 153 | isr.close();
|
---|
[6147] | 154 | br = null;
|
---|
[6850] | 155 | isr = null;
|
---|
[6147] | 156 | } // cfg file
|
---|
| 157 | ///ystem.err.println("Parsed collect.cfg");
|
---|
| 158 | ///ystem.err.println("name = " + name);
|
---|
| 159 | ///ystem.err.println("creator = " + creator);
|
---|
| 160 | ///ystem.err.println("maintainer = " + maintainer);
|
---|
| 161 | ///ystem.err.println("description = " + description);
|
---|
[14049] | 162 |
|
---|
| 163 | if(filename.endsWith(".xml")) {
|
---|
| 164 |
|
---|
| 165 | Document xml_file_doc = XMLTools.parseXMLFile(file);
|
---|
| 166 | Element root = xml_file_doc.getDocumentElement();
|
---|
| 167 |
|
---|
[19945] | 168 | Node metadataListNode = XMLTools.getChildByTagName(root, StaticStrings.METADATALIST_STR);
|
---|
| 169 | Node displayItemListNode = XMLTools.getChildByTagName(root, StaticStrings.DISPLAYITEMLIST_STR);
|
---|
| 170 |
|
---|
[25666] | 171 | // Get the metadataList and displayItemList metadata for the current gli language
|
---|
| 172 | String default_language = StaticStrings.ENGLISH_LANGUAGE_STR; // is this always the case?
|
---|
| 173 | String gli_language_code = Configuration.getLanguage(); // definitely returns some language code
|
---|
| 174 |
|
---|
[19945] | 175 | if (metadataListNode != null) {
|
---|
| 176 | ArrayList metadata_list = XMLTools.getChildElementsByTagName((Element)metadataListNode, StaticStrings.METADATA_STR);
|
---|
[25666] | 177 |
|
---|
| 178 | String fallback_lang_creator = "";
|
---|
| 179 | String fallback_lang_maintainer = "";
|
---|
| 180 | String fallback_lang_is_public = "";
|
---|
| 181 |
|
---|
[19945] | 182 | for (int i=0; i<metadata_list.size(); i++) {
|
---|
| 183 | Element meta = (Element) metadata_list.get(i);
|
---|
| 184 | String meta_name = meta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
[25666] | 185 |
|
---|
| 186 | // want to get the metadata matching GLI's language, or else fall back on the default language
|
---|
| 187 | // version, or else fall back onto the first value for any language provided for the metadata
|
---|
| 188 | String meta_lang = meta.getAttribute(StaticStrings.LANG_ATTRIBUTE); // returns "" if no such attribute
|
---|
| 189 | if(meta_lang.equals("")) {
|
---|
| 190 | meta_lang = default_language;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[19945] | 193 | if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
|
---|
[25666] | 194 | if (meta_lang.equals(gli_language_code)) { // metadata's language matched the requested language
|
---|
| 195 | creator = XMLTools.getNodeText(meta);
|
---|
| 196 | } else if (meta_lang.equals(default_language)) { // meta_lang matched fall back language
|
---|
| 197 | fallback_lang_creator = XMLTools.getNodeText(meta);
|
---|
| 198 | } else if (creator.equals("") && fallback_lang_creator.equals("")) { // resort to first value for meta we can find
|
---|
| 199 | fallback_lang_creator = XMLTools.getNodeText(meta);
|
---|
| 200 | }
|
---|
[19945] | 201 | } else if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
|
---|
[25666] | 202 | if (meta_lang.equals(gli_language_code)) {
|
---|
| 203 | maintainer = XMLTools.getNodeText(meta);
|
---|
| 204 | } else if (meta_lang.equals(default_language)) {
|
---|
| 205 | fallback_lang_maintainer = XMLTools.getNodeText(meta);
|
---|
| 206 | } else if (maintainer.equals("") && fallback_lang_maintainer.equals("")) { // resort to first value for meta we can find
|
---|
| 207 | fallback_lang_maintainer = XMLTools.getNodeText(meta);
|
---|
| 208 | }
|
---|
[19945] | 209 | } else if (meta_name.equals(StaticStrings.COLLECTIONMETADATA_PUBLIC_STR)) {
|
---|
[25666] | 210 | if (meta_lang.equals(gli_language_code)) {
|
---|
| 211 | is_public = XMLTools.getNodeText(meta);
|
---|
| 212 | } else if (meta_lang.equals(default_language)) {
|
---|
| 213 | fallback_lang_is_public = XMLTools.getNodeText(meta);
|
---|
| 214 | } else if (is_public.equals("") && fallback_lang_is_public.equals("")) { // resort to first value for meta we can find
|
---|
| 215 | fallback_lang_is_public = XMLTools.getNodeText(meta);
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
[19945] | 218 | }
|
---|
[25666] | 219 | // if no metadata found for the requested language, use the one set in the default language
|
---|
| 220 | if(creator.equals("")) {
|
---|
| 221 | creator = fallback_lang_creator;
|
---|
| 222 | }
|
---|
| 223 | if(maintainer.equals("")) {
|
---|
| 224 | maintainer = fallback_lang_maintainer;
|
---|
| 225 | }
|
---|
| 226 | if(is_public.equals("")) {
|
---|
| 227 | is_public = fallback_lang_is_public;
|
---|
| 228 | }
|
---|
[14049] | 229 | }
|
---|
[25666] | 230 |
|
---|
| 231 |
|
---|
[19945] | 232 | if (displayItemListNode != null) {
|
---|
| 233 | ArrayList display_list = XMLTools.getChildElementsByTagName((Element)displayItemListNode, StaticStrings.DISPLAYITEM_STR);
|
---|
[25666] | 234 |
|
---|
| 235 | String fallback_lang_name = "";
|
---|
| 236 | String fallback_lang_description = "";
|
---|
| 237 |
|
---|
[19945] | 238 | for (int i=0; i<display_list.size(); i++) {
|
---|
| 239 | Element meta = (Element) display_list.get(i);
|
---|
| 240 | String meta_name = meta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
[25666] | 241 | String meta_lang = meta.getAttribute(StaticStrings.LANG_ATTRIBUTE);
|
---|
| 242 | if(meta_lang.equals("")) {
|
---|
| 243 | meta_lang = default_language;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[19945] | 246 | if (meta_name.equals(StaticStrings.NAME_STR)) {
|
---|
[25666] | 247 | if (meta_lang.equals(gli_language_code)) {
|
---|
| 248 | name = XMLTools.getNodeText(meta);
|
---|
| 249 | } else if (meta_lang.equals(default_language)) {
|
---|
| 250 | fallback_lang_name = XMLTools.getNodeText(meta);
|
---|
| 251 | } else if (name.equals("") && fallback_lang_name.equals("")) {
|
---|
| 252 | fallback_lang_name = XMLTools.getNodeText(meta);
|
---|
| 253 | }
|
---|
[19945] | 254 | } else if (meta_name.equals(StaticStrings.DESCRIPTION_STR)) {
|
---|
[25666] | 255 | if (meta_lang.equals(gli_language_code)) {
|
---|
| 256 | description = XMLTools.getNodeText(meta);
|
---|
| 257 | } else if (meta_lang.equals(default_language)) {
|
---|
| 258 | fallback_lang_description = XMLTools.getNodeText(meta);
|
---|
| 259 | } else if (description.equals("") && fallback_lang_description.equals("")) {
|
---|
| 260 | fallback_lang_description = XMLTools.getNodeText(meta);
|
---|
| 261 | }
|
---|
[19945] | 262 | }
|
---|
| 263 |
|
---|
| 264 | }
|
---|
[25666] | 265 |
|
---|
| 266 | if(name.equals("")) {
|
---|
| 267 | name = fallback_lang_name;
|
---|
| 268 | }
|
---|
| 269 | if(description.equals("")) {
|
---|
| 270 | description = fallback_lang_description;
|
---|
| 271 | }
|
---|
[19945] | 272 | }
|
---|
[14049] | 273 | }
|
---|
[19945] | 274 |
|
---|
[6051] | 275 | }
|
---|
[6147] | 276 | catch(Exception error) {
|
---|
[8236] | 277 | DebugStream.println("Error in CollectionConfiguration.<init>(): " + error);
|
---|
| 278 | DebugStream.printStackTrace(error);
|
---|
[6051] | 279 | }
|
---|
[14049] | 280 |
|
---|
[6147] | 281 | }
|
---|
[6051] | 282 |
|
---|
[6147] | 283 | /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
|
---|
| 284 | * @param other the other Object which is presumably another basic collection configuration
|
---|
| 285 | * @return an integer which is either <0, 0 or >0 if this configuration is naturally less than, equal to or greater than the target object
|
---|
| 286 | */
|
---|
| 287 | public int compareTo(Object other) {
|
---|
| 288 | if(other == null) {
|
---|
| 289 | return -1;
|
---|
[6051] | 290 | }
|
---|
[6147] | 291 | return toString().compareTo(other.toString());
|
---|
| 292 | }
|
---|
[6051] | 293 |
|
---|
[6147] | 294 | public boolean equals(Object other) {
|
---|
| 295 | return (compareTo(other) == 0);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /** Retrieve the creators email for this collection.
|
---|
| 299 | * @return a String
|
---|
| 300 | */
|
---|
| 301 | public String getCreator() {
|
---|
[7603] | 302 | return creator;
|
---|
[6147] | 303 | }
|
---|
[6051] | 304 |
|
---|
[6147] | 305 | public String getDescription() {
|
---|
[7603] | 306 | return description;
|
---|
[6147] | 307 | }
|
---|
[6051] | 308 |
|
---|
[6147] | 309 | public File getFile() {
|
---|
| 310 | return file;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | public String getMaintainer() {
|
---|
[7603] | 314 | return maintainer;
|
---|
[6147] | 315 | }
|
---|
[6051] | 316 |
|
---|
[6147] | 317 | public String getName() {
|
---|
[7603] | 318 | return name;
|
---|
[6147] | 319 | }
|
---|
[14049] | 320 |
|
---|
| 321 | public String getIsPublic() {
|
---|
| 322 | return is_public;
|
---|
| 323 | }
|
---|
[16267] | 324 |
|
---|
| 325 | public String getCollectGroup() {
|
---|
| 326 | return collectgroup;
|
---|
| 327 | }
|
---|
[6147] | 328 |
|
---|
| 329 | /** Retrieve the short name for this collection which, given this current file is in <col_name>/etc/collect.cfg, is the name of this file's parent file's parent.
|
---|
| 330 | * @return the short name of this collection as a String
|
---|
| 331 | */
|
---|
| 332 | public String getShortName() {
|
---|
[19220] | 333 | if (short_name.equals("")) {
|
---|
| 334 | String collectDir = Gatherer.getCollectDirectoryPath();
|
---|
| 335 | String currentCollDir = file.getParentFile().getParentFile().getAbsolutePath();
|
---|
| 336 | if (currentCollDir.startsWith(collectDir)) {
|
---|
| 337 | short_name = currentCollDir.substring(collectDir.length());
|
---|
[22455] | 338 | short_name = short_name.replace('\\', '/');
|
---|
[19220] | 339 | } else {
|
---|
| 340 | short_name = file.getParentFile().getParentFile().getName();
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | return short_name;
|
---|
| 344 |
|
---|
[6147] | 345 | }
|
---|
| 346 |
|
---|
[7340] | 347 | /** set the site for this coll */
|
---|
| 348 | public void setSite(String site) {
|
---|
| 349 | this.site = site;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 |
|
---|
[6147] | 353 | /** Display the title for this collection. */
|
---|
| 354 | public String toString() {
|
---|
[7340] | 355 | if (this.site == null) {
|
---|
| 356 | return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
|
---|
| 357 | } else {
|
---|
| 358 | return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName()+", "+this.site + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
|
---|
| 359 | }
|
---|
[6147] | 360 | }
|
---|
[6051] | 361 | }
|
---|