KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ssi > SSIConditional


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation. Licensed under the
3  * Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the License
5  * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
6  * law or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */

11 package org.apache.catalina.ssi;
12
13
14 import java.io.PrintWriter JavaDoc;
15 import java.text.ParseException JavaDoc;
16 /**
17  * SSI command that handles all conditional directives.
18  *
19  * @version $Revision: 467222 $
20  * @author Paul Speed
21  * @author David Becker
22  */

23 public class SSIConditional implements SSICommand {
24     /**
25      * @see SSICommand
26      */

27     public long process(SSIMediator ssiMediator, String JavaDoc commandName,
28             String JavaDoc[] paramNames, String JavaDoc[] paramValues, PrintWriter JavaDoc writer)
29             throws SSIStopProcessingException {
30         // Assume anything using conditionals was modified by it
31
long lastModified = System.currentTimeMillis();
32         // Retrieve the current state information
33
SSIConditionalState state = ssiMediator.getConditionalState();
34         if ("if".equalsIgnoreCase(commandName)) {
35             // Do nothing if we are nested in a false branch
36
// except count it
37
if (state.processConditionalCommandsOnly) {
38                 state.nestingCount++;
39                 return lastModified;
40             }
41             state.nestingCount = 0;
42             // Evaluate the expression
43
if (evaluateArguments(paramNames, paramValues, ssiMediator)) {
44                 // No more branches can be taken for this if block
45
state.branchTaken = true;
46             } else {
47                 // Do not process this branch
48
state.processConditionalCommandsOnly = true;
49                 state.branchTaken = false;
50             }
51         } else if ("elif".equalsIgnoreCase(commandName)) {
52             // No need to even execute if we are nested in
53
// a false branch
54
if (state.nestingCount > 0) return lastModified;
55             // If a branch was already taken in this if block
56
// then disable output and return
57
if (state.branchTaken) {
58                 state.processConditionalCommandsOnly = true;
59                 return lastModified;
60             }
61             // Evaluate the expression
62
if (evaluateArguments(paramNames, paramValues, ssiMediator)) {
63                 // Turn back on output and mark the branch
64
state.processConditionalCommandsOnly = false;
65                 state.branchTaken = true;
66             } else {
67                 // Do not process this branch
68
state.processConditionalCommandsOnly = true;
69                 state.branchTaken = false;
70             }
71         } else if ("else".equalsIgnoreCase(commandName)) {
72             // No need to even execute if we are nested in
73
// a false branch
74
if (state.nestingCount > 0) return lastModified;
75             // If we've already taken another branch then
76
// disable output otherwise enable it.
77
state.processConditionalCommandsOnly = state.branchTaken;
78             // And in any case, it's safe to say a branch
79
// has been taken.
80
state.branchTaken = true;
81         } else if ("endif".equalsIgnoreCase(commandName)) {
82             // If we are nested inside a false branch then pop out
83
// one level on the nesting count
84
if (state.nestingCount > 0) {
85                 state.nestingCount--;
86                 return lastModified;
87             }
88             // Turn output back on
89
state.processConditionalCommandsOnly = false;
90             // Reset the branch status for any outer if blocks,
91
// since clearly we took a branch to have gotten here
92
// in the first place.
93
state.branchTaken = true;
94         } else {
95             throw new SSIStopProcessingException();
96             //throw new SsiCommandException( "Not a conditional command:" +
97
// cmdName );
98
}
99         return lastModified;
100     }
101
102
103     /**
104      * Retrieves the expression from the specified arguments and peforms the
105      * necessary evaluation steps.
106      */

107     private boolean evaluateArguments(String JavaDoc[] names, String JavaDoc[] values,
108             SSIMediator ssiMediator) throws SSIStopProcessingException {
109         String JavaDoc expr = getExpression(names, values);
110         if (expr == null) {
111             throw new SSIStopProcessingException();
112             //throw new SsiCommandException( "No expression specified." );
113
}
114         try {
115             ExpressionParseTree tree = new ExpressionParseTree(expr,
116                     ssiMediator);
117             return tree.evaluateTree();
118         } catch (ParseException JavaDoc e) {
119             //throw new SsiCommandException( "Error parsing expression." );
120
throw new SSIStopProcessingException();
121         }
122     }
123
124
125     /**
126      * Returns the "expr" if the arg name is appropriate, otherwise returns
127      * null.
128      */

129     private String JavaDoc getExpression(String JavaDoc[] paramNames, String JavaDoc[] paramValues) {
130         if ("expr".equalsIgnoreCase(paramNames[0])) return paramValues[0];
131         return null;
132     }
133 }
Popular Tags