source: trunk/gli/src/org/greenstone/gatherer/metadata/ProfileXMLFile.java@ 13680

Last change on this file since 13680 was 8990, checked in by mdewsnip, 19 years ago

Now caches the metadata mapping so subsequent reads don't require reading the profile.xml file each time. This makes a huge difference when opening collections containing a lot of metadata that must be mapped into a metadata set.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 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.*;
31import java.util.*;
32import org.greenstone.gatherer.util.XMLTools;
33import org.w3c.dom.*;
34
35
36/** This class represents one profile.xml file */
37public class ProfileXMLFile
38 extends File
39{
40 private HashMap metadata_mapping = null;
41
42
43 public ProfileXMLFile(String profile_xml_file_path)
44 {
45 super(profile_xml_file_path);
46 }
47
48
49 public String getMetadataElementFor(String metadata_element_name_full)
50 {
51 if (getMetadataMapping() == null) {
52 return null;
53 }
54
55 return (String) metadata_mapping.get(metadata_element_name_full);
56 }
57
58
59 public HashMap getMetadataMapping()
60 {
61 // If we have already loaded the metadata mapping, use it
62 if (metadata_mapping != null) {
63 return metadata_mapping;
64 }
65
66 // Build the metadata mapping from the profile.xml file
67 metadata_mapping = new HashMap();
68
69 // Parse the profile.xml file
70 Document document = XMLTools.parseXMLFile(this);
71 if (document == null) {
72 System.err.println("Error: Could not parse profile.xml file " + getAbsolutePath());
73 return null;
74 }
75
76 // Read all the Action elements in the file
77 NodeList action_elements_nodelist = document.getElementsByTagName("Action");
78 for (int i = 0; i < action_elements_nodelist.getLength(); i++) {
79 Element current_action_element = (Element) action_elements_nodelist.item(i);
80 String source_metadata_element_name_full = current_action_element.getAttribute("source");
81 String target_metadata_element_name_full = current_action_element.getAttribute("target");
82 metadata_mapping.put(source_metadata_element_name_full, target_metadata_element_name_full);
83 }
84
85 return metadata_mapping;
86 }
87
88
89 public void mapElement(String source_metadata_element_name_full, String target_metadata_element_name_full)
90 {
91 // Parse the profile.xml file
92 Document document = XMLTools.parseXMLFile(this);
93 if (document == null) {
94 System.err.println("Error: Could not parse profile.xml file " + getAbsolutePath());
95 return;
96 }
97
98 // Create a new Action element to record this mapping
99 Element new_action_element = document.createElement("Action");
100 new_action_element.setAttribute("source", source_metadata_element_name_full);
101 new_action_element.setAttribute("target", target_metadata_element_name_full);
102 document.getDocumentElement().appendChild(new_action_element);
103
104 // Rewrite the profile.xml file
105 XMLTools.writeXMLFile(this, document);
106
107 // Invalidate the metadata mapping
108 metadata_mapping = null;
109 }
110}
Note: See TracBrowser for help on using the repository browser.