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

Last change on this file since 36185 was 36185, checked in by kjdon, 2 years ago

we now have a new SearchMeta class, inherits from CollectionMeta. This add a type field to a collection meta, and is used for storing index, level, sortfield etc display texts. We want to keep the two lists separate now. and searchmeta need a type - index/sortfield/level etc. otherwise can't distinguish between the display text for a titles index or a titles sortfield.

  • 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
29import org.greenstone.gatherer.Gatherer;
30import org.greenstone.gatherer.Configuration;
31import org.greenstone.gatherer.util.Codec;
32import org.greenstone.gatherer.util.StaticStrings;
33import org.greenstone.gatherer.util.XMLTools;
34import org.w3c.dom.*;
35
36
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 */
41public class CollectionMeta
42 implements DOMProxyListEntry {
43
44 static final public boolean TEXT = true;
45 static final public boolean GREENSTONE = false;
46
47 protected boolean dummy = false;
48 protected Element element = null;
49 protected String text = null;
50
51 /** default no args constructor which gets called explicitly from subclass construcotrs */
52 public CollectionMeta() {
53 }
54 /** Constructor.
55 * @param element the Element from which we will determine metadata details
56 */
57 public CollectionMeta(Element element) {
58 this.element = element;
59 }
60
61 /** Constructor to create a new piece of metadata given its name. */
62 public CollectionMeta(String name) {
63 element = CollectionConfiguration.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
64 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
65 element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, Configuration.getLanguage());
66 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
67 }
68
69 /** Constructor to create a new piece of metadata given its name. */
70 public CollectionMeta(String name, String language) {
71 element = CollectionConfiguration.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
72 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
73 element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, language);
74 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
75 }
76
77 /** Constructor to create a new piece of metadata given its name. */
78 public CollectionMeta(String name, String language, boolean dummy) {
79 this(name, language);
80 this.dummy = dummy;
81 }
82
83 /** Method to compare two collection metadata objects to calculate their respective ordering.
84 * @param object the other metadata to compare to, as an Object
85 * @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.
86 * @see org.greenstone.gatherer.cdm.Language
87 */
88 public int compareTo(Object object) {
89 return toString().compareTo(object.toString());
90 }
91
92 /** Factory constructor. */
93 public DOMProxyListEntry create(Element element) {
94 return new CollectionMeta(element);
95 }
96
97 /** Method to compare two collection metadata objects for equality.
98 * @param object The other metadata to compare to, as an <strong>Object</strong>.
99 * @return A <i>boolean</i> value of <i>true</i> if the object are equal, <i>false</i> otherwise.
100 */
101 public boolean equals(Object object) {
102 System.err.println("collmeat equals");
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(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.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(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.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 setValue(raw_value, false);
169 }
170
171 public void setValue(String raw_value, boolean preserveTags) {
172 // we need to check if the value has changed
173 String current_value = XMLTools.getValue(element);
174
175 String new_value = preserveTags ?
176 Codec.transform(raw_value, Codec.TEXT_TO_DOM_PRESERVE_TAGS)
177 : Codec.transform(raw_value, Codec.TEXT_TO_DOM);
178 if (!current_value.equals(new_value)) {
179 // Only raw html text can be given to setValue so we need to encode it
180 XMLTools.setValue(element, new_value);
181 text = null; // Reset text
182 // And determine if this makes the metadata assigned
183 setAssigned(raw_value != null && raw_value.length() > 0);
184 }
185 }
186
187
188 /** Method to print out this class as it would appear within the collection configuration file.
189 * @return A <strong>String</strong> containing the text value of this class.
190 */
191 public String toString() {
192 if(text == null) {
193 text = CollectCfgReadWrite.toString(element);
194 if (text.equals("")){
195 text = getName();
196 }
197 }
198 return text;
199 }
200}
Note: See TracBrowser for help on using the repository browser.