- Timestamp:
- 2003-07-15T13:55:22+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMetaManager.java
r4675 r4932 6 6 * University of Waikato, New Zealand. 7 7 * 8 * <BR><BR>9 *10 8 * Author: John Thompson, Greenstone Digital Library, University of Waikato 11 9 * 12 * <BR><BR>13 *14 10 * Copyright (C) 1999 New Zealand Digital Library Project 15 *16 * <BR><BR>17 11 * 18 12 * This program is free software; you can redistribute it and/or modify … … 21 15 * (at your option) any later version. 22 16 * 23 * <BR><BR>24 *25 17 * This program is distributed in the hope that it will be useful, 26 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 27 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 20 * GNU General Public License for more details. 29 *30 * <BR><BR>31 21 * 32 22 * You should have received a copy of the GNU General Public License … … 35 25 *######################################################################## 36 26 */ 37 38 39 40 41 42 43 /* GPL_HEADER */44 27 package org.greenstone.gatherer.cdm; 45 /**************************************************************************************46 * Title: Gatherer47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.48 * Copyright: Copyright (c) 200149 * Company: The University of Waikato50 * Written: /05/0251 * Revised: 22/08/02 Revamped, Optimized and Commented.52 **************************************************************************************/53 28 import java.awt.*; 54 29 import java.awt.event.*; … … 59 34 import org.greenstone.gatherer.cdm.CollectionDesignManager; 60 35 import org.greenstone.gatherer.cdm.CollectionMeta; 36 import org.greenstone.gatherer.cdm.DOMProxyListModel; 61 37 import org.greenstone.gatherer.cdm.Index; 62 import org.greenstone.gatherer.cdm.Language;63 38 import org.greenstone.gatherer.gui.EditorDialog; 39 import org.greenstone.gatherer.util.StaticStrings; 64 40 import org.greenstone.gatherer.util.Utility; 41 import org.w3c.dom.*; 65 42 /** This class is responsible for maintaining a list of assigned collection level metadata, and for allows manipulations on the aforementioned data. 66 43 * @author John Thompson, Greenstone Digital Library, University of Waikato 67 * @version 2.3 44 * @version 2.3d 68 45 */ 69 // #################################################################################### 70 // Optimization Saving 71 // #################################################################################### 72 // Removed Vector and Hashtable + Memory, + Processor 73 // #################################################################################### 74 public class CollectionMetaManager 75 extends DefaultListModel { 76 /** A reference to the cdm manager so we can access indexes and languages. */ 77 private CollectionDesignManager manager = null; 78 /** A reference to ourself so that inner classes can use us as a model. */ 79 private DefaultListModel model = null; 80 /** A reference to the Gatherer. */ 81 private Gatherer gatherer = null; 82 /** The language the most recent metadata returned was in. */ 83 private Language current_language = null; 84 /** We can't safely parse metadata commands until after all the other commands have been parsed, so we store commands here for now. */ 85 private ArrayList unresolved_commands = null; 86 /** Constructor. 87 * @param gatherer A reference to the <strong>Gatherer</strong>. 88 * @param manager A reference to the <strong>CollectionDesignManager</strong> for access to other configuration managers. 89 */ 90 public CollectionMetaManager(Gatherer gatherer, CollectionDesignManager manager) { 91 super(); 92 this.gatherer = gatherer; 93 this.manager = manager; 94 this.model = this; 95 this.unresolved_commands = new ArrayList(); 46 public class CollectionMetaManager 47 extends DOMProxyListModel { 48 49 /** Constructor. */ 50 public CollectionMetaManager() { 51 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.COLLECTIONMETADATA_ELEMENT, new CollectionMeta("")); 52 Gatherer.println("CollectionMetaManager: " + getSize() + " metadata parsed."); 96 53 } 97 54 /** Method to add a new piece of metadata. 98 * @param metadata The new <strong>CollectionMeta</strong>. 99 */ 100 public void addMetadata(CollectionMeta metadata) { 101 CollectionMeta existing = getMetadata(metadata.getName().toString(), metadata.getLanguage(), false); 102 if(existing != null) { 103 removeElement(existing); 55 * @param metadata the new CollectionMeta 56 */ 57 public void addMetadatum(CollectionMeta metadata) { 58 if(!contains(metadata)) { 59 Element element = metadata.getElement(); 60 // Locate where we should insert this new subcollection. 61 Node target_node = CollectionConfiguration.findInsertionPoint(element); 62 // Failing that we insert immediately after a language string 63 add(root, metadata, target_node); 64 Gatherer.c_man.configurationChanged(); 104 65 } 105 addElement(metadata); 106 gatherer.c_man.configurationChanged(); 66 Gatherer.c_man.configurationChanged(); 107 67 } 108 /** Retrieve the collectionextra metadata in the default language, if present. 109 * @return A <strong>CollectionMeta</strong> containing the collectionextra in the default language if present, or else the first collectionextra of any language, otherwise <i>null</i>. 110 * @see org.greenstone.gatherer.cdm.Language 111 */ 112 public CollectionMeta getCollectionExtra() { 113 CollectionMeta result = getMetadata("collectionextra", Gatherer.config.interface_language, true); 114 if(result == null) { 115 result = new CollectionMeta(manager, "collectionextra", Gatherer.config.interface_language, ""); 116 addMetadata(result); 68 69 public CollectionMeta get(int i) { 70 return (CollectionMeta) getElementAt(i); 71 } 72 73 /** Retrieve all of the general metadata. */ 74 public ArrayList getMetadata() { 75 ArrayList result = new ArrayList(); 76 int size = getSize(); 77 for(int i = 0; i < size; i++) { 78 CollectionMeta metadata = (CollectionMeta) getElementAt(i); 79 if(!metadata.getName().startsWith(StaticStrings.STOP_CHARACTER)) { 80 result.add(metadata); 81 } 117 82 } 118 83 return result; 119 84 } 120 /** Retrieve the collectionname metadata in the default language, if present.121 * @return A <strong>CollectionMeta</strong> containing the collectionname in the default language if present, or else the first collectionname of any language, otherwise <i>null</i>.122 * @see org.greenstone.gatherer.cdm.Language123 */124 public CollectionMeta getCollectionName() {125 return getMetadata("collectionname", Gatherer.config.interface_language, true);126 }127 /** Retrieve the iconcollection metadata in the default language, if present.128 * @return A <strong>CollectionMeta</strong> containing the iconcollection in the default language if present, or else the first iconcollection of any language, otherwise <i>null</i>.129 * @see org.greenstone.gatherer.cdm.Language130 */131 public CollectionMeta getIconCollection() {132 CollectionMeta result = getMetadata("iconcollection", Gatherer.config.interface_language, true);133 if(result == null) {134 result = new CollectionMeta(manager, "iconcollection", Gatherer.config.interface_language, "");135 addMetadata(result);136 }137 return result;138 }139 /** Retrieve the iconcollection metadata in the default language, if present.140 * @return A <strong>CollectionMeta</strong> containing the iconcollectionsmall in the default language if present, or else the first iconcollectionsmall of any language, otherwise <i>null</i>.141 * @see org.greenstone.gatherer.cdm.Language142 */143 public CollectionMeta getIconCollectionSmall() {144 CollectionMeta result = getMetadata("iconcollectionsmall", Gatherer.config.interface_language, true);145 if(result == null) {146 result = new CollectionMeta(manager, "iconcollectionsmall", Gatherer.config.interface_language, "");147 addMetadata(result);148 }149 return result;150 }151 /** Method to retrieve the list of metadata.152 * @return An <strong>ArrayList</strong> containing the metadata.153 */154 public ArrayList getMetadata() {155 ArrayList metadata = new ArrayList();156 for(int i = 0; i < size(); i++) {157 metadata.add(get(i));158 }159 Collections.sort(metadata);160 return metadata;161 }162 85 163 86 /** Retrieve all of the metadata for the given feature, regardless of language. */ 164 public ArrayList getMetadata( Objectname) {87 public ArrayList getMetadata(String name) { 165 88 ArrayList result = new ArrayList(); 166 for(int i = 0; i < size(); i++) { 167 CollectionMeta metadata = (CollectionMeta) get(i); 89 int size = getSize(); // Refresh DOM Model 90 for(int i = 0; i < size; i++) { 91 CollectionMeta metadata = (CollectionMeta) getElementAt(i); 168 92 if(metadata.getName().equals(name)) { 169 93 result.add(metadata); … … 172 96 return result; 173 97 } 98 99 /** Retrieve the named piece of metadata, in the default language, if available. If no such metadata is available then it is created. 100 * @param name the name of the metadatum to retrieve as a String 101 * @return the dom Element containing the specified metadatum 102 */ 103 public CollectionMeta getMetadatum(String name) { 104 return getMetadatum(name, true); 105 } 106 107 public CollectionMeta getMetadatum(String name, boolean add_if_not_found) { 108 ///atherer.println("Get the metadata for " + name + " in the default language."); 109 int size = getSize(); 110 for(int i = 0; i < size; i++) { 111 CollectionMeta metadatum = (CollectionMeta) getElementAt(i); 112 if(metadatum.getName().equals(name) && metadatum.getLanguage().equals(Gatherer.config.interface_language)) { 113 ///atherer.println("Found '" + metadatum + "'"); 114 return metadatum; 115 } 116 else { 117 ///atherer.println("No match with: " + metadatum.getName() + " [l=" + metadatum.getLanguage() + "] \"" + metadatum.getValue() + "\""); 118 } 119 metadatum = null; 120 } 121 if(add_if_not_found) { 122 CollectionMeta result = new CollectionMeta(name); 123 addMetadatum(result); 124 ///atherer.println("Added new metadata."); 125 return result; 126 } 127 else { 128 return null; 129 } 130 } 131 174 132 /** Method to retrieve a certain piece of metadata based on its name and language. 175 176 * @param language The <strong>Language</strong>of the metadata.177 178 179 180 public CollectionMeta getMetadata( Object name, Languagelanguage, boolean partial) {133 * @param name the name of the metadata as an Object (as it may actually be a refernce to an Index or SubIndex) 134 * @param language the language of the metadata. 135 * @param partial <i>true</i> to return the first partial match (ie matches name but not language). 136 * @return The <strong>CollectionMeta</strong> requested, or <i>null</i> if no such metadata. 137 */ 138 public CollectionMeta getMetadata(String name, String language, boolean partial) { 181 139 CollectionMeta partial_match = null; 182 for(int i = 0; i < size(); i++) {183 CollectionMeta metadata = (CollectionMeta) get (i);140 for(int i = 0; i < getSize(); i++) { 141 CollectionMeta metadata = (CollectionMeta) getElementAt(i); 184 142 Object metadata_name = metadata.getName(); 185 143 // We test the case of an object match (ie Index to Index)... 186 144 if(metadata_name.equals(name)) { 187 if (metadata.getLanguage().equals(language)) {188 return metadata;189 }190 partial_match = metadata;191 }192 // But if that fails we also try a string match such that "section:dls.Title" == Index193 if(metadata_name.toString().equals(name.toString())) {194 145 if (metadata.getLanguage().equals(language)) { 195 146 return metadata; … … 203 154 return null; 204 155 } 205 /** Method that attempts to parse a collection metadata command from the given text. If a command is parsed successfully it is immediately added to the the collections metadata. 206 * @param command The command text we wish to parse, as a <strong>String</strong>. 207 * @return A <i>boolean</i> which is <i>true</i> if a collection metadata command was successfully parsed, <i>false</i> otherwise. 208 * @see org.greenstone.gatherer.cdm.CollectionDesignManager 209 * @see org.greenstone.gatherer.cdm.CommandTokenizer 210 * @see org.greenstone.gatherer.cdm.Language 211 * @see org.greenstone.gatherer.cdm.LanguageManager 212 * @see org.greenstone.gatherer.util.Utility 213 */ 214 public boolean parse(String command, boolean finished) { 215 String temp = command.toLowerCase(); 216 if(temp.startsWith("collectionmeta")) { 217 if(finished) { 218 CommandTokenizer ct = new CommandTokenizer(command); 219 if(ct.countTokens() >= 3) { 220 ct.nextToken(); // Throw away collectionmeta 221 Object key = ct.nextToken(); 222 Language language = null; 223 String language_code = null; 224 String value = ct.nextToken(); 225 // Arg. Remember a language token will be '[l=<code>]' 226 if(value.startsWith("[") && value.endsWith("]")) { 227 language_code = value.substring(3, value.length() - 1); 228 ///ystem.err.println("Language code = " + language_code); 229 value = ct.nextToken(); 230 } 231 // Check if the key is an index, and if so retrieve it. 232 if(((String)key).startsWith(".")) { 233 ///ystem.err.println("Detected '.' prefix."); 234 String key_str = (String)key; 235 key = null; 236 key_str = key_str.substring(1); 237 Index ind = Index.parseIndexConfig(key_str, manager); 238 if (ind != null) { 239 240 key = manager.indexes.getIndex(ind.toString(false)); 241 } 242 243 // If that didn't work, then it might be a subindex so have a bash at retrieving that instead. 244 if(key == null) { 245 key = manager.subcollections.getSubIndex(key_str); 246 //if(key != null) { 247 ///ystem.err.println("Its a subindex."); 248 //} 249 //else { 250 ///ystem.err.println("Error Will Robinson."); 251 //} 252 } 253 //else { 254 ///ystem.err.println("Its an index."); 255 //} 256 } 257 // An if we have a language code, retrieve its object too. 258 if(language_code != null) { 259 language = manager.languages.getLanguage(language_code, false); 260 } 261 // Otherwise set language to the default language. 262 else { 263 language = Gatherer.config.interface_language; 264 } 265 if(key != null) { 266 // Trim any " 267 if(value.equals("\"\"")) { 268 value = ""; 269 } 270 else { 271 if(value.startsWith("\"")) { 272 value = value.substring(1); 273 } 274 if(value.endsWith("\"")) { 275 value = value.substring(0, value.length() - 1); 276 } 277 } 278 CollectionMeta meta = new CollectionMeta(manager, key, language, Utility.decodeGreenstone(value)); 279 addMetadata(meta); 280 } 281 return true; 156 157 /** Method to remove a piece of metadata. 158 * @param metadata metadata 159 */ 160 public void removeMetadata(CollectionMeta metadata) { 161 if(metadata != null) { 162 String name = metadata.getName(); 163 String language = metadata.getLanguage(); 164 for(int i = 0; i < getSize(); i++) { 165 CollectionMeta other = (CollectionMeta) getElementAt(i); 166 if(name.equals(other.getName()) && language.equals(other.getLanguage())) { 167 remove(i); 168 Gatherer.c_man.configurationChanged(); 169 return; 282 170 } 283 } 284 else { 285 unresolved_commands.add(command); 286 return true; 287 } 171 other = null; 172 } 173 language = null; 174 name = null; 288 175 } 289 return false;290 }291 /** Ensure that the values being showed are the most up to date. */292 public void refresh() {293 fireContentsChanged(this, 0, size());294 176 } 295 177 296 /** Method to remove a piece of metadata. 297 * @param metadata The <strong>CollectionMeta</strong> you wish to remove. 298 */ 299 public void removeMetadata(CollectionMeta metadata) { 300 removeElement(metadata); 301 gatherer.c_man.configurationChanged(); 302 } 303 /** Method which attempts to reparse obvious metadata commands which used unresovable references. 304 */ 305 public void reparseUnresolved() { 306 for(int i = 0; i < unresolved_commands.size(); i++) { 307 parse((String)unresolved_commands.get(i), true); 178 /** Removes all of the metadata with a certain name, regardless of language or value. */ 179 public void removeMetadata(String name) { 180 boolean removed_entry = false; 181 for(int i = getSize(); i != 0; i--) { 182 CollectionMeta other = (CollectionMeta) getElementAt(i - 1); 183 if(name.equals(other.getName())) { 184 remove(i - 1); 185 removed_entry = true; 186 } 187 other = null; 188 } 189 if(removed_entry) { 190 Gatherer.c_man.configurationChanged(); 308 191 } 309 // Regardless of if they work, clear the commands.310 unresolved_commands.clear();311 }312 /** Sets the value of a certain metadata.313 * @param name The name of the metadata as a <strong>String</strong>.314 * @param language The <strong>Language</strong> to use.315 * @param value The value of the metadata also as a <strong>String</strong>.316 */317 public void setMetadata(String name, Language language, String value) {318 addMetadata(new CollectionMeta(manager, name, language, value));319 }320 /** Method to produce the list of metadata in a string such as you would find in the collection configuration file.321 * @return A <strong>String</strong> containing the list of collection metadata.322 */323 public String toString() {324 // We sort the metadata first.325 ArrayList metadatum = new ArrayList();326 for(int i = 0; i < size(); i++) {327 metadatum.add(get(i));328 }329 Collections.sort(metadatum, new CollectionMetaComparator());330 // Now we print them331 StringBuffer text = new StringBuffer("");332 for(int i = 0; i < metadatum.size(); i++) {333 CollectionMeta data = (CollectionMeta) metadatum.get(i);334 if(data.valid()) {335 text.append(data.toString());336 }337 }338 metadatum = null;339 text.append("\n");340 return text.toString();341 }342 /** Overloaded to call get with both a key and an empty argument array.343 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.344 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.345 */346 private String get(String key) {347 return get(key, null);348 }349 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>350 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.351 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.352 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.353 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.354 * @see org.greenstone.gatherer.Gatherer355 * @see org.greenstone.gatherer.Dictionary356 */357 private String get(String key, String args[]) {358 if(key.indexOf('.') == -1) {359 key = "CDM.CollectionMetaManager." + key;360 }361 return gatherer.dictionary.get(key, args);362 192 } 363 193 364 private class CollectionMetaComparator 365 implements Comparator { 366 public int compare(Object o1, Object o2) { 367 String s1 = o1.toString(); 368 String s2 = o2.toString(); 369 if(s1.startsWith(".")) { 370 return 1; 371 } 372 else if(s2.startsWith(".")) { 373 return -1; 374 } 375 return o1.toString().compareTo(o2.toString()); 376 } 377 public boolean equals(Object o2) { 378 return (compare(this, o2) == 0); 379 } 194 public int size() { 195 return getSize(); 380 196 } 381 197 }
Note:
See TracChangeset
for help on using the changeset viewer.