Coverage Report - org.melati.template.webmacro.PassbackEvaluationExceptionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
PassbackEvaluationExceptionHandler
0%
0/19
0%
0/14
3.6
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/template/webmacro/PassbackEvaluationExceptionHandler.java,v $
 3  
  * $Revision: 1.8 $
 4  
  *
 5  
  * Copyright (C) 2000 Tim Joyce
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     Tim Joyce <timj At paneris.org>
 42  
  */
 43  
 
 44  
 package org.melati.template.webmacro;
 45  
 
 46  
 import java.io.IOException;
 47  
 
 48  
 import org.melati.template.MarkupLanguage;
 49  
 
 50  
 import org.webmacro.PropertyException;
 51  
 import org.webmacro.Context;
 52  
 import org.webmacro.Broker;
 53  
 import org.webmacro.engine.EvaluationExceptionHandler;
 54  
 import org.webmacro.engine.Variable;
 55  
 import org.webmacro.util.Settings;
 56  
 
 57  
 /**
 58  
  * An implementation of EvaluationExceptionHandler which attempts to use a 
 59  
  * templet to render the exception.
 60  
  */
 61  0
 public class PassbackEvaluationExceptionHandler 
 62  
   implements EvaluationExceptionHandler {
 63  
 
 64  
   /**
 65  
    * {@inheritDoc}
 66  
    * @see org.webmacro.engine.EvaluationExceptionHandler#
 67  
    *      init(org.webmacro.Broker, org.webmacro.util.Settings)
 68  
    */
 69  0
   public void init(Broker b, Settings config) {}
 70  
 
 71  
   /**
 72  
    * {@inheritDoc}
 73  
    * @see org.webmacro.engine.EvaluationExceptionHandler#
 74  
    * evaluate(org.webmacro.engine.Variable, org.webmacro.Context, java.lang.Exception)
 75  
    */
 76  
   public void evaluate(Variable variable, 
 77  
                         Context context, 
 78  
                         Exception problem) 
 79  
    throws PropertyException {
 80  0
      if (problem instanceof PropertyException.NoSuchVariableException
 81  
       || problem instanceof PropertyException.NullValueException
 82  
       || problem instanceof PropertyException.NullToStringException) 
 83  0
        return;
 84  0
      throw new PropertyException("Failed to evaluate " + 
 85  
      variable.getVariableName() + ": " + problem,problem);
 86  
    }
 87  
 
 88  
   /**
 89  
    * {@inheritDoc}
 90  
    * @see org.webmacro.engine.EvaluationExceptionHandler#
 91  
    * expand(org.webmacro.engine.Variable, 
 92  
    *        org.webmacro.Context, java.lang.Exception)
 93  
    */
 94  
   public String expand(Variable variable, 
 95  
                         Context context, 
 96  
                         Exception problem) 
 97  
    throws PropertyException {
 98  0
      MarkupLanguage ml = (MarkupLanguage)context.get("ml");
 99  0
      if (ml == null) throw new PropertyException(
 100  
        "Error, to use the Passback Evaluation Exception Handler, you must " +
 101  
        "place your MarkupLanguage in the context as $ml" + 
 102  
        variable.getVariableName() + ": " + problem, problem);
 103  0
      Throwable underlying = problem;
 104  0
      if (problem instanceof PropertyException) {
 105  0
        PropertyException prob = (PropertyException)problem;
 106  0
        if (prob.getCause() != null) { 
 107  0
          if (prob.getCause()  instanceof PropertyException)
 108  0
            prob = (PropertyException)prob.getCause();
 109  0
          underlying = prob.getCause();
 110  
        }
 111  
      }
 112  
      try {
 113  0
        return ml.rendered(underlying);
 114  0
      } catch (IOException e) {
 115  0
        throw new PropertyException(
 116  
          "The exception failed to render " + 
 117  
          variable.getVariableName() + ": " + e, e);
 118  
      }
 119  
    }
 120  
 
 121  
    /**
 122  
    * {@inheritDoc}
 123  
    * @see org.webmacro.engine.EvaluationExceptionHandler#warningString(java.lang.String)
 124  
    */
 125  
   public String warningString(String warningText) throws PropertyException {
 126  0
       throw new PropertyException("Evaluation warning: " + warningText);
 127  
    }
 128  
 
 129  
    /**
 130  
    * {@inheritDoc}
 131  
    * @see org.webmacro.engine.EvaluationExceptionHandler#errorString(java.lang.String)
 132  
    */
 133  
   public String errorString(String errorText) throws PropertyException {
 134  0
       throw new PropertyException("Evaluation error: " + errorText);
 135  
    }
 136  
 }
 137