Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/util/XMLTransformer.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/util/XMLTransformer.java	(revision 18434)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/util/XMLTransformer.java	(revision 18435)
@@ -24,4 +24,5 @@
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
+import javax.xml.transform.ErrorListener;
 
 import javax.xml.transform.stream.StreamSource;
@@ -61,5 +62,5 @@
 
     static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLTransformer.class.getName());
-
+	
   /** The transformer factory we're using */
     TransformerFactory t_factory=null;
@@ -98,4 +99,5 @@
 	    // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
 	    Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
+		transformer.setErrorListener(new TransformErrorListener());
 
 	    // Use the Transformer to transform an XML Source and send the output to a Result object.
@@ -109,5 +111,5 @@
 	    return "";
 	} catch (TransformerException e) {
-	    logger.error("couldn't transform the source: " + e.getMessage());
+	    logger.error("couldn't transform the source: " + e.getMessageAndLocation());
 	    return "";
 	}	
@@ -123,4 +125,5 @@
 	    // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
 	    Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
+		transformer.setErrorListener(new TransformErrorListener());
 	    if (parameters != null) {
 		Set params = parameters.entrySet();
@@ -140,76 +143,100 @@
 	    return output.toString();
 	} catch (TransformerConfigurationException e) {
-	    logger.error("couldn't create transformer object: "+e.getMessageAndLocation());
+		logger.error("couldn't create transformer object: "+e.getMessageAndLocation());
 	    logger.error(e.getLocationAsString());  
 	    return "";
 	} catch (TransformerException e) {
-	    logger.error("couldn't transform the source: " + e.getMessage());
+		logger.error("couldn't transform the source: " + e.getMessageAndLocation());
 	    return "";
-	}	
-      }
+	}
+    }
 
     public Node transform(Document stylesheet, Document source) {
-	return transform(stylesheet, source, null);
-    }
-
-    public Node transform(Document stylesheet, Document source, HashMap parameters) {
-
-	try {
-	    // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
-	    Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
-	    
-	    if (parameters != null) {
-		Set params = parameters.entrySet();
-		Iterator i = params.iterator();
-		while (i.hasNext()) {
-		    Map.Entry m = (Map.Entry)i.next();
-		    transformer.setParameter((String)m.getKey(), m.getValue());
-		}
-	    }
-
-	    DOMResult result = new DOMResult();
-	    transformer.transform(new DOMSource(source), result);
-	    return result.getNode().getFirstChild();
-	} catch (TransformerConfigurationException e) {
-		String message = "TransformerConfigurationException: "+e.getMessageAndLocation();
-		//System.err.println(message);
-		logger.error("couldn't create transformer object: "+e.getMessageAndLocation());
-	    logger.error(e.getLocationAsString());  
-	    return constructErrorXHTMLPage(message);
-	} catch (TransformerException e) {
-		String message = "TransformerException: " + e.getMessageAndLocation();
-		//System.err.println(message);
-		logger.error("couldn't transform the source: " + e.getMessage());
-	    return constructErrorXHTMLPage(message);
-	}	
-	
-
-    }
-
-
-    
+	return transform(stylesheet, source, null, null);
+    }
+
+    public Node transform(Document stylesheet, Document source, HashMap parameters, Document docDocType) {
+		try {
+			// Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
+			Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
+			transformer.setErrorListener(new TransformErrorListener());
+			if (parameters != null) {
+				Set params = parameters.entrySet();
+				Iterator i = params.iterator();
+				while (i.hasNext()) {
+					Map.Entry m = (Map.Entry)i.next();
+					transformer.setParameter((String)m.getKey(), m.getValue());
+				}
+			}
+
+			// When we transform the DOMResult, we need to make sure the result of
+			// the transformation has a DocType. For that to happen, we need to create
+			// the DOMResult using a Document with a predefined docType.
+			// If we don't have a DocType then do the transformation with a DOMResult
+			// that does not contain any doctype (like we use to do before).
+			DOMResult result = docDocType == null ? new DOMResult() : new DOMResult(docDocType);
+			transformer.transform(new DOMSource(source), result);
+			return result.getNode(); // pass the entire document
+		} 
+		catch (TransformerConfigurationException e) {
+			return transformError("couldn't create transformer object", e);
+		} 
+		catch (TransformerException e) {
+			return transformError("couldn't transform the source", e);
+		}
+    }
+
     public Node transform(File stylesheet, File source) {
-
 	try {
 	    Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
-	    
+		transformer.setErrorListener(new TransformErrorListener());
 	    DOMResult result = new DOMResult();
 	    transformer.transform(new StreamSource(source), result);
 	    return result.getNode().getFirstChild();
 	} catch (TransformerConfigurationException e) {
-	    logger.error("couldn't create transformer object: "+e.getMessageAndLocation());
-	    logger.error(e.getLocationAsString());  
-	    String message = "TransformerConfigurationException: "+e.getMessageAndLocation();
-	    return constructErrorXHTMLPage(message);
-	} catch (TransformerException e) {
-	    logger.error("couldn't transform the source: " + e.getMessage());
-	    String message = "TransformerException: " + e.getMessageAndLocation();
-	    return constructErrorXHTMLPage(message);
+			return transformError("couldn't create transformer object", e);
+	} 
+	catch (TransformerException e) {
+		return transformError("couldn't transform the source", e);
 	}	
+ }
+ 
+    public Node transform(File stylesheet, File source, Document docDocType) {
+    	try {
+    	    Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
+			transformer.setErrorListener(new TransformErrorListener());
+    	    DOMResult result = new DOMResult(docDocType);
+    	    transformer.transform(new StreamSource(source), result);
+    	    return result.getNode().getFirstChild();
+    	} catch (TransformerConfigurationException e) {
+			return transformError("couldn't create transformer object", e);
+		} 
+		catch (TransformerException e) {
+			return transformError("couldn't transform the source", e);
+		}	
     }
     
+	// Given a heading string on the sort of transformation error that occurred and the exception object itself, 
+	// this method prints the exception to the tomcat window (system.err) and the greenstone log and then returns
+	// an xhtml error page that is constructed from it.
+   	protected Node transformError(String heading, TransformerException e) {
+		String message = heading + "\n" + e.getMessage();
+		logger.error(heading + ": " + e.getMessage());
+		
+		String location = e.getLocationAsString();		
+		if(location != null) {
+			logger.error(location);
+			message = message + "\n" + location;
+		}
+		System.err.println("****\n" + message + "\n****");
+		return constructErrorXHTMLPage(message);
+	}
     
-    protected Element constructErrorXHTMLPage(String message) {
+	// Given an error message, splits it into separate lines based on any newlines present and generates an xhtml page
+	// (xml Element)  with paragraphs for each line. This is then returned so that it can be displayed in the browser.
+    public static Element constructErrorXHTMLPage(String message) {
     	try{
+			String[] lines = message.split("\n");
+		
     		Document xhtmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
     		// <html></html>
@@ -235,7 +262,14 @@
     		h1Node.appendChild(headingString);
     		
-    		Node textNode = xhtmlDoc.createTextNode(message);
-    		bodyNode.appendChild(textNode);
-    		
+    		//Node textNode = xhtmlDoc.createTextNode(message);
+    		//bodyNode.appendChild(textNode);
+    		
+			for (int i = 0; i < lines.length; i++) {
+				Node pNode = xhtmlDoc.createElement("p");
+				Node textNode = xhtmlDoc.createTextNode(lines[i]);
+				pNode.appendChild(textNode);
+				bodyNode.appendChild(pNode);
+			}
+			
     		return xhtmlDoc.getDocumentElement();
     		
@@ -249,5 +283,48 @@
     }
     
+	// ErrorListener class that can be used to register a handler for any fatal errors, errors and warnings that may
+	// occur when transforming an xml file with an xslt stylesheet. The errors are printed both to the greenstone.log and 
+	// to the tomcat console (System.err), and the error message is stored in the errorMessage variable so that it can
+	// be retrieved and be used to generate an xhtml error page.
+	static public class TransformErrorListener implements ErrorListener {
+		protected String errorMessage = null;
+	
+		//  Receive notification of a recoverable error.
+		public void error(TransformerException exception) {
+			handleError("Error:\n", exception);
+		}
+        //   Receive notification of a non-recoverable error.
+		public void fatalError(TransformerException exception) {
+			handleError("Fatal Error:\n", exception);
+		}
+		// Receive notification of a warning.
+        public void warning(TransformerException exception) {
+			handleError("Warning:\n", exception);
+        }
+		
+		public String toString(TransformerException e) {
+			String location = e.getLocationAsString();
+			if(location == null) {
+				return e.getMessage();
+			}
+			return e.getMessage() + "\n" + location;
+		}
+		
+		// clears the errorPage variable after first call to this method
+		public String getErrorMessage() {
+			String errMsg = this.errorMessage;
+			if(this.errorMessage != null) {
+				this.errorMessage = null;
+			}
+			return errMsg;
+		}
+		
+		// sets the errorMessage member variable to the data stored in the exception
+		// and writes the errorMessage to the logger and tomcat's System.err
+		protected void handleError(String errorType, TransformerException exception) {
+			this.errorMessage = errorType + toString(exception); 
+			System.err.println("\n****Error transforming xml:\n" + this.errorMessage + "\n****\n");
+			logger.error(this.errorMessage);
+		}
+	}
 }
-
- 
