[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.MetadataTools;
|
---|
[4932] | 34 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
[8015] | 35 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[4932] | 36 | import org.w3c.dom.*;
|
---|
[5590] | 37 |
|
---|
[4932] | 38 | /** This class encapsulates a single indexing pair.
|
---|
| 39 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 40 | * @version 2.3d
|
---|
| 41 | */
|
---|
| 42 | public class Index
|
---|
| 43 | implements Comparable, DOMProxyListEntry {
|
---|
| 44 | /** An values of items in the index level enumeration. */
|
---|
[12641] | 45 | static protected final String LEVEL[] = {StaticStrings.DOCUMENT_STR, StaticStrings.SECTION_STR, StaticStrings.PARAGRAPH_STR};
|
---|
[4932] | 46 |
|
---|
| 47 | private ArrayList sources = null;
|
---|
| 48 | /** The level of this index (if old sckool MG). */
|
---|
| 49 | private int level = -1;
|
---|
| 50 | /** The element this index is based upon. */
|
---|
| 51 | private Element element = null;
|
---|
| 52 | /** The unique, if cryptic, identifier of an index. */
|
---|
| 53 | private String id = null;
|
---|
| 54 |
|
---|
| 55 | /** Default constructor, which should only be used during DOMProxyListModel creation. */
|
---|
| 56 | public Index() {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /** Constructor. */
|
---|
| 60 | public Index(Element element) {
|
---|
| 61 | this.element = element;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /** Constructor for a newly assigned index. */
|
---|
| 65 | public Index(ArrayList sources) {
|
---|
| 66 | this.sources = sources;
|
---|
| 67 | // Create a new element
|
---|
| 68 | Document document = CollectionDesignManager.collect_config.document;
|
---|
[12641] | 69 | this.element = document.createElement(StaticStrings.INDEX_ELEMENT);
|
---|
[4932] | 70 | // For each source add a content element
|
---|
| 71 | int size = sources.size();
|
---|
| 72 | for(int i = 0; i < size; i++) {
|
---|
[12641] | 73 | Element content_element = document.createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
[4932] | 74 | Object source_object = sources.get(i);
|
---|
[8313] | 75 | if (source_object instanceof MetadataElement) {
|
---|
| 76 | // System.err.println("Constructing new Index with MetadataElement source...");
|
---|
[12641] | 77 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, ((MetadataElement) source_object).getFullName());
|
---|
[4932] | 78 | }
|
---|
| 79 | else {
|
---|
[8313] | 80 | // System.err.println("Constructing new Index with String source...");
|
---|
[12641] | 81 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
[4932] | 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);
|
---|
| 92 | this.level = level;
|
---|
[12641] | 93 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[level]);
|
---|
[4932] | 94 | }
|
---|
| 95 |
|
---|
[4967] | 96 | public Index(String level, ArrayList sources) {
|
---|
| 97 | this(sources);
|
---|
| 98 | for(int i = 0; i < LEVEL.length; i++) {
|
---|
| 99 | if(LEVEL[i].equalsIgnoreCase(level)) {
|
---|
| 100 | this.level = i;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[12641] | 103 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[this.level]);
|
---|
[4967] | 104 | }
|
---|
| 105 |
|
---|
[4932] | 106 | /** Method to compare two indexes.
|
---|
| 107 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 108 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
| 109 | * @see java.lang.String
|
---|
| 110 | */
|
---|
| 111 | public int compareTo(Object object) {
|
---|
[5037] | 112 | if(object == null) {
|
---|
| 113 | return -1;
|
---|
| 114 | }
|
---|
| 115 | String id = getID();
|
---|
[4932] | 116 | return id.compareTo(((Index)object).getID());
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public DOMProxyListEntry create(Element element) {
|
---|
| 120 | return new Index(element);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /** Method to test for the equality of two indexes.
|
---|
| 124 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 125 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
| 126 | */
|
---|
| 127 | public boolean equals(Object object) {
|
---|
| 128 | return (compareTo(object) == 0);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public Element getElement() {
|
---|
| 132 | return element;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Method to get the value of level.
|
---|
| 136 | * @return the level as a int
|
---|
| 137 | */
|
---|
| 138 | public int getLevel() {
|
---|
[9995] | 139 | if(level == -1 && element != null) {
|
---|
[12641] | 140 | String level_str = element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
|
---|
[4932] | 141 | for(int i = 0; level == -1 && i < LEVEL.length; i++) {
|
---|
| 142 | if(level_str.equals(LEVEL[i])) {
|
---|
| 143 | level = i;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | level_str = null;
|
---|
| 147 | }
|
---|
| 148 | return level;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public String getID() {
|
---|
[5159] | 152 | if(element == null) {
|
---|
| 153 | id="";
|
---|
| 154 | }
|
---|
| 155 | else if(id == null) {
|
---|
[4932] | 156 | StringBuffer id_buffer = new StringBuffer();
|
---|
| 157 | // Write level information, if any.
|
---|
| 158 | int level = getLevel();
|
---|
| 159 | if(0 <= level && level < 3) {
|
---|
| 160 | id_buffer.append(LEVEL[level]);
|
---|
| 161 | id_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
| 162 | }
|
---|
| 163 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
| 164 | ArrayList sources = getSources();
|
---|
| 165 | int sources_size = sources.size();
|
---|
| 166 | for(int i = 0; i < sources_size; i++) {
|
---|
| 167 | Object source_object = sources.get(i);
|
---|
[8313] | 168 | if (source_object instanceof MetadataElement) {
|
---|
| 169 | String full_element_name = ((MetadataElement)source_object).getFullName();
|
---|
[7624] | 170 | if(full_element_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE)) {
|
---|
| 171 | id_buffer.append(full_element_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length()));
|
---|
[5037] | 172 | }
|
---|
| 173 | else {
|
---|
| 174 | id_buffer.append(full_element_name);
|
---|
| 175 | }
|
---|
[4932] | 176 | }
|
---|
| 177 | else {
|
---|
| 178 | id_buffer.append(source_object.toString());
|
---|
| 179 | }
|
---|
| 180 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 181 | }
|
---|
| 182 | sources = null;
|
---|
[10964] | 183 | if (id_buffer.length()==0) {
|
---|
| 184 | id = "";
|
---|
| 185 | } else {
|
---|
| 186 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
| 187 | }
|
---|
[4932] | 188 | }
|
---|
| 189 | return id;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[5375] | 192 |
|
---|
[4932] | 193 | /** Retrieve the sources of this index.
|
---|
| 194 | * @return the sources as an ArrayList
|
---|
| 195 | */
|
---|
| 196 | public ArrayList getSources() {
|
---|
| 197 | if(sources == null) {
|
---|
| 198 | sources = new ArrayList();
|
---|
[12641] | 199 | NodeList content_elements = element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
|
---|
[4932] | 200 | int content_elements_length = content_elements.getLength();
|
---|
| 201 | for(int i = 0; i < content_elements_length; i++) {
|
---|
| 202 | Element content_element = (Element) content_elements.item(i);
|
---|
[12641] | 203 | String metadata_element_name_full = (String) content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
[10964] | 204 | MetadataElement metadata_element = MetadataTools.getMetadataElementWithName(metadata_element_name_full);
|
---|
[8313] | 205 | if (metadata_element != null) {
|
---|
| 206 | sources.add(metadata_element);
|
---|
[5037] | 207 | }
|
---|
| 208 | else {
|
---|
[8313] | 209 | sources.add(metadata_element_name_full);
|
---|
[5037] | 210 | }
|
---|
[4932] | 211 | }
|
---|
| 212 | content_elements = null;
|
---|
| 213 | }
|
---|
| 214 | return sources;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | public boolean isAssigned() {
|
---|
[12641] | 218 | return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
|
---|
[4932] | 219 | }
|
---|
| 220 |
|
---|
| 221 | public void setAssigned(boolean assigned) {
|
---|
| 222 | if(element != null) {
|
---|
[12641] | 223 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
[4932] | 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | public void setElement(Element element) {
|
---|
| 228 | this.element = element;
|
---|
| 229 | this.level = -1;
|
---|
| 230 | this.id = null;
|
---|
| 231 | this.sources = null;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /** Method to set the level of this index which can only be used for the default index.
|
---|
[6770] | 235 | * @param new_level the new level as an int
|
---|
[4932] | 236 | */
|
---|
| 237 | public void setLevel(int new_level) {
|
---|
[9700] | 238 | // System.err.println("SetLevel(" + new_level + ")");
|
---|
[12641] | 239 | if(element != null && element.getNodeName().equals(StaticStrings.INDEX_DEFAULT_ELEMENT)) {
|
---|
[10964] | 240 | if (0 <= new_level && new_level < 3) {
|
---|
[12641] | 241 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, LEVEL[new_level]);
|
---|
[10964] | 242 | } else {
|
---|
[12641] | 243 | element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, "");
|
---|
[10964] | 244 | }
|
---|
[4932] | 245 | this.id = null; // Regenerate ID.
|
---|
| 246 | this.level = new_level;
|
---|
| 247 | }
|
---|
| 248 | else {
|
---|
[8236] | 249 | DebugStream.println("Error! Called setLevel() of index other than the default.");
|
---|
[4932] | 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
| 254 | * @param sources an ArrayList of source names
|
---|
| 255 | */
|
---|
| 256 | public void setSources(ArrayList sources) {
|
---|
[12641] | 257 | if(element != null && element.getNodeName().equals(StaticStrings.INDEX_DEFAULT_ELEMENT)) {
|
---|
[4932] | 258 | // Erase old sources
|
---|
[8015] | 259 | XMLTools.clear(element);
|
---|
[4932] | 260 | // For each entry in the sources array add a new content element.
|
---|
| 261 | int size = sources.size();
|
---|
| 262 | for(int i = 0; i < size; i++) {
|
---|
[12641] | 263 | Element content_element = element.getOwnerDocument().createElement(StaticStrings.CONTENT_ELEMENT);
|
---|
[5159] | 264 | Object source_object = sources.get(i);
|
---|
[8313] | 265 | if (source_object instanceof MetadataElement) {
|
---|
| 266 | String name = ((MetadataElement) source_object).getFullName();
|
---|
[12641] | 267 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
|
---|
[5159] | 268 | name = null;
|
---|
| 269 | }
|
---|
| 270 | else {
|
---|
[8236] | 271 | //DebugStream.println("Found String as source: " + source_object.toString());
|
---|
[12641] | 272 | content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, source_object.toString());
|
---|
[5159] | 273 | }
|
---|
| 274 | source_object = null;
|
---|
[4932] | 275 | element.appendChild(content_element);
|
---|
| 276 | content_element = null;
|
---|
| 277 | }
|
---|
| 278 | this.id = null; // Regenerate ID.
|
---|
| 279 | this.sources = sources;
|
---|
| 280 | }
|
---|
| 281 | else {
|
---|
[8236] | 282 | DebugStream.println("Error! Called setSource() of index other than the default.");
|
---|
[4932] | 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
| 287 | * @return A <strong>String</strong> containing the information of this class.
|
---|
| 288 | */
|
---|
| 289 | public String toString() {
|
---|
[5037] | 290 | StringBuffer text_buffer = new StringBuffer("");
|
---|
| 291 | // Generate language dependant id (include extracted metadata namespace)
|
---|
| 292 | // Write level information, if any.
|
---|
| 293 | int level = getLevel();
|
---|
| 294 | if(0 <= level && level < 3) {
|
---|
| 295 | text_buffer.append(LEVEL[level]);
|
---|
| 296 | text_buffer.append(StaticStrings.COLON_CHARACTER);
|
---|
| 297 | }
|
---|
| 298 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
| 299 | ArrayList sources = getSources();
|
---|
| 300 | int sources_size = sources.size();
|
---|
| 301 | for(int i = 0; i < sources_size; i++) {
|
---|
| 302 | String source_name = (sources.get(i)).toString();
|
---|
| 303 | text_buffer.append(source_name);
|
---|
| 304 | if(i < sources_size - 1) {
|
---|
| 305 | text_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | sources = null;
|
---|
| 309 | return text_buffer.toString();
|
---|
[4932] | 310 | }
|
---|
| 311 | }
|
---|