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

Last change on this file since 12080 was 9193, checked in by mdewsnip, 19 years ago

Unnecessary import of Gatherer class.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 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.util.StaticStrings;
30import org.greenstone.gatherer.util.XMLTools;
31import org.w3c.dom.*;
32
33
34/** This class encapsulates one subcollection entry in the collection configuration file.
35 * @author John Thompson, Greenstone Digital Library, University of Waikato
36 * @version 2.4
37 */
38public class Subcollection
39 implements Comparable, DOMProxyListEntry {
40 /** A <i>boolean</i> which is <i>true</i> if the condition is an include one, <i>false</i> otherwise. */
41 private boolean include = true;
42 /** The DOM Element this Subcollection is based upon. */
43 private Element element = null;
44 /** A String containing a Perl expression which is used as the pattern by which to filter this subcollection. */
45 private String pattern = null;
46 /** A series of flags to be used when matching the expression. */
47 private String flags = null;
48 /** A String which is a unique identifier of a subcollection. */
49 private String name = null;
50 /** 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. */
51 private String source = null;
52 private String text = null;
53
54 /** Default constructor which should only be used during DOMProxyListModel initialization. */
55 public Subcollection() {
56 }
57
58 /** Constructor for representing an existing assigned Subcollection.
59 * @param element the Element this subcollection is based upon
60 */
61 public Subcollection(Element element) {
62 this.element = element;
63 }
64
65 /** Constructor for assigning a brand new Subcollection.
66 * @param name a unique identifier for this collection as a String
67 * @param include true if this the pattern should be an inclusion filter, false for exclusion
68 * @param source either the fully qualified name of an element as a String, or null to filter filenames
69 * @param pattern the matching pattern as a String
70 * @param flags any flags to use while matching, as a String
71 */
72 public Subcollection(String name, boolean include, String source, String pattern, String flags) {
73 // Cache the details
74 this.flags = flags;
75 this.include = include;
76 this.name = name;
77 this.pattern = pattern;
78 if(source != null) {
79 this.source = source;
80 }
81 else {
82 this.source = StaticStrings.FILENAME_STR;
83 }
84 // Create a new DOM Element with the appropriate attributes and text value
85 element = CollectionDesignManager.collect_config.document.createElement(StaticStrings.SUBCOLLECTION_ELEMENT);
86 element.setAttribute(StaticStrings.CONTENT_ATTRIBUTE, source);
87 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
88 element.setAttribute(StaticStrings.OPTIONS_ATTRIBUTE, flags);
89 element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, (include ? StaticStrings.INCLUDE_STR : StaticStrings.EXCLUDE_STR));
90 XMLTools.setValue(element, pattern);
91 }
92
93 /** Method to compare two subcollections.
94 * @param object he other subcollection to compare to, as an Object
95 * @return an int which is &gt;0, 0, or &lt;0 if this subcollection is before, equal to, or after the target object respectively.
96 */
97 public int compareTo(Object object)
98 {
99 if (object == null) {
100 return -1;
101 }
102
103 // Object may be a String
104 if (object instanceof String) {
105 return getName().compareTo((String) object);
106 }
107
108 // Otherwise assume it is a Subcollection object
109 return getName().compareTo(((Subcollection) object).getName());
110 }
111
112
113 public DOMProxyListEntry create(Element element) {
114 return new Subcollection(element);
115 }
116
117 /** Method to check two subcollections for equality.
118 * @param object the other subcollection to compare to, as an Object
119 * @return true if the subcollections are equal, false otherwise.
120 */
121 public boolean equals(Object object) {
122 return (compareTo(object) == 0);
123 }
124
125 /** Retrieve the DOM Element this Subcollection is based upon.
126 * @return an Element
127 */
128 public Element getElement() {
129 return element;
130 }
131
132 /** Method to get the value of flags.
133 * @return the value of flags as a String
134 */
135 public String getFlags() {
136 if(flags == null && element != null) {
137 flags = element.getAttribute(StaticStrings.OPTIONS_ATTRIBUTE);
138 }
139 return flags;
140 }
141
142 /** Method to get the value of name.
143 * @return String The value of name as a <strong>String</string>.
144 */
145 public String getName() {
146 if(name == null && element != null) {
147 name = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
148 }
149 return name;
150 }
151
152 public String getPattern() {
153 if(pattern == null && element != null) {
154 pattern = XMLTools.getValue(element);
155 }
156 return pattern;
157 }
158
159 /** Method to get the name of the source of the strings used in pattern matching.
160 * @return a String which is either the fully qualified name of a metadata element, or filename
161 */
162 public String getSource()
163 {
164 if (source == null && element != null) {
165 source = element.getAttribute(StaticStrings.CONTENT_ATTRIBUTE);
166 }
167
168 return source;
169 }
170
171 public boolean isAssigned() {
172 return (element != null && !element.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.FALSE_STR));
173 }
174
175 /** Method to get the value of include.
176 * @return boolean true if this is an inclusion filter, false otherwise
177 */
178 public boolean isInclusive() {
179 if (element != null) {
180 include = element.getAttribute(StaticStrings.TYPE_ATTRIBUTE).equals(StaticStrings.INCLUDE_STR);
181 }
182
183 return include;
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 = true;
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 include = new_include;
215 element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, (include ? StaticStrings.INCLUDE_STR : StaticStrings.EXCLUDE_STR));
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 XMLTools.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}
Note: See TracBrowser for help on using the repository browser.