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

Last change on this file since 4932 was 4932, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

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