KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > IfInterceptor


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 scriptella.core;
17
18 import scriptella.configuration.Location;
19 import scriptella.configuration.ScriptingElement;
20 import scriptella.expression.Expression;
21
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27
28 /**
29  * Handles if expressions specified by if attribute on query/script elements.
30  *
31  * @author Fyodor Kupolov
32  * @version 1.0
33  * @see scriptella.expression.JexlExpression
34  */

35 public class IfInterceptor extends ElementInterceptor {
36     private static final Logger JavaDoc LOG = Logger.getLogger(IfInterceptor.class.getName());
37     private static final Set JavaDoc<CharSequence JavaDoc> trueStrs = new LinkedHashSet JavaDoc<CharSequence JavaDoc>();
38
39     static {
40         trueStrs.add("true");
41         trueStrs.add("yes");
42         trueStrs.add("1");
43         trueStrs.add("on");
44     }
45
46     private Expression expression;
47     private Location location;
48
49     public IfInterceptor(ExecutableElement next, ScriptingElement scr) {
50         super(next);
51         expression = Expression.compile(scr.getIf());
52         location = scr.getLocation();
53     }
54
55     public void execute(final DynamicContext ctx) {
56         boolean ok = false;
57
58         try {
59             final Object JavaDoc res = expression.evaluate(ctx);
60
61             if (res != null) {
62                 if (res instanceof Boolean JavaDoc) {
63                     ok = (Boolean JavaDoc) res;
64                 } else if (trueStrs.contains(String.valueOf(res))) {
65                     ok = true;
66                 }
67             }
68         } catch (Expression.EvaluationException e) {
69             LOG.log(Level.WARNING,
70                     "Unable to evaluate if condition \"" +
71                             expression.getExpression() + "\" for script " + location +
72                             ": " + e.getMessage(), e);
73         }
74
75         if (ok) { //if expr evaluated to true
76
executeNext(ctx);
77         } else {
78             if (LOG.isLoggable(Level.FINE)) {
79                 LOG.fine("if=\""+expression.getExpression()+"\" is false, element body is skipped.");
80             }
81         }
82     }
83
84     public static ExecutableElement prepare(
85             final ExecutableElement next, final ScriptingElement s) {
86         final String JavaDoc ifExpr = s.getIf();
87
88         if ((ifExpr == null) || (ifExpr.length() == 0)) {
89             return next;
90         }
91
92         return new IfInterceptor(next, s);
93     }
94 }
95
Popular Tags