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

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

Removed unnecessary imports of org.greenstone.gatherer.Gatherer.

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