Coverage Report - org.kuali.rice.kew.mail.EmailStyleHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
EmailStyleHelper
0%
0/21
0%
0/4
8
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.mail;
 17  
 
 18  
 import org.kuali.rice.core.mail.EmailContent;
 19  
 import org.kuali.rice.core.util.xml.XmlJotter;
 20  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 21  
 import org.w3c.dom.Document;
 22  
 import org.w3c.dom.Node;
 23  
 
 24  
 import javax.xml.transform.Templates;
 25  
 import javax.xml.transform.TransformerException;
 26  
 import javax.xml.transform.dom.DOMResult;
 27  
 import javax.xml.transform.dom.DOMSource;
 28  
 import javax.xml.xpath.XPath;
 29  
 import javax.xml.xpath.XPathConstants;
 30  
 import javax.xml.xpath.XPathExpressionException;
 31  
 import javax.xml.xpath.XPathFactory;
 32  
 
 33  
 
 34  
 /**
 35  
  * A class which has some convenience methods for handling Emails and stylesheets. 
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  *
 39  
  */
 40  0
 public class EmailStyleHelper {
 41  
 
 42  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EmailStyleHelper.class);
 43  
     
 44  
     public EmailContent generateEmailContent(Templates style, Document document) {
 45  0
         DOMResult result = new DOMResult();
 46  0
         if (LOG.isDebugEnabled()) {
 47  0
             LOG.debug("Input document: " + XmlJotter.jotNode(document.getDocumentElement(), true));
 48  
         }
 49  
         try {
 50  0
             style.newTransformer().transform(new DOMSource(document), result);
 51  0
         } catch (TransformerException te) {
 52  0
             String message = "Error transforming immediate reminder DOM";
 53  0
             LOG.error(message, te);
 54  0
             throw new WorkflowRuntimeException(message, te);
 55  0
         }
 56  
 
 57  0
         Node node = result.getNode();
 58  
         
 59  0
         if (LOG.isDebugEnabled()) {
 60  0
             LOG.debug("Email document: " + XmlJotter.jotNode(document));
 61  
         }
 62  0
         XPathFactory xpf = XPathFactory.newInstance();
 63  0
         XPath xpath = xpf.newXPath();
 64  
         try {
 65  0
             String subject = (String) xpath.evaluate("/email/subject", node, XPathConstants.STRING);
 66  0
             String body = (String) xpath.evaluate("/email/body", node, XPathConstants.STRING);
 67  
             // simple heuristic to determine whether content is HTML
 68  0
             return new EmailContent(subject, body, body.matches("(?msi).*<(\\w+:)?html.*"));
 69  0
         } catch (XPathExpressionException xpee) {
 70  0
             throw new WorkflowRuntimeException("Error evaluating generated email content", xpee);
 71  
         }
 72  
     }
 73  
 
 74  
 }