[4932] | 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 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 9 | *
|
---|
| 10 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or modify
|
---|
| 13 | * it under the terms of the GNU General Public License as published by
|
---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | * (at your option) any later version.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful,
|
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | * GNU General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, write to the Free Software
|
---|
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | *########################################################################
|
---|
| 26 | */
|
---|
| 27 | package org.greenstone.gatherer.cdm;
|
---|
| 28 |
|
---|
| 29 | import java.util.*;
|
---|
[8236] | 30 | import org.greenstone.gatherer.DebugStream;
|
---|
[4932] | 31 | import org.greenstone.gatherer.Gatherer;
|
---|
[8313] | 32 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
| 33 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
| 34 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
| 35 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
[4932] | 36 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
| 37 | import org.greenstone.gatherer.util.Utility;
|
---|
[8015] | 38 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[4932] | 39 | import org.w3c.dom.*;
|
---|
[5590] | 40 |
|
---|
[4932] | 41 | /** This class encapsulates a single indexing pair.
|
---|
| 42 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 43 | * @version 2.3d
|
---|
| 44 | */
|
---|
| 45 | public class Index
|
---|
| 46 | implements Comparable, DOMProxyListEntry {
|
---|
| 47 | /** An values of items in the index level enumeration. */
|
---|
[6539] | 48 | static public final String LEVEL[] = {CollectionConfiguration.DOCUMENT_STR, CollectionConfiguration.PARAGRAPH_STR, CollectionConfiguration.SECTION_STR};
|
---|
[4932] | 49 |
|
---|
| 50 | private ArrayList sources = null;
|
---|
| 51 | /** The level of this index (if old sckool MG). */
|
---|
| 52 | private int level = -1;
|
---|
| 53 | /** The element this index is based upon. */
|
---|
| 54 | private Element element = null;
|
---|
| 55 | /** The unique, if cryptic, identifier of an index. */
|
---|
| 56 | private String id = null;
|
---|
| 57 |
|
---|
| 58 | /** Default constructor, which should only be used during DOMProxyListModel creation. */
|
---|
| 59 | public Index() {
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /** Constructor. */
|
---|
| 63 | public Index(Element element) {
|
---|
| 64 | this.element = element;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /** Constructor for a newly assigned index. */
|
---|
| 68 | public Index(ArrayList sources) {
|
---|
| 69 | this.sources = sources;
|
---|
| 70 | // Create a new element
|
---|
| 71 | Document document = CollectionDesignManager.collect_config.document;
|
---|
| 72 | element = document.createElement(CollectionConfiguration.INDEX_ELEMENT);
|
---|
| 73 | // For each source add a content element
|
---|
| 74 | int size = sources.size();
|
---|
| 75 | for(int i = 0; i < size; i++) {
|
---|
| 76 | Element content_element = document.createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
| 77 | Object source_object = sources.get(i);
|
---|
[8313] | 78 | if (source_object instanceof MetadataElement) {
|
---|
| 79 | // System.err.println("Constructing new Index with MetadataElement source...");
|
---|
| 80 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, ((MetadataElement) source_object).getFullName());
|
---|
[4932] | 81 | }
|
---|
| 82 | else {
|
---|
[8313] | 83 | // System.err.println("Constructing new Index with String source...");
|
---|
[4932] | 84 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString());
|
---|
| 85 | }
|
---|
| 86 | element.appendChild(content_element);
|
---|
| 87 | content_element = null;
|
---|
| 88 | }
|
---|
| 89 | document = null;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /** Constructor for a newly assigned index with specified level (old skool). */
|
---|
| 93 | public Index(int level, ArrayList sources) {
|
---|
| 94 | this(sources);
|
---|
| 95 | this.level = level;
|
---|
| 96 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[level]);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[4967] | 99 | public Index(String level, ArrayList sources) {
|
---|
| 100 | this(sources);
|
---|
| 101 | for(int i = 0; i < LEVEL.length; i++) {
|
---|
| 102 | if(LEVEL[i].equalsIgnoreCase(level)) {
|
---|
| 103 | this.level = i;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[this.level]);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[4932] | 109 | /** Method to compare two indexes.
|
---|
| 110 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 111 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
| 112 | * @see java.lang.String
|
---|
| 113 | */
|
---|
| 114 | public int compareTo(Object object) {
|
---|
[5037] | 115 | if(object == null) {
|
---|
| 116 | return -1;
|
---|
| 117 | }
|
---|
| 118 | String id = getID();
|
---|
[4932] | 119 | return id.compareTo(((Index)object).getID());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | public DOMProxyListEntry create(Element element) {
|
---|
| 123 | return new Index(element);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Method to test for the equality of two indexes.
|
---|
| 127 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 128 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
| 129 | */
|
---|
| 130 | public boolean equals(Object object) {
|
---|
| 131 | return (compareTo(object) == 0);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public Element getElement() {
|
---|
| 135 | return element;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** Method to get the value of level.
|
---|
| 139 | * @return the level as a int
|
---|
| 140 | */
|
---|
| 141 | public int getLevel() {
|
---|
| 142 | if(level == -1) {
|
---|
| 143 | String level_str = element.getAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE);
|
---|
| 144 | for(int i = 0; level == -1 && i < LEVEL.length; i++) {
|
---|
| 145 | if(level_str.equals(LEVEL[i])) {
|
---|
| 146 | level = i;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | level_str = null;
|
---|
| 150 | }
|
---|
| 151 | return level;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public String getID() {
|
---|
[5159] | 155 | if(element == null) {
|
---|
| 156 | id="";
|
---|
| 157 | }
|
---|
| 158 | else if(id == null) {
|
---|
[4932] | 159 | StringBuffer id_buffer = new StringBuffer();
|
---|
| 160 | // Write level information, if any.
|
---|
| 161 | int level = getLevel();
|
---|
| 162 | if(0 <= level && level < 3) {
|
---|
| 163 | id_buffer.append(LEVEL[level]);
|
---|
| 164 | id_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
| 165 | }
|
---|
| 166 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
| 167 | ArrayList sources = getSources();
|
---|
| 168 | int sources_size = sources.size();
|
---|
| 169 | for(int i = 0; i < sources_size; i++) {
|
---|
| 170 | Object source_object = sources.get(i);
|
---|
[8313] | 171 | if (source_object instanceof MetadataElement) {
|
---|
| 172 | String full_element_name = ((MetadataElement)source_object).getFullName();
|
---|
[7624] | 173 | if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
|
---|
| 174 | id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
|
---|
[5037] | 175 | }
|
---|
| 176 | else {
|
---|
| 177 | id_buffer.append(full_element_name);
|
---|
| 178 | }
|
---|
[4932] | 179 | }
|
---|
| 180 | else {
|
---|
| 181 | id_buffer.append(source_object.toString());
|
---|
| 182 | }
|
---|
| 183 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 184 | }
|
---|
| 185 | sources = null;
|
---|
| 186 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
| 187 | }
|
---|
| 188 | return id;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[5375] | 191 | /** Tries to retrieve this indexes name according to the CollectionMetaManager. */
|
---|
| 192 | public String getName() {
|
---|
| 193 | CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
|
---|
| 194 | if(metadatum != null) {
|
---|
| 195 | return metadatum.getValue(CollectionMeta.TEXT);
|
---|
| 196 | }
|
---|
| 197 | return "";
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[4932] | 200 | /** Retrieve the sources of this index.
|
---|
| 201 | * @return the sources as an ArrayList
|
---|
| 202 | */
|
---|
| 203 | public ArrayList getSources() {
|
---|
| 204 | if(sources == null) {
|
---|
| 205 | sources = new ArrayList();
|
---|
| 206 | NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
| 207 | int content_elements_length = content_elements.getLength();
|
---|
| 208 | for(int i = 0; i < content_elements_length; i++) {
|
---|
| 209 | Element content_element = (Element) content_elements.item(i);
|
---|
[8313] | 210 | String metadata_element_name_full = (String) content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE);
|
---|
| 211 | MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(metadata_element_name_full);
|
---|
| 212 | if (metadata_element != null) {
|
---|
| 213 | sources.add(metadata_element);
|
---|
[5037] | 214 | }
|
---|
| 215 | else {
|
---|
[8313] | 216 | sources.add(metadata_element_name_full);
|
---|
[5037] | 217 | }
|
---|
[4932] | 218 | }
|
---|
| 219 | content_elements = null;
|
---|
[5937] | 220 | if(sources != null && sources.size() > 1) {
|
---|
| 221 | Collections.sort(sources);
|
---|
| 222 | }
|
---|
[4932] | 223 | }
|
---|
| 224 | return sources;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | public boolean isAssigned() {
|
---|
| 228 | return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | public void setAssigned(boolean assigned) {
|
---|
| 232 | if(element != null) {
|
---|
| 233 | element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | public void setElement(Element element) {
|
---|
| 238 | this.element = element;
|
---|
| 239 | this.level = -1;
|
---|
| 240 | this.id = null;
|
---|
| 241 | this.sources = null;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /** Method to set the level of this index which can only be used for the default index.
|
---|
[6770] | 245 | * @param new_level the new level as an int
|
---|
[4932] | 246 | */
|
---|
| 247 | public void setLevel(int new_level) {
|
---|
[5159] | 248 | System.err.println("SetLevel(" + new_level + ")");
|
---|
[4932] | 249 | if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
|
---|
| 250 | element.setAttribute(CollectionConfiguration.LEVEL_ATTRIBUTE, LEVEL[new_level]);
|
---|
| 251 | this.id = null; // Regenerate ID.
|
---|
| 252 | this.level = new_level;
|
---|
| 253 | }
|
---|
| 254 | else {
|
---|
[8236] | 255 | DebugStream.println("Error! Called setLevel() of index other than the default.");
|
---|
[4932] | 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
| 260 | * @param sources an ArrayList of source names
|
---|
| 261 | */
|
---|
| 262 | public void setSources(ArrayList sources) {
|
---|
| 263 | if(element != null && element.getNodeName().equals(CollectionConfiguration.INDEX_DEFAULT_ELEMENT)) {
|
---|
| 264 | // Erase old sources
|
---|
[8015] | 265 | XMLTools.clear(element);
|
---|
[4932] | 266 | // For each entry in the sources array add a new content element.
|
---|
| 267 | int size = sources.size();
|
---|
| 268 | for(int i = 0; i < size; i++) {
|
---|
| 269 | Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
[5159] | 270 | Object source_object = sources.get(i);
|
---|
[8313] | 271 | if (source_object instanceof MetadataElement) {
|
---|
| 272 | String name = ((MetadataElement) source_object).getFullName();
|
---|
[5159] | 273 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, name);
|
---|
| 274 | name = null;
|
---|
| 275 | }
|
---|
| 276 | else {
|
---|
[8236] | 277 | //DebugStream.println("Found String as source: " + source_object.toString());
|
---|
[5159] | 278 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, source_object.toString());
|
---|
| 279 | }
|
---|
| 280 | source_object = null;
|
---|
[4932] | 281 | element.appendChild(content_element);
|
---|
| 282 | content_element = null;
|
---|
| 283 | }
|
---|
| 284 | this.id = null; // Regenerate ID.
|
---|
| 285 | this.sources = sources;
|
---|
| 286 | }
|
---|
| 287 | else {
|
---|
[8236] | 288 | DebugStream.println("Error! Called setSource() of index other than the default.");
|
---|
[4932] | 289 | }
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
| 293 | * @return A <strong>String</strong> containing the information of this class.
|
---|
| 294 | */
|
---|
| 295 | public String toString() {
|
---|
[5037] | 296 | StringBuffer text_buffer = new StringBuffer("");
|
---|
| 297 | // Generate language dependant id (include extracted metadata namespace)
|
---|
| 298 | // Write level information, if any.
|
---|
| 299 | int level = getLevel();
|
---|
| 300 | if(0 <= level && level < 3) {
|
---|
| 301 | text_buffer.append(LEVEL[level]);
|
---|
| 302 | text_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
| 303 | }
|
---|
| 304 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
| 305 | ArrayList sources = getSources();
|
---|
| 306 | int sources_size = sources.size();
|
---|
| 307 | for(int i = 0; i < sources_size; i++) {
|
---|
| 308 | String source_name = (sources.get(i)).toString();
|
---|
| 309 | text_buffer.append(source_name);
|
---|
| 310 | if(i < sources_size - 1) {
|
---|
| 311 | text_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 | sources = null;
|
---|
| 315 | CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + getID(), false);
|
---|
[4932] | 316 | if(metadatum != null) {
|
---|
[5037] | 317 | text_buffer.append(" \"");
|
---|
[5255] | 318 | text_buffer.append(metadatum.getValue(CollectionMeta.TEXT));
|
---|
[5037] | 319 | text_buffer.append("\"");
|
---|
[4932] | 320 | }
|
---|
[5037] | 321 | return text_buffer.toString();
|
---|
[4932] | 322 | }
|
---|
| 323 | }
|
---|