[8164] | 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 |
|
---|
[8127] | 27 | package org.greenstone.gatherer.metadata;
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | import java.io.*;
|
---|
[8136] | 31 | import java.util.*;
|
---|
[13808] | 32 | import org.greenstone.gatherer.Gatherer;
|
---|
| 33 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
| 34 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
[8127] | 35 | import org.greenstone.gatherer.util.XMLTools;
|
---|
| 36 | import org.w3c.dom.*;
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | /** This class represents one profile.xml file */
|
---|
| 40 | public class ProfileXMLFile
|
---|
| 41 | extends File
|
---|
| 42 | {
|
---|
[8990] | 43 | private HashMap metadata_mapping = null;
|
---|
| 44 |
|
---|
| 45 |
|
---|
[8127] | 46 | public ProfileXMLFile(String profile_xml_file_path)
|
---|
| 47 | {
|
---|
| 48 | super(profile_xml_file_path);
|
---|
[8128] | 49 | }
|
---|
[8127] | 50 |
|
---|
[8128] | 51 |
|
---|
[8990] | 52 | public String getMetadataElementFor(String metadata_element_name_full)
|
---|
[8128] | 53 | {
|
---|
[8990] | 54 | if (getMetadataMapping() == null) {
|
---|
[8128] | 55 | return null;
|
---|
[8127] | 56 | }
|
---|
[8128] | 57 |
|
---|
[8990] | 58 | return (String) metadata_mapping.get(metadata_element_name_full);
|
---|
[8127] | 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
[8136] | 62 | public HashMap getMetadataMapping()
|
---|
| 63 | {
|
---|
[8990] | 64 | // If we have already loaded the metadata mapping, use it
|
---|
| 65 | if (metadata_mapping != null) {
|
---|
| 66 | return metadata_mapping;
|
---|
| 67 | }
|
---|
[8136] | 68 |
|
---|
[8990] | 69 | // Build the metadata mapping from the profile.xml file
|
---|
| 70 | metadata_mapping = new HashMap();
|
---|
| 71 |
|
---|
[8136] | 72 | // Parse the profile.xml file
|
---|
| 73 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 74 | if (document == null) {
|
---|
| 75 | System.err.println("Error: Could not parse profile.xml file " + getAbsolutePath());
|
---|
| 76 | return null;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // Read all the Action elements in the file
|
---|
| 80 | NodeList action_elements_nodelist = document.getElementsByTagName("Action");
|
---|
| 81 | for (int i = 0; i < action_elements_nodelist.getLength(); i++) {
|
---|
| 82 | Element current_action_element = (Element) action_elements_nodelist.item(i);
|
---|
| 83 | String source_metadata_element_name_full = current_action_element.getAttribute("source");
|
---|
| 84 | String target_metadata_element_name_full = current_action_element.getAttribute("target");
|
---|
[8990] | 85 | metadata_mapping.put(source_metadata_element_name_full, target_metadata_element_name_full);
|
---|
[8136] | 86 | }
|
---|
| 87 |
|
---|
[8990] | 88 | return metadata_mapping;
|
---|
[8136] | 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
[8128] | 92 | public void mapElement(String source_metadata_element_name_full, String target_metadata_element_name_full)
|
---|
[8127] | 93 | {
|
---|
[8128] | 94 | // Parse the profile.xml file
|
---|
| 95 | Document document = XMLTools.parseXMLFile(this);
|
---|
| 96 | if (document == null) {
|
---|
| 97 | System.err.println("Error: Could not parse profile.xml file " + getAbsolutePath());
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // Create a new Action element to record this mapping
|
---|
| 102 | Element new_action_element = document.createElement("Action");
|
---|
| 103 | new_action_element.setAttribute("source", source_metadata_element_name_full);
|
---|
| 104 | new_action_element.setAttribute("target", target_metadata_element_name_full);
|
---|
| 105 | document.getDocumentElement().appendChild(new_action_element);
|
---|
| 106 |
|
---|
| 107 | // Rewrite the profile.xml file
|
---|
| 108 | XMLTools.writeXMLFile(this, document);
|
---|
[8990] | 109 |
|
---|
[13808] | 110 | // This is inefficient but for simplicity we'll just upload the file every time it is changed
|
---|
| 111 | if (Gatherer.isGsdlRemote) {
|
---|
[19668] | 112 | Gatherer.remoteGreenstoneServer.uploadCollectionFile(CollectionManager.getLoadedGroupQualifiedCollectionName(true), this);
|
---|
[13808] | 113 | }
|
---|
| 114 |
|
---|
[8990] | 115 | // Invalidate the metadata mapping
|
---|
| 116 | metadata_mapping = null;
|
---|
[8127] | 117 | }
|
---|
| 118 | }
|
---|