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 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 | package org.greenstone.gatherer.msm;
|
---|
38 |
|
---|
39 | import java.io.*;
|
---|
40 | import org.greenstone.gatherer.Gatherer;
|
---|
41 | import org.greenstone.gatherer.collection.Collection;
|
---|
42 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
43 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
44 | import org.greenstone.gatherer.msm.MetadataSetManager;
|
---|
45 | import org.greenstone.gatherer.valuetree.GValueModel;
|
---|
46 | import org.greenstone.gatherer.valuetree.GValueNode;
|
---|
47 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
48 | import org.w3c.dom.Element;
|
---|
49 | /** Stores a mapping between a particular metadata element, and a certain value node.
|
---|
50 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
51 | * @version 2.3
|
---|
52 | */
|
---|
53 | public class Metadata
|
---|
54 | implements Comparable {
|
---|
55 | /** Is this an accumulating piece of metadata? */
|
---|
56 | private boolean accumulate = false;
|
---|
57 | /** Indicates what level of metadata this is, relative to the file it was retrieved for. */
|
---|
58 | private boolean file_level = true;
|
---|
59 | /** The file for which this metadata was retrieved. This allows us to determine the source of folder level metadata. */
|
---|
60 | private File file;
|
---|
61 | /** The value node this metadata maps to. */
|
---|
62 | private GValueNode value = null;
|
---|
63 | /** Used to count the number of references to this piece of metadata within the metadata table. */
|
---|
64 | private int count = 1;
|
---|
65 | /** The metadata element this metadata maps to. */
|
---|
66 | private ElementWrapper element = null;
|
---|
67 | /** Constructs a new Metadata object with no current value.
|
---|
68 | * @param element The <strong>ElementWrapper</strong> associated with this metadata.
|
---|
69 | */
|
---|
70 | public Metadata(ElementWrapper element) {
|
---|
71 | super();
|
---|
72 | this.element = element;
|
---|
73 | this.value = null;
|
---|
74 | }
|
---|
75 | /** Default constructor simply creates a new Metadata object based on the given parameters.
|
---|
76 | * @param element The <strong>ElementWrapper</strong> associated with this metadata.
|
---|
77 | * @param value The assigned value for the given element, as a <strong>GValueNode</strong>.
|
---|
78 | */
|
---|
79 | public Metadata(ElementWrapper element, GValueNode value) {
|
---|
80 | super();
|
---|
81 | this.element = element;
|
---|
82 | this.value = value;
|
---|
83 | }
|
---|
84 | /** Constructs a new Metadata object, given the value. The metadata element is extracted from the value tree information.
|
---|
85 | * @param value The assigned value for the given element, as a <strong>GValueNode</strong>.
|
---|
86 | * @see org.greenstone.gatherer.Gatherer
|
---|
87 | * @see org.greenstone.gatherer.collection.CollectionManager
|
---|
88 | * @see org.greenstone.gatherer.msm.MetadataSetManager
|
---|
89 | */
|
---|
90 | public Metadata(GValueNode value) {
|
---|
91 | super();
|
---|
92 | this.element = Gatherer.c_man.getCollection().msm.getElement(value.getMetadataElementName());
|
---|
93 | this.value = value;
|
---|
94 | }
|
---|
95 | /** Determine if this metadata overwrites or accumulates. */
|
---|
96 | public boolean accumulates() {
|
---|
97 | return accumulate;
|
---|
98 | }
|
---|
99 | /** Compares two Metadata objects for ordering purposes.
|
---|
100 | * @param object The other metadata as an <strong>Object</strong>.
|
---|
101 | * @return An <i>int</i> value as specified in java.lang.String#compareTo
|
---|
102 | * @see java.lang.String#compareTo
|
---|
103 | */
|
---|
104 | public int compareTo(Object object) {
|
---|
105 | return toString().compareTo(object.toString());
|
---|
106 | }
|
---|
107 | /** Decrease the reference count by 1. */
|
---|
108 | public void dec() {
|
---|
109 | count--;
|
---|
110 | }
|
---|
111 | /** Tests to metadata objects for equality.
|
---|
112 | * @param object The other metadata as an <strong>Object</strong>.
|
---|
113 | * @return <i>true</i> if the metadata are equal, <i>false</i> otherwise.
|
---|
114 | */
|
---|
115 | public boolean equals(Object object) {
|
---|
116 | if(compareTo(object) == 0) {
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 | /** Determine the absolute path value of this metadata, taking into account whether this value is hierarchical. */
|
---|
122 | public String getAbsoluteValue() {
|
---|
123 | String abs_result = "";
|
---|
124 | if(value != null) {
|
---|
125 | abs_result = value.toString(GValueNode.GREENSTONE);
|
---|
126 | }
|
---|
127 | return abs_result;
|
---|
128 |
|
---|
129 | //String abs_value = getValue();
|
---|
130 | // What actually gets written as the value depends on whether this is a hierarchy based element.
|
---|
131 | //GValueModel model = Gatherer.c_man.getCollection().msm.getValueTree(element);
|
---|
132 | // Return the result
|
---|
133 | //return abs_value;
|
---|
134 | }
|
---|
135 | /** Retrieve the reference count.
|
---|
136 | * @return The count as an <i>int</i>.
|
---|
137 | */
|
---|
138 | public int getCount() {
|
---|
139 | return count;
|
---|
140 | }
|
---|
141 | /** Retrieve the element associated with this metadata.
|
---|
142 | * @return An <strong>ElementWrapper</strong>.
|
---|
143 | */
|
---|
144 | public ElementWrapper getElement() {
|
---|
145 | return element;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Retrieve the source file if any. */
|
---|
149 | public File getFile() {
|
---|
150 | return file;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Get the textual value of this metadata.
|
---|
154 | * @return A <strong>String</strong> representing the value of this Metadata. Note that if the value node is null, then the String returned is "".
|
---|
155 | */
|
---|
156 | public String getValue() {
|
---|
157 | String result = "";
|
---|
158 | if(value != null) {
|
---|
159 | result = value.toString();
|
---|
160 | }
|
---|
161 | return result;
|
---|
162 | }
|
---|
163 | /** Retrieve the value node associated with this metadata.
|
---|
164 | * @return A <strong>GValueNode</strong>.
|
---|
165 | */
|
---|
166 | public GValueNode getValueNode() {
|
---|
167 | return value;
|
---|
168 | }
|
---|
169 | /** Increase the reference count by 1. */
|
---|
170 | public void inc() {
|
---|
171 | count++;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Determine if this metadata is file level or folder level. */
|
---|
175 | public boolean isFileLevel() {
|
---|
176 | return file_level;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Inform this metadata whether it will accumulate with any other metadata of the same type. */
|
---|
180 | public void setAccumulate(boolean accumulate) {
|
---|
181 | this.accumulate = accumulate;
|
---|
182 | }
|
---|
183 | /** Sets the reference count.
|
---|
184 | * @param value The new value of count as an <i>int</i>.
|
---|
185 | */
|
---|
186 | public void setCount(int value) {
|
---|
187 | count = value;
|
---|
188 | }
|
---|
189 | /** Set the level of this metadata (whether it is associated with this file explicitly or found in some folder above this file). */
|
---|
190 | public void setFileLevel(boolean file_level) {
|
---|
191 | this.file_level = file_level;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** Set the current source file for this metadata to the given collection. This value should not be counted are remaining valid at any time other than immediately after a getMetadata call as it is relative the the file that metadata call was for. */
|
---|
195 | public void setFile(File file) {
|
---|
196 | this.file = file;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** Translates this object into a string representation.
|
---|
200 | * @return A <strong>String</strong>.
|
---|
201 | */
|
---|
202 | public String toString() {
|
---|
203 | return element.toString() + "=" + getValue();
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** Retrieve the value node associated with a certain value string. */
|
---|
207 | static final public GValueNode getDefaultValueNode(ElementWrapper element, String value) {
|
---|
208 | // Retrieve the GValueNode
|
---|
209 | GValueModel model = Gatherer.c_man.getCollection().msm.getValueTree(element);
|
---|
210 | GValueNode value_node;
|
---|
211 | if(model != null) {
|
---|
212 | value_node = model.getValue(value);
|
---|
213 | }
|
---|
214 | else {
|
---|
215 | value_node = new GValueNode(element.getName(), value);
|
---|
216 | }
|
---|
217 | model = null;
|
---|
218 | return value_node;
|
---|
219 | }
|
---|
220 | }
|
---|