source: trunk/gli/src/org/greenstone/gatherer/msm/Metadata.java@ 4365

Last change on this file since 4365 was 4365, checked in by mdewsnip, 21 years ago

Fixed tabbing.

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