KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openinventions > webappfilter > processor > Match


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 JavaDoc {
12         // combination
13
log.debug("numChildren = " + context.getNumElements("child::*"));
14         String JavaDoc 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 JavaDoc("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 JavaDoc {
46         StateXPath stateXPath = (StateXPath) state.get("com.openinventions.webappfilter.util.StateXPath");
47         String JavaDoc xpath = context.getValue("child::*[" + xpathPos + "]");
48         String JavaDoc value = stateXPath.getValue(state, xpath);
49         boolean matches = false;
50
51         String JavaDoc conditionName = context.getValue("name(child::*[" + conditionPos + "])");
52         String JavaDoc 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 /* ====================================================================
81  * The webappfilter License, Version 1.1
82  *
83  * Copyright (c) 2002 Ivar Chan. All rights
84  * reserved.
85  *
86  * Redistribution and use in source and binary forms, with or without
87  * modification, are permitted provided that the following conditions
88  * are met:
89  *
90  * 1. Redistributions of source code must retain the above copyright
91  * notice, this list of conditions and the following disclaimer.
92  *
93  * 2. Redistributions in binary form must reproduce the above copyright
94  * notice, this list of conditions and the following disclaimer in
95  * the documentation and/or other materials provided with the
96  * distribution.
97  *
98  * 3. The end-user documentation included with the redistribution,
99  * if any, must include the following acknowledgment:
100  * "This product includes software developed by
101  * Ivar Chan (http://www.openinventions.com/webappfilter/)."
102  * Alternately, this acknowledgment may appear in the software itself,
103  * if and wherever such third-party acknowledgments normally appear.
104  *
105  * 4. The name "webappfilter" must not be used to endorse or promote products
106  * derived from this software without prior written permission. For
107  * written permission, please contact ivarchan@acm.org.
108  *
109  * 5. Products derived from this software may not be called "webappfilter",
110  * nor may "webappfilter" appear in their name, without
111  * prior written permission of Ivar Chan.
112  *
113  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
114  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
115  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
116  * DISCLAIMED. IN NO EVENT SHALL THE IVAR CHAN BE LIABLE FOR ANY
117  * DIRECT, INDIRECT, INCIDENTAL,
118  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
119  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
120  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
121  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
122  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
123  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124  * SUCH DAMAGE.
125  * ====================================================================
126  *
127  * This software consists of voluntary contributions made by many
128  * individuals. For more information on webappfilter, please see
129  * <http://www.openinventions/webappfilter/>.
130  */

131
Popular Tags