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

Last change on this file since 4527 was 4502, checked in by kjdon, 21 years ago

ElementWrapper now stores the namespace that its element had when it was created. this is passed to MSMUtils getFullName and getFullIdentifier to be used if the element has no parent

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