[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 |
|
---|
[10556] | 29 | import org.greenstone.gatherer.Gatherer;
|
---|
[4932] | 30 | import org.greenstone.gatherer.Configuration;
|
---|
[5254] | 31 | import org.greenstone.gatherer.util.Codec;
|
---|
[4932] | 32 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
[8015] | 33 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[4932] | 34 | import org.w3c.dom.*;
|
---|
[8015] | 35 |
|
---|
| 36 |
|
---|
[4932] | 37 | /** This class encapsulates a single collection level metadata assignment, which constitutes a name, language and value.
|
---|
| 38 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 39 | * @version 2.4
|
---|
| 40 | */
|
---|
| 41 | public class CollectionMeta
|
---|
| 42 | implements DOMProxyListEntry {
|
---|
[5254] | 43 |
|
---|
| 44 | static final public boolean TEXT = true;
|
---|
| 45 | static final public boolean GREENSTONE = false;
|
---|
| 46 |
|
---|
[5899] | 47 | private boolean dummy = false;
|
---|
[5930] | 48 | private Element element = null;
|
---|
| 49 | private String text = null;
|
---|
[4932] | 50 |
|
---|
| 51 | /** Constructor.
|
---|
| 52 | * @param element the Element from which we will determine metadata details
|
---|
| 53 | */
|
---|
| 54 | public CollectionMeta(Element element) {
|
---|
| 55 | this.element = element;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /** Constructor to create a new piece of metadata given its name. */
|
---|
| 59 | public CollectionMeta(String name) {
|
---|
[12798] | 60 | element = CollectionConfiguration.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
|
---|
[4932] | 61 | element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
|
---|
[8231] | 62 | element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, Configuration.getLanguage());
|
---|
[5930] | 63 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
|
---|
[4932] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /** Constructor to create a new piece of metadata given its name. */
|
---|
| 67 | public CollectionMeta(String name, String language) {
|
---|
[12798] | 68 | element = CollectionConfiguration.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
|
---|
[4932] | 69 | element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
|
---|
| 70 | element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, language);
|
---|
[5930] | 71 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
|
---|
[4932] | 72 | }
|
---|
| 73 |
|
---|
[5899] | 74 | /** Constructor to create a new piece of metadata given its name. */
|
---|
| 75 | public CollectionMeta(String name, String language, boolean dummy) {
|
---|
| 76 | this(name, language);
|
---|
| 77 | this.dummy = dummy;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[4932] | 80 | /** Method to compare two collection metadata objects to calculate their respective ordering.
|
---|
| 81 | * @param object the other metadata to compare to, as an Object
|
---|
| 82 | * @return an int which is less than 0 if this object proceeds the given object, 0 if they are equal and greater than 0 otherwise.
|
---|
| 83 | * @see org.greenstone.gatherer.cdm.Language
|
---|
| 84 | */
|
---|
| 85 | public int compareTo(Object object) {
|
---|
| 86 | return toString().compareTo(object.toString());
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Factory constructor. */
|
---|
| 90 | public DOMProxyListEntry create(Element element) {
|
---|
| 91 | return new CollectionMeta(element);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Method to compare two collection metadata objects for equality.
|
---|
| 95 | * @param object The other metadata to compare to, as an <strong>Object</strong>.
|
---|
| 96 | * @return A <i>boolean</i> value of <i>true</i> if the object are equal, <i>false</i> otherwise.
|
---|
| 97 | */
|
---|
| 98 | public boolean equals(Object object) {
|
---|
| 99 | return (compareTo(object) == 0);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public Element getElement() {
|
---|
| 103 | return element;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Method to retrieve the value of language.
|
---|
[6389] | 107 | * @return the value of language as a <strong>String</strong>.
|
---|
[4932] | 108 | */
|
---|
| 109 | public String getLanguage() {
|
---|
| 110 | // Retrieve the language string
|
---|
| 111 | return element.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /** Method to retrieve the value of name.
|
---|
[6389] | 115 | * @return the name attribute of the collection meta as a String
|
---|
[4932] | 116 | */
|
---|
| 117 | public String getName() {
|
---|
| 118 | return element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
|
---|
| 119 | }
|
---|
| 120 | /** Method to retrieve the value of value (well great choice of name there).
|
---|
| 121 | * @return The value of value as a <strong>String</strong>.
|
---|
| 122 | */
|
---|
[5254] | 123 | public String getValue(boolean text_value) {
|
---|
[8015] | 124 | String raw_value = XMLTools.getValue(element);
|
---|
[5254] | 125 | // Decode the raw value depending on whether the user asked for the TEXT or GREENSTONE version
|
---|
| 126 | if(text_value == TEXT) {
|
---|
| 127 | return Codec.transform(raw_value, Codec.DOM_TO_TEXT);
|
---|
| 128 | }
|
---|
| 129 | else {
|
---|
| 130 | return Codec.transform(raw_value, Codec.DOM_TO_GREENSTONE);
|
---|
| 131 | }
|
---|
[4932] | 132 | }
|
---|
| 133 |
|
---|
| 134 | public boolean isAssigned() {
|
---|
[12641] | 135 | return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
|
---|
[4932] | 136 | }
|
---|
| 137 |
|
---|
[5899] | 138 | public boolean isDummy() {
|
---|
| 139 | return dummy;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[4932] | 142 | /** Determine if this metadata is one of the four special pieces of metadata.
|
---|
| 143 | * @return true if this metadata is special, false otherwise.
|
---|
| 144 | */
|
---|
| 145 | public boolean isSpecial() {
|
---|
| 146 | return (element != null && element.getAttribute(StaticStrings.SPECIAL_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public void setAssigned(boolean assigned) {
|
---|
| 150 | if(element != null) {
|
---|
[12641] | 151 | element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
|
---|
[4932] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public void setElement(Element element) {
|
---|
| 156 | this.element = element;
|
---|
| 157 | text = null;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /** Change the value of value.
|
---|
[6770] | 161 | * @param raw_value the new value as a String.
|
---|
[4932] | 162 | */
|
---|
[5254] | 163 | public void setValue(String raw_value) {
|
---|
[10556] | 164 | // we need to check if the value has changed
|
---|
| 165 | String current_value = XMLTools.getValue(element);
|
---|
| 166 | String new_value = Codec.transform(raw_value, Codec.TEXT_TO_DOM);
|
---|
| 167 | if (!current_value.equals(new_value)) {
|
---|
[5254] | 168 | // Only raw html text can be given to setValue so we need to encode it
|
---|
[10556] | 169 | XMLTools.setValue(element, new_value);
|
---|
| 170 | text = null; // Reset text
|
---|
| 171 | // And determine if this makes the metadata assigned
|
---|
| 172 | setAssigned(raw_value != null && raw_value.length() > 0);
|
---|
| 173 | Gatherer.c_man.configurationChanged();
|
---|
| 174 | }
|
---|
[4932] | 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Method to print out this class as it would appear within the collection configuration file.
|
---|
[5590] | 178 | * @return A <strong>String</strong> containing the text value of this class.
|
---|
| 179 | */
|
---|
[4932] | 180 | public String toString() {
|
---|
| 181 | if(text == null) {
|
---|
[11869] | 182 | text = CollectionConfiguration.toString(element, true);
|
---|
| 183 | if (text.equals("")){
|
---|
[11870] | 184 | text = getName();
|
---|
[11869] | 185 | }
|
---|
[4932] | 186 | }
|
---|
| 187 | return text;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|