Index: /trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java	(revision 13576)
+++ /trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java	(revision 13577)
@@ -45,4 +45,8 @@
     static final private String METADATA_ELEMENT = "Metadata";
 
+    // To speed things up a bit we keep the last accessed metadata.xml file in memory
+    static private File loaded_file = null;
+    static private Document loaded_file_document = null;
+
 
     public MetadataXMLFile(String metadata_xml_file_path)
@@ -54,9 +58,15 @@
     public void addMetadata(File file, ArrayList metadata_values)
     {
-	// Parse the metadata.xml file
-	Document document = XMLTools.parseXMLFile(this);
-	if (document == null) {
-	    System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
-	    return;
+	// If this metadata.xml file isn't the one currently loaded, load it now
+	if (loaded_file != this) {
+	    // Parse the metadata.xml file
+	    Document document = XMLTools.parseXMLFile(this);
+	    if (document == null) {
+		System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
+		return;
+	    }
+
+	    loaded_file = this;
+	    loaded_file_document = document;
 	}
 
@@ -83,5 +93,5 @@
 
 	// Read all the FileSet elements in the file
-	NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
+	NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
 	for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
 	    Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
@@ -104,14 +114,14 @@
 	if (appropriate_fileset_element == null) {
 	    DebugStream.println("Creating new FileSet element for file since none exists...");
-	    appropriate_fileset_element = document.createElement(FILESET_ELEMENT);
-
-	    Element new_filename_element = document.createElement(FILENAME_ELEMENT);
-	    new_filename_element.appendChild(document.createTextNode(file_path_regexp));
+	    appropriate_fileset_element = loaded_file_document.createElement(FILESET_ELEMENT);
+
+	    Element new_filename_element = loaded_file_document.createElement(FILENAME_ELEMENT);
+	    new_filename_element.appendChild(loaded_file_document.createTextNode(file_path_regexp));
 	    appropriate_fileset_element.appendChild(new_filename_element);
 
-	    Element new_description_element = document.createElement(DESCRIPTION_ELEMENT);
+	    Element new_description_element = loaded_file_document.createElement(DESCRIPTION_ELEMENT);
 	    appropriate_fileset_element.appendChild(new_description_element);
 
-	    document.getDocumentElement().appendChild(appropriate_fileset_element);
+	    loaded_file_document.getDocumentElement().appendChild(appropriate_fileset_element);
 	}
 
@@ -151,8 +161,8 @@
 	    if (!metadata_already_assigned) {
 		// Create a new Metadata element to record this metadata
-		Element new_metadata_element = document.createElement(METADATA_ELEMENT);
+		Element new_metadata_element = loaded_file_document.createElement(METADATA_ELEMENT);
 		new_metadata_element.setAttribute("name", metadata_value.getMetadataElement().getFullName());
 		new_metadata_element.setAttribute("mode", (metadata_value.isAccumulatingMetadata() ? "accumulate" : "override"));
-		new_metadata_element.appendChild(document.createTextNode(metadata_value_string));
+		new_metadata_element.appendChild(loaded_file_document.createTextNode(metadata_value_string));
 
 		// Accumulating metadata: add at the end
@@ -168,5 +178,5 @@
 
 	// Rewrite the metadata.xml file
-	XMLTools.writeXMLFile(this, document);
+	XMLTools.writeXMLFile(this, loaded_file_document);
     }
 
@@ -174,9 +184,15 @@
     public ArrayList getMetadataAssignedToFile(File file)
     {
-	// Parse the metadata.xml file
-	Document document = XMLTools.parseXMLFile(this);
-	if (document == null) {
-	    System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
-	    return new ArrayList();
+	// If this metadata.xml file isn't the one currently loaded, load it now
+	if (loaded_file != this) {
+	    // Parse the metadata.xml file
+	    Document document = XMLTools.parseXMLFile(this);
+	    if (document == null) {
+		System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
+		return new ArrayList();
+	    }
+
+	    loaded_file = this;
+	    loaded_file_document = document;
 	}
 
@@ -192,5 +208,5 @@
 
 	// Read all the FileSet elements in the file
-	NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
+	NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
 	for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
 	    Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
@@ -301,9 +317,15 @@
     public void removeMetadata(File file, ArrayList metadata_values)
     {
-	// Parse the metadata.xml file
-	Document document = XMLTools.parseXMLFile(this);
-	if (document == null) {
-	    System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
-	    return;
+	// If this metadata.xml file isn't the one currently loaded, load it now
+	if (loaded_file != this) {
+	    // Parse the metadata.xml file
+	    Document document = XMLTools.parseXMLFile(this);
+	    if (document == null) {
+		System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
+		return;
+	    }
+
+	    loaded_file = this;
+	    loaded_file_document = document;
 	}
 
@@ -330,5 +352,5 @@
 
 	// Read all the FileSet elements in the file
-	NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
+	NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
 	for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
 	    Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
@@ -392,5 +414,5 @@
 
 	// Rewrite the metadata.xml file
-	XMLTools.writeXMLFile(this, document);
+	XMLTools.writeXMLFile(this, loaded_file_document);
     }
 
@@ -398,9 +420,15 @@
     public void replaceMetadata(File file, MetadataValue old_metadata_value, MetadataValue new_metadata_value)
     {
-	// Parse the metadata.xml file
-	Document document = XMLTools.parseXMLFile(this);
-	if (document == null) {
-	    System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
-	    return;
+	// If this metadata.xml file isn't the one currently loaded, load it now
+	if (loaded_file != this) {
+	    // Parse the metadata.xml file
+	    Document document = XMLTools.parseXMLFile(this);
+	    if (document == null) {
+		System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
+		return;
+	    }
+
+	    loaded_file = this;
+	    loaded_file_document = document;
 	}
 
@@ -434,5 +462,5 @@
 
 	// Read all the FileSet elements in the file
-	NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
+	NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
 	for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
 	    Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
@@ -497,5 +525,5 @@
 
 	// Rewrite the metadata.xml file
-	XMLTools.writeXMLFile(this, document);
+	XMLTools.writeXMLFile(this, loaded_file_document);
     }
 
