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

Last change on this file since 10345 was 10345, checked in by mdewsnip, 19 years ago

Removed some more crap out of the Utility class.

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