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

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

Finally committing the (many) changes to the GLI to use the new metadata code... I hope this doesn't have too many bugs in it and committing it now doesn't stuff anyone up! (Katherine said I could commit it, so blame her if anything goes wrong).

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