source: gli/trunk/src/org/greenstone/gatherer/metadata/MetadataTools.java@ 14567

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

Major changes to the metadata value table and tree in the Enrich pane. The metadata value table now allows direct editing in the table -- hopefully much more obvious to the user. Classes involved have been untangled and are much more re-usable. Special code for replacing metadata has been added, which uses a more direct and efficient method rather than removing then adding metadata as before. And many many minor tidy-ups.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/**
2 *############################################################################
3 * A component of the Greenstone Librarian Interface, part of the Greenstone
4 * digital library suite from the New Zealand Digital Library Project at the
5 * University of Waikato, New Zealand.
6 *
7 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
8 *
9 * Copyright (C) 2004 New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *############################################################################
25 */
26
27package org.greenstone.gatherer.metadata;
28
29
30/** This class is a static class containing useful metadata functions */
31public class MetadataTools
32{
33 static final public String NAMESPACE_SEPARATOR = ".";
34 static final public String SUBELEMENT_SEPARATOR = "^";
35
36
37 static public String getDisplayNameForMetadataElementWithName(String metadata_element_name_full)
38 {
39 MetadataElement metadata_element = getMetadataElementWithName(metadata_element_name_full);
40 if (metadata_element != null) {
41 return metadata_element.getDisplayName();
42 }
43
44 return metadata_element_name_full;
45 }
46
47
48 static public String getMetadataElementAttribute(MetadataElement metadata_element, String attribute_name, String language_code, String fallback_language_code)
49 {
50 String metadata_element_attribute = metadata_element.getAttribute(attribute_name, language_code);
51
52 // If the attribute isn't defined in the desired language, resort to the fallback
53 if (metadata_element_attribute == null && !language_code.equals(fallback_language_code)) {
54 metadata_element_attribute = metadata_element.getAttribute(attribute_name, fallback_language_code);
55 }
56
57 return metadata_element_attribute;
58 }
59
60
61 static public String getMetadataElementName(String metadata_element_name_full)
62 {
63 // Full element name contains namespace
64 if (metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) != -1) {
65 return metadata_element_name_full.substring(metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) + 1);
66 }
67
68 // No namespace
69 return metadata_element_name_full;
70 }
71
72
73 static public MetadataElement getMetadataElementWithDisplayName(String metadata_element_display_name_full)
74 {
75 String metadata_set_namespace = getMetadataSetNamespace(metadata_element_display_name_full);
76 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
77 if (metadata_set == null) {
78 return null;
79 }
80
81 return metadata_set.getMetadataElementWithDisplayName(metadata_element_display_name_full);
82 }
83
84
85 static public MetadataElement getMetadataElementWithName(String metadata_element_name_full)
86 {
87 String metadata_set_namespace = getMetadataSetNamespace(metadata_element_name_full);
88 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
89 if (metadata_set == null) {
90 return null;
91 }
92
93 String metadata_element_name = getMetadataElementName(metadata_element_name_full);
94 return metadata_set.getMetadataElementWithName(metadata_element_name);
95 }
96
97
98 static public String getMetadataSetAttribute(MetadataSet metadata_set, String attribute_name, String language_code, String fallback_language_code)
99 {
100 String metadata_set_attribute = metadata_set.getAttribute(attribute_name, language_code);
101
102 // If the attribute isn't defined in the desired language, resort to the fallback
103 if (metadata_set_attribute == null && !language_code.equals(fallback_language_code)) {
104 metadata_set_attribute = metadata_set.getAttribute(attribute_name, fallback_language_code);
105 }
106
107 return metadata_set_attribute;
108 }
109
110
111 static public String getMetadataSetNamespace(String metadata_element_name_full)
112 {
113 // Full element name contains namespace
114 if (metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) != -1) {
115 return metadata_element_name_full.substring(0, metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR));
116 }
117
118 // No namespace
119 return "";
120 }
121
122
123 static public String getRegularExpressionThatMatchesFilePath(String file_path)
124 {
125 // Convert the file path into a regular expression that will match it
126 String file_path_regexp = file_path;
127 file_path_regexp = file_path_regexp.replaceAll("\\.", "\\\\.");
128 file_path_regexp = file_path_regexp.replaceAll("\\(", "\\\\(");
129 file_path_regexp = file_path_regexp.replaceAll("\\)", "\\\\)");
130 file_path_regexp = file_path_regexp.replaceAll("\\[", "\\\\[");
131 file_path_regexp = file_path_regexp.replaceAll("\\]", "\\\\]");
132 file_path_regexp = file_path_regexp.replaceAll("\\{", "\\\\{");
133 file_path_regexp = file_path_regexp.replaceAll("\\}", "\\\\}");
134 file_path_regexp = file_path_regexp.replaceAll("\\+", "\\\\+");
135 return file_path_regexp;
136 }
137}
Note: See TracBrowser for help on using the repository browser.