[12564] | 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 | * <BR><BR>
|
---|
| 9 | *
|
---|
| 10 | * Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
| 11 | *
|
---|
| 12 | * <BR><BR>
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
| 15 | *
|
---|
| 16 | * <BR><BR>
|
---|
| 17 | *
|
---|
| 18 | * This program is free software; you can redistribute it and/or modify
|
---|
| 19 | * it under the terms of the GNU General Public License as published by
|
---|
| 20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 21 | * (at your option) any later version.
|
---|
| 22 | *
|
---|
| 23 | * <BR><BR>
|
---|
| 24 | *
|
---|
| 25 | * This program is distributed in the hope that it will be useful,
|
---|
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 28 | * GNU General Public License for more details.
|
---|
| 29 | *
|
---|
| 30 | * <BR><BR>
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with this program; if not, write to the Free Software
|
---|
| 34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 35 | *########################################################################
|
---|
| 36 | */
|
---|
| 37 | package org.greenstone.gatherer.gems;
|
---|
| 38 |
|
---|
| 39 | import java.io.*;
|
---|
| 40 | import javax.swing.JFileChooser;
|
---|
| 41 | import javax.swing.JOptionPane;
|
---|
| 42 | import java.util.ArrayList;
|
---|
| 43 |
|
---|
| 44 | import org.greenstone.gatherer.Configuration;
|
---|
| 45 | import org.greenstone.gatherer.util.Utility;
|
---|
| 46 | import org.greenstone.gatherer.file.*;
|
---|
| 47 | import org.greenstone.gatherer.util.XMLTools;
|
---|
| 48 | import org.greenstone.gatherer.Dictionary;
|
---|
| 49 |
|
---|
| 50 | import org.apache.xerces.parsers.*;
|
---|
| 51 | import org.apache.xml.serialize.*;
|
---|
| 52 | import org.w3c.dom.*;
|
---|
| 53 | import org.xml.sax.*;
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | /** This class is responsible for managing all the metadata sets in the collection and for providing methods for manipulating the aforementioned sets contents.
|
---|
| 58 | * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
|
---|
| 59 | * @version 2.4
|
---|
| 60 | */
|
---|
| 61 | public class MetadataSetManager
|
---|
| 62 | {
|
---|
| 63 |
|
---|
| 64 | private ArrayList mds_list = new ArrayList();
|
---|
| 65 |
|
---|
| 66 | private MetadataSetModel metadata_set_model;
|
---|
| 67 |
|
---|
| 68 | private ArrayList languageList;
|
---|
| 69 |
|
---|
| 70 | private String current_language = GEMSConstants.DEFAULT_LANGUAGE;
|
---|
| 71 |
|
---|
| 72 | /** Constructor. */
|
---|
| 73 | public MetadataSetManager(String gsdl_path) {
|
---|
| 74 | // Determine the GLI user directory path
|
---|
| 75 | String gli_user_directory_path = System.getProperty("user.home") + File.separator;
|
---|
| 76 |
|
---|
| 77 | if (Utility.isWindows()) {
|
---|
| 78 | gli_user_directory_path += "Application Data" + File.separator + "Greenstone" + File.separator + "GLI" + File.separator;
|
---|
| 79 | }
|
---|
| 80 | else {
|
---|
| 81 | gli_user_directory_path += ".gli" + File.separator;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[12666] | 84 | new Configuration(gli_user_directory_path, gsdl_path, null, null, null);
|
---|
| 85 | current_language = Configuration.getLanguage();
|
---|
[12564] | 86 |
|
---|
| 87 | languageList = new ArrayList();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public ArrayList getLanguageList(){
|
---|
| 91 | //already loaded
|
---|
| 92 | if (languageList.size() > 0 ) return languageList;
|
---|
| 93 |
|
---|
| 94 | Document document = XMLTools.parseXMLFile(getLanguageXMLPath(), true);
|
---|
| 95 |
|
---|
| 96 | NodeList languages = document.getDocumentElement().getElementsByTagName(GEMSConstants.LANGUAGE_ELEMENT);
|
---|
| 97 | for(int i=0; i< languages.getLength();i++){
|
---|
| 98 | Element language_element = (Element)languages.item(i);
|
---|
| 99 | languageList.add(language_element.getAttribute(GEMSConstants.CODE_ATTRIBUTE).toLowerCase()+" "+language_element.getAttribute(GEMSConstants.NAME_ATTRIBUTE));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return languageList;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | public boolean isAttributeRequired(String name){
|
---|
[12666] | 107 | String tmp_name = name.trim();
|
---|
| 108 | for(int i=0;i<GEMSConstants.SET_REQUIRED_ATTRIBUTES.length;i++){
|
---|
| 109 | if (tmp_name.equals(GEMSConstants.SET_REQUIRED_ATTRIBUTES[i])) return true;
|
---|
[12564] | 110 | }
|
---|
| 111 | return false;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public void save(boolean confirm) {
|
---|
| 115 | MetadataSetInfo meta_info = metadata_set_model.getMetadataSetInfo();
|
---|
| 116 |
|
---|
| 117 | if (meta_info ==null) return;
|
---|
| 118 |
|
---|
| 119 | if (confirm){
|
---|
| 120 |
|
---|
[12666] | 121 | //int result = JOptionPane.showOptionDialog(null, Dictionary.get("GEMS.Confirm_Save"), Dictionary.get("GEMS.Confirm_Save_Title"), JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,null,Dictionary.get("General.No"),Dictionary.get("General.No"));
|
---|
| 122 | int result = JOptionPane.showConfirmDialog(null, Dictionary.get("GEMS.Confirm_Save"), Dictionary.get("GEMS.Confirm_Save_Title"), JOptionPane.YES_NO_OPTION);
|
---|
| 123 | if (result != JOptionPane.YES_OPTION) return;
|
---|
[12564] | 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | File file_path = null;
|
---|
| 128 | //get new file name
|
---|
| 129 | if(meta_info.isNew()){
|
---|
| 130 | JFileChooser file_chooser = new JFileChooser(new File(getGLIMetadataDirectoryPath()));
|
---|
| 131 | file_chooser.setMultiSelectionEnabled(false);
|
---|
[12666] | 132 | file_chooser.setSelectedFile(new File(getGLIMetadataDirectoryPath(), meta_info.getNamespace()+GEMSConstants.METADATA_SET_FILE_EXTENSION));
|
---|
[12564] | 133 | int result = file_chooser.showSaveDialog(null);
|
---|
| 134 | if (result == JFileChooser.APPROVE_OPTION){
|
---|
| 135 | file_path = file_chooser.getSelectedFile();
|
---|
| 136 | meta_info.setFilePath(file_path.toString());
|
---|
| 137 | meta_info.setNew(false);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
[12666] | 140 | else {
|
---|
[12564] | 141 | file_path = new File(meta_info.getFilePath());
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[12666] | 144 | if (file_path != null) save(file_path);
|
---|
[12564] | 145 |
|
---|
[12572] | 146 | for(int i=0;i<mds_list.size();i++){
|
---|
| 147 | MetadataSetInfo infoItem = (MetadataSetInfo)mds_list.get(i);
|
---|
| 148 | if (infoItem.getFilePath().trim().equals(meta_info.getFilePath().trim())) {
|
---|
| 149 | mds_list.remove(infoItem);
|
---|
| 150 | mds_list.add(meta_info);
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[12666] | 154 | return;
|
---|
[12572] | 155 |
|
---|
[12564] | 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | private void save(File file_path){
|
---|
| 160 |
|
---|
| 161 | try {
|
---|
| 162 |
|
---|
| 163 | XMLTools.writeXMLFile(file_path, metadata_set_model.getMetadataSetDocument());
|
---|
| 164 |
|
---|
| 165 | }
|
---|
| 166 | catch (Exception error) {
|
---|
| 167 | error.printStackTrace();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | public Document getMetadataSetDocument(String filePath){
|
---|
| 174 | if (filePath == null || filePath.equals("")) return null;
|
---|
| 175 | return XMLTools.parseXMLFile(filePath, true);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | public void setMetadataSetModel(MetadataSetModel model){
|
---|
| 179 | metadata_set_model = model;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | public MetadataSetModel getMetadataSetModel(){
|
---|
| 183 | return metadata_set_model;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | public ArrayList getAvailableMetadataSets(){
|
---|
| 188 | mds_list.clear();
|
---|
| 189 | // Load all the core metadata sets (in the GLI "metadata" directory)
|
---|
| 190 | File metadata_directory = new File(getGLIMetadataDirectoryPath());
|
---|
| 191 | if (metadata_directory.exists()) {
|
---|
| 192 | // Load just those .mds files in this directory, and return them
|
---|
| 193 | File[] directory_files = metadata_directory.listFiles();
|
---|
| 194 | for (int i = 0; i < directory_files.length; i++) {
|
---|
| 195 | File child_file = directory_files[i];
|
---|
| 196 |
|
---|
[12666] | 197 | if (!child_file.isDirectory() && child_file.getName().endsWith(GEMSConstants.METADATA_SET_FILE_EXTENSION)) {
|
---|
[12564] | 198 | Document document = XMLTools.parseXMLFile(child_file.toString(), true);
|
---|
| 199 | MetadataSetInfo info = constructMetadataInfo(document);
|
---|
| 200 | info.setFilePath(child_file.toString());
|
---|
| 201 | mds_list.add(info);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | return mds_list;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[12666] | 209 | public boolean isNamespaceAlreadyUsed(String namespace){
|
---|
[12564] | 210 | for(int i=0;i<mds_list.size();i++){
|
---|
| 211 | MetadataSetInfo info = (MetadataSetInfo)mds_list.get(i);
|
---|
| 212 | if (info.getNamespace().trim().equals(namespace)) return true;
|
---|
| 213 | }
|
---|
| 214 | return false;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | public MetadataSetInfo getMetadataSet(String metadata_path){
|
---|
[12666] | 218 | MetadataSetInfo info = constructMetadataInfo(XMLTools.parseXMLFile(metadata_path,true));
|
---|
[12564] | 219 |
|
---|
[12666] | 220 | // check whether there is metadata set name for the current language
|
---|
[12564] | 221 | // if not reset the current_language to the language first encountered
|
---|
[12666] | 222 | ArrayList attrs = info.getLanguageDependentAttributes();
|
---|
[12564] | 223 | boolean foundLanguage = false;
|
---|
| 224 | for (int i=0;i<attrs.size();i++){
|
---|
| 225 | Attribute attr = (Attribute)attrs.get(i);
|
---|
| 226 | if (attr.getLanguage().equals(current_language)) {
|
---|
| 227 | info.setCurrentLanguage(current_language);
|
---|
| 228 | foundLanguage = true;
|
---|
| 229 | break;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | }
|
---|
| 233 | if (!foundLanguage && attrs.size()>0){
|
---|
| 234 | info.setCurrentLanguage(((Attribute)attrs.get(0)).getLanguage());
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | info.setFilePath(metadata_path);
|
---|
| 238 | return info;
|
---|
| 239 |
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | public void deleteMetadataSet(MetadataSetInfo info){
|
---|
| 243 | String filepath = info.getFilePath();
|
---|
| 244 |
|
---|
| 245 | if(filepath != null && !filepath.trim().equals("")){
|
---|
| 246 | File file = new File(filepath);
|
---|
| 247 |
|
---|
| 248 | boolean deleted = file.delete();
|
---|
| 249 | if (!deleted){
|
---|
[13467] | 250 | JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.File_Deletion_Error_Message"), Dictionary.get("GEMS.File_Deletion_Error"),JOptionPane.ERROR_MESSAGE);
|
---|
[12564] | 251 | }
|
---|
[12572] | 252 | else{
|
---|
| 253 | for(int i=0;i<mds_list.size();i++){
|
---|
| 254 | MetadataSetInfo infoItem = (MetadataSetInfo)mds_list.get(i);
|
---|
| 255 | if (infoItem.getFilePath().trim().equals(info.getFilePath().trim())) {
|
---|
| 256 | mds_list.remove(infoItem);
|
---|
| 257 | break;
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
[12564] | 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | public String getCurrentLanguage(){
|
---|
| 265 | return current_language;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | private MetadataSetInfo constructMetadataInfo(Document document){
|
---|
| 269 | NamedNodeMap attrs = document.getDocumentElement().getAttributes();
|
---|
| 270 | ArrayList attrList = new ArrayList();
|
---|
| 271 | String lang = null;
|
---|
| 272 | boolean findLang = false;
|
---|
| 273 |
|
---|
| 274 | for (int i=0;i<attrs.getLength();i++){
|
---|
| 275 | Attr item = (Attr)attrs.item(i);
|
---|
| 276 | Attribute attr = new Attribute(item.getName(),item.getValue());
|
---|
| 277 | attr.setRequired(isAttributeRequired(attr.getName()));
|
---|
| 278 | attrList.add(attr);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | ArrayList languageList = new ArrayList();
|
---|
| 282 |
|
---|
| 283 | //load all <SetLanguage> elements
|
---|
| 284 | NodeList setLanguages = document.getDocumentElement().getElementsByTagName(GEMSConstants.SET_LANGUAGE_ELEMENT);
|
---|
| 285 |
|
---|
| 286 | if (setLanguages.getLength() >0){
|
---|
| 287 | for(int i=0;i<setLanguages.getLength();i++){
|
---|
| 288 | Element set_language = (Element)setLanguages.item(i);
|
---|
| 289 | lang = set_language.getAttribute(GEMSConstants.CODE_ATTRIBUTE);
|
---|
[12666] | 290 | setNameAndDescription(languageList, set_language,lang);
|
---|
[12564] | 291 | }
|
---|
| 292 |
|
---|
| 293 | }
|
---|
| 294 | //no set langugae elements, try load name and description elements
|
---|
| 295 | else{
|
---|
[12666] | 296 | setNameAndDescription(languageList, document.getDocumentElement(),lang);
|
---|
[12564] | 297 | }
|
---|
| 298 |
|
---|
| 299 | MetadataSetInfo info = new MetadataSetInfo();
|
---|
[12700] | 300 | info.setLanguageDependentAttributes(languageList);
|
---|
[12564] | 301 | info.setAttributes(attrList);
|
---|
| 302 | return info;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[12666] | 305 | private void setNameAndDescription(ArrayList languageList, Element parent,String lang){
|
---|
[12564] | 306 |
|
---|
| 307 | ArrayList names = XMLTools.getChildElementsByTagName(parent,GEMSConstants.NAME_ELEMENT);
|
---|
| 308 | String name_lang = "";
|
---|
| 309 |
|
---|
| 310 | if (names.size() > 0){
|
---|
| 311 | Element name_element = (Element)names.get(0);
|
---|
[12666] | 312 | Attribute attr = new Attribute(GEMSConstants.NAME_ATTRIBUTE,XMLTools.getElementTextValue(name_element), true);
|
---|
[12564] | 313 | if (lang == null){
|
---|
| 314 | attr.setLanguage(name_element.getAttribute(GEMSConstants.LANGUAGE_ATTRIBUTE));
|
---|
| 315 | name_lang = attr.getLanguage();
|
---|
| 316 | }
|
---|
| 317 | else{
|
---|
| 318 | attr.setLanguage(lang);
|
---|
| 319 | }
|
---|
[12666] | 320 | languageList.add(attr);
|
---|
| 321 | // if (current_language.equals(attr.getLanguage())){
|
---|
| 322 | // attr.setRequired(true);
|
---|
| 323 | // languageList.add(0,attr);
|
---|
| 324 | // }
|
---|
| 325 | // else{
|
---|
| 326 | // languageList.add(attr);
|
---|
| 327 | // }
|
---|
[12564] | 328 |
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 | ArrayList des = XMLTools.getChildElementsByTagName(parent,GEMSConstants.DESCRIPTION_ELEMENT);
|
---|
| 333 | if (des.size() > 0){
|
---|
| 334 | Element des_element = (Element)des.get(0);
|
---|
[12666] | 335 | Attribute attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE,XMLTools.getElementTextValue(des_element), true);
|
---|
[12564] | 336 | if (lang == null){
|
---|
| 337 | attr.setLanguage(des_element.getAttribute(GEMSConstants.LANGUAGE_ATTRIBUTE));
|
---|
| 338 | }
|
---|
| 339 | else{
|
---|
| 340 | attr.setLanguage(lang);
|
---|
| 341 | }
|
---|
| 342 | languageList.add(attr);
|
---|
| 343 | }
|
---|
| 344 | else{
|
---|
[12666] | 345 | Attribute attr = new Attribute(GEMSConstants.DESCRIPTION_ATTRIBUTE,"", true);
|
---|
[12564] | 346 | if (lang == null){
|
---|
| 347 | attr.setLanguage(name_lang);
|
---|
| 348 | }
|
---|
| 349 | else{
|
---|
| 350 | attr.setLanguage(lang);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | languageList.add(attr);
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 |
|
---|
| 359 | static public String getGLIDirectoryPath()
|
---|
| 360 | {
|
---|
| 361 | return System.getProperty("user.dir") + File.separator;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 |
|
---|
| 365 | static public String getGLIMetadataDirectoryPath()
|
---|
| 366 | {
|
---|
| 367 | return getGLIDirectoryPath() + "metadata" + File.separator;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | static public String getLanguageXMLPath()
|
---|
| 371 | {
|
---|
| 372 | return getGLIDirectoryPath() + "classes" + File.separator+"xml"+ File.separator+ "languages.xml";
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | static public String getCollectionPath()
|
---|
| 376 | {
|
---|
[12666] | 377 | return Configuration.gsdl_path + File.separator + "collect";
|
---|
[12564] | 378 | }
|
---|
| 379 | }
|
---|