source: trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionIndex.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

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