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 |
|
---|
27 | package org.greenstone.gatherer.metadata;
|
---|
28 |
|
---|
29 |
|
---|
30 | /** This class is a static class containing useful metadata functions */
|
---|
31 | public class MetadataTools
|
---|
32 | {
|
---|
33 | static final public String NAMESPACE_SEPARATOR = ".";
|
---|
34 |
|
---|
35 |
|
---|
36 | static public String getDisplayNameForMetadataElementWithName(String metadata_element_name_full)
|
---|
37 | {
|
---|
38 | MetadataElement metadata_element = getMetadataElementWithName(metadata_element_name_full);
|
---|
39 | if (metadata_element != null) {
|
---|
40 | return metadata_element.getDisplayName();
|
---|
41 | }
|
---|
42 |
|
---|
43 | return metadata_element_name_full;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | static public String getMetadataElementAttribute(MetadataElement metadata_element, String attribute_name, String language_code, String fallback_language_code)
|
---|
48 | {
|
---|
49 | String metadata_element_attribute = metadata_element.getAttribute(attribute_name, language_code);
|
---|
50 |
|
---|
51 | // If the attribute isn't defined in the desired language, resort to the fallback
|
---|
52 | if (metadata_element_attribute == null && !language_code.equals(fallback_language_code)) {
|
---|
53 | metadata_element_attribute = metadata_element.getAttribute(attribute_name, fallback_language_code);
|
---|
54 | }
|
---|
55 |
|
---|
56 | return metadata_element_attribute;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | static public String getMetadataElementName(String metadata_element_name_full)
|
---|
61 | {
|
---|
62 | // Full element name contains namespace
|
---|
63 | if (metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) != -1) {
|
---|
64 | return metadata_element_name_full.substring(metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) + 1);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // No namespace
|
---|
68 | return metadata_element_name_full;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | static public MetadataElement getMetadataElementWithDisplayName(String metadata_element_display_name_full)
|
---|
73 | {
|
---|
74 | String metadata_set_namespace = getMetadataSetNamespace(metadata_element_display_name_full);
|
---|
75 | MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
|
---|
76 | if (metadata_set == null) {
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return metadata_set.getMetadataElementWithDisplayName(metadata_element_display_name_full);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | static public MetadataElement getMetadataElementWithName(String metadata_element_name_full)
|
---|
85 | {
|
---|
86 | String metadata_set_namespace = getMetadataSetNamespace(metadata_element_name_full);
|
---|
87 | MetadataSet metadata_set = MetadataSetManager.getMetadataSet(metadata_set_namespace);
|
---|
88 | if (metadata_set == null) {
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 |
|
---|
92 | String metadata_element_name = getMetadataElementName(metadata_element_name_full);
|
---|
93 | return metadata_set.getMetadataElementWithName(metadata_element_name);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static public String getMetadataSetAttribute(MetadataSet metadata_set, String attribute_name, String language_code, String fallback_language_code)
|
---|
98 | {
|
---|
99 | String metadata_set_attribute = metadata_set.getAttribute(attribute_name, language_code);
|
---|
100 |
|
---|
101 | // If the attribute isn't defined in the desired language, resort to the fallback
|
---|
102 | if (metadata_set_attribute == null && !language_code.equals(fallback_language_code)) {
|
---|
103 | metadata_set_attribute = metadata_set.getAttribute(attribute_name, fallback_language_code);
|
---|
104 | }
|
---|
105 |
|
---|
106 | return metadata_set_attribute;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | static public String getMetadataSetNamespace(String metadata_element_name_full)
|
---|
111 | {
|
---|
112 | // Full element name contains namespace
|
---|
113 | if (metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR) != -1) {
|
---|
114 | return metadata_element_name_full.substring(0, metadata_element_name_full.indexOf(NAMESPACE_SEPARATOR));
|
---|
115 | }
|
---|
116 |
|
---|
117 | // No namespace
|
---|
118 | return "";
|
---|
119 | }
|
---|
120 | }
|
---|