KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > conditional > RegexpCondition


1 /*
2  * $Id: RegexpCondition.java 6237 2005-12-02 10:49:47Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.conditional;
25
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.oro.text.regex.MalformedPatternException;
31 import org.apache.oro.text.regex.Pattern;
32 import org.apache.oro.text.regex.PatternCompiler;
33 import org.apache.oro.text.regex.PatternMatcher;
34 import org.apache.oro.text.regex.Perl5Compiler;
35 import org.apache.oro.text.regex.Perl5Matcher;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.GeneralException;
38 import org.ofbiz.base.util.ObjectType;
39 import org.ofbiz.base.util.string.FlexibleStringExpander;
40 import org.ofbiz.minilang.SimpleMethod;
41 import org.ofbiz.minilang.method.ContextAccessor;
42 import org.ofbiz.minilang.method.MethodContext;
43 import org.w3c.dom.Element JavaDoc;
44
45 /**
46  * Implements compare to a constant condition.
47  *
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version $Rev: 6237 $
50  * @since 2.1
51  */

52 public class RegexpCondition implements Conditional {
53     
54     public static final String JavaDoc module = RegexpCondition.class.getName();
55     
56     SimpleMethod simpleMethod;
57     
58     static PatternMatcher matcher = new Perl5Matcher();
59     static PatternCompiler compiler = new Perl5Compiler();
60
61     List JavaDoc subOps = new LinkedList JavaDoc();
62     List JavaDoc elseSubOps = null;
63
64     ContextAccessor mapAcsr;
65     ContextAccessor fieldAcsr;
66
67     FlexibleStringExpander exprExdr;
68     
69     public RegexpCondition(Element JavaDoc element, SimpleMethod simpleMethod) {
70         this.simpleMethod = simpleMethod;
71         
72         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
73         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
74
75         this.exprExdr = new FlexibleStringExpander(element.getAttribute("expr"));
76     }
77
78     public boolean checkCondition(MethodContext methodContext) {
79         String JavaDoc fieldString = getFieldString(methodContext);
80
81         Pattern pattern = null;
82         try {
83             pattern = compiler.compile(methodContext.expandString(this.exprExdr));
84         } catch (MalformedPatternException e) {
85             Debug.logError(e, "Regular Expression [" + this.exprExdr + "] is mal-formed: " + e.toString(), module);
86         }
87
88         if (matcher.matches(fieldString, pattern)) {
89             //Debug.logInfo("The string [" + fieldString + "] matched the pattern expr [" + pattern.getPattern() + "]", module);
90
return true;
91         } else {
92             //Debug.logInfo("The string [" + fieldString + "] did NOT match the pattern expr [" + pattern.getPattern() + "]", module);
93
return false;
94         }
95     }
96     
97     protected String JavaDoc getFieldString(MethodContext methodContext) {
98         String JavaDoc fieldString = null;
99         Object JavaDoc fieldVal = null;
100
101         if (!mapAcsr.isEmpty()) {
102             Map JavaDoc fromMap = (Map JavaDoc) mapAcsr.get(methodContext);
103             if (fromMap == null) {
104                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module);
105             } else {
106                 fieldVal = fieldAcsr.get(fromMap, methodContext);
107             }
108         } else {
109             // no map name, try the env
110
fieldVal = fieldAcsr.get(methodContext);
111         }
112
113         if (fieldVal != null) {
114             try {
115                 fieldString = (String JavaDoc) ObjectType.simpleTypeConvert(fieldVal, "String", null, null);
116             } catch (GeneralException e) {
117                 Debug.logError(e, "Could not convert object to String, using empty String", module);
118             }
119         }
120         // always use an empty string by default
121
if (fieldString == null) fieldString = "";
122         
123         return fieldString;
124     }
125
126     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
127         messageBuffer.append("regexp[");
128         messageBuffer.append("[");
129         if (!this.mapAcsr.isEmpty()) {
130             messageBuffer.append(this.mapAcsr);
131             messageBuffer.append(".");
132         }
133         messageBuffer.append(this.fieldAcsr);
134         messageBuffer.append("=");
135         messageBuffer.append(getFieldString(methodContext));
136         messageBuffer.append("] matches ");
137         messageBuffer.append(methodContext.expandString(this.exprExdr));
138         messageBuffer.append("]");
139     }
140 }
141
Popular Tags