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