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

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

Moved clear() and setValue() functions from MSMUtils into XMLTools. Moved NS_SEP string from MSMUtils into StaticStrings.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 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.Gatherer;
31import org.greenstone.gatherer.cdm.CollectionConfiguration;
32import org.greenstone.gatherer.cdm.CollectionDesignManager;
33import org.greenstone.gatherer.cdm.CollectionMeta;
34import org.greenstone.gatherer.cdm.DOMProxyListEntry;
35import org.greenstone.gatherer.util.StaticStrings;
36import org.greenstone.gatherer.util.Utility;
37import org.greenstone.gatherer.util.XMLTools;
38import org.w3c.dom.*;
39
40/** This class encapsulates a single indexing pair.
41 * @author John Thompson, Greenstone Digital Library, University of Waikato
42 * @version 2.4
43 */
44public 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
162 XMLTools.clear(element);
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(" \"");
188 text.append(metadatum.getValue(CollectionMeta.TEXT));
189 text.append("\"");
190 }
191 return text.toString();
192 }
193}
Note: See TracBrowser for help on using the repository browser.