Coverage Report

##### Coverage Report - org.webmacro.engine.DefaultEvaluationExceptionHandler

Classes in this File | Line Coverage | Branch Coverage | Complexity
DefaultEvaluationExceptionHandler |
44% | 11/25
27% | 6/22
| 3.142857142857143;3.143

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 |  |
     /**
34 |  |
      * DefaultEvaluationExceptionHandler
35 |  |
      *
36 |  |
      * An implementation of EvaluationExceptionHandler which throws under most
37 |  |
      * error conditions.  Users who are generating non-HTML output
38 |  |
      * should replace the ExceptionHandler in their context with one that
39 |  |
      * generates the appropriate comments.  This should be the only place
40 |  |
      * in WM where HTML comments are generated into the output.
41 |  |
      *
42 |  |
      * @author Brian Goetz
43 |  |
      * @since 0.96
44 |  |
      */
45 |  |
46 |  |
     public class DefaultEvaluationExceptionHandler implements EvaluationExceptionHandler
47 |  |
     {
48 |  |
49 | 2 |
         static Logger _log =  LoggerFactory.getLogger(DefaultEvaluationExceptionHandler.class);
50 |  |
51 |  |
         public DefaultEvaluationExceptionHandler ()
52 | 6 |
         {
53 | 6 |
         }
54 |  |
55 |  |
         public DefaultEvaluationExceptionHandler (Broker b)
56 | 0 |
         {
57 | 0 |
             init(b, b.getSettings());
58 | 0 |
         }
59 |  |
60 |  |
         public void init (Broker b, Settings config)
61 |  |
         {
62 | 6 |
         }
63 |  |
64 |  |
         public void evaluate (Variable variable, Context context, Exception problem)
65 |  |
                 throws PropertyException
66 |  |
         {
67 |  |
68 |  |
             // if we were given a ProperyException, record the context location.
69 | 0 |
             if (problem instanceof PropertyException)
70 |  |
             {
71 | 0 |
                 ((PropertyException) problem)
72 |  |
                         .setContextLocation(context.getCurrentLocation());
73 |  |
             }
74 |  |
             else
75 |  |
             {
76 |  |
                 // wrap the exception in a PropertyException
77 | 0 |
                 problem = new PropertyException("Error evaluating $"
78 |  |
                         + variable.getVariableName(),
79 |  |
                         problem,
80 |  |
                         context.getCurrentLocation());
81 |  |
             }
82 |  |
83 |  |
84 |  |
             // log the warning message
85 | 0 |
             if (_log != null)
86 |  |
             {
87 | 0 |
                 _log.error(problem.getMessage());
88 |  |
             }
89 |  |
90 |  |
91 |  |
             // we want to silently ignore these exceptions
92 | 0 |
             if (problem instanceof PropertyException.NoSuchVariableException
93 |  |
                     || problem instanceof PropertyException.NullValueException
94 |  |
                     || problem instanceof PropertyException.NullToStringException)
95 |  |
             {
96 |  |
97 | 0 |
                 return;
98 |  |
             }
99 |  |
             else
100 |  |
             {
101 |  |
                 // but we need to complain about anything else
102 | 0 |
                 throw (PropertyException) problem;
103 |  |
104 |  |
             }
105 |  |
         }
106 |  |
107 |  |
         public String expand (Variable variable, Context context, Exception problem)
108 |  |
                 throws PropertyException
109 |  |
         {
110 |  |
111 |  |
             // if we were given a ProperyException, record the context location.
112 | 30 |
             if (problem instanceof PropertyException)
113 |  |
             {
114 | 30 |
                 ((PropertyException) problem)
115 |  |
                         .setContextLocation(context.getCurrentLocation());
116 |  |
             }
117 |  |
             else
118 |  |
             {
119 |  |
                 // wrap the exception in a PropertyException
120 | 0 |
                 problem = new PropertyException("Error expanding $"
121 |  |
                         + variable.getVariableName(),
122 |  |
                         problem,
123 |  |
                         context.getCurrentLocation());
124 |  |
             }
125 |  |
126 |  |
             // log the error message
127 | 30 |
             if (_log != null)
128 |  |
             {
129 | 30 |
                 _log.error(problem.getMessage());
130 |  |
             }
131 |  |
132 |  |
             // we just want to return an error message for these exceptions
133 | 30 |
             if (problem instanceof PropertyException.NoSuchVariableException
134 |  |
                     || problem instanceof PropertyException.UndefinedVariableException
135 |  |
                     || problem instanceof PropertyException.NullValueException
136 |  |
                     || problem instanceof PropertyException.NullToStringException)
137 |  |
             {
138 |  |
139 | 30 |
                 return errorString(problem.getMessage());
140 |  |
             }
141 |  |
             else
142 |  |
             {
143 |  |
                 // but we need to complain about anything else
144 | 0 |
                 throw (PropertyException) problem;
145 |  |
             }
146 |  |
         }
147 |  |
148 |  |
149 |  |
         public String warningString (String warningText)
150 |  |
         {
151 | 0 |
             return "<!-- " + warningText + " -->";
152 |  |
         }
153 |  |
154 |  |
155 |  |
         public String errorString (String errorText)
156 |  |
         {
157 | 30 |
             return "<!-- " + errorText + " -->";
158 |  |
         }
159 |  |
     }