source: trunk/gli/src/org/greenstone/gatherer/metadata/MetadataSet.java@ 8267

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

Added a .mds file filter class for creating file filters for metadata sets.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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
30import java.io.File;
31import java.util.*;
32import javax.swing.filechooser.*;
33import org.greenstone.gatherer.Dictionary;
34import org.greenstone.gatherer.util.XMLTools;
35import org.w3c.dom.*;
36
37
38/** This class represents one metadata set */
39public class MetadataSet
40{
41 private File metadata_set_file = null;
42 private ArrayList metadata_set_elements = new ArrayList();
43 private String metadata_set_namespace = null;
44
45
46 public MetadataSet(File metadata_set_file)
47 {
48 this.metadata_set_file = metadata_set_file;
49
50 // Parse the specified metadata set file
51 Document document = XMLTools.parseXMLFile(metadata_set_file);
52 if (document == null) {
53 System.err.println("Error: Could not parse metadata set file " + metadata_set_file.getAbsolutePath());
54 return;
55 }
56
57 // Read the metadata set's namespace
58 metadata_set_namespace = document.getDocumentElement().getAttribute("namespace");
59
60 // Read the Element elements in the metadata set
61 NodeList element_elements_nodelist = document.getElementsByTagName("Element");
62 for (int i = 0; i < element_elements_nodelist.getLength(); i++) {
63 Element metadata_element_element = (Element) element_elements_nodelist.item(i);
64 String metadata_element_name = metadata_element_element.getAttribute("name");
65 String metadata_element_name_full = metadata_set_namespace + MetadataTools.NAMESPACE_SEPARATOR + metadata_element_name;
66 metadata_set_elements.add(new MetadataElement(metadata_element_name_full, metadata_element_element));
67 }
68 }
69
70
71 public MetadataElement addMetadataElementForThisSession(String metadata_element_name)
72 {
73 String metadata_element_name_full = metadata_set_namespace + MetadataTools.NAMESPACE_SEPARATOR + metadata_element_name;
74 MetadataElement metadata_element = new MetadataElement(metadata_element_name_full, null);
75 metadata_set_elements.add(metadata_element);
76 return metadata_element;
77 }
78
79
80 public int compareMetadataElements(MetadataElement metadata_element_a, MetadataElement metadata_element_b)
81 {
82 // Extracted metadata is sorted alphabetically
83 if (metadata_set_namespace.equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE)) {
84 return metadata_element_a.getName().compareTo(metadata_element_b.getName());
85 }
86
87 // Elements in metadata sets are sorted according to the other they are specified in the .mds file
88 return metadata_set_elements.indexOf(metadata_element_a) - metadata_set_elements.indexOf(metadata_element_b);
89 }
90
91
92 public String getAttribute(String attribute_name, String language_code)
93 {
94 // Parse the specified metadata set file
95 Document document = XMLTools.parseXMLFile(metadata_set_file);
96 if (document == null) {
97 System.err.println("Error: Could not parse metadata set file " + metadata_set_file.getAbsolutePath());
98 return null;
99 }
100
101 Element document_element = document.getDocumentElement();
102 Element parent_element = document_element;
103 boolean language_matches = false;
104
105 NodeList setlanguage_elements_nodelist = document_element.getElementsByTagName("SetLanguage");
106 for (int i = 0; i < setlanguage_elements_nodelist.getLength(); i++) {
107 Element current_setlanguage_element = (Element) setlanguage_elements_nodelist.item(i);
108 String current_setlanguage_element_code = current_setlanguage_element.getAttribute("code");
109 if (current_setlanguage_element_code.equals(language_code)) {
110 // Found language; use it for the next step
111 parent_element = current_setlanguage_element;
112 language_matches = true;
113 break;
114 }
115 }
116
117 NodeList attribute_elements_nodelist = parent_element.getElementsByTagName(attribute_name);
118 for (int i = 0; i < attribute_elements_nodelist.getLength(); i++) {
119 Element current_attribute_element = (Element) attribute_elements_nodelist.item(i);
120
121 // Check if the language matches
122 String current_attribute_element_language = current_attribute_element.getAttribute("language");
123 if (language_matches || current_attribute_element_language.equals(language_code)) {
124 // Found attribute; returns its value
125 return XMLTools.getElementTextValue(current_attribute_element);
126 }
127 }
128
129 return null;
130 }
131
132
133 public MetadataElement getMetadataElementWithDisplayName(String metadata_element_display_name_full)
134 {
135 // Find the desired metadata element in the list
136 for (int i = 0; i < metadata_set_elements.size(); i++) {
137 MetadataElement metadata_element = (MetadataElement) metadata_set_elements.get(i);
138 if (metadata_element.getDisplayName().equals(metadata_element_display_name_full)) {
139 return metadata_element;
140 }
141 }
142
143 // Not found
144 return null;
145 }
146
147
148 public MetadataElement getMetadataElementWithName(String metadata_element_name)
149 {
150 // Find the desired metadata element in the list
151 for (int i = 0; i < metadata_set_elements.size(); i++) {
152 MetadataElement metadata_element = (MetadataElement) metadata_set_elements.get(i);
153 if (metadata_element.getName().equals(metadata_element_name)) {
154 return metadata_element;
155 }
156 }
157
158 // Not found
159 return null;
160 }
161
162
163 public ArrayList getMetadataSetElements()
164 {
165 // We don't want anyone to modify the list, so return a copy of it
166 return (ArrayList) metadata_set_elements.clone();
167 }
168
169
170 public File getMetadataSetFile()
171 {
172 return metadata_set_file;
173 }
174
175
176 public String getNamespace()
177 {
178 return metadata_set_namespace;
179 }
180
181
182 static public class MetadataSetFileFilter
183 extends FileFilter
184 {
185 public boolean accept(File file)
186 {
187 String file_name = file.getName().toLowerCase();
188 if (file_name.endsWith(".mds") || file_name.indexOf(".") == -1) {
189 return true;
190 }
191
192 return false;
193 }
194
195
196 public String getDescription()
197 {
198 return Dictionary.get("MetadataSet.Files");
199 }
200 }
201}
Note: See TracBrowser for help on using the repository browser.