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

Last change on this file since 10965 was 10345, checked in by mdewsnip, 19 years ago

Removed some more crap out of the Utility class.

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