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

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

More GEMS fixes, by Matthew Whyte. Can now create subelements of subelements.

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