View Javadoc
1   /*
2    * $Source$
3    * $Revision$
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 org.webmacro.PropertyException;
47  import org.webmacro.Context;
48  import org.webmacro.Broker;
49  import org.webmacro.engine.EvaluationExceptionHandler;
50  import org.webmacro.engine.Variable;
51  import org.webmacro.util.Settings;
52  
53  /**
54   * An implementation of EvaluationExceptionHandler which throws an exception
55   * whenever it is called.  
56   * 
57   * This will generally cause the exception to be
58   * displayed to the user -- useful for debugging.
59   *
60   * Modified to allow #if ($null).
61   *
62   * @author Tim Joyce
63   * @since 2001-11-05
64   */
65  
66  public class PropagateEvaluationExceptionHandler 
67    implements EvaluationExceptionHandler {
68  
69     /**
70     * Constructor.
71     */
72    public PropagateEvaluationExceptionHandler() {
73     }
74  
75    /**
76     * Forced upon us by the interface, but unused.
77     */
78    @Override
79    public void init(Broker b, Settings config) {
80     }
81  
82    @Override
83    public void evaluate(Variable variable,
84                          Context context, 
85                          Exception problem) 
86     throws PropertyException {
87       if (problem instanceof PropertyException.NoSuchVariableException
88        || problem instanceof PropertyException.NullValueException
89        || problem instanceof PropertyException.NullToStringException) 
90         return;
91       if (problem instanceof PropertyException)
92         throw (PropertyException) problem;
93       else 
94         throw new PropertyException("Error evaluating variable " 
95                                     + variable.getVariableName() + ": " 
96                                     + problem, problem);
97     }
98  
99     /**
100    * {@inheritDoc}
101    * @see org.webmacro.engine.EvaluationExceptionHandler#expand(
102    *          org.webmacro.engine.Variable, org.webmacro.Context, java.lang.Exception)
103    */
104   public String expand(Variable variable, 
105                         Context context, 
106                         Exception problem) 
107    throws PropertyException {
108      if (problem instanceof PropertyException)
109        throw (PropertyException) problem;
110      else 
111        throw new PropertyException("Error evaluating variable " 
112                                    + variable.getVariableName() + ": " 
113                                    + problem, problem);
114    }
115 
116 
117    /**
118    * {@inheritDoc}
119    * @see org.webmacro.engine.EvaluationExceptionHandler#warningString(java.lang.String)
120    */
121   public String warningString(String warningText) throws PropertyException {
122       throw new PropertyException("Evaluation warning: " + warningText);
123    }
124 
125 
126    /**
127    * {@inheritDoc}
128    * 
129    * I do not believe that this can be exercised, without a bug in webmacro.
130    * @see org.webmacro.engine.EvaluationExceptionHandler#errorString(java.lang.String)
131    */
132   public String errorString(String errorText) throws PropertyException {
133       throw new PropertyException("Evaluation error: " + errorText);
134    }
135 }
136