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

Last change on this file since 8270 was 8270, checked in by mdewsnip, 20 years ago

Source files for the Greenstone Editor for Metadata Sets (GEMS). This is currently just the old MetadataEditorManager, modified to run stand-alone. It will be substantially improved by Attila Aros.

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