package org.greenstone.gatherer.msm;
/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: John Thompson, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 1999 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
import java.io.*;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.collection.Collection;
import org.greenstone.gatherer.collection.CollectionManager;
import org.greenstone.gatherer.msm.ElementWrapper;
import org.greenstone.gatherer.msm.MetadataSetManager;
import org.greenstone.gatherer.valuetree.GValueModel;
import org.greenstone.gatherer.valuetree.GValueNode;
import org.w3c.dom.Element;
/** Stores a mapping between a particular metadata element, and a certain value node.
* @author John Thompson, Greenstone Digital Library, University of Waikato
* @version 2.3
*/
public class Metadata
implements Comparable {
/** Is this an accumulating piece of metadata? */
transient private boolean accumulate = false;
/** Indicates what level of metadata this is, relative to the file it was retrieved for. */
transient private boolean file_level = true;
/** The file for which this metadata was retrieved. This allows us to determine the source of folder level metadata. */
transient private File file;
/** The value node this metadata maps to. */
private GValueNode value = null;
/** Used to count the number of references to this piece of metadata within the metadata table. */
private int count = 1;
/** The metadata element this metadata maps to. */
private ElementWrapper element = null;
/** Constructs a new Metadata object with no current value.
* @param element The ElementWrapper associated with this metadata.
*/
public Metadata(ElementWrapper element) {
super();
this.element = element;
this.value = null;
}
/** Default constructor simply creates a new Metadata object based on the given parameters.
* @param element The ElementWrapper associated with this metadata.
* @param value The assigned value for the given element, as a GValueNode.
*/
public Metadata(ElementWrapper element, GValueNode value) {
super();
this.element = element;
this.value = value;
}
/** Constructs a new Metadata object, given the value. The metadata element is extracted from the value tree information.
* @param value The assigned value for the given element, as a GValueNode.
* @see org.greenstone.gatherer.Gatherer
* @see org.greenstone.gatherer.collection.CollectionManager
* @see org.greenstone.gatherer.msm.MetadataSetManager
*/
public Metadata(GValueNode value) {
super();
this.element = Gatherer.c_man.getCollection().msm.getElement(value.getMetadataElementName());
this.value = value;
}
/** Determine if this metadata overwrites or accumulates. */
public boolean accumulates() {
return accumulate;
}
/** Compares two Metadata objects for ordering purposes.
* @param object The other metadata as an Object.
* @return An int value as specified in java.lang.String#compareTo
* @see java.lang.String#compareTo
*/
public int compareTo(Object object) {
return toString().compareTo(object.toString());
}
/** Decrease the reference count by 1. */
public void dec() {
count--;
}
/** Tests to metadata objects for equality.
* @param object The other metadata as an Object.
* @return true if the metadata are equal, false otherwise.
*/
public boolean equals(Object object) {
if(compareTo(object) == 0) {
return true;
}
return false;
}
/** Determine the absolute path value of this metadata, taking into account whether this value is hierarchical. */
public String getAbsoluteValue() {
String abs_value = getValue();
// What actually gets written as the value depends on whether this is a hierarchy based element.
GValueModel model = Gatherer.c_man.getCollection().msm.getValueTree(element);
if(model != null && model.isHierarchy()) {
if(value != null) {
GValueNode node = (GValueNode)value.getParent();
while(node != null) {
GValueNode next = (GValueNode)node.getParent();
if(next != null) {
abs_value = node.toString() + "\\" + abs_value;
}
node = next;
}
}
abs_value = model.getHIndex(abs_value);
}
// Return the result
return abs_value;
}
/** Retrieve the reference count.
* @return The count as an int.
*/
public int getCount() {
return count;
}
/** Retrieve the element associated with this metadata.
* @return An ElementWrapper.
*/
public ElementWrapper getElement() {
return element;
}
/** Retrieve the source file if any. */
public File getFile() {
return file;
}
/** Get the textual value of this metadata.
* @return A String representing the value of this Metadata. Note that if the value node is null, then the String returned is "".
*/
public String getValue() {
String result = "";
if(value != null) {
result = value.toString();
}
return result;
}
/** Retrieve the value node associated with this metadata.
* @return A GValueNode.
*/
public GValueNode getValueNode() {
return value;
}
/** Increase the reference count by 1. */
public void inc() {
count++;
}
/** Determine if this metadata is file level or folder level. */
public boolean isFileLevel() {
return file_level;
}
/** Inform this metadata whether it will accumulate with any other metadata of the same type. */
public void setAccumulate(boolean accumulate) {
this.accumulate = accumulate;
}
/** Sets the reference count.
* @param value The new value of count as an int.
*/
public void setCount(int value) {
count = value;
}
/** Set the level of this metadata (whether it is associated with this file explicitly or found in some folder above this file). */
public void setFileLevel(boolean file_level) {
this.file_level = file_level;
}
/** 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. */
public void setFile(File file) {
this.file = file;
}
/** Translates this object into a string representation.
* @return A String.
*/
public String toString() {
return element.toString() + "=" + getValue();
}
/** Retrieve the value node associated with a certain value string. */
static final public GValueNode getDefaultValueNode(ElementWrapper element, String value) {
// Retrieve the GValueNode
GValueModel model = Gatherer.c_man.getCollection().msm.getValueTree(element);
GValueNode value_node;
if(model != null) {
value_node = model.getValue(value);
}
else {
value_node = new GValueNode(element.getName(), value);
}
model = null;
return value_node;
}
}