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