source: trunk/gli/src/org/greenstone/gatherer/gems/ElementWrapper.java@ 9647

Last change on this file since 9647 was 9191, checked in by mdewsnip, 19 years ago

Removed some dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.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.gems;
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 java.util.ArrayList;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.greenstone.gatherer.util.Utility;
50import org.greenstone.gatherer.util.XMLTools;
51import org.w3c.dom.*;
52
53/** This class provides a convience wrapper around a DOM model Element to allow Components such as the MetadataTable to display this information properly.
54 * @author John Thompson
55 * @version 2.3
56 */
57public class ElementWrapper
58 implements Comparable {
59 /** The DOM element this wrapper is wrapped around. */
60 private Element element = null;
61 /** A string prefix identifying the metadata set namespace. */
62 private String namespace = "";
63 private ArrayList subelements = null; //The list of subelements
64
65 /** Constructor for elements with no namespace necessary.
66 * @param element The DOM <strong>Element</strong> this is to be based on.
67 */
68 public ElementWrapper(Element element) {
69 this.element = element;
70 Element parent = (Element)element.getParentNode();
71 if (parent != null)
72 {
73 this.namespace = parent.getAttribute("namespace");
74 }
75
76 subelements = XMLTools.getChildElementsByTagName(element, "Element");
77 //Should read in each subelement's value tree?
78 }
79
80 public void addAttribute(String name, String value) {
81 MSMUtils.addElementAttribute(element, name, "en", value);
82 }
83
84 public void addAttribute(String name, String language, String value) {
85 MSMUtils.addElementAttribute(element, name, language, value);
86 }
87
88
89 /**
90 Check to see if the element contains a particular subelement
91 @return true if element already contains a subelement of name name.
92 @param name the name of the subelement to check for, as a <strong>String</strong>
93 @author Matthew Whyte
94 Date last modified: 19/01/04
95 */
96 public boolean containsSubelement(String name)
97 {
98 //System.err.println("Want to see if subelement: " + name + " exists."); //debug
99 for(int i = 0; i < subelements.size(); i++)
100 {
101 Element sibling = (Element)subelements.get(i);
102 String sibling_name = sibling.getAttribute("name");
103 if(sibling_name.equals(name))
104 {
105 return true;
106 }
107 }
108
109 return false;
110 }
111
112 /*
113 Check to see if the element contains any subelements
114 @return tree if element contains subelements
115 @author Matthew Whyte
116 Date last modified: 2/02/05
117 */
118 public boolean containsSubelements()
119 {
120 return(subelements.size() != 0);
121 }
122
123 /**
124 Method to add a subelement to the list of subelements.
125 @param element the subelement to add to the list, as a <strong>Element</strong>
126 @author Matthew Whyte
127 Date last modified: 27/01/05
128 */
129 public void addSubelementToList(Element element)
130 {
131 subelements.add(element);
132 }
133
134 /**
135 Method to remove a subelement from the list of subelements.
136 @param element the subelement to remove from the list, as a <strong>Element</strong>
137 @author Matthew Whyte
138 Date last modified: 27/01/05
139 */
140 public void removeSubelementFromList(Element element)
141 {
142 subelements.remove(element);
143 }
144
145 /**
146 Method to acquire a list of all the subelements in the element.
147 @return A <strong>NodeList</strong> containing all of this element's subelements.
148
149 @author: Matthew Whyte
150 @date last modified: 19/01/04
151 */
152 public ArrayList getSubelements()
153 {
154 return subelements;
155 }
156
157
158 /** Create a copy of this element wrapper.
159 * @return A new <strong>ElementWrapper</strong> based on the same element as this one.
160 */
161 public ElementWrapper copy() {
162 Element element_copy = (Element) element.cloneNode(true);
163 return new ElementWrapper(element_copy);
164 }
165 /** Compare two element wrappers for ordering.
166 * @param object An <strong>Object</strong> which is most likely another element wrapper to be compared with.
167 * @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.
168 */
169 public int compareTo(Object object) {
170 return toString().compareTo(object.toString());
171 }
172 /** Decrement the number of occurances of this metadata element.
173 * @see org.greenstone.gatherer.msm.MSMUtils
174 */
175 public void dec() {
176 MSMUtils.setOccurance(element, -1);
177 }
178 /** Test if two ElementWrappers are equal.
179 * @param object The <strong>Object</strong> to test against.
180 */
181 public boolean equals(Object object) {
182 if(object == null) {
183 return false;
184 }
185 if(object instanceof ElementWrapper) {
186 String our_full_name = MSMUtils.getFullName(element, namespace);
187 String their_full_name = MSMUtils.getFullName(((ElementWrapper)object).getElement());
188 return our_full_name.equals(their_full_name);
189 }
190 return toString().equals(object.toString());
191 }
192 /** Retrieve the attributes associated with the element this element wrapper is built around.
193 * @return A <strong>TreeSet</strong> of the attributes.
194 * @see org.greenstone.gatherer.msm.MSMUtils
195 */
196 public TreeSet getAttributes() {
197 return MSMUtils.getAttributes(element);
198 }
199 /** Retrieve the element this is wrapped around.
200 * @return A DOM <strong>Element</strong> which represents a metadata element.
201 */
202 public Element getElement() {
203 return element;
204 }
205 /** Retrieve the identity of this element (not necessary the same as this elements name). Identity is language and locale dependent.
206 * @return The identity as a <strong>String</strong>.
207 * @see org.greenstone.gatherer.msm.MSMUtils
208 */
209 public String getIdentity() {
210 return MSMUtils.getIdentifier(element);
211 }
212
213 /** Retrieve the name of this element. Name is unique.
214 @return The name (without the namespace) as a <strong>String</strong>.
215 @see org.greenstone.gatherer.msm.MSMUtils
216 @author Matthew Whyte
217 @date last modified: 21/01/04
218 */
219 public String getNameOnly() {
220 return MSMUtils.getNameOnly(element);
221 }
222
223 /** Retrieve the name of this element. Name is unique.
224 * @return The name as a <strong>String</strong>.
225 * @see org.greenstone.gatherer.msm.MSMUtils
226 */
227 public String getName() {
228 return MSMUtils.getFullName(element, namespace);
229 }
230
231
232 /** Retrieve the namespace prefix for this element wrapper.
233 * @return A <strong>String</strong> containing the namespace or "" if there is no namespace for this element.
234 * @see org.greenstone.gatherer.msm.MSMUtils
235 */
236 public String getNamespace() {
237 if (!namespace.equals("")) {
238 return namespace;
239 }
240 String name = getName();
241 int pos;
242 if((pos = name.indexOf(MSMUtils.NS_SEP)) != -1) {
243 return name.substring(0, pos);
244 }
245 else {
246 return "";
247 }
248 }
249 /** Look for the occurances 'field' of the element and return it if found.
250 * @return An <i>int</i> which matches the number in the occurances attribute of the element, or 0 if no such attribute.
251 * @see org.greenstone.gatherer.msm.MSMUtils
252 */
253 public int getOccurances() {
254 return MSMUtils.getOccurances(element);
255 }
256 /** 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.
257 * @return A String containing the HTML formatted versions of definition and content (comment).
258 * @see org.greenstone.gatherer.msm.MSMUtils
259 * @see org.greenstone.gatherer.util.Utility
260 */
261 public String getToolTip() {
262 // Add HTML formatting
263 return Utility.formatHTMLWidth(MSMUtils.getDescription(element), 60);
264 }
265
266 /** Increment the number of occurances of this metadata element.
267 * @see org.greenstone.gatherer.msm.MSMUtils
268 */
269 public void inc() {
270 MSMUtils.setOccurance(element, 1);
271 }
272
273 public boolean isHierarchy() {
274 return element.getAttribute(StaticStrings.HIERARCHY_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR);
275 }
276
277 /** Removes an Attribute node from the element. */
278 public boolean removeAttribute(String name, String language, String value) {
279 boolean sucess = MSMUtils.removeElementAttribute(element, name, language, value);
280 if(!sucess)
281 {
282 //This should not happen.
283 System.err.println("Error removing attribute \'" + name + "\' with value \'" + value +"\'");
284 }
285 return sucess;
286 }
287
288 public String toString() {
289 return getNamespace() + MSMUtils.NS_SEP + getIdentity();
290 }
291
292}
Note: See TracBrowser for help on using the repository browser.