Changeset 4932 for trunk/gli/src/org/greenstone/gatherer/cdm/Index.java
- 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/Index.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 27 package org.greenstone.gatherer.cdm; 44 /************************************************************************************** 45 * Title: Gatherer 46 * Description: The Gatherer: a tool for gathering and enriching a digital collection. 47 * Copyright: Copyright (c) 2001 48 * Company: The University of Waikato 49 * Written: 03/05/02 50 * Revised: 17/11/02 51 **************************************************************************************/ 52 import java.util.Collections; 53 import java.util.Comparator; 54 import java.util.StringTokenizer; 55 import java.util.Vector; 28 29 import java.util.*; 30 import org.greenstone.gatherer.Gatherer; 31 import org.greenstone.gatherer.cdm.CollectionConfiguration; 32 import org.greenstone.gatherer.cdm.CollectionDesignManager; 56 33 import org.greenstone.gatherer.cdm.CollectionMeta; 34 import org.greenstone.gatherer.cdm.DOMProxyListEntry; 35 import org.greenstone.gatherer.msm.ElementWrapper; 57 36 import org.greenstone.gatherer.msm.MSMUtils; 37 import org.greenstone.gatherer.util.StaticStrings; 58 38 import org.greenstone.gatherer.util.Utility; 59 import org.w3c.dom. Element;39 import org.w3c.dom.*; 60 40 /** This class encapsulates a single indexing pair. 61 41 * @author John Thompson, Greenstone Digital Library, University of Waikato 62 * @version 2. 242 * @version 2.3d 63 43 */ 64 44 public class Index 65 implements Comparable { 66 /** A refernce to the main manager for access to other sub-managers. */ 67 private CollectionDesignManager manager = null; 68 /** The level of this index. */ 69 private int level = 0; 70 /** The sources for data this index is built apon, which are either fully qualified metadata element names or 'text'. */ 71 private Vector sources = null; 72 /** An element in the index level enumeration. */ 73 static public final int DOCUMENT = 0; 74 /** An element in the index level enumeration. */ 75 static public final int PARAGRAPH = 1; 76 /** An element in the index level enumeration. */ 77 static public final int SECTION = 2; 45 implements Comparable, DOMProxyListEntry { 78 46 /** An values of items in the index level enumeration. */ 79 47 static public final String LEVEL[] = {"document","paragraph","section"}; 80 48 81 /** Constructor. 82 * @param level The level of this index as a <strong>String</string>. 83 * @param metadata The fully qualified name of the metadata this index is built on as a <strong>String</strong>, or <i>null</i> in which case the data defaults to "text". 84 */ 85 public Index(int level, Vector sources, CollectionDesignManager manager) { 49 private ArrayList sources = null; 50 /** The level of this index (if old sckool MG). */ 51 private int level = -1; 52 /** The element this index is based upon. */ 53 private Element element = null; 54 /** The unique, if cryptic, identifier of an index. */ 55 private String id = null; 56 57 /** Default constructor, which should only be used during DOMProxyListModel creation. */ 58 public Index() { 59 } 60 61 /** Constructor. */ 62 public Index(Element element) { 63 this.element = element; 64 } 65 66 /** Constructor for a newly assigned index. */ 67 public Index(ArrayList sources) { 68 this.sources = sources; 69 // Create a new element 70 Document document = CollectionDesignManager.collect_config.document; 71 element = document.createElement(CollectionConfiguration.INDEX_ELEMENT); 72 // For each source add a content element 73 int size = sources.size(); 74 for(int i = 0; i < size; i++) { 75 Element content_element = document.createElement(CollectionConfiguration.CONTENT_ELEMENT); 76 Object source_object = sources.get(i); 77 if(source_object instanceof ElementWrapper) { 78 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, ((ElementWrapper)source_object).getName()); 79 } 80 else { 81 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString()); 82 } 83 element.appendChild(content_element); 84 content_element = null; 85 } 86 document = null; 87 } 88 89 /** Constructor for a newly assigned index with specified level (old skool). */ 90 public Index(int level, ArrayList sources) { 91 this(sources); 86 92 this.level = level; 87 this.manager = manager; 88 this.sources = sources; 89 if(this.sources == null) { 90 this.sources = new Vector(); 91 this.sources.add("text"); 92 } 93 } 93 element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[level]); 94 } 95 94 96 /** Method to compare two indexes. 95 96 97 98 97 * @param object The other index as an <strong>Object</strong>. 98 * @return An <i>int</i> which indicates how the indexes compare. 99 * @see java.lang.String 100 */ 99 101 public int compareTo(Object object) { 100 return toString(false).compareTo(object.toString()); 101 } 102 return id.compareTo(((Index)object).getID()); 103 } 104 105 public DOMProxyListEntry create(Element element) { 106 return new Index(element); 107 } 108 102 109 /** Method to test for the equality of two indexes. 103 104 105 110 * @param object The other index as an <strong>Object</strong>. 111 * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise. 112 */ 106 113 public boolean equals(Object object) { 107 if(compareTo(object) == 0) { 108 return true; 109 } 110 return false; 111 } 112 /** Method to get the data source of this index. 113 * @return A <strong>String</string> which is a comma separated list of either fully qualified names of an assigned metadata elements, or "text". 114 */ 115 public String getData() { 116 String result = ""; 117 Collections.sort(sources, new IndexComparator()); 118 for(int i = 0; i < sources.size(); i++) { 119 result = result + sources.get(i).toString(); 120 if(i < sources.size() - 1) { 121 result = result + ","; 122 } 123 } 124 return result; 125 } 126 /** Method to get the data source of this index. 127 * @return A <strong>String</string> which is a comma separated list of either fully qualified names of an assigned metadata elements, or "text". 128 * Note, greenstone extracted metadata do not have the ex. 129 */ 130 public String getDataConfig() { 131 String result = ""; 132 Collections.sort(sources, new IndexComparator()); 133 for(int i = 0; i < sources.size(); i++) { 134 String source = sources.get(i).toString(); 135 if (source.startsWith(Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP)) { 136 // remove the ex. bit 137 source = source.substring(source.indexOf(MSMUtils.NS_SEP)+1); 138 } 139 result = result + source; 140 if(i < sources.size() - 1) { 141 result = result + ","; 142 } 143 } 144 return result; 145 } 146 114 return (compareTo(object) == 0); 115 } 116 117 public Element getElement() { 118 return element; 119 } 120 147 121 /** Method to get the value of level. 148 * @return The value of level as a <strong>String</strong>. 149 122 * @return the level as a int 123 */ 150 124 public int getLevel() { 125 if(level == -1) { 126 String level_str = element.getAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE); 127 for(int i = 0; level == -1 && i < LEVEL.length; i++) { 128 if(level_str.equals(LEVEL[i])) { 129 level = i; 130 } 131 } 132 level_str = null; 133 } 151 134 return level; 152 135 } 153 /** Method to get the value of metadata. 154 * @return The value of metadata as an <strong>Vector</strong>. 155 */ 156 public Vector getMetadata() { 136 137 public String getID() { 138 if(id == null) { 139 StringBuffer id_buffer = new StringBuffer(); 140 // Write level information, if any. 141 int level = getLevel(); 142 if(0 <= level && level < 3) { 143 id_buffer.append(LEVEL[level]); 144 id_buffer.append(StaticStrings.COLON_CHARACTER); 145 } 146 // Write data information. Retrieve each of the content sources and add them in a comma separated list. 147 ArrayList sources = getSources(); 148 int sources_size = sources.size(); 149 for(int i = 0; i < sources_size; i++) { 150 Object source_object = sources.get(i); 151 if(source_object instanceof ElementWrapper) { 152 id_buffer.append(((ElementWrapper)source_object).getName()); 153 } 154 else { 155 id_buffer.append(source_object.toString()); 156 } 157 id_buffer.append(StaticStrings.COMMA_CHARACTER); 158 } 159 sources = null; 160 id = id_buffer.substring(0, id_buffer.length() - 1); 161 } 162 return id; 163 } 164 165 /** Retrieve the sources of this index. 166 * @return the sources as an ArrayList 167 */ 168 public ArrayList getSources() { 169 if(sources == null) { 170 sources = new ArrayList(); 171 NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT); 172 int content_elements_length = content_elements.getLength(); 173 for(int i = 0; i < content_elements_length; i++) { 174 Element content_element = (Element) content_elements.item(i); 175 sources.add(content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE)); 176 } 177 content_elements = null; 178 Collections.sort(sources); 179 } 157 180 return sources; 158 181 } 159 /** Method to retrieve this indexes name. 160 * @return A <strong>String</strong>. 161 */ 162 public String getName() { 163 if(manager != null) { 164 String name = LEVEL[level] + ":" + getData(); 165 Language language = manager.languages.getDefaultLanguage(); 166 CollectionMeta metadata = manager.collectionmetadatum.getMetadata(name, language, true); 167 if(metadata != null) { 168 return metadata.getValue(); 169 } 170 } 171 return ""; 172 173 } 182 183 public boolean isAssigned() { 184 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR)); 185 } 186 187 public void setAssigned(boolean assigned) { 188 if(element != null) { 189 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR)); 190 } 191 } 192 193 public void setElement(Element element) { 194 this.element = element; 195 this.level = -1; 196 this.id = null; 197 this.sources = null; 198 } 199 200 /** Method to set the level of this index which can only be used for the default index. 201 * @param level the new level as an int 202 */ 203 public void setLevel(int new_level) { 204 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) { 205 element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]); 206 this.id = null; // Regenerate ID. 207 this.level = new_level; 208 } 209 else { 210 Gatherer.println("Error! Called setLevel() of index other than the default."); 211 } 212 } 213 214 /** Method to set the sources for this index which can only be used for the default index. 215 * @param sources an ArrayList of source names 216 */ 217 public void setSources(ArrayList sources) { 218 if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) { 219 // Erase old sources 220 MSMUtils.clear(element); 221 // For each entry in the sources array add a new content element. 222 int size = sources.size(); 223 for(int i = 0; i < size; i++) { 224 Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT); 225 content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, (String) sources.get(i)); 226 element.appendChild(content_element); 227 content_element = null; 228 } 229 this.id = null; // Regenerate ID. 230 this.sources = sources; 231 } 232 else { 233 Gatherer.println("Error! Called setSource() of index other than the default."); 234 } 235 } 236 174 237 /** Method to turn this object into a string representation ready to be placed in the collection configuration file. 175 176 238 * @return A <strong>String</strong> containing the information of this class. 239 */ 177 240 public String toString() { 178 return LEVEL[level] + ":" + getData(); 179 } 180 /** Retrieve a textual representation of this index. 181 * @param show_name <i>true</i> if you want the name of this index, <i>false</i> for the gsdl index reference. 182 */ 183 public String toString(boolean show_name) { 184 if(show_name) { 185 return getName(); 186 } 187 return LEVEL[level] + ":" + getData(); 188 } 189 190 /** Retrieve a textual representation of this index, specifically for the configuration files. Extracted metadata is shown to teh user and stored as ex.MD, however in the config file it should have no namespace */ 191 public String toStringConfig() { 192 193 return LEVEL[level] + ":" + getDataConfig(); 194 } 195 196 /** Method to parse a configuration file index specification into an Index 197 * object. returns null if invalid syntax */ 198 public static Index parseIndexConfig(String entry, CollectionDesignManager manager) { 199 // all (mg) indexes must start with level: 200 if(entry.indexOf(":") == -1) { 201 return null; 202 } 203 204 String level_str = entry.substring(0, entry.indexOf(":")); 205 String sources_raw = entry.substring(entry.indexOf(":") + 1); 206 Vector sources = new Vector(); 207 StringTokenizer st = new StringTokenizer(sources_raw, ","); 208 while(st.hasMoreTokens()) { 209 String token = st.nextToken(); 210 // We may have to replace old : with whatever namespace separator we are using. 211 token = token.replace(':', MSMUtils.NS_SEP); 212 // if there is no namespace, we need to add one, except for text 213 if (token.indexOf(MSMUtils.NS_SEP)==-1) { 214 if (!token.equals("text")) { 215 token = Utility.EXTRACTED_METADATA_NAMESPACE+MSMUtils.NS_SEP+token; 216 } 217 } 218 219 sources.add(token); 220 } 221 Index index = null; 222 for(int i = 0; i < Index.LEVEL.length; i++) { 223 if(level_str.equals(Index.LEVEL[i])) { 224 return new Index(i, sources, manager); 225 226 } 227 } 228 // somethings gone wrong 229 return null; 230 231 } 232 233 /** A custom comparator for comparing Indexes. */ 234 private class IndexComparator 235 implements Comparator { 236 /** Method to compare two objects, which may be either indexes or strings. 237 * @param object1 One object as an <strong>Object</strong>. 238 * @param object2 Another object as an <strong>Object</strong>. 239 * @return An <i>int</i> which indicates how they compare. 240 * @see java.lang.String 241 */ 242 public int compare(Object object1, Object object2) { 243 if(object1 instanceof Index && object2 instanceof Index) { 244 Index index1 = (Index)object1; 245 Index index2 = (Index)object2; 246 return index1.toString(false).compareTo(index2.toString(false)); 247 } 248 else if(object1 instanceof Index) { 249 Index index = (Index) object1; 250 return index.toString(false).compareTo(object2.toString()); 251 } 252 else if(object2 instanceof Index) { 253 Index index = (Index) object2; 254 return object1.toString().compareTo(index.toString(false)); 255 } 256 return object1.toString().compareTo(object2.toString()); 257 } 258 /** Method to test for the equality of two objects, which may be indexes or strings. 259 * @param object Another object as an <strong>Object</strong>. 260 * @return A <i>boolean</i> which is <i>true</i> if the two objects are equal, <i>false</i> otherwise. 261 */ 262 public boolean equals(Object object) { 263 if(compareTo(object) == 0) { 264 return true; 265 } 266 return false; 267 } 241 String id = getID(); 242 StringBuffer text = new StringBuffer(id); 243 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + id, false); 244 if(metadatum != null) { 245 text.append(" \""); 246 text.append(metadatum.getValue()); 247 text.append("\""); 248 } 249 return text.toString(); 268 250 } 269 251 }
Note:
See TracChangeset
for help on using the changeset viewer.