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

Last change on this file since 20448 was 20448, checked in by kjdon, 15 years ago

toSTring for metadtaa element now in CollectCfgReadWrite instead of COllectionConfiguration

  • 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.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 private boolean dummy = false;
48 private Element element = null;
49 private String text = null;
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) {
60 element = CollectionConfiguration.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
61 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
62 element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, Configuration.getLanguage());
63 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
64 }
65
66 /** Constructor to create a new piece of metadata given its name. */
67 public CollectionMeta(String name, String language) {
68 element = CollectionConfiguration.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
69 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
70 element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, language);
71 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
72 }
73
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
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.
107 * @return the value of language as a <strong>String</strong>.
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.
115 * @return the name attribute of the collection meta as a String
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 */
123 public String getValue(boolean text_value) {
124 String raw_value = XMLTools.getValue(element);
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 }
132 }
133
134 public boolean isAssigned() {
135 return (element != null && !element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR));
136 }
137
138 public boolean isDummy() {
139 return dummy;
140 }
141
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) {
151 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
152 }
153 }
154
155 public void setElement(Element element) {
156 this.element = element;
157 text = null;
158 }
159
160 /** Change the value of value.
161 * @param raw_value the new value as a String.
162 */
163 public void setValue(String raw_value) {
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)) {
168 // Only raw html text can be given to setValue so we need to encode it
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 }
174 }
175
176 /** Method to print out this class as it would appear within the collection configuration file.
177 * @return A <strong>String</strong> containing the text value of this class.
178 */
179 public String toString() {
180 if(text == null) {
181 text = CollectCfgReadWrite.toString(element);
182 if (text.equals("")){
183 text = getName();
184 }
185 }
186 return text;
187 }
188}
Note: See TracBrowser for help on using the repository browser.