Coverage Report - org.webmacro.engine.DebugEvaluationExceptionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DebugEvaluationExceptionHandler
0%
0/31
0%
0/12
2.125
 
 1  
 /*
 2  
  * Copyright (C) 1998-2001 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 package org.webmacro.engine;
 25  
 
 26  
 import org.slf4j.Logger;
 27  
 import org.slf4j.LoggerFactory;
 28  
 import org.webmacro.Broker;
 29  
 import org.webmacro.Context;
 30  
 import org.webmacro.PropertyException;
 31  
 import org.webmacro.util.Settings;
 32  
 
 33  
 import java.util.ArrayList;
 34  
 
 35  
 /**
 36  
  * An implementation of EvaluationExceptionHandler which throws an exception
 37  
  * whenever it is called.  The error is stored back into the webcontect and
 38  
  * could be handled by the templatewriter.
 39  
  *
 40  
  * We use this in case of a live-situation to mail the error, or in other
 41  
  * situations to display the error rightaway.
 42  
  *
 43  
  * Example:
 44  
  * <pre>
 45  
  * #if ( $Variable.isDefined("WMERROR") )
 46  
  * {
 47  
  *     Error(s):<HR>
 48  
  *     #foreach $item in $WMERROR
 49  
  *     {
 50  
  *         $item<BR>
 51  
  *     }
 52  
  * }
 53  
  * </pre>
 54  
  * This will generally cause the exception to be
 55  
  * displayed to the user -- useful for debugging.
 56  
  *
 57  
  * @author Marcel Huijkman (Thanks to Brian Goetz & Keats Kirsch)
 58  
  *
 59  
  *  @since  03-12-2001
 60  
  *  @version    17-07-2002
 61  
  *
 62  
  */
 63  
 public class DebugEvaluationExceptionHandler implements EvaluationExceptionHandler
 64  
 {
 65  
 
 66  0
     static Logger _log =  LoggerFactory.getLogger(DebugEvaluationExceptionHandler.class);
 67  
 
 68  
     public DebugEvaluationExceptionHandler ()
 69  0
     {
 70  0
     }
 71  
 
 72  
     public DebugEvaluationExceptionHandler (Broker b)
 73  0
     {
 74  0
         init(b, b.getSettings());
 75  0
     }
 76  
 
 77  
     public void init (Broker b, Settings config)
 78  
     {
 79  0
     }
 80  
 
 81  
     public void evaluate (Variable variable, Context context, Exception problem) throws PropertyException
 82  
     {
 83  0
         handleError(variable, context, problem);
 84  0
     }
 85  
 
 86  
     public String expand (Variable variable, Context context, Exception problem) throws PropertyException
 87  
     {
 88  0
         return handleError(variable, context, problem);
 89  
     }
 90  
 
 91  
 
 92  
     private String handleError (Variable variable, Context context, Exception problem) throws PropertyException
 93  
     {
 94  
         String strError;
 95  
 
 96  0
         ArrayList arlErrors = null;
 97  0
         PropertyException propEx = null;
 98  0
         if (problem instanceof PropertyException)
 99  
         {
 100  0
             propEx = (PropertyException) problem;
 101  
         }
 102  
         else
 103  
         {
 104  0
             propEx = new PropertyException("Error expanding $" + variable.getVariableName());
 105  
         }
 106  0
         propEx.setContextLocation(context.getCurrentLocation());
 107  0
         strError = propEx.getMessage();
 108  
 
 109  0
         if ((context.containsKey("WMERROR")) && (context.get("WMERROR") instanceof ArrayList))
 110  
         {
 111  0
             arlErrors = (ArrayList) context.get("WMERROR");
 112  
         }
 113  
         else
 114  
         {
 115  0
             arlErrors = new ArrayList();
 116  0
             context.put("WMERROR", arlErrors);
 117  
         }
 118  
 
 119  0
         if (strError.lastIndexOf("\r\n") >= 0)
 120  
         {
 121  0
             strError = strError.substring(strError.lastIndexOf("\r\n"));
 122  0
             strError = strError.trim();
 123  
         }
 124  
 
 125  0
         if (!arlErrors.contains(strError))
 126  
         {
 127  0
             arlErrors.add(strError);
 128  
         }
 129  
 
 130  0
         if (_log != null)
 131  
         {
 132  0
             _log.warn(strError, propEx);
 133  
         }
 134  
         // and rethrow it
 135  0
         throw propEx;
 136  
     }
 137  
 
 138  
 
 139  
     public String warningString (String strText) throws PropertyException
 140  
     {
 141  0
         throw new PropertyException("Evaluation warning: " + strText);
 142  
     }
 143  
 
 144  
 
 145  
     public String errorString (String strText) throws PropertyException
 146  
     {
 147  0
         throw new PropertyException("Evaluation error: " + strText);
 148  
     }
 149  
 }
 150