source: trunk/gli/src/org/greenstone/gatherer/cdm/Subcollection.java@ 8243

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

Removed all occurrences of classes explicitly importing other classes in the same package.

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