source: trunk/gli/src/org/greenstone/gatherer/msm/ElementWrapper.java@ 5714

Last change on this file since 5714 was 5714, checked in by jmt12, 21 years ago

The equals method occasionally bombed - which may have caused a problem when occasionally importing

  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 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 * <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 */
37package org.greenstone.gatherer.msm;
38
39/**************************************************************************************
40 * Title: Gatherer
41 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
42 * Company: The University of Waikato
43 * Written: / /01
44 * Revised: 03/10/02 - Commented
45 **************************************************************************************/
46import java.util.TreeSet;
47import org.greenstone.gatherer.msm.MSMUtils;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.greenstone.gatherer.util.Utility;
50import org.w3c.dom.*;
51
52/** This class provides a convience wrapper around a DOM model Element to allow Components such as the MetadataTable to display this information properly.
53 * @author John Thompson
54 * @version 2.3
55 */
56public class ElementWrapper
57 implements Comparable {
58 /** The DOM element this wrapper is wrapped around. */
59 private Element element = null;
60 /** A string prefix identifying the metadata set namespace. */
61 private String namespace = "";
62 /** Constructor for elements with no namespace necessary.
63 * @param element The DOM <strong>Element</strong> this is to be based on.
64 */
65 public ElementWrapper(Element element) {
66 this.element = element;
67 Element parent = (Element)element.getParentNode();
68 if (parent != null) {
69 this.namespace = parent.getAttribute("namespace");
70 }
71
72 }
73
74 public void addAttribute(String name, String value) {
75 MSMUtils.addElementAttribute(element, name, "en", value);
76 }
77
78 public void addAttribute(String name, String language, String value) {
79 MSMUtils.addElementAttribute(element, name, language, value);
80 }
81
82 /** Create a copy of this element wrapper.
83 * @return A new <strong>ElementWrapper</strong> based on the same element as this one.
84 */
85 public ElementWrapper copy() {
86 Element element_copy = (Element) element.cloneNode(true);
87 return new ElementWrapper(element_copy);
88 }
89 /** Compare two element wrappers for ordering.
90 * @param object An <strong>Object</strong> which is most likely another element wrapper to be compared with.
91 * @return An <i>int</i> indicating order, -1, 0, 1 if this element wrapper is less than, equal to or greater than the given object.
92 */
93 public int compareTo(Object object) {
94 return toString().compareTo(object.toString());
95 }
96 /** Decrement the number of occurances of this metadata element.
97 * @see org.greenstone.gatherer.msm.MSMUtils
98 */
99 public void dec() {
100 MSMUtils.setOccurance(element, -1);
101 }
102 /** Test if two ElementWrappers are equal.
103 * @param object The <strong>Object</strong> to test against.
104 */
105 public boolean equals(Object object) {
106 if(object == null) {
107 return false;
108 }
109 if(object instanceof ElementWrapper) {
110 String our_full_name = MSMUtils.getFullName(element, namespace);
111 String their_full_name = MSMUtils.getFullName(((ElementWrapper)object).getElement());
112 return our_full_name.equals(their_full_name);
113 }
114 return toString().equals(object.toString());
115 }
116 /** Retrieve the attributes associated with the element this element wrapper is built around.
117 * @return A <strong>TreeSet</strong> of the attributes.
118 * @see org.greenstone.gatherer.msm.MSMUtils
119 */
120 public TreeSet getAttributes() {
121 return MSMUtils.getAttributes(element);
122 }
123 /** Retrieve the element this is wrapped around.
124 * @return A DOM <strong>Element</strong> which represents a metadata element.
125 */
126 public Element getElement() {
127 return element;
128 }
129 /** Retrieve the identity of this element (not necessary the same as this elements name). Identity is language and locale dependant.
130 * @return The identity as a <strong>String</strong>.
131 * @see org.greenstone.gatherer.msm.MSMUtils
132 */
133 public String getIdentity() {
134 return MSMUtils.getIdentifier(element);
135 }
136 /** Retrieve the name of this element. Name is unique.
137 * @return The name as a <strong>String</strong>.
138 * @see org.greenstone.gatherer.msm.MSMUtils
139 */
140 public String getName() {
141 return MSMUtils.getFullName(element, namespace);
142 }
143 /** Retrieve the namespace prefix for this element wrapper.
144 * @return A <strong>String</strong> containing the namespace or "" if there is no namespace for this element.
145 * @see org.greenstone.gatherer.msm.MSMUtils
146 */
147 public String getNamespace() {
148 if (!namespace.equals("")) {
149 return namespace;
150 }
151 String name = getName();
152 int pos;
153 if((pos = name.indexOf(MSMUtils.NS_SEP)) != -1) {
154 return name.substring(0, pos);
155 }
156 else {
157 return "";
158 }
159 }
160 /** Look for the occurances 'field' of the element and return it if found.
161 * @return An <i>int</i> which matches the number in the occurances attribute of the element, or 0 if no such attribute.
162 * @see org.greenstone.gatherer.msm.MSMUtils
163 */
164 public int getOccurances() {
165 return MSMUtils.getOccurances(element);
166 }
167 /** 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.
168 * @return A String containing the HTML formatted versions of definition and content (comment).
169 * @see org.greenstone.gatherer.msm.MSMUtils
170 * @see org.greenstone.gatherer.util.Utility
171 */
172 public String getToolTip() {
173 // Add HTML formatting
174 return Utility.formatHTMLWidth(MSMUtils.getDescription(element), 60);
175 }
176
177 public boolean hasValueTree() {
178 return element.getAttribute(StaticStrings.VALUE_TREE_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR);
179 }
180
181 /** Increment the number of occurances of this metadata element.
182 * @see org.greenstone.gatherer.msm.MSMUtils
183 */
184 public void inc() {
185 MSMUtils.setOccurance(element, 1);
186 }
187
188 public boolean isHierarchy() {
189 return element.getAttribute(StaticStrings.HIERARCHY_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR);
190 }
191
192 /** Removes an Attribute node from the element. */
193 public boolean removeAttribute(String name, String language, String value)
194 {
195 // Find the attribute to remove
196 for (Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
197 if (n.getNodeName().equals("Attribute")) {
198 Element e = (Element) n;
199 if (e.getAttribute("name").equals(name) &&
200 e.getAttribute("language").equalsIgnoreCase(language) &&
201 MSMUtils.getValue(e).equals(value)) {
202 // Match found, so remove the attribute node and return
203 element.removeChild(n);
204 return true;
205 }
206 }
207 }
208
209 // No match found
210 return false;
211 }
212
213 public String toString() {
214 return MSMUtils.getFullIdentifier(element, namespace);
215 }
216}
Note: See TracBrowser for help on using the repository browser.