/** *######################################################################### * * 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. *######################################################################## */ package org.greenstone.gatherer.gems; /************************************************************************************** * Title: Gatherer * Description: The Gatherer: a tool for gathering and enriching a digital collection. * Company: The University of Waikato * Written: / /01 * Revised: 03/10/02 - Commented **************************************************************************************/ import java.util.TreeSet; import java.util.ArrayList; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Troolean; import org.greenstone.gatherer.util.Utility; import org.greenstone.gatherer.util.XMLTools; import org.w3c.dom.*; /** This class provides a convience wrapper around a DOM model Element to allow Components such as the MetadataTable to display this information properly. * @author John Thompson * @version 2.3 */ public class ElementWrapper implements Comparable { /** The DOM element this wrapper is wrapped around. */ private Element element = null; /** A string prefix identifying the metadata set namespace. */ private String namespace = ""; private ArrayList subelements = null; //The list of subelements private Troolean is_extracted = new Troolean(); /** Constructor for elements with no namespace necessary. * @param element The DOM Element this is to be based on. */ public ElementWrapper(Element element) { this.element = element; Element parent = (Element)element.getParentNode(); if (parent != null) { this.namespace = parent.getAttribute("namespace"); } subelements = XMLTools.getChildElementsByTagName(element, "Element"); //Should read in each subelement's value tree? } public void addAttribute(String name, String value) { MSMUtils.addElementAttribute(element, name, "en", value); } public void addAttribute(String name, String language, String value) { MSMUtils.addElementAttribute(element, name, language, value); } /** Check to see if the element contains a particular subelement @return true if element already contains a subelement of name name. @param name the name of the subelement to check for, as a String @author Matthew Whyte Date last modified: 19/01/04 */ public boolean containsSubelement(String name) { //System.err.println("Want to see if subelement: " + name + " exists."); //debug for(int i = 0; i < subelements.size(); i++) { Element sibling = (Element)subelements.get(i); String sibling_name = sibling.getAttribute("name"); if(sibling_name.equals(name)) { return true; } } return false; } /* Check to see if the element contains any subelements @return tree if element contains subelements @author Matthew Whyte Date last modified: 2/02/05 */ public boolean containsSubelements() { return(subelements.size() != 0); } /** Method to add a subelement to the list of subelements. @param element the subelement to add to the list, as a Element @author Matthew Whyte Date last modified: 27/01/05 */ public void addSubelementToList(Element element) { subelements.add(element); } /** Method to remove a subelement from the list of subelements. @param element the subelement to remove from the list, as a Element @author Matthew Whyte Date last modified: 27/01/05 */ public void removeSubelementFromList(Element element) { subelements.remove(element); } /** Method to acquire a list of all the subelements in the element. @return A NodeList containing all of this element's subelements. @author: Matthew Whyte @date last modified: 19/01/04 */ public ArrayList getSubelements() { return subelements; } /** Create a copy of this element wrapper. * @return A new ElementWrapper based on the same element as this one. */ public ElementWrapper copy() { Element element_copy = (Element) element.cloneNode(true); return new ElementWrapper(element_copy); } /** Compare two element wrappers for ordering. * @param object An Object which is most likely another element wrapper to be compared with. * @return An int indicating order, -1, 0, 1 if this element wrapper is less than, equal to or greater than the given object. */ public int compareTo(Object object) { return toString().compareTo(object.toString()); } /** Decrement the number of occurances of this metadata element. * @see org.greenstone.gatherer.msm.MSMUtils */ public void dec() { MSMUtils.setOccurance(element, -1); } /** Test if two ElementWrappers are equal. * @param object The Object to test against. */ public boolean equals(Object object) { if(object == null) { return false; } if(object instanceof ElementWrapper) { String our_full_name = MSMUtils.getFullName(element, namespace); String their_full_name = MSMUtils.getFullName(((ElementWrapper)object).getElement()); return our_full_name.equals(their_full_name); } return toString().equals(object.toString()); } /** Retrieve the attributes associated with the element this element wrapper is built around. * @return A TreeSet of the attributes. * @see org.greenstone.gatherer.msm.MSMUtils */ public TreeSet getAttributes() { return MSMUtils.getAttributes(element); } /** Retrieve the element this is wrapped around. * @return A DOM Element which represents a metadata element. */ public Element getElement() { return element; } /** Retrieve the identity of this element (not necessary the same as this elements name). Identity is language and locale dependant. * @return The identity as a String. * @see org.greenstone.gatherer.msm.MSMUtils */ public String getIdentity() { return MSMUtils.getIdentifier(element); } /** Retrieve the name of this element. Name is unique. @return The name (without the namespace) as a String. @see org.greenstone.gatherer.msm.MSMUtils @author Matthew Whyte @date last modified: 21/01/04 */ public String getNameOnly() { return MSMUtils.getNameOnly(element); } /** Retrieve the name of this element. Name is unique. * @return The name as a String. * @see org.greenstone.gatherer.msm.MSMUtils */ public String getName() { return MSMUtils.getFullName(element, namespace); } /** Retrieve the namespace prefix for this element wrapper. * @return A String containing the namespace or "" if there is no namespace for this element. * @see org.greenstone.gatherer.msm.MSMUtils */ public String getNamespace() { if (!namespace.equals("")) { return namespace; } String name = getName(); int pos; if((pos = name.indexOf(MSMUtils.NS_SEP)) != -1) { return name.substring(0, pos); } else { return ""; } } /** Look for the occurances 'field' of the element and return it if found. * @return An int which matches the number in the occurances attribute of the element, or 0 if no such attribute. * @see org.greenstone.gatherer.msm.MSMUtils */ public int getOccurances() { return MSMUtils.getOccurances(element); } /** This method is essentially the same as getDescription() in that it does indeed return this metaelements description. However this method uses the Utility function formatHTMLWidth() to ensure the String can be displayed in a tool-tip window using html markup. * @return A String containing the HTML formatted versions of definition and content (comment). * @see org.greenstone.gatherer.msm.MSMUtils * @see org.greenstone.gatherer.util.Utility */ public String getToolTip() { // Add HTML formatting return Utility.formatHTMLWidth(MSMUtils.getDescription(element), 60); } public boolean hasValueTree() { return element.getAttribute(StaticStrings.VALUE_TREE_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR); } /** Increment the number of occurances of this metadata element. * @see org.greenstone.gatherer.msm.MSMUtils */ public void inc() { MSMUtils.setOccurance(element, 1); } public boolean isExtracted() { if(!is_extracted.isDecided()) { String raw_namespace = namespace; if(raw_namespace != null && raw_namespace.length() > 0 && !raw_namespace.endsWith(StaticStrings.STOP_CHARACTER)) { raw_namespace = raw_namespace + StaticStrings.STOP_CHARACTER; } is_extracted.set(raw_namespace == null || raw_namespace.equals(StaticStrings.EMPTY_STR) || raw_namespace.equals(StaticStrings.EXTRACTED_NAMESPACE)); } return is_extracted.isTrue(); } public boolean isHierarchy() { return element.getAttribute(StaticStrings.HIERARCHY_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR); } /** Removes an Attribute node from the element. */ public boolean removeAttribute(String name, String language, String value) { boolean sucess = MSMUtils.removeElementAttribute(element, name, language, value); if(!sucess) { //This should not happen. System.err.println("Error removing attribute \'" + name + "\' with value \'" + value +"\'"); } return sucess; } public String toString() { return getNamespace() + MSMUtils.NS_SEP + getIdentity(); } }