greenstone.org greenstone wiki greenstone trac planet greenstone

Changeset 17024

Show
Ignore:
Timestamp:
2008-08-27 18:14:55 (4 months ago)
Author:
max
Message:

When something goes wrong, send a document containing the error message in HTML instead of a null document.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/util/XMLTransformer.java

    r13270 r17024  
    3030import javax.xml.transform.dom.DOMResult; 
    3131 
     32import javax.xml.parsers.DocumentBuilderFactory; 
     33import javax.xml.parsers.DocumentBuilder; 
     34import org.w3c.dom.Element; 
    3235import org.w3c.dom.Document; 
     36 
    3337import org.w3c.dom.Node; 
    3438import org.w3c.dom.NodeList; 
     
    4246import java.util.Map; 
    4347import java.util.Iterator; 
     48 
     49import org.apache.xml.utils.DefaultErrorHandler; 
    4450 
    4551import org.apache.log4j.*; 
     
    152158            // Use the TransformerFactory to process the stylesheet Source and generate a Transformer. 
    153159            Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet)); 
     160             
    154161            if (parameters != null) { 
    155162                Set params = parameters.entrySet(); 
     
    165172            return result.getNode().getFirstChild(); 
    166173        } catch (TransformerConfigurationException e) { 
    167             logger.error("couldn't create transformer object: "+e.getMessageAndLocation()); 
    168             logger.error(e.getLocationAsString());   
    169             return null; 
    170         } catch (TransformerException e) { 
    171             logger.error("couldn't transform the source: " + e.getMessage()); 
    172             return null; 
    173         }        
    174          
    175  
    176     } 
    177  
     174                String message = "TransformerConfigurationException: "+e.getMessageAndLocation(); 
     175                //System.err.println(message); 
     176                logger.error("couldn't create transformer object: "+e.getMessageAndLocation()); 
     177            logger.error(e.getLocationAsString());   
     178            return constructErrorXHTMLPage(message); 
     179        } catch (TransformerException e) { 
     180                String message = "TransformerException: " + e.getMessageAndLocation(); 
     181                //System.err.println(message); 
     182                logger.error("couldn't transform the source: " + e.getMessage()); 
     183            return constructErrorXHTMLPage(message); 
     184        }        
     185         
     186 
     187    } 
     188 
     189 
     190     
    178191    public Node transform(File stylesheet, File source) { 
    179192 
     
    187200            logger.error("couldn't create transformer object: "+e.getMessageAndLocation()); 
    188201            logger.error(e.getLocationAsString());   
    189             return null; 
     202            String message = "TransformerConfigurationException: "+e.getMessageAndLocation(); 
     203            return constructErrorXHTMLPage(message); 
    190204        } catch (TransformerException e) { 
    191205            logger.error("couldn't transform the source: " + e.getMessage()); 
    192             return null; 
    193         }        
    194     } 
     206            String message = "TransformerException: " + e.getMessageAndLocation(); 
     207            return constructErrorXHTMLPage(message); 
     208        }        
     209    } 
     210     
     211     
     212    protected Element constructErrorXHTMLPage(String message) { 
     213        try{ 
     214                Document xhtmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 
     215                // <html></html> 
     216                Node htmlNode = xhtmlDoc.createElement("html"); 
     217                xhtmlDoc.appendChild(htmlNode); 
     218                // <head></head> 
     219                Node headNode = xhtmlDoc.createElement("head"); 
     220                htmlNode.appendChild(headNode); 
     221                // <title></title> 
     222                Node titleNode = xhtmlDoc.createElement("title"); 
     223                headNode.appendChild(titleNode); 
     224                Node titleString = xhtmlDoc.createTextNode("Error occurred"); 
     225                titleNode.appendChild(titleString); 
     226                 
     227                // <body></body> 
     228                Node bodyNode = xhtmlDoc.createElement("body"); 
     229                htmlNode.appendChild(bodyNode); 
     230                 
     231                // finally put the message in the body 
     232                Node h1Node = xhtmlDoc.createElement("h1"); 
     233                bodyNode.appendChild(h1Node); 
     234                Node headingString = xhtmlDoc.createTextNode("The following error occurred:"); 
     235                h1Node.appendChild(headingString); 
     236                 
     237                Node textNode = xhtmlDoc.createTextNode(message); 
     238                bodyNode.appendChild(textNode); 
     239                 
     240                return xhtmlDoc.getDocumentElement(); 
     241                 
     242        }catch(Exception e) { 
     243                String errmsg = "Exception trying to construct error xhtml page from message: " + message 
     244                        + "\n" + e.getMessage(); 
     245                System.err.println(errmsg); 
     246                logger.error(errmsg); 
     247                return null; 
     248        } 
     249    } 
     250     
    195251} 
    196252