1 package com.openinventions.webappfilter.processor; 2 3 import com.openinventions.metaframework.*; 4 import com.openinventions.webappfilter.util.*; 5 import java.util.regex.*; 6 import org.apache.commons.logging.*; 7 8 public class Match implements Processor { 9 private static final Log log = LogFactory.getLog(Match.class); 10 11 public void process(State state, Element context) throws Exception { 12 log.debug("numChildren = " + context.getNumElements("child::*")); 14 String combine = context.getValue("name(child::*[3])"); 15 if (combine.equals("and") || (combine.equals("or"))) { 16 if (combine.equals("and")) { 17 boolean first = eval(state, context, 1, 2); 18 boolean second = eval(state, context, 4, 5); 19 if (first && second) { 20 state.set("nextCommand", context.getPath() + "/child::*[6]"); 21 } else { 22 state.set("nextCommand", context.getPath() + "/parent::*/following-sibling::*[1]"); 23 } 24 } else if (combine.equals("or")) { 25 boolean first = eval(state, context, 1, 2); 26 boolean second = eval(state, context, 4, 5); 27 if (first || second) { 28 state.set("nextCommand", context.getPath() + "/child::*[6]"); 29 } else { 30 state.set("nextCommand", context.getPath() + "/parent::*/following-sibling::*[1]"); 31 } 32 } else { 33 log.error("command not supported " + combine); 34 throw new Exception ("command not supported " + combine); 35 } 36 } else { 37 if (eval(state, context, 1, 2)) { 38 state.set("nextCommand", context.getPath() + "/child::*[3]"); 39 } else { 40 state.set("nextCommand", context.getPath() + "/parent::*/following-sibling::*[1]"); 41 } 42 } 43 } 44 45 private boolean eval(State state, Element context, int xpathPos, int conditionPos) throws Exception { 46 StateXPath stateXPath = (StateXPath) state.get("com.openinventions.webappfilter.util.StateXPath"); 47 String xpath = context.getValue("child::*[" + xpathPos + "]"); 48 String value = stateXPath.getValue(state, xpath); 49 boolean matches = false; 50 51 String conditionName = context.getValue("name(child::*[" + conditionPos + "])"); 52 String conditionValue = context.getValue("child::*[" + conditionPos + "]"); 53 54 if (log.isDebugEnabled()) { 55 log.debug("xpath = " + xpath); 56 log.debug("value = " + value); 57 log.debug("conditionName = " + conditionName); 58 log.debug("conditionValue = " + conditionValue); 59 } 60 61 62 if (conditionName.equals("string")) { 63 if (value.equals(conditionValue)) { 64 return true; 65 } 66 } else if (conditionName.equals("regex")) { 67 Pattern pattern = Pattern.compile(conditionValue); 68 Matcher matcher = pattern.matcher(value); 69 70 if (matcher.matches()) { 71 return true; 72 } 73 } else { 74 log.debug("only string and regex match expressions supported"); 75 } 76 77 return false; 78 } 79 } 80 131 | Popular Tags |