1 11 package org.apache.catalina.ssi; 12 13 14 import java.io.PrintWriter ; 15 import java.text.ParseException ; 16 23 public class SSIConditional implements SSICommand { 24 27 public long process(SSIMediator ssiMediator, String commandName, 28 String [] paramNames, String [] paramValues, PrintWriter writer) 29 throws SSIStopProcessingException { 30 long lastModified = System.currentTimeMillis(); 32 SSIConditionalState state = ssiMediator.getConditionalState(); 34 if ("if".equalsIgnoreCase(commandName)) { 35 if (state.processConditionalCommandsOnly) { 38 state.nestingCount++; 39 return lastModified; 40 } 41 state.nestingCount = 0; 42 if (evaluateArguments(paramNames, paramValues, ssiMediator)) { 44 state.branchTaken = true; 46 } else { 47 state.processConditionalCommandsOnly = true; 49 state.branchTaken = false; 50 } 51 } else if ("elif".equalsIgnoreCase(commandName)) { 52 if (state.nestingCount > 0) return lastModified; 55 if (state.branchTaken) { 58 state.processConditionalCommandsOnly = true; 59 return lastModified; 60 } 61 if (evaluateArguments(paramNames, paramValues, ssiMediator)) { 63 state.processConditionalCommandsOnly = false; 65 state.branchTaken = true; 66 } else { 67 state.processConditionalCommandsOnly = true; 69 state.branchTaken = false; 70 } 71 } else if ("else".equalsIgnoreCase(commandName)) { 72 if (state.nestingCount > 0) return lastModified; 75 state.processConditionalCommandsOnly = state.branchTaken; 78 state.branchTaken = true; 81 } else if ("endif".equalsIgnoreCase(commandName)) { 82 if (state.nestingCount > 0) { 85 state.nestingCount--; 86 return lastModified; 87 } 88 state.processConditionalCommandsOnly = false; 90 state.branchTaken = true; 94 } else { 95 throw new SSIStopProcessingException(); 96 } 99 return lastModified; 100 } 101 102 103 107 private boolean evaluateArguments(String [] names, String [] values, 108 SSIMediator ssiMediator) throws SSIStopProcessingException { 109 String expr = getExpression(names, values); 110 if (expr == null) { 111 throw new SSIStopProcessingException(); 112 } 114 try { 115 ExpressionParseTree tree = new ExpressionParseTree(expr, 116 ssiMediator); 117 return tree.evaluateTree(); 118 } catch (ParseException e) { 119 throw new SSIStopProcessingException(); 121 } 122 } 123 124 125 129 private String getExpression(String [] paramNames, String [] paramValues) { 130 if ("expr".equalsIgnoreCase(paramNames[0])) return paramValues[0]; 131 return null; 132 } 133 } | Popular Tags |