Index: gli/trunk/src/org/greenstone/gatherer/util/RemoveContentBeforeRootElementXMLReader.java
===================================================================
--- gli/trunk/src/org/greenstone/gatherer/util/RemoveContentBeforeRootElementXMLReader.java	(revision 18170)
+++ gli/trunk/src/org/greenstone/gatherer/util/RemoveContentBeforeRootElementXMLReader.java	(revision 18170)
@@ -0,0 +1,86 @@
+package org.greenstone.gatherer.util;
+
+import java.io.Reader;
+import java.io.IOException;
+
+public class RemoveContentBeforeRootElementXMLReader extends Reader {
+
+	static final byte[] xmlIndicator = "<?xml".getBytes();
+
+	Reader ur;
+	int foundBytesReturned = 0;
+	boolean allFoundBytesReturned = false; //redundant, but may help performance
+
+
+	public RemoveContentBeforeRootElementXMLReader( Reader ur ) {
+
+		this.ur = ur;
+
+		//read up to the xml indicator
+		int foundBytes = 0;	
+		char c = 0;
+		while ( c != -1 ) {
+			try {
+				c = (char)ur.read();
+			} catch( Exception e ) {
+				System.err.println( "Exception while reading underlying Reader in RemoveContentBeforeRootElementXMLReader" );
+			}
+
+			if ( c == xmlIndicator[foundBytes] ) {
+				foundBytes++;
+			} else {
+				System.out.print( c );
+				foundBytes = 0;
+			}
+
+			if ( foundBytes == xmlIndicator.length ) {
+				//System.out.println();
+				return;
+			}
+
+		}
+		System.err.println( "RemoveContentBeforeRootElementXMLReader:\n" +
+			"The XML being loaded was did not contain the '<?xml' string as expected" );
+
+	}
+
+	public int read( char[] cbuf, int off, int len ) throws IOException {
+
+		for ( int i=off; i<off+len && i<cbuf.length; i++ ) {
+
+			//read from underlying reader
+			int c = read();
+
+			//catch end of stream
+			if ( c == -1 ) {
+				if ( i == off ) {
+					return -1;
+				}
+				return i - off;
+			}
+
+			//insert character into the array
+			cbuf[i] = (char)c;
+		}
+		return len;
+
+	}
+
+	public int read() throws IOException {
+
+		if ( allFoundBytesReturned ) {
+			return ur.read();
+		}
+
+		if ( foundBytesReturned < xmlIndicator.length ) {
+			return xmlIndicator[foundBytesReturned++];
+		}
+
+		allFoundBytesReturned = true;
+		return ur.read();
+	}
+
+	public void close() throws IOException {
+		ur.close();
+	}
+}
Index: gli/trunk/src/org/greenstone/gatherer/util/XMLTools.java
===================================================================
--- gli/trunk/src/org/greenstone/gatherer/util/XMLTools.java	(revision 18136)
+++ gli/trunk/src/org/greenstone/gatherer/util/XMLTools.java	(revision 18170)
@@ -261,5 +261,5 @@
     static final public String NOTWELLFORMED= "not well-formed";
     static final private String HEADER = "<?xml version='1.0' encoding='UTF-8'?><collectionConfig xmlns:gsf='http://www.greenstone.org/greenstone3/schema/ConfigFormat' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>";
-	static final private String FOOTER = "</collectionConfig>";
+    static final private String FOOTER = "</collectionConfig>";
        
 public static String parse (String xml_str) {
@@ -332,5 +332,5 @@
             SAXParser parser = factory.newSAXParser ();
             FileReader r = new FileReader(xml_file);
-	        InputSource iSource = new InputSource(r);
+            InputSource iSource = new InputSource(r);
             XMLReader reader = parser.getXMLReader ();
             reader.setContentHandler(new DefaultHandler());
@@ -410,6 +410,5 @@
         try {
             InputStreamReader isr = new InputStreamReader (xml_input_stream, "UTF-8");
-            Reader xml_reader = new BufferedReader (isr);
-            document = parseXML (xml_reader);
+            document = parseXML(isr);
             isr.close ();
             xml_input_stream.close ();
@@ -425,57 +424,72 @@
     /** Parse an XML document from a given reader */
     static public Document parseXML (Reader xml_reader) {
-	// If debugging, the following will store the XML contents to be parsed, 
-	// which can then be inspected upon encountering a SAXException (need to run GLI with -debug on)
-	String xmlContents = "";
-
-	Document document = null;
-	try {
-	    // (1) In case parsing exceptions are thrown (SAX Exceptions), we want to get some
-	    // idea of where things went wrong. This will print the "XML" contents to either
-	    // system.out (if debugging is off) or to the DebugStream otherwise.
-	    // We need to read the XML twice to know the line where things went wrong, so
-	    // do the additional reading only if we're debugging 
-	    if(DebugStream.isDebuggingEnabled()) { 		
-		StringBuffer buf = new StringBuffer();
-		char[] buffer = new char[500];
-		int numCharsRead = xml_reader.read(buffer, 0, buffer.length); 
-		while(numCharsRead != -1) {
-		    buf.append(buffer, 0, numCharsRead);
-		    numCharsRead = xml_reader.read(buffer, 0, buffer.length); 
-		}
-		xmlContents = buf.toString();
-		xml_reader.close(); // closing the old Reader
-		xml_reader = null;
-		buffer = null;
-		buf = null;
-		// we need a Reader to parse the same contents as the Reader that was just closed
-		xml_reader = new StringReader(xmlContents);
-	    }
-	    
-	    // (2) The actual XML parsing
-            InputSource isc       = new InputSource (xml_reader);
-	    DOMParser parser      = new DOMParser ();
-	    parser.setFeature ("http://xml.org/sax/features/validation", false);
-            parser.setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
-            // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
-            parser.setFeature ("http://apache.org/xml/features/dom/defer-node-expansion", true);
-            parser.setFeature ("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
-            parser.parse (isc);
-            document = parser.getDocument ();
-        
-	} catch(SAXParseException e) {
-	    showXMLParseFailureLine(e, xmlContents);
-	} catch (SAXException exception) {
-	    System.err.println ("SAX exception: " + exception.getMessage ());
-	    if(DebugStream.isDebuggingEnabled()) {
-		DebugStream.println("Encountered a SAX exception when parsing the following:\n*********START\n" 
-				    + xmlContents + "\n************END\n");
-		// Exit to let the user view the erroneous line/xml before it goes past the screen buffer?
-		DebugStream.println("Debug mode: Exiting the program as there was trouble parsing the XML..."); 
-		System.exit(-1);
-	    } 
-	    // else, not running in debug mode, so don't exit after exception  
-	    System.out.println("***Turn debugging on (run GLI with -debug) to view the XML contents that could not be parsed.");
-	    DebugStream.printStackTrace (exception);
+        Document document = null;
+
+    // If debugging, the following will store the XML contents to be parsed, 
+    // which can then be inspected upon encountering a SAXException (need to run GLI with -debug on)
+    String xmlContents = "";
+
+    try {
+        Reader reader = null;
+
+        // (1) By default, GLI will remove any contents preceeding (and invalidating) 
+        // the XML and present these lines separately to the user
+        if(!DebugStream.isDebuggingEnabled()) {
+            try {
+                reader = new BufferedReader( new RemoveContentBeforeRootElementXMLReader(xml_reader) );
+            } catch ( Exception e ) {
+                System.err.println( "Exception while wrapping the reader in parseXML(Reader)" );
+                e.printStackTrace();
+            }
+        } 
+
+        // (2) If we are running GLI in debug mode:
+        // In case parsing exceptions are thrown (SAX Exceptions), we want to get some
+        // idea of where things went wrong. This will print the "XML" contents to either
+        // system.out (if debugging is off) or to the DebugStream otherwise.
+        // We need to read the XML twice to know the line where things went wrong, so
+        // do the additional reading only if we're debugging 
+        else {
+            StringBuffer buf = new StringBuffer();
+            char[] buffer = new char[500];
+            int numCharsRead = xml_reader.read(buffer, 0, buffer.length); 
+            while(numCharsRead != -1) {
+                buf.append(buffer, 0, numCharsRead);
+                numCharsRead = xml_reader.read(buffer, 0, buffer.length); 
+            }
+            xmlContents = buf.toString();
+            xml_reader.close(); // closing the old Reader
+            xml_reader = null;
+            buffer = null;
+            buf = null;
+            // we need a Reader to parse the same contents as the Reader that was just closed
+            reader = new BufferedReader(new StringReader(xmlContents));
+        }
+        
+        // (2) The actual XML parsing
+        InputSource isc       = new InputSource (reader);
+        DOMParser parser      = new DOMParser ();
+        parser.setFeature ("http://xml.org/sax/features/validation", false);
+        parser.setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+        // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
+        parser.setFeature ("http://apache.org/xml/features/dom/defer-node-expansion", true);
+        parser.setFeature ("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
+        parser.parse (isc);
+        document = parser.getDocument ();
+        
+    } catch(SAXParseException e) {
+        showXMLParseFailureLine(e, xmlContents);
+    } catch (SAXException exception) {
+        System.err.println ("SAX exception: " + exception.getMessage ());
+        if(DebugStream.isDebuggingEnabled()) {
+        DebugStream.println("Encountered a SAX exception when parsing the following:\n*********START\n" 
+                    + xmlContents + "\n************END\n");
+        // Exit to let the user view the erroneous line/xml before it goes past the screen buffer?
+        DebugStream.println("Debug mode: Exiting the program as there was trouble parsing the XML..."); 
+        System.exit(-1);
+        } 
+        // else, not running in debug mode, so don't exit after exception  
+        System.out.println("***Turn debugging on (run GLI with -debug) to view the XML contents that could not be parsed.");
+        DebugStream.printStackTrace (exception);
         }
         catch (Exception exception) {
@@ -493,41 +507,41 @@
      */
     public static void showXMLParseFailureLine(SAXParseException e, String xmlContents) {
-	
-	// There should be no characters at all that preceed the <?xml>... bit. 
-	// The first check is for starting spaces:
-	if(xmlContents.startsWith("\n") || xmlContents.startsWith(" ") || xmlContents.startsWith("\t")) {
-	    DebugStream.println("ERROR: illegal start of XML. Space/tab/newline should not preceed xml declaration.\n");
-	    DebugStream.println("xmlContents (length is " + xmlContents.length() + "):\n" + xmlContents);
-	    return; // nothing more to do, first error identified
-	}
-
-	// the actual line (String literal) where parsing failed and the SAXParseException occurred.
-	String line = "";
-	int linenumber = e.getLineNumber();
-	DebugStream.println("\n****SAXParseException on LINE NUMBER: " + linenumber);
-	if(DebugStream.isDebuggingEnabled()) {
-	    if(linenumber != -1) { 
-		// find the line in xmlContents string (xmlContents is only set if GLI is run with debugging turned on)
-		int start = 0;
-		int end = xmlContents.length();
-		for(int i = 1; i <= linenumber; i++) {
-		    end = xmlContents.indexOf("\n");
-		    if(end > 0) {
-			line = xmlContents.substring(start, end);
-		    }
-		    start = end+1;
-		}
-		DebugStream.println("The parsing error occurred on this line:\n***********START\n" + line + "\n***********END");
-		DebugStream.println("SAXParseException message: " + e.getMessage() + "\n");
-	    } else { // no particular line number, print out all the xml so debugger can inspect it
-		DebugStream.println("Encountered a SAX exception when parsing the following:\n*********START\n" 
-				    + xmlContents + "\n************END\n");
-	    }
-	    // Exit to let the user view the erroneous line/xml before it goes past the screen buffer?
-	    DebugStream.println("\nDebug mode: Exiting the program as there was trouble parsing the XML..."); 
-	    System.exit(-1);
-	} else { // not running in debug mode
-	    System.out.println("***Turn debugging on (run GLI with -debug) to view the XML contents/line that could not be parsed.");
-	}
+    
+        // There should be no characters at all that preceed the <?xml>... bit. 
+        // The first check is for starting spaces:
+        if(xmlContents.startsWith("\n") || xmlContents.startsWith(" ") || xmlContents.startsWith("\t")) {
+            DebugStream.println("ERROR: illegal start of XML. Space/tab/newline should not preceed xml declaration.\n");
+            DebugStream.println("xmlContents (length is " + xmlContents.length() + "):\n" + xmlContents);
+            return; // nothing more to do, first error identified
+        }
+    
+        // the actual line (String literal) where parsing failed and the SAXParseException occurred.
+        String line = "";
+        int linenumber = e.getLineNumber();
+        DebugStream.println("\n****SAXParseException on LINE NUMBER: " + linenumber);
+        if(DebugStream.isDebuggingEnabled()) {
+            if(linenumber != -1) { 
+                // find the line in xmlContents string (xmlContents is only set if GLI is run with debugging turned on)
+                int start = 0;
+                int end = xmlContents.length();
+                for(int i = 1; i <= linenumber; i++) {
+                    end = xmlContents.indexOf("\n");
+                    if(end > 0) {
+                        line = xmlContents.substring(start, end);
+                    }
+                    start = end+1;
+                }
+                DebugStream.println("The parsing error occurred on this line:\n***********START\n" + line + "\n***********END");
+                DebugStream.println("SAXParseException message: " + e.getMessage() + "\n");
+            } else { // no particular line number, print out all the xml so debugger can inspect it
+                DebugStream.println("Encountered a SAX exception when parsing the following:\n*********START\n" 
+                    + xmlContents + "\n************END\n");
+            }
+            // Exit to let the user view the erroneous line/xml before it goes past the screen buffer?
+            DebugStream.println("\nDebug mode: Exiting the program as there was trouble parsing the XML..."); 
+            System.exit(-1);
+        } else { // not running in debug mode
+            System.out.println("***Turn debugging on (run GLI with -debug) to view the XML contents/line that could not be parsed.");
+        }
     }
 
@@ -625,6 +639,6 @@
             f.setPreserveSpace (false);
             if (nonEscapingTagNames != null) {
-	      f.setNonEscapingElements (nonEscapingTagNames);
-	    }
+          f.setNonEscapingElements (nonEscapingTagNames);
+        }
             // Create the necessary writer stream for serialization.
             OutputStreamWriter osw = new OutputStreamWriter (os, "UTF-8");
@@ -797,9 +811,9 @@
             int len = children.getLength ();
             for (int i = 0; i < len; i++) {
-	      if (depth >= 0) {
+          if (depth >= 0) {
                 xmlNodeToStringWithoutNewline (sb,children.item (i), depth + 1);
-	      } else {
-		xmlNodeToStringWithoutNewline (sb,children.item (i), depth);
-	      }
+          } else {
+        xmlNodeToStringWithoutNewline (sb,children.item (i), depth);
+          }
             }
             
