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

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

Added a new method for determining if the given element is extracted or not

  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 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.Troolean;
50import org.greenstone.gatherer.util.Utility;
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
64 private Troolean is_extracted = new Troolean();
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 this.namespace = parent.getAttribute("namespace");
73 }
74
75 }
76
77 public void addAttribute(String name, String value) {
78 MSMUtils.addElementAttribute(element, name, "en", value);
79 }
80
81 public void addAttribute(String name, String language, String value) {
82 MSMUtils.addElementAttribute(element, name, language, value);
83 }
84
85 /** Create a copy of this element wrapper.
86 * @return A new <strong>ElementWrapper</strong> based on the same element as this one.
87 */
88 public ElementWrapper copy() {
89 Element element_copy = (Element) element.cloneNode(true);
90 return new ElementWrapper(element_copy);
91 }
92 /** Compare two element wrappers for ordering.
93 * @param object An <strong>Object</strong> which is most likely another element wrapper to be compared with.
94 * @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.
95 */
96 public int compareTo(Object object) {
97 return toString().compareTo(object.toString());
98 }
99 /** Decrement the number of occurances of this metadata element.
100 * @see org.greenstone.gatherer.msm.MSMUtils
101 */
102 public void dec() {
103 MSMUtils.setOccurance(element, -1);
104 }
105 /** Test if two ElementWrappers are equal.
106 * @param object The <strong>Object</strong> to test against.
107 */
108 public boolean equals(Object object) {
109 if(object == null) {
110 return false;
111 }
112 if(object instanceof ElementWrapper) {
113 String our_full_name = MSMUtils.getFullName(element, namespace);
114 String their_full_name = MSMUtils.getFullName(((ElementWrapper)object).getElement());
115 return our_full_name.equals(their_full_name);
116 }
117 return toString().equals(object.toString());
118 }
119 /** Retrieve the attributes associated with the element this element wrapper is built around.
120 * @return A <strong>TreeSet</strong> of the attributes.
121 * @see org.greenstone.gatherer.msm.MSMUtils
122 */
123 public TreeSet getAttributes() {
124 return MSMUtils.getAttributes(element);
125 }
126 /** Retrieve the element this is wrapped around.
127 * @return A DOM <strong>Element</strong> which represents a metadata element.
128 */
129 public Element getElement() {
130 return element;
131 }
132 /** Retrieve the identity of this element (not necessary the same as this elements name). Identity is language and locale dependant.
133 * @return The identity as a <strong>String</strong>.
134 * @see org.greenstone.gatherer.msm.MSMUtils
135 */
136 public String getIdentity() {
137 return MSMUtils.getIdentifier(element);
138 }
139 /** Retrieve the name of this element. Name is unique.
140 * @return The name as a <strong>String</strong>.
141 * @see org.greenstone.gatherer.msm.MSMUtils
142 */
143 public String getName() {
144 return MSMUtils.getFullName(element, namespace);
145 }
146 /** Retrieve the namespace prefix for this element wrapper.
147 * @return A <strong>String</strong> containing the namespace or "" if there is no namespace for this element.
148 * @see org.greenstone.gatherer.msm.MSMUtils
149 */
150 public String getNamespace() {
151 if (!namespace.equals("")) {
152 return namespace;
153 }
154 String name = getName();
155 int pos;
156 if((pos = name.indexOf(MSMUtils.NS_SEP)) != -1) {
157 return name.substring(0, pos);
158 }
159 else {
160 return "";
161 }
162 }
163 /** Look for the occurances 'field' of the element and return it if found.
164 * @return An <i>int</i> which matches the number in the occurances attribute of the element, or 0 if no such attribute.
165 * @see org.greenstone.gatherer.msm.MSMUtils
166 */
167 public int getOccurances() {
168 return MSMUtils.getOccurances(element);
169 }
170 /** 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.
171 * @return A String containing the HTML formatted versions of definition and content (comment).
172 * @see org.greenstone.gatherer.msm.MSMUtils
173 * @see org.greenstone.gatherer.util.Utility
174 */
175 public String getToolTip() {
176 // Add HTML formatting
177 return Utility.formatHTMLWidth(MSMUtils.getDescription(element), 60);
178 }
179
180 public boolean hasValueTree() {
181 return element.getAttribute(StaticStrings.VALUE_TREE_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR);
182 }
183
184 /** Increment the number of occurances of this metadata element.
185 * @see org.greenstone.gatherer.msm.MSMUtils
186 */
187 public void inc() {
188 MSMUtils.setOccurance(element, 1);
189 }
190
191 public boolean isExtracted() {
192 if(!is_extracted.isDecided()) {
193 String raw_namespace = namespace;
194 if(raw_namespace != null && raw_namespace.length() > 0 && !raw_namespace.endsWith(StaticStrings.STOP_CHARACTER)) {
195 raw_namespace = raw_namespace + StaticStrings.STOP_CHARACTER;
196 }
197 is_extracted.set(raw_namespace == null || raw_namespace.equals(StaticStrings.EMPTY_STR) || raw_namespace.equals(StaticStrings.EXTRACTED_NAMESPACE));
198 }
199 return is_extracted.isTrue();
200 }
201
202 public boolean isHierarchy() {
203 return element.getAttribute(StaticStrings.HIERARCHY_ATTRIBUTE).equalsIgnoreCase(StaticStrings.TRUE_STR);
204 }
205
206 /** Removes an Attribute node from the element. */
207 public boolean removeAttribute(String name, String language, String value)
208 {
209 // Find the attribute to remove
210 for (Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
211 if (n.getNodeName().equals("Attribute")) {
212 Element e = (Element) n;
213 if (e.getAttribute("name").equals(name) &&
214 e.getAttribute("language").equalsIgnoreCase(language) &&
215 MSMUtils.getValue(e).equals(value)) {
216 // Match found, so remove the attribute node and return
217 element.removeChild(n);
218 return true;
219 }
220 }
221 }
222
223 // No match found
224 return false;
225 }
226
227 public void setHierarchy(boolean value) {
228 element.setAttribute(StaticStrings.HIERARCHY_ATTRIBUTE, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
229 }
230
231 public String toString() {
232 String element_name = getName();
233 String element_identifier = getIdentity();
234
235 // Generate the element name without the namespace
236 String element_name_no_namespace = element_name;
237 int namespace_end = element_name.indexOf(MSMUtils.NS_SEP);
238 if (namespace_end != -1) {
239 element_name_no_namespace = element_name.substring(namespace_end + 1);
240 }
241
242 // Return just the element name, unless the element identifier differs
243 //if (element_name_no_namespace.equals(element_identifier)) {
244 return element_name;
245 //}
246 //else {
247 // return element_name + " (" + element_identifier + ")";
248 //}
249 }
250}
Note: See TracBrowser for help on using the repository browser.