Index: /trunk/gli/src/org/greenstone/gatherer/msm/MetadataXMLFile.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/msm/MetadataXMLFile.java	(revision 6880)
+++ /trunk/gli/src/org/greenstone/gatherer/msm/MetadataXMLFile.java	(revision 6881)
@@ -84,40 +84,15 @@
     public void addMetadata(String filename, Metadata metadata, boolean force_accumulate)
     {
-	System.err.println("Add '" + metadata + "' to " + (filename != null ? filename : "directory."));
+	// System.err.println("Add '" + metadata + "' to " + (filename != null ? filename : "directory."));
 	try {
-	    // Retrieve the document element.
-	    Element directorymetadata_element = base_document.getDocumentElement();
-	    // Iterate through the filesets looking for one that matches the given filename.
-	    Element fileset_element = null;
-	    boolean found = false;
-	    NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT);
-	    for(int i = 0; !found && i < fileset_elements.getLength(); i++) {
-		fileset_element = (Element) fileset_elements.item(i);
-		NodeList filename_elements = fileset_element.getElementsByTagName(FILENAME_ELEMENT);
-		for(int j = 0; !found && j < filename_elements.getLength(); j++) {
-		    Element filename_element = (Element) filename_elements.item(j);
-		    String filename_pattern = MSMUtils.getValue(filename_element);
-		    System.err.println("Checking " + filename + " against " + filename_pattern + "|");
-		    // Have we found a match. If so break out of for loop.
-		    if (filename != null && filename.matches(filename_pattern) && !filename_pattern.equals(DIRECTORY_FILENAME)) {
-			System.err.println("Adding to existing file fileset!");
-			found = true;
-		    }
-		    else if (filename == null && filename_pattern.equals(DIRECTORY_FILENAME)) {
-			System.err.println("Adding to existing folder fileset!");
-			found = true;
-		    }
-		    // No match. On to the next one.
-		    else {
-			System.err.println("No match at all!");
-		    }
-		    filename_pattern = null;
-		    filename_element = null;
-		}
-	    }
-	    fileset_elements = null;
-	    // If we still haven't found an existing fileset, then its time to create one.
+	    // Retrieve the document element
+	    Element gdm_element = base_document.getDocumentElement();
+
+	    // Find the fileset element for the filename
+	    Element fileset_element = findFileSetElementForFile(gdm_element, filename);
+
+	    // If there is no existing fileset, then create one
 	    if (fileset_element == null) {
-		System.err.println("Creating a new fileset.");
+		// System.err.println("Creating a new fileset.");
 		fileset_element = base_document.createElement(FILESET_ELEMENT);
 		Element filename_element = base_document.createElement(FILENAME_ELEMENT);
@@ -126,21 +101,21 @@
 		fileset_element.appendChild(description_element);
 		Text filename_text = null;
-		// If the filename is null then we add a directory metadata set as directorymetadata_element's first child
+		// If the filename is null then we add a directory metadata set as gdm_element's first child
 		if (filename == null) {
 		    filename_text = base_document.createTextNode(DIRECTORY_FILENAME);
-		    if (directorymetadata_element.hasChildNodes()) {
-			directorymetadata_element.insertBefore(fileset_element, directorymetadata_element.getFirstChild());
+		    if (gdm_element.hasChildNodes()) {
+			gdm_element.insertBefore(fileset_element, gdm_element.getFirstChild());
 		    }
 		    else {
-			directorymetadata_element.appendChild(fileset_element);
+			gdm_element.appendChild(fileset_element);
 		    }
 		}
-		// Otherwise we append the new fileset to directorymetadata_element's children
+		// Otherwise we append the new fileset to gdm_element's children
 		else {
-		    System.err.println("New fileset for " + filename);
+		    // System.err.println("New fileset for " + filename);
 		    filename = Codec.transform(filename, Codec.TEXT_TO_REGEXP);
-		    System.err.println("After transform: " + filename);
+		    // System.err.println("After transform: " + filename);
 		    filename_text = base_document.createTextNode(filename);
-		    directorymetadata_element.appendChild(fileset_element);
+		    gdm_element.appendChild(fileset_element);
 		}
 		filename_element.appendChild(filename_text);
@@ -208,7 +183,6 @@
 	    description_element = null;
 	    metadata_element = null;
-				//mode = null;
 	    fileset_element = null;
-	    directorymetadata_element = null;
+	    gdm_element = null;
 	    up_to_date = false;
 	}
@@ -249,4 +223,38 @@
 	return count;
     }
+
+
+    private Element findFileSetElementForFile(Element gdm_element, String filename)
+    {
+	// Iterate through the filesets looking for one that matches the given filename
+	NodeList fileset_elements = gdm_element.getElementsByTagName(FILESET_ELEMENT);
+	for (int i = 0; i < fileset_elements.getLength(); i++) {
+	    Element fileset_element = (Element) fileset_elements.item(i);
+
+	    NodeList filename_elements = fileset_element.getElementsByTagName(FILENAME_ELEMENT);
+	    for (int j = 0; j < filename_elements.getLength(); j++) {
+		Element filename_element = (Element) filename_elements.item(j);
+		String filename_pattern = MSMUtils.getValue(filename_element);
+
+		// System.err.println("Checking " + filename + " against " + filename_pattern);
+
+		// Is this a file match?
+		if (filename != null && filename.matches(filename_pattern) &&
+		    !filename_pattern.equals(DIRECTORY_FILENAME)) {
+		    // System.err.println("Existing file fileset!");
+		    return fileset_element;
+		}
+		// Is this a folder match?
+		else if (filename == null && filename_pattern.equals(DIRECTORY_FILENAME)) {
+		    // System.err.println("Existing folder fileset!");
+		    return fileset_element;
+		}
+	    }
+	}
+
+	// No match found
+	return null;
+    }
+
 
     /** Retrieve the document this class is wrapping. */
