source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/metadata/MetadataTools.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 6.7 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
29import java.util.StringTokenizer;
30
31/** This class is a static class containing useful metadata functions */
32public class MetadataTools
33{
34 static final public String NAMESPACE_SEPARATOR = ".";
35 static final public String SUBELEMENT_SEPARATOR = "^";
36
37
38 static public String getDisplayNameForMetadataElementWithName(String metadata_element_name_full)
39 {
40 MetadataElement metadata_element = getMetadataElementWithName(metadata_element_name_full);
41 if (metadata_element != null) {
42 return metadata_element.getDisplayName();
43 }
44
45 return metadata_element_name_full;
46 }
47
48
49 static public String getMetadataElementAttribute(MetadataElement metadata_element, String attribute_name, String language_code, String fallback_language_code)
50 {
51 String metadata_element_attribute = metadata_element.getAttribute(attribute_name, language_code);
52
53 // If the attribute isn't defined in the desired language, resort to the fallback
54 if (metadata_element_attribute == null && !language_code.equals(fallback_language_code)) {
55 metadata_element_attribute = metadata_element.getAttribute(attribute_name, fallback_language_code);
56 }
57
58 return metadata_element_attribute;
59 }
60
61
62 static public String getMetadataElementName(String metadata_element_name_full)
63 {
64 // Full element name contains namespace
65 if (metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) != -1) {
66 return metadata_element_name_full.substring(metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) + 1);
67 }
68
69 // No namespace
70 return metadata_element_name_full;
71 }
72
73
74 static public MetadataElement getMetadataElementWithDisplayName(String metadata_element_display_name_full)
75 {
76 String metadata_set_namespace = getMetadataSetNamespace(metadata_element_display_name_full);
77 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
78 if (metadata_set == null) {
79 return null;
80 }
81
82 return metadata_set.getMetadataElementWithDisplayName(metadata_element_display_name_full);
83 }
84
85
86 static public MetadataElement getMetadataElementWithName(String metadata_element_name_full)
87 {
88 String metadata_set_namespace = getMetadataSetNamespace(metadata_element_name_full);
89 MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
90 if (metadata_set == null) {
91 return null;
92 }
93
94 String metadata_element_name = getMetadataElementName(metadata_element_name_full);
95 return metadata_set.getMetadataElementWithName(metadata_element_name);
96 }
97
98
99 static public String getMetadataSetAttribute(MetadataSet metadata_set, String attribute_name, String language_code, String fallback_language_code)
100 {
101 String metadata_set_attribute = metadata_set.getAttribute(attribute_name, language_code);
102
103 // If the attribute isn't defined in the desired language, resort to the fallback
104 if (metadata_set_attribute == null && !language_code.equals(fallback_language_code)) {
105 metadata_set_attribute = metadata_set.getAttribute(attribute_name, fallback_language_code);
106 }
107
108 return metadata_set_attribute;
109 }
110
111
112 static public String getMetadataSetNamespace(String metadata_element_name_full)
113 {
114 // Full element name contains namespace
115 if (metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) != -1) {
116 return metadata_element_name_full.substring(0, metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR));
117 }
118
119 // No namespace
120 return "";
121 }
122
123
124 static public String getRegularExpressionThatMatchesFilePath(String file_path)
125 {
126 // Convert the file path into a regular expression that will match it
127 String file_path_regexp = file_path;
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 file_path_regexp = file_path_regexp.replaceAll("\\+", "\\\\+");
136 return file_path_regexp;
137 }
138
139 static public boolean TO_DISPLAY_NAMES = true;
140 static public boolean FROM_DISPLAY_NAMES = false;
141
142 static public String convertMetadataElementListNames(String value_str, boolean to_display) {
143 if (value_str == null || value_str.length () == 0) {
144 return value_str;
145 }
146
147 // final true arg to return delims as tokens
148 StringTokenizer string_tokenizer = new StringTokenizer (value_str, ",;/", true);
149 StringBuffer value_buffer = new StringBuffer ();
150 while (string_tokenizer.hasMoreElements ()) {
151 String raw_token = (String) string_tokenizer.nextElement ();
152 String token = raw_token.trim ();
153 boolean modified_token = false;
154 // not a delimiter token
155 if (!raw_token.equals(",") && !raw_token.equals(";") && !raw_token.equals("/")) {
156 MetadataElement meta_elem = null;
157 if (to_display) {
158 meta_elem = getMetadataElementWithName(token);
159 if (meta_elem !=null) {
160 token = meta_elem.getDisplayName();
161 modified_token = true;
162 }
163 } else {
164 meta_elem = getMetadataElementWithDisplayName(token);
165
166 if (meta_elem != null) {
167 token = meta_elem.getFullName ();
168 modified_token = true;
169 }
170 }
171 }
172 if (modified_token) {
173 value_buffer.append (token);
174 } else {
175 // we may have had whitespace etc that was part of the string
176 value_buffer.append (raw_token);
177 }
178 }
179
180
181 if(value_buffer.length () == 0) {
182 return "";
183 }
184 return value_buffer.toString();
185 }
186
187
188}
189
Note: See TracBrowser for help on using the repository browser.