source: trunk/gli/src/org/greenstone/gatherer/gems/MSMUtils.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: 47.5 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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gems;
28
29/**************************************************************************************
30 * Title: Gatherer
31 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
32 * Company: The University of Waikato
33 * Written: / /01
34 * Revised: 16/08/02 Improved
35 * 06/08/03 Bug fixes
36 * @author John Thompson, Greenstone Digital Libraries
37 * @version 2.3
38 **************************************************************************************/
39import java.io.*;
40import java.util.*;
41import org.greenstone.gatherer.Configuration;
42import org.greenstone.gatherer.cdm.CommandTokenizer;
43import org.greenstone.gatherer.util.ArrayTools;
44import org.greenstone.gatherer.util.StaticStrings;
45import org.greenstone.gatherer.util.Utility;
46import org.w3c.dom.*;
47/** This class contains a plethora of methods associated with handling the content of <strong>MetadataSet</strong>s and the <strong>Element</strong>s within. For example this is where you will find methods for comparing various parts of two <strong>MetadataSet</strong>s for equality. It also has methods for extracting common and useful data from <strong>Element</strong>s such as the AssignedValue nodes.
48 * @author John Thompson, Greenstone Digital Libraries
49 * @version 2.3
50 */
51public class MSMUtils {
52 /** Used to order metadata according to set standard element order then alphabetically by value. */
53 // static private MetadataComparator METADATA_COMPARATOR = new MetadataComparator();
54 /** An element of the enumeration of type filter. */
55 static public int NONE = 0;
56 /** An element of the enumeration of type filter. */
57 static public int VALUES = 1;
58 /** An element of the enumeration of type filter. */
59 static public int ALIASES = 2;
60 /** An element of the enumeration of type filter. */
61 static public int BOTH = 3;
62 /** The character used to separate name space from metadata element. */
63 static public char NS_SEP= '.';
64 /** The character used to separate subfields from metadata element. */
65 static public String SF_SEP= "#";
66 /** Method to add one node as a child of another, after migrating into the target document.
67 * @param parent The <strong>Node</strong> we are inserting into.
68 * @param child The original <strong>Node</strong> we are inserting. Must first be cloned into the parents document.
69 */
70 static public void add(Node parent, Node child) {
71 Document document = parent.getOwnerDocument();
72 Node new_child = document.importNode(child, true);
73 parent.appendChild(new_child);
74 }
75
76 /** Method to add an attribute element to the given element. This method makes use of the language_dependant attribute of the document to not only determine if the attribute is language dependant, but also to see whether a Language element should be created if doesn't already exist.
77 * @param element_element the Element to add the attribute element to
78 * @param attribute_name_str the name of the new attribute to add as a String
79 * @param language_code_str the two letter code String of the language this attribute is to be added as
80 * @param value_str the String to be assigned as the attribute elements value
81 * @see org.greenstone.gatherer.msm.MSMUtils#isAttributeLanguageDependant
82 * @see org.greenstone.gatherer.msm.MSMUtils#setValue(Element, String)
83 * @see org.greenstone.gatherer.util.StaticStrings#ATTRIBUTE_ELEMENT
84 * @see org.greenstone.gatherer.util.StaticStrings#CODE_ATTRIBUTE
85 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ATTRIBUTE
86 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ELEMENT
87 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
88 */
89 static public void addElementAttribute(Element element_element, String attribute_name_str, String language_code_str, String value_str) {
90 Document document = element_element.getOwnerDocument();
91 // Create the basic new attribute (everything except language attribute)
92 Element attribute_element = document.createElement(StaticStrings.ATTRIBUTE_ELEMENT);
93 attribute_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, attribute_name_str);
94 MSMUtils.setValue(attribute_element, value_str);
95 // Start off by determining if we have to add this node in the new multilingual optimized way
96 if(isAttributeLanguageDependant(document, attribute_name_str)) {
97 boolean found = false;
98 // Try to retrieve a language element for the given language code
99 NodeList language_elements = element_element.getElementsByTagName(StaticStrings.LANGUAGE_ELEMENT);
100 for(int i = 0; i < language_elements.getLength(); i++) {
101 Element language_element = (Element) language_elements.item(i);
102 if(language_element.getAttribute(StaticStrings.CODE_ATTRIBUTE).equals(language_code_str)) {
103 found = true;
104 // Add attribute
105 language_element.appendChild(attribute_element);
106 }
107 language_element = null;
108 }
109 language_elements = null;
110 // If it still hasn't been found, then add it
111 if(!found) {
112 Element language_element = document.createElement(StaticStrings.LANGUAGE_ELEMENT);
113 language_element.setAttribute(StaticStrings.CODE_ATTRIBUTE, language_code_str);
114 element_element.appendChild(language_element);
115 // Add attribute
116 language_element.appendChild(attribute_element);
117 language_element = null;
118 }
119 }
120 // Just add the attribute the old fashioned way
121 else {
122 attribute_element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, language_code_str);
123 element_element.appendChild(attribute_element);
124 }
125 // Clean up
126 attribute_element = null;
127 document = null;
128 }
129
130 /** A method for comparing two AssignedValues trees. This compares not only the Subject hierarchies but also the values themselves.
131 * @param avt A <strong>Node</strong> which is the root of an AssignedValues tree.
132 * @param bvt The <strong>Node</strong> which is the root of the tree we wish to compare it to.
133 * @return <i>true</i> if the two trees are equal, <i>false</i> otherwise.
134 */
135 static final private boolean assignedValuesEqual(Node avt, Node bvt) {
136 if(avt == null && bvt == null) {
137 return true; // Both are null so both are equal.
138 }
139 else if(avt == null || bvt == null) {
140 // One is null and the other isn't.
141 return false;
142 }
143 else {
144 Hashtable a_map = new Hashtable();
145 getValueMappings(avt, null, a_map);
146 Hashtable b_map = new Hashtable();
147 getValueMappings(bvt, null, b_map);
148 if(a_map.size() == b_map.size()) {
149 /** @TODO - Figure out what to do now. */
150 return true;
151 }
152 }
153 return false;
154 }
155 /** A method for comparing two attribute nodes of type Node not Attr as you might think. Attr objects are used to describe the attributes of tags themselves, while in a metadata set we intend attribute nodes to describe qualities of metadata elements. It's just confusing because the two systems (DOM model and Dublin Core) are quite similar.
156 * @param an A <strong>Node</strong> representing some attribute of an element.
157 * @param bn The <strong>Node</strong> we wish to compare it to.
158 * @return <i>true</i> if and only if the attributes are equal.
159 */
160 static final public boolean attributesEqual(Node an, Node bn) {
161 // Check we are comparing apples and apples...
162 if(an.getNodeName().equals("Attribute") && bn.getNodeName().equals("Attribute")) {
163 Element ae = (Element) an;
164 Element be = (Element) bn;
165 // Ensure we are comparing the same type of attribute.
166 if(ae.getAttribute("name").equals(be.getAttribute("name"))) {
167 // And finally retrieve and compare the values.
168 if(getValue(ae).equals(getValue(be))) {
169 // We have a match.
170 return true;
171 }
172 }
173 }
174 // And if anything goes wrong we can't be dealing with equal attributes.
175 return false;
176 }
177
178 /** Remove all of the child nodes from a certain node. */
179 static final public void clear(Node parent) {
180 while(parent.hasChildNodes()) {
181 parent.removeChild(parent.getFirstChild());
182 }
183 }
184
185 /** Method to compare two metadata elements (of type Element, which is bound to get more than a bit confusing) for equality. This test may only check the structural (ie pretty much unchanging) consistancy, or may include the AssignedValue tree as well (which will be different for each collection I'd imagine).
186 * @param a_set The <strong>MetadataSet</strong> a comes from.
187 * @param ae An <strong>Element</strong>.
188 * @param b_set The <strong>MetadataSet</strong> b comes from.
189 * @param be The <strong>Element</strong> to compare it to.
190 * @param values <i>true</i> if the AssignedValues tree should also be compared, <i>false</i> otherwise.
191 * @return <i>true</i> if the elements are equal, <i>false</i> otherwise.
192 */
193 static final public boolean elementsEqual(MetadataSet a_set, Element ae, MetadataSet b_set, Element be, boolean values) {
194 // Compare Element Attr(ibutes) within the DOM, not to be confused with comparing element attributes in a Dublin Core sense...
195 NamedNodeMap aas = ae.getAttributes();
196 NamedNodeMap bas = be.getAttributes();
197 // For each attribute in a...
198 for(int i = 0; i < aas.getLength(); i++) {
199 Attr aa = (Attr)aas.item(i);
200 // Try to retrieve an attribute of the same name from b.
201 Attr ba = (Attr)bas.getNamedItem(aa.getNodeName());
202 // Now if there was no such attribute, or if the values for the
203 // two attributes are different the structures different.
204 if(ba == null || (!aa.getValue().equals(ba.getValue()))) {
205 //ystem.err.println("Attributes are not equal");
206 return false;
207 }
208 }
209 // Quickest test of children is to see we have the same number in
210 // each. Remember to modify for missing AssignedValues which have
211 // nothing to do with structure.
212 int anc = getAttributeCount(ae);
213 int bnc = getAttributeCount(be);
214 if(anc != bnc) {
215 return false;
216 }
217 // Now we compare the child nodes of the two Elements taking into
218 // account three special cases...
219 // 1. We don't test the AssignedValues element here.
220 // 2. Remember OptionList node.
221 // 3. The Attributes of each metadata element.
222 // For each child node of a.
223 for(Node an = ae.getFirstChild(); an !=null; an =an.getNextSibling()) {
224 if(an.getNodeName().equals("OptionList")) {
225 //ystem.err.println("Matching OptionLists.");
226 Node bn = getNodeFromNamed(be, "OptionList");
227 if(bn == null || !optionListsEqual(an, bn)) {
228 //ystem.err.println("OptionLists are not equal");
229 return false;
230 }
231 }
232 // Matching attributes.
233 else if(an.getNodeName().equals("Attribute")) {
234 //ystem.err.println("Matching Attributes.");
235 boolean matched = false;
236 for(Node bn = be.getFirstChild(); bn != null && !matched;
237 bn = bn.getNextSibling()) {
238 if(bn.getNodeName().equals("Attribute")) {
239 matched = attributesEqual(an, bn);
240 }
241 }
242 if(!matched) {
243 //ystem.err.println("Cannot match attribute.");
244 return false;
245 }
246 }
247 }
248 // Finally, if we've been asked to compares value trees (for some unknown reason) go ahead and compare them too.
249 if(values) {
250 GValueModel avt = a_set.getValueTree(new ElementWrapper(ae));
251 GValueModel bvt = b_set.getValueTree(new ElementWrapper(be));
252 return assignedValuesEqual(avt.getDocument().getDocumentElement(), bvt.getDocument().getDocumentElement());
253 }
254 // If we've got this far the elements match!
255 return true;
256 }
257 /** This method extracts the assigned value trees details, if any, from a certain element and stores them in an array ready to be passed as arguments to the Dictionary.
258 * @param element The <strong>Element</strong> whose values we wish to view.
259 * @return A <strong>String[]</strong> containing the details of the assigned values tree.
260 * @see org.greenstone.gatherer.Dictionary
261 */
262 static final public String[] getAssignedValuesDetails(MetadataSet mds, Element element) {
263 String details[] = null;
264 //Node avt = getNodeFromNamed(element, "AssignedValues");
265 GValueModel avt = mds.getValueTree(new ElementWrapper(element));
266 if(avt != null) {
267 Hashtable mapping = new Hashtable();
268 getValueMappings(avt.getDocument().getDocumentElement(), null, mapping);
269 ArrayList values = new ArrayList(mapping.keySet());
270 Collections.sort(values);
271 details = new String[1];
272 for(int i = 0; i < values.size(); i++) {
273 if(details[0] == null) {
274 details[0] = " " + values.get(i);
275 }
276 else {
277 details[0] = details[0] + "\n " + values.get(i);
278 }
279 }
280 mapping = null;
281 values = null;
282 }
283 avt = null;
284 return details;
285 }
286
287 /** Retrieve all of the attributes for the given element as a tree set. Note that this requires significant manipulation if the source is a multilingual optimized metadata set.
288 * @param element the Element whose attributes we wish to catalog
289 * @return a TreeSet of the attributes sorted by their natural ordering
290 * @see org.greenstone.gatherer.msm.MSMUtils#getValue(Node)
291 * @see org.greenstone.gatherer.util.StaticStrings#ATTRIBUTE_ELEMENT
292 * @see org.greenstone.gatherer.util.StaticStrings#CODE_ATTRIBUTE
293 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ATTRIBUTE
294 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ELEMENT
295 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
296 */
297 static public TreeSet getAttributes(Element element) {
298 TreeSet attribute_tree = new TreeSet();
299 for(Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
300 if(node instanceof Element) {
301 Element some_element = (Element) node;
302 String some_element_name = some_element.getNodeName();
303 if(some_element_name.equals(StaticStrings.ATTRIBUTE_ELEMENT)) {
304 attribute_tree.add(new Attribute(some_element.getAttribute(StaticStrings.NAME_ATTRIBUTE), some_element.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE), MSMUtils.getValue(some_element)));
305 }
306 else if(some_element_name.equals(StaticStrings.LANGUAGE_ELEMENT)) {
307 String language_code = some_element.getAttribute(StaticStrings.CODE_ATTRIBUTE);
308 NodeList attribute_elements = some_element.getElementsByTagName(StaticStrings.ATTRIBUTE_ELEMENT);
309 for(int i = 0; i < attribute_elements.getLength(); i++) {
310 Element attribute_element = (Element) attribute_elements.item(i);
311 attribute_tree.add(new Attribute(attribute_element.getAttribute(StaticStrings.NAME_ATTRIBUTE), language_code, MSMUtils.getValue(attribute_element)));
312 attribute_element = null;
313 }
314 attribute_elements = null;
315 language_code = null;
316 }
317 some_element_name = null;
318 some_element = null;
319 }
320 }
321 return attribute_tree;
322 }
323
324 /** Method to count the number of Attribute nodes under a certain Element. This ignores other nodes such as #text, OptionList and AssignedValues nodes.
325 * @param element The <strong>Element</strong> whose attributes you want to count.
326 * @return An <i>int</i> which is the number of attribute nodes.
327 */
328 static final private int getAttributeCount(Node element) {
329 int count = 0;
330 for(Node n = element.getFirstChild(); n != null;
331 n = n.getNextSibling()) {
332 if(n.getNodeName().equals("Attribute")) {
333 count++;
334 }
335 }
336 return count;
337 }
338
339 /** This method is a slight variation on getNodeNamed in that it is especially written to retrieve the attribute Nodes of a certain name present under the given element.
340 * @param element The target element <strong>Node</strong>.
341 * @param name The name of the attribute you wish to return.
342 * @return An <strong>Element[]</strong> containing the attributes you requested, or <i>null</i> if no such attributes exists.
343 */
344 static final public Element[] getAttributeNodesNamed(Node element, String name) {
345 Element attributes[] = null;
346 for(Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
347 if(n.getNodeName().equals("Attribute")) {
348 Element e = (Element)n;
349 if(e.getAttribute("name").equals(name)) {
350 if(attributes == null) {
351 attributes = new Element[1];
352 attributes[0] = e;
353 }
354 else {
355 Element temp[] = attributes;
356 attributes = new Element[temp.length + 1];
357 System.arraycopy(temp, 0, attributes, 0, temp.length);
358 attributes[temp.length] = e;
359 temp = null;
360 }
361 }
362 e = null;
363 }
364 }
365 return attributes;
366 }
367
368 /** Method to construct an elements description by retrieving the correct attribute.
369 * @param element the Element whose name we wish to retrieve
370 * @return a String which is the elements description, or an empty string if no description exists
371 * @see org.greenstone.gatherer.msm.MSMUtils#getElementAttribute
372 * @see org.greenstone.gatherer.util.StaticStrings#COMMENT_VALUE
373 * @see org.greenstone.gatherer.util.StaticStrings#DEFINITION_VALUE
374 * @see org.greenstone.gatherer.util.StaticStrings#EMPTY_STR
375 * @see org.greenstone.gatherer.util.StaticStrings#SPACE_CHARACTER
376 */
377 static public String getDescription(Element element) {
378 String language_code_str = Configuration.getLanguage();
379 StringBuffer description = new StringBuffer(StaticStrings.EMPTY_STR);
380 description.append(getElementAttribute(element, StaticStrings.DEFINITION_VALUE, language_code_str));
381 if(description.length() > 0) {
382 description.append(StaticStrings.SPACE_CHARACTER);
383 }
384 description.append(getElementAttribute(element, StaticStrings.COMMENT_VALUE, language_code_str));
385 language_code_str = null;
386 return description.toString();
387 }
388
389 /** Retrieve the value for the requested attribute in the required language. Once again this method must be aware of the differences between the old metadata sets and the new multilingual optimized ones.
390 * @param element_element the Element whose attributes we are searching through
391 * @param attribute_name_str the name of the desired attribute as a String
392 * @param language_code_str the two letter code String indicating the desired language
393 * @see org.greenstone.gatherer.msm.MSMUtils#getValue
394 * @see org.greenstone.gatherer.msm.MSMUtils#isAttributeLanguageDependant
395 * @see org.greenstone.gatherer.util.StaticStrings#ATTRIBUTE_ELEMENT
396 * @see org.greenstone.gatherer.util.StaticStrings#CODE_ATTRIBUTE
397 * @see org.greenstone.gatherer.util.StaticStrings#EMPTY_STR
398 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ATTRIBUTE
399 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ELEMENT
400 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
401 */
402 static private String getElementAttribute(Element element_element, String attribute_name_str, String language_code_str) {
403 boolean found = false;
404 String result = StaticStrings.EMPTY_STR;
405 Document document = element_element.getOwnerDocument();
406 // Determine if the attribute is language specific
407 if(isAttributeLanguageDependant(document, attribute_name_str)) {
408 NodeList language_elements = element_element.getElementsByTagName(StaticStrings.LANGUAGE_ELEMENT);
409 for(int i = 0; !found && i < language_elements.getLength(); i++) {
410 Element language_element = (Element) language_elements.item(i);
411 if(language_element.getAttribute(StaticStrings.CODE_ATTRIBUTE).equals(language_code_str)) {
412 NodeList attribute_elements = language_element.getElementsByTagName(StaticStrings.ATTRIBUTE_ELEMENT);
413 for(int j = 0; !found && j < attribute_elements.getLength(); j++) {
414 Element attribute_element = (Element) attribute_elements.item(j);
415 if(attribute_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equals(attribute_name_str)) {
416 found = true;
417 result = MSMUtils.getValue(attribute_element);
418 }
419 attribute_element = null;
420 }
421 attribute_elements = null;
422 }
423 language_element = null;
424 }
425 language_elements = null;
426 }
427 else {
428 boolean first_match = false;
429 NodeList attribute_elements = element_element.getElementsByTagName(StaticStrings.ATTRIBUTE_ELEMENT);
430 for(int k = 0; !found && k < attribute_elements.getLength(); k++) {
431 Element attribute_element = (Element) attribute_elements.item(k);
432 // We don't want to consider those attributes found inside language elements
433 if(attribute_element.getParentNode() == element_element) {
434 ///ystem.err.println("First level");
435 String target_name_str = attribute_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
436 String target_language_str = attribute_element.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
437 ///ystem.err.println("Does " + target_name_str + " equal " + attribute_name_str + "?");
438 if(attribute_name_str.equals(target_name_str)) {
439 ///ystem.err.println("Does " + target_language_str + " equal " + language_code_str + "?");
440 if(language_code_str.equals(target_language_str)) {
441 ///ystem.err.println("Perfect match!");
442 found = true;
443 result = MSMUtils.getValue(attribute_element);
444 }
445 else if((result == StaticStrings.EMPTY_STR || first_match) && isLegacyMDS(document)) {
446 ///ystem.err.println("Legacy MDS");
447 // Special case for old style documents, where the english match is good enough
448 if(target_language_str.equals(StaticStrings.ENGLISH_LANGUAGE_STR)) {
449 ///ystem.err.println("English plate.");
450 result = MSMUtils.getValue(attribute_element);
451 }
452 // Super special case where the first match is better than nothing
453 else if(result == StaticStrings.EMPTY_STR && !first_match) {
454 ///ystem.err.println("First match.");
455 first_match = true;
456 result = MSMUtils.getValue(attribute_element);
457 }
458 }
459 }
460 target_language_str = null;
461 target_name_str = null;
462 }
463 //else {
464 ///ystem.err.println("Second level");
465 //}
466 attribute_element = null;
467 }
468 attribute_elements = null;
469
470 }
471 document = null;
472 return result;
473 }
474
475 /*************************************************************************/
476 /** Method to construct an elements fully qualified name. Note that this is different from a nodes identifier. Think of name as a short, unique reference to a metadata element, whereas identifier can be much longer, language specific and non-unique.
477 * @param element An <strong>Element</strong> whose name we are interested in.
478 * @return A <strong>String</strong> representing this given elements fully namespace qualified name.
479 */
480 static final public String getFullName(Element element) {
481 return getFullName(element, "");
482
483 }
484 /*************************************************************************/
485 /** Method to construct an elements fully qualified name. Note that this is different from a nodes identifier. Think of name as a short, unique reference to a metadata element, whereas identifier can be much longer, language specific and non-unique.
486 * @param element An <strong>Element</strong> whose name we are interested in.
487 * @return A <strong>String</strong> representing this given elements fully namespace qualified name.
488 */
489 static final public String getFullName(Element element, String namespace) {
490 StringBuffer name_buffer = new StringBuffer();
491 if(element == null) {
492 return "Error";
493 }
494 // First get the root node.
495 Document document = element.getOwnerDocument();
496 Element root = document.getDocumentElement();
497 document = null;
498 // Retrieve this elements name
499 name_buffer.append(element.getAttribute("name"));
500 // Now we check if element has a parent node, other than root. If so we begin building up the full name
501 Element parent_element = (Element) element.getParentNode();
502 while(parent_element != null && parent_element != root) {
503 name_buffer.insert(0, SF_SEP);
504 name_buffer.insert(0, parent_element.getAttribute("name"));
505 parent_element = (Element)parent_element.getParentNode();
506 }
507 parent_element = null;
508 // Finally insert the namespace and we are all done.
509 if(root != null) {
510 namespace = root.getAttribute("namespace");
511 }
512 root = null;
513 // If no root, or no namespace found, assume its extracted (at least then they can't edit it)
514 if(namespace == null || namespace.equals("")) {
515 namespace = Utility.EXTRACTED_METADATA_NAMESPACE;
516 }
517 name_buffer.insert(0, NS_SEP);
518 name_buffer.insert(0, namespace);
519 namespace = null;
520 return name_buffer.toString();
521 } // static public String getFullName(Element element)
522
523 /** Method to construct an elements name (sic identifier) by retrieving the correct attribute, language specific.
524 * @param element the Element whose name we wish to retrieve
525 * @return a String which is the elements identifier, or an empty string if no identifier exists
526 * @see org.greenstone.gatherer.msm.MSMUtils#getElementAttribute
527 * @see org.greenstone.gatherer.util.StaticStrings#IDENTIFIER_VALUE
528 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
529 */
530 static final public String getIdentifier(Element element) {
531 String identifier = getElementAttribute(element, StaticStrings.IDENTIFIER_VALUE, Configuration.getLanguage());
532 // Failing the above we return the nodes name instead.
533 if(identifier == null || identifier.length() == 0) {
534 identifier = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
535 }
536 return identifier;
537 }
538
539 /** Method to retrieve from the node given, a certain child node with the specified name.
540 * @param parent The <strong>Node</strong> whose children should be searched.
541 * @param name The required nodes name as a <strong>String</strong>.
542 * @return The requested <strong>Node</strong> if it is found, <i>null</i> otherwise.
543 */
544 static final public Node getNodeFromNamed(Node parent, String name) {
545 Node child = null;
546 for(Node i = parent.getFirstChild(); i != null && child == null;
547 i = i.getNextSibling()) {
548 if(i.getNodeName().equals(name)) {
549 child = i;
550 }
551 }
552 return child;
553 }
554 /** Look for the occurances 'field' of the element and return it if found.
555 * @return An <i>int</i> which matches the number in the occurances attribute of the element, or 0 if no such attribute.
556 */
557 static final public int getOccurances(Element element) {
558 int count = 0;
559 String number = null;
560 if((number = element.getAttribute("occurances")) != null) {
561 try {
562 count = Integer.parseInt(number);
563 }
564 catch(Exception error) {
565 count = 0;
566 }
567 }
568 return count;
569 }
570 /** This method extracts the option list details, if any, from a certain element and stores them in an array ready to be passed as arguments to the <strong>Dictionary</strong>.
571 * @param element The <strong>Element</strong> whose option list we wish to view.
572 * @return A <strong>String[]</strong> containing the details of the option list.
573 * TODO implement.
574 * @see org.greenstone.gatherer.Dictionary
575 */
576 static final public String[] getOptionListDetails(Element element) {
577 return null;
578 }
579
580
581 /** This method extracts the structural details from a certain element and stores them in an array, all ready for passing to the <strong>Dictionary</strong>.
582 * @param element The <Strong>Element</strong> whose details we wish to gather.
583 * @return A <strong>String[]</strong> containing the structural details.
584 */
585 static final public String[] getStructuralDetails(MetadataSet mds, Element element) {
586 String details[] = new String[4];
587 //Element root = (Element)element.getParentNode();
588 //details[0] = root.getAttribute("name");
589 //details[1] = root.getAttribute("namespace");
590 details[0] = mds.getName();
591 details[1] = mds.getNamespace();
592 details[2] = getFullName(element);
593 details[3] = null;
594 // Get attributes
595 Vector attributes = new Vector();
596 for(Node n=element.getFirstChild(); n!=null; n=n.getNextSibling()) {
597 if(n.getNodeName().equals("Attribute")) {
598 Element temp = (Element)n;
599 attributes.add(temp.getAttribute("name") + "=" + getValue(n));
600 }
601 }
602 // Sort attributes
603 Collections.sort(attributes);
604 // Add attributes to details.
605 for(int i = 0; i < attributes.size(); i++) {
606 if(details[3] == null) {
607 details[3] = " " + attributes.get(i);
608 }
609 else {
610 details[3] = details[3] + "\n " + attributes.get(i);
611 }
612 }
613 return details;
614 }
615
616 /** Method to retrieve the value of a given node (not the assigned values tree!).
617 * @param element The <strong>Element</strong> whose value we wish to find.
618 * @return The value found as a <strong>String</strong>, or <i>null</i> if this element has no value.
619 */
620 static final public String getValue(Node element) {
621 // If we've been given a subject node first retrieve its value node.
622 if(element.getNodeName().equals("Subject")) {
623 element = getNodeFromNamed(element, "Value");
624 }
625 // If we've got a value node, then reconstruct the text. Remember that DOM will split text over 256 characters into several text nodes
626 if(element != null && element.hasChildNodes()) {
627 StringBuffer text_buffer = new StringBuffer();
628 NodeList text_nodes = element.getChildNodes();
629 for(int i = 0; i < text_nodes.getLength(); i++) {
630 Node possible_text = text_nodes.item(i);
631 if(possible_text.getNodeName().equals(StaticStrings.TEXT_NODE)) {
632 text_buffer.append(possible_text.getNodeValue());
633 }
634 }
635 return text_buffer.toString();
636 }
637 return "";
638 }
639
640 /** Method to traverse the given value tree, and build up a hashtable of mappings between the value path key names and the Subject nodes of the tree.
641 * @param current The root <strong>Node</strong> of a subtree of the AssignedValues tree.
642 * @param prefix The value path key <strong>String</string>, which shows the path from the root of the AssignedValue tree to <i>current</i>s parent using '\' as a separator.
643 * @param values A <strong>Hashtable</strong> containing the mapping discovered so far in our tree traversal.
644 */
645 static final private void getValueMappings(Node current, String prefix, Hashtable values) {
646 if(current != null) {
647 String name = current.getNodeName();
648 String new_prefix = prefix;
649 // If we've found the outer layer of a new value, add it to our mapping
650 if(name.equals("Subject")) {
651 Node value_node = getNodeFromNamed(current, "Value");
652 String value = getValue(value_node);
653 if(new_prefix != null) {
654 new_prefix = new_prefix + "\\" + value;
655 }
656 else {
657 new_prefix = value;
658 }
659 values.put(new_prefix, current);
660 }
661 if(name.equals("Subject") || name.equals("AssignedValues")) {
662 for(Node child = current.getFirstChild(); child != null;
663 child = child.getNextSibling()) {
664 getValueMappings(child, new_prefix, values);
665 }
666 }
667 }
668 }
669 /** Parses the value tree template file.
670 * @return The Document parsed.
671 */
672 static final public Document getValueTreeTemplate() {
673 return Utility.parse(Utility.METADATA_VALUE_TEMPLATE, true);
674 }
675
676 /** Determine if the named attribute is language specific for this collection. This information is found in a DOM attribute of the document element, as a comma separated list of attribute names.
677 * @param document the Document for which we wish to check the language requirements
678 * @param attribute_name_str the name of the attribute we a testing as a String
679 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGEDEPENDANT_ATTRIBUTE
680 */
681 static private boolean isAttributeLanguageDependant(Document document, String attribute_name_str) {
682 String language_specific_attributes = document.getDocumentElement().getAttribute(StaticStrings.LANGUAGEDEPENDANT_ATTRIBUTE).toLowerCase();
683 return language_specific_attributes.indexOf(attribute_name_str) != -1;
684 }
685
686 /** Determine if the given document is a legacy MDS or a new multilingual one. The easiest way to tell is whether there is a language_dependant attribute in the document element.
687 * @param document the Document to test
688 * @return true if this is an old mds, false otherwise
689 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGEDEPENDANT_ATTRIBUTE
690 * @see org.greenstone.gatherer.util.StaticStrings#EMPTY_STR
691 */
692 static private boolean isLegacyMDS(Document document) {
693 ///ystem.err.println("isLegacyMDS(): l_d = " + document.getDocumentElement().getAttribute(StaticStrings.LANGUAGEDEPENDANT_ATTRIBUTE));
694 return (document.getDocumentElement().getAttribute(StaticStrings.LANGUAGEDEPENDANT_ATTRIBUTE)).equals(StaticStrings.EMPTY_STR);
695 }
696
697 /** Method to compare two OptionsLists for equality.
698 * @param al A <strong>Node</strong> which represents an OptionList.
699 * @param bl The <strong>Node</strong> we wish to test against.
700 * @return A <i>boolean</i> which is <i>true</i> if the two option lists are equal, <i>false</i> otherwise.
701 * TODO Implementation
702 */
703 static final private boolean optionListsEqual(Node al, Node bl) {
704 // Compare the 'restricted' attribute of the two lists.
705 Element ae = (Element) al;
706 Element be = (Element) bl;
707 if(!ae.getAttribute("restricted").equals
708 (be.getAttribute("restricted"))) {
709 return false;
710 }
711 // Compare the Values under each list.
712 for(Node an = al.getFirstChild(); an != null;
713 an = an.getNextSibling()){
714 if(an.getNodeName().equals("Value")) {
715 boolean matched = false;
716 for(Node bn = bl.getFirstChild(); bn != null && !matched;
717 bn = bn.getNextSibling()) {
718 if(bn.getNodeName().equals("Value")) {
719 matched = valuesEqual(an, bn);
720 }
721 }
722 if(!matched) {
723 return false;
724 }
725 }
726 }
727 return true;
728 }
729
730 /** A method to remove a specific attribute element from an element. This attribute must match in name, language and in value before being removed. Note that this method supports both legacy and multilingual optimized versions of the mds.
731 * @param element_element the Element which represent the metadata element we are altering
732 * @param attribute_name_str the name of the attribute to remove as a String
733 * @param language_code_str the language code we must match as a String
734 * @param value_str the value String which also must match before we remove anything
735 * @return true if the desired attribute was successfully found and removed, false otherwise
736 * @see org.greenstone.gatherer.msm.MSMUtils#isAttributeLanguageDependant
737 * @see org.greenstone.gatherer.msm.MSMUtils#getValue(Node)
738 * @see org.greenstone.gatherer.util.StaticStrings#ATTRIBUTE_ELEMENT
739 * @see org.greenstone.gatherer.util.StaticStrings#CODE_ATTRIBUTE
740 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ATTRIBUTE
741 * @see org.greenstone.gatherer.util.StaticStrings#LANGUAGE_ELEMENT
742 * @see org.greenstone.gatherer.util.StaticStrings#NAME_ATTRIBUTE
743 */
744 static public boolean removeElementAttribute(Element element_element, String attribute_name_str, String language_code_str, String value_str) {
745 // Multilingual Optimized version
746 // 1. Determine the if this is one of the language specific attributes
747 if(isAttributeLanguageDependant(element_element.getOwnerDocument(), attribute_name_str)) {
748 // Retrieve the language elements, and determine the correct one
749 NodeList language_elements = element_element.getElementsByTagName(StaticStrings.LANGUAGE_ELEMENT);
750 for(int i = 0; i < language_elements.getLength(); i++) {
751 Element language_element = (Element) language_elements.item(i);
752 if(language_element.getAttribute(StaticStrings.CODE_ATTRIBUTE).equalsIgnoreCase(language_code_str)) {
753 NodeList attribute_elements = language_element.getElementsByTagName(StaticStrings.ATTRIBUTE_ELEMENT);
754 for(int j = 0; j < attribute_elements.getLength(); j++) {
755 Element attribute_element = (Element) attribute_elements.item(j);
756 String target_name_str = attribute_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
757 String target_value_str = MSMUtils.getValue(attribute_element);
758 if(attribute_name_str.equals(target_name_str) && value_str.equals(target_value_str)) {
759 language_element.removeChild(attribute_element);
760 if(attribute_elements.getLength() == 0) {
761 element_element.removeChild(language_element);
762 }
763 target_value_str = null;
764 target_name_str = null;
765 attribute_element = null;
766 attribute_elements = null;
767 language_element = null;
768 language_elements = null;
769 return true;
770 }
771 target_value_str = null;
772 target_name_str = null;
773 attribute_element = null;
774 }
775 attribute_elements = null;
776 }
777 language_element = null;
778 }
779 language_elements = null;
780 // Not found
781 return false;
782 }
783 // Otherwise just use the old method
784
785 // Find the attribute to remove
786 NodeList attribute_elements = element_element.getElementsByTagName(StaticStrings.ATTRIBUTE_ELEMENT);
787 for (int k = 0; k < attribute_elements.getLength(); k++) {
788 Element attribute_element = (Element) attribute_elements.item(k);
789 // Remember to ignore any attributes that live within nested language elements
790 if (attribute_element.getParentNode() == element_element && attribute_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equals(attribute_name_str) && attribute_element.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE).equalsIgnoreCase(language_code_str) && MSMUtils.getValue(attribute_element).equals(value_str)) {
791 // Match found, so remove the attribute node and return
792 element_element.removeChild(attribute_element);
793 attribute_element = null;
794 attribute_elements = null;
795 return true;
796 }
797 attribute_element = null;
798 }
799 attribute_elements = null;
800 // No match found
801 return false;
802 }
803
804 /** can only be used to set the english value */
805 static final public void setIdentifier(Node element, String value) {
806 // Get the 'identifier' Element
807 for(Node node = element.getFirstChild(); node != null;
808 node = node.getNextSibling()) {
809 if(node.getNodeName().equals("Attribute")) {
810 Element target = (Element)node;
811 if(target.getAttribute("name").equals("identifier") && target.getAttribute("language").equals("en")) {
812 Node text = target.getFirstChild();
813 text.setNodeValue(value);
814 break;
815 }
816 }
817 }
818 }
819 /** Set the value of the element attribute occurances.
820 * @param element The <strong>Element</strong> to change.
821 * @param value The value to change by as an <i>int</i>.
822 */
823 static final public void setOccurance(Element element, int value) {
824 Integer new_value = new Integer(getOccurances(element) + value);
825 element.setAttribute("occurances", new_value.toString());
826 }
827
828 /** Set the #text node value of some element.
829 * @param element the Element whose value we wish to set
830 * @param value the new value for the element as a String
831 */
832 static final public void setValue(Element element, String value) {
833 // Remove any existing child node(s)
834 clear(element);
835 // Add new text node.
836 if (value != null) {
837 element.appendChild(element.getOwnerDocument().createTextNode(value));
838 }
839 }
840
841 /** This method also traverses the tree, but this one is used to gather all the values and aliases at once, and to 'prune' the tree if necessary.
842 * @param current The current root <strong>Node</strong> of this AssignedValues tree subtree.
843 * @param return_filter This <i>int</i> specifies what nodes from the tree should be returned, where;<br>VALUES = return values only<br>ALIASES = return aliases only<br>BOTH = return both values and aliases<br>NONE = return nothing.
844 * @param remove_leaves A leaf node is a subject that contains no child subjects. If this <i>boolean</i> is set to <i>true</i> then the leaf nodes will be removed from the tree.
845 * @return A <strong>Node[]</strong> containing whatever values or aliases have been found during the trees traversal.
846 */
847 static final public Node[] traverseTree(Node current, int return_filter, boolean remove_leaves) {
848 Node leaves[] = null;
849 String name = current.getNodeName();
850 if(name.equals("Value") && (return_filter == VALUES || return_filter == BOTH)) {
851 leaves = ArrayTools.add(leaves, current);
852 }
853 else if(name.equals("Alias") && (return_filter == ALIASES || return_filter == BOTH)) {
854 leaves = ArrayTools.add(leaves, current);
855 }
856 else if(name.equals("Subject")) {
857 boolean has_subject_child = false;
858 Node children[] = ArrayTools.nodeListToNodeArray(current.getChildNodes());
859 for(int i = 0; i < children.length; i++) {
860 if(children[i].getNodeName().equals("Subject")) {
861 has_subject_child = true;
862 }
863 leaves = ArrayTools.add(leaves, traverseTree(children[i], return_filter, remove_leaves));
864 }
865 if(!has_subject_child && remove_leaves) {
866 Node parent = current.getParentNode();
867 parent.removeChild(current);
868 }
869 }
870 else if(name.equals("AssignedValues")) {
871 Node children[] = ArrayTools.nodeListToNodeArray(current.getChildNodes());
872 for(int i = 0; i < children.length; i++) {
873 leaves = ArrayTools.add(leaves, traverseTree(children[i], return_filter, remove_leaves));
874 }
875 }
876 return leaves;
877 }
878
879 /** This method is used to systematically merge two AssignedValues tree. Both trees have their current values mapped, then the new tree is searched for key paths that don't exist in the current tree. If such a key is found, the Subject <strong>Node</strong> it maps to is retrieved and then imported and added to whatever was the closest available node (in terms of tree path) in the current tree.
880 * @param a_set The MetadataSet from which the Element a came from.
881 * @param a The Element at the root of the current AssignedValues tree.
882 * @param b_set The MetadataSet from which the Element b came from.
883 * @param b The root Element of the tree that is being merged.
884 * @return A <i>boolean</i> which is <i>true</i> if the trees merged without error, <i>false</i> otherwise.
885 */
886 static final public boolean updateValueTree(MetadataSet a_set, Element a, MetadataSet b_set, Element b) {
887 GValueModel avt = a_set.getValueTree(new ElementWrapper(a));
888 GValueModel bvt = b_set.getValueTree(new ElementWrapper(b));
889 // If neither element even has a value tree, we're all done.
890 if(avt == null && bvt == null) {
891 avt = null;
892 bvt = null;
893 return true;
894 }
895 // If the new element has no value tree then nothing needs to be done.
896 else if(avt != null && bvt == null) {
897 avt = null;
898 bvt = null;
899 return true;
900 }
901 // If only the new element has a value tree, then add all of its values
902 // immediately.
903 else if(avt == null && bvt != null) {
904 a_set.addValueTree(new ElementWrapper(a), bvt);
905 avt = null;
906 bvt = null;
907 return true;
908 }
909 // We have both trees for both elements, time to merge.
910 else {
911 Document document = avt.getDocument();
912 Hashtable a_map = new Hashtable();
913 getValueMappings(document.getDocumentElement(), null, a_map);
914 Hashtable b_map = new Hashtable();
915 getValueMappings(bvt.getDocument().getDocumentElement(), null, b_map);
916 // For each new entry in b_map
917 for(Enumeration b_keys = b_map.keys(); b_keys.hasMoreElements(); ) {
918 String b_key = (String)b_keys.nextElement();
919 // Test if there is already an entry in a_map.
920 if(!a_map.containsKey(b_key)) {
921 // If not, search through a_map for the longest match.
922 Node target = document.getDocumentElement();
923 String last_match = null;
924 for(Enumeration a_keys = a_map.keys();
925 a_keys.hasMoreElements(); ) {
926 String a_key = (String)a_keys.nextElement();
927 if(b_key.startsWith(a_key)) {
928 if(last_match == null || a_key.length() > last_match.length()) {
929 last_match = a_key;
930 target = (Node)a_map.get(a_key);
931 }
932 }
933 a_key = null;
934 }
935 // Now import the node at b_key and add it to target.
936 Node subtree = (Node)b_map.get(b_key);
937 subtree = document.importNode(subtree, true);
938 // Find the node to insert before...
939 String name = getValue(subtree);
940 Node move = null;
941 for(Node n = target.getFirstChild(); n != null && move == null; n = n.getNextSibling()) {
942 if(n.getNodeName().equals("Subject")) {
943 if(name.compareTo(getValue(n)) <= 0) {
944 move = n;
945 }
946 }
947 }
948 if(move == null) {
949 target.appendChild(subtree);
950 }
951 else {
952 target.insertBefore(subtree, move);
953 }
954 target = null;
955 last_match = null;
956 subtree = null;
957 name = null;
958 move = null;
959 }
960 b_key = null;
961 }
962 document = null;
963 a_map = null;
964 b_map = null;
965 avt = null;
966 bvt = null;
967 return true;
968 }
969 }
970
971 /** Method to determine if two Value nodes are equal.
972 * @param av A <strong>Node</strong> representing a value.
973 * @param bv The <strong>Node</strong> we want to compare it to.
974 * @return A <i>boolean</i> which is <i>true</i> if the two value nodes are equal, <i>false</i> otherwise.
975 */
976 static final private boolean valuesEqual(Node av, Node bv) {
977 // Check we are comparing apples and apples...
978 if(av.getNodeName().equals("Value") &&
979 bv.getNodeName().equals("Value")) {
980 // Retrieve and then compare their text values.
981 Node at = av.getFirstChild();
982 Node bt = bv.getFirstChild();
983 if(at.getNodeValue().equals(bt.getNodeValue())) {
984 return true;
985 }
986 }
987 return false;
988 }
989
990 /** A comparator for sorting metadata element-value pairs into their standard order (elements) then alphabetical order (values). */
991// static final private class MetadataComparator
992// implements Comparator {
993// /** Compares its two arguments for order. */
994// public int compare(Object o1, Object o2) {
995// int result = 0;
996// ElementWrapper e1 = null;
997// ElementWrapper e2 = null;
998// String v1 = null;
999// String v2 = null;
1000// if(o1 instanceof Metadata && o2 instanceof Metadata) {
1001// Metadata m1 = (Metadata) o1;
1002// Metadata m2 = (Metadata) o2;
1003// ///ystem.err.println("MSMUtils.compare(" + m1 + ", " + m2 + ") = ");
1004// e1 = m1.getElement();
1005// e2 = m2.getElement();
1006// v1 = m1.getValue().toLowerCase();
1007// v2 = m2.getValue().toLowerCase();
1008// }
1009// else if(o1 instanceof ElementWrapper && o2 instanceof ElementWrapper) {
1010// e1 = (ElementWrapper) o1;
1011// e2 = (ElementWrapper) o2;
1012// }
1013// if(e1 != null && e2 != null) {
1014// // First we compare the namespaces
1015// result = e1.getNamespace().compareTo(e2.getNamespace());
1016// if(result == 0 && Gatherer.c_man != null && Gatherer.c_man.ready() && e1.getNamespace() != null) {
1017// // Now, given both elements are in the same set, we compare the element ordering using methods in MetadataSet
1018// MetadataSet set = Gatherer.c_man.getCollection().msm.getSet(e1.getNamespace());
1019// ///ystem.err.print("MetadataSet.compare(" + e1 + ", " + e2 + ") = ");
1020// if(set != null) {
1021// result = set.compare(e1.getElement(), e2.getElement());
1022// ///ystem.err.println(result);
1023// if(result == 0 && v1 != null && v2 != null) {
1024// // Finally we compare the values alphabetically.
1025// result = v1.compareTo(v2);
1026// }
1027// }
1028// else {
1029// return 0;
1030// }
1031// }
1032// }
1033// else {
1034// result = o1.toString().compareTo(o2.toString());
1035// }
1036// ///ystem.err.println("Result: " + result);
1037// return result;
1038// }
1039
1040// /** Indicates whether some other object is "equal to" this Comparator. */
1041// public boolean equals(Object obj) {
1042// return compare(this, obj) == 0;
1043// }
1044// }
1045}
Note: See TracBrowser for help on using the repository browser.