[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;
|
---|
[5590] | 28 |
|
---|
[4932] | 29 | import java.util.*;
|
---|
| 30 | import org.greenstone.gatherer.Gatherer;
|
---|
| 31 | import org.greenstone.gatherer.cdm.CollectionConfiguration;
|
---|
| 32 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
| 33 | import org.greenstone.gatherer.cdm.CollectionMeta;
|
---|
| 34 | import org.greenstone.gatherer.cdm.DOMProxyListEntry;
|
---|
| 35 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
| 36 | import org.greenstone.gatherer.util.Utility;
|
---|
[8015] | 37 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[4932] | 38 | import org.w3c.dom.*;
|
---|
[5590] | 39 |
|
---|
[4932] | 40 | /** This class encapsulates a single indexing pair.
|
---|
| 41 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 42 | * @version 2.4
|
---|
| 43 | */
|
---|
| 44 | public class SubcollectionIndex
|
---|
| 45 | implements Comparable, DOMProxyListEntry {
|
---|
| 46 |
|
---|
| 47 | private ArrayList sources = null;
|
---|
| 48 | /** The element this index is based upon. */
|
---|
| 49 | private Element element = null;
|
---|
| 50 | /** The unique, if cryptic, identifier of an index. */
|
---|
| 51 | private String id = null;
|
---|
| 52 |
|
---|
| 53 | /** Default constructor, which should only be used during DOMProxyListModel creation. */
|
---|
| 54 | public SubcollectionIndex() {
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /** Constructor. */
|
---|
| 58 | public SubcollectionIndex(Element element) {
|
---|
| 59 | this.element = element;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public SubcollectionIndex(Object[] raw_sources) {
|
---|
| 63 | this.sources = new ArrayList();
|
---|
| 64 | // Create a new element
|
---|
| 65 | Document document = CollectionDesignManager.collect_config.document;
|
---|
| 66 | element = document.createElement(CollectionConfiguration.INDEX_ELEMENT);
|
---|
| 67 | // For each source add a content element
|
---|
| 68 | int size = raw_sources.length;
|
---|
| 69 | for(int i = 0; i < size; i++) {
|
---|
| 70 | Subcollection subcollection = (Subcollection) raw_sources[i];
|
---|
| 71 | String subcollection_name = subcollection.getName();
|
---|
| 72 | sources.add(subcollection_name);
|
---|
| 73 | Element content_element = document.createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
| 74 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, subcollection_name);
|
---|
| 75 | element.appendChild(content_element);
|
---|
| 76 | content_element = null;
|
---|
| 77 | }
|
---|
| 78 | document = null;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /** Method to compare two indexes.
|
---|
| 82 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 83 | * @return An <i>int</i> which indicates how the indexes compare.
|
---|
| 84 | * @see java.lang.String
|
---|
| 85 | */
|
---|
| 86 | public int compareTo(Object object) {
|
---|
| 87 | return id.compareTo(((SubcollectionIndex)object).getID());
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public DOMProxyListEntry create(Element element) {
|
---|
| 91 | return new SubcollectionIndex(element);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Method to test for the equality of two indexes.
|
---|
| 95 | * @param object The other index as an <strong>Object</strong>.
|
---|
| 96 | * @return A <i>boolean</i> which is <i>true</i> if the two indexes are equal, <i>false</i> otherwise.
|
---|
| 97 | */
|
---|
| 98 | public boolean equals(Object object) {
|
---|
| 99 | return (compareTo(object) == 0);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public Element getElement() {
|
---|
| 103 | return element;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public String getID() {
|
---|
| 107 | if(id == null) {
|
---|
| 108 | StringBuffer id_buffer = new StringBuffer();
|
---|
| 109 | // Write data information. Retrieve each of the content sources and add them in a comma separated list.
|
---|
| 110 | ArrayList sources = getSources();
|
---|
| 111 | int sources_size = sources.size();
|
---|
| 112 | for(int i = 0; i < sources_size; i++) {
|
---|
| 113 | id_buffer.append((String)sources.get(i));
|
---|
| 114 | id_buffer.append(StaticStrings.COMMA_CHARACTER);
|
---|
| 115 | }
|
---|
| 116 | sources = null;
|
---|
| 117 | id = id_buffer.substring(0, id_buffer.length() - 1);
|
---|
| 118 | }
|
---|
| 119 | return id;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Retrieve the sources of this index.
|
---|
| 123 | * @return the sources as an ArrayList
|
---|
| 124 | */
|
---|
| 125 | public ArrayList getSources() {
|
---|
| 126 | if(sources == null) {
|
---|
| 127 | sources = new ArrayList();
|
---|
| 128 | NodeList content_elements = element.getElementsByTagName(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
| 129 | int content_elements_length = content_elements.getLength();
|
---|
| 130 | for(int i = 0; i < content_elements_length; i++) {
|
---|
| 131 | Element content_element = (Element) content_elements.item(i);
|
---|
| 132 | sources.add(content_element.getAttribute(CollectionConfiguration.NAME_ATTRIBUTE));
|
---|
| 133 | }
|
---|
| 134 | content_elements = null;
|
---|
| 135 | Collections.sort(sources);
|
---|
| 136 | }
|
---|
| 137 | return sources;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | public boolean isAssigned() {
|
---|
| 141 | return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public void setAssigned(boolean assigned) {
|
---|
| 145 | if(element != null) {
|
---|
| 146 | element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public void setElement(Element element) {
|
---|
| 151 | this.element = element;
|
---|
| 152 | this.id = null;
|
---|
| 153 | this.sources = null;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /** Method to set the sources for this index which can only be used for the default index.
|
---|
| 157 | * @param sources an ArrayList of source names
|
---|
| 158 | */
|
---|
| 159 | public void setSources(ArrayList sources) {
|
---|
| 160 | if(element != null) {
|
---|
| 161 | // Erase old sources
|
---|
[8015] | 162 | XMLTools.clear(element);
|
---|
[4932] | 163 | // For each entry in the sources array add a new content element.
|
---|
| 164 | int size = sources.size();
|
---|
| 165 | for(int i = 0; i < size; i++) {
|
---|
| 166 | Element content_element = element.getOwnerDocument().createElement(CollectionConfiguration.CONTENT_ELEMENT);
|
---|
| 167 | content_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, (String) sources.get(i));
|
---|
| 168 | element.appendChild(content_element);
|
---|
| 169 | content_element = null;
|
---|
| 170 | }
|
---|
| 171 | this.id = null; // Regenerate ID.
|
---|
| 172 | this.sources = sources;
|
---|
| 173 | }
|
---|
| 174 | else {
|
---|
| 175 | Gatherer.println("Error! Called setSource() of index other than the default.");
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /** Method to turn this object into a string representation ready to be placed in the collection configuration file.
|
---|
| 180 | * @return A <strong>String</strong> containing the information of this class.
|
---|
| 181 | */
|
---|
| 182 | public String toString() {
|
---|
| 183 | String id = getID();
|
---|
| 184 | StringBuffer text = new StringBuffer(id);
|
---|
| 185 | CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + id, false);
|
---|
| 186 | if(metadatum != null) {
|
---|
| 187 | text.append(" \"");
|
---|
[5255] | 188 | text.append(metadatum.getValue(CollectionMeta.TEXT));
|
---|
[4932] | 189 | text.append("\"");
|
---|
| 190 | }
|
---|
| 191 | return text.toString();
|
---|
| 192 | }
|
---|
| 193 | }
|
---|