source: trunk/gli/src/org/greenstone/gatherer/cdm/Subcollection.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: 9.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: 03/05/02
30 * Revised: 17/11/02 - Commenting
31 * 04/07/03 - Completely rewritten to support DOM
32 **************************************************************************************/
33import org.greenstone.gatherer.Gatherer;
34import org.greenstone.gatherer.cdm.CollectionConfiguration;
35import org.greenstone.gatherer.cdm.CollectionDesignManager;
36import org.greenstone.gatherer.cdm.DOMProxyListEntry;
37import org.greenstone.gatherer.msm.ElementWrapper;
38import org.greenstone.gatherer.msm.MSMUtils;
39import org.greenstone.gatherer.util.StaticStrings;
40import org.greenstone.gatherer.util.Troolean;
41import org.w3c.dom.*;
42/** This class encapsulates one subcollection entry in the collection configuration file.
43 * @author John Thompson, Greenstone Digital Library, University of Waikato
44 * @version 2.4
45 */
46public class Subcollection
47 implements Comparable, DOMProxyListEntry {
48 /** A <i>boolean</i> which is <i>true</i> if the condition is an include one, <i>false</i> otherwise. */
49 private Troolean include = new Troolean();
50 /** The DOM Element this Subcollection is based upon. */
51 private Element element = null;
52 /** A String containing a Perl expression which is used as the pattern by which to filter this subcollection. */
53 private String pattern = null;
54 /** A series of flags to be used when matching the expression. */
55 private String flags = null;
56 /** A String which is a unique identifier of a subcollection. */
57 private String name = null;
58 /** Either the fully qualified name of the metadata whose value should be matched against the given expression, or <i>null</i> if you wish to match against the file name. */
59 private String source = null;
60 private String text = null;
61
62 /** Default constructor which should only be used during DOMProxyListModel initialization. */
63 public Subcollection() {
64 }
65
66 /** Constructor for representing an existing assigned Subcollection.
67 * @param element the Element this subcollection is based upon
68 */
69 public Subcollection(Element element) {
70 this.element = element;
71 }
72
73 /** Constructor for assigning a brand new Subcollection.
74 * @param name a unique identifier for this collection as a String
75 * @param include true if this the pattern should be an inclusion filter, false for exclusion
76 * @param source either the fully qualified name of an element as a String, or null to filter filenames
77 * @param pattern the matching pattern as a String
78 * @param flags any flags to use while matching, as a String
79 */
80 public Subcollection(String name, boolean include, String source, String pattern, String flags) {
81 // Cache the details
82 this.flags = flags;
83 this.include.set(include);
84 this.name = name;
85 this.pattern = pattern;
86 if(source != null) {
87 this.source = source;
88 }
89 else {
90 this.source = StaticStrings.FILENAME_STR;
91 }
92 // Create a new DOM Element with the appropriate attributes and text value
93 element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.SUBCOLLECTION_ELEMENT);
94 element.setAttribute(StaticStrings.CONTENT_ATTRIBUTE, source);
95 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
96 element.setAttribute(StaticStrings.OPTIONS_ATTRIBUTE, flags);
97 element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, (include ? StaticStrings.INCLUDE_STR : StaticStrings.EXCLUDE_STR));
98 MSMUtils.setValue(element, pattern);
99 }
100
101 /** Method to compare two subcollections.
102 * @param object he other subcollection to compare to, as an Object
103 * @return an int which is &gt;0, 0, or &lt;0 if this subcollection is before, equal to, or after the target object respectively.
104 */
105 public int compareTo(Object object) {
106 return getName().compareTo(((Subcollection)object).getName());
107 }
108
109 public DOMProxyListEntry create(Element element) {
110 return new Subcollection(element);
111 }
112
113 /** Method to check two subcollections for equality.
114 * @param object the other subcollection to compare to, as an Object
115 * @return true if the subcollections are equal, false otherwise.
116 */
117 public boolean equals(Object object) {
118 return (compareTo(object) == 0);
119 }
120
121 /** Retrieve the DOM Element this Subcollection is based upon.
122 * @return an Element
123 */
124 public Element getElement() {
125 return element;
126 }
127
128 /** Method to get the value of flags.
129 * @return the value of flags as a String
130 */
131 public String getFlags() {
132 if(flags == null && element != null) {
133 flags = element.getAttribute(StaticStrings.OPTIONS_ATTRIBUTE);
134 }
135 return flags;
136 }
137
138 /** Method to get the value of name.
139 * @param String The value of name as a <strong>String</string>.
140 */
141 public String getName() {
142 if(name == null && element != null) {
143 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
144 }
145 return name;
146 }
147
148 public String getPattern() {
149 if(pattern == null && element != null) {
150 pattern = MSMUtils.getValue(element);
151 }
152 return pattern;
153 }
154
155 /** Method to get the name of the source of the strings used in pattern matching.
156 * @return a String which is either the fully qualified name of a metadata element, or filename
157 */
158 public String getSource() {
159 if(source == null &&element != null) {
160 source = element.getAttribute(StaticStrings.CONTENT_ATTRIBUTE);
161 // If this is a MSM Element then retrieve the appropriate ElementWrapper and use the language specific name
162 if(!source.equals(StaticStrings.FILENAME_STR)) {
163 ElementWrapper element_wrapper = Gatherer.c_man.getCollection().msm.getElement(source);
164 if(element_wrapper != null) {
165 source = element_wrapper.toString();
166 }
167 }
168 }
169 return source;
170 }
171
172 public boolean isAssigned() {
173 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
174 }
175
176 /** Method to get the value of include.
177 * @param boolean true if this is an inclusion filter, false otherwise
178 */
179 public boolean isInclusive() {
180 if(!include.isDecided() && element != null) {
181 include.set(element.getAttribute(StaticStrings.TYPE_ATTRIBUTE).equals(StaticStrings.INCLUDE_STR));
182 }
183 return include.isTrue();
184 }
185
186 public void setAssigned(boolean assigned) {
187 if(element != null) {
188 element.setAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE, (assigned ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR));
189 }
190 }
191
192 public void setElement(Element element) {
193 this.element = element;
194 include.reset();
195 flags = null;
196 name = null;
197 pattern = null;
198 source = null;
199 text = null;
200 }
201
202 public void setFlags(String new_flags) {
203 if(element != null) {
204 // Change element
205 element.setAttribute(StaticStrings.OPTIONS_ATTRIBUTE, new_flags);
206 flags = new_flags;
207 text = null;
208 }
209 }
210
211 public void setInclusive(boolean new_include) {
212 if(element != null) {
213 // Change element
214 element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, (new_include ? StaticStrings.INCLUDE_STR : StaticStrings.EXCLUDE_STR));
215 include.set(new_include);
216 text = null;
217 }
218 }
219
220 public void setName(String new_name) {
221 if(element != null) {
222 // Change element
223 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, new_name);
224 name = new_name;
225 text = null;
226 }
227 }
228
229 public void setPattern(String new_pattern) {
230 if(element != null) {
231 // Change element
232 MSMUtils.setValue(element, new_pattern);
233 pattern = new_pattern;
234 text = null;
235 }
236 }
237
238 public void setSource(String new_source) {
239 if(element != null) {
240 // Change element
241 element.setAttribute(StaticStrings.CONTENT_ATTRIBUTE, new_source);
242 source = new_source;
243 text = null;
244 }
245 }
246
247 /** Method to display the contents of this class as it would appear in the collection configuration file.
248 * @return a String representing this subcollection
249 */
250 public String toString() {
251 if(text == null && element != null) {
252 text = CollectionConfiguration.toString(element, true);
253 }
254 return text;
255 }
256}
257
258
259
260
261
Note: See TracBrowser for help on using the repository browser.