source: trunk/gli/src/org/greenstone/gatherer/cdm/CollectionMeta.java@ 6003

Last change on this file since 6003 was 5930, checked in by jmt12, 21 years ago

Metadata is now only written to collect.cfg if assigned. It used to instead return an empty string if no value was set - which of course meant that any two different collection meta which just happened to have no strings set were taken as the same object by the equals method in the DOMProxyListModel

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