KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ifops > IfRegexp


1 /*
2  * $Id: IfRegexp.java 6237 2005-12-02 10:49:47Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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.ifops;
25
26 import java.util.*;
27
28 import org.apache.oro.text.regex.*;
29 import org.w3c.dom.*;
30
31 import org.ofbiz.base.util.*;
32 import org.ofbiz.base.util.string.FlexibleStringExpander;
33 import org.ofbiz.minilang.*;
34 import org.ofbiz.minilang.method.*;
35
36 /**
37  * Iff the specified field complies with the pattern specified by the regular expression, process sub-operations
38  *
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @version $Rev: 6237 $
41  * @since 2.0
42  */

43 public class IfRegexp extends MethodOperation {
44     
45     public static final String JavaDoc module = IfRegexp.class.getName();
46
47     static PatternMatcher matcher = new Perl5Matcher();
48     static PatternCompiler compiler = new Perl5Compiler();
49
50     List subOps = new LinkedList();
51     List elseSubOps = null;
52
53     ContextAccessor mapAcsr;
54     ContextAccessor fieldAcsr;
55
56     FlexibleStringExpander exprExdr;
57
58     public IfRegexp(Element element, SimpleMethod simpleMethod) {
59         super(element, simpleMethod);
60         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
61         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
62
63         this.exprExdr = new FlexibleStringExpander(element.getAttribute("expr"));
64
65         SimpleMethod.readOperations(element, subOps, simpleMethod);
66
67         Element elseElement = UtilXml.firstChildElement(element, "else");
68
69         if (elseElement != null) {
70             elseSubOps = new LinkedList();
71             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
72         }
73     }
74
75     public boolean exec(MethodContext methodContext) {
76         // if conditions fails, always return true; if a sub-op returns false
77
// return false and stop, otherwise return true
78

79         String JavaDoc fieldString = null;
80         Object JavaDoc fieldVal = null;
81
82         if (!mapAcsr.isEmpty()) {
83             Map fromMap = (Map) mapAcsr.get(methodContext);
84             if (fromMap == null) {
85                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module);
86             } else {
87                 fieldVal = fieldAcsr.get(fromMap, methodContext);
88             }
89         } else {
90             // no map name, try the env
91
fieldVal = fieldAcsr.get(methodContext);
92         }
93
94         if (fieldVal != null) {
95             try {
96                 fieldString = (String JavaDoc) ObjectType.simpleTypeConvert(fieldVal, "String", null, null);
97             } catch (GeneralException e) {
98                 Debug.logError(e, "Could not convert object to String, using empty String", module);
99             }
100         }
101         // always use an empty string by default
102
if (fieldString == null) fieldString = "";
103
104         Pattern pattern = null;
105         try {
106             pattern = compiler.compile(methodContext.expandString(this.exprExdr));
107         } catch (MalformedPatternException e) {
108             Debug.logError(e, "Regular Expression [" + this.exprExdr + "] is mal-formed: " + e.toString(), module);
109         }
110
111         if (matcher.matches(fieldString, pattern)) {
112             return SimpleMethod.runSubOps(subOps, methodContext);
113         } else {
114             if (elseSubOps != null) {
115                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
116             } else {
117                 return true;
118             }
119         }
120     }
121
122     public String JavaDoc rawString() {
123         // TODO: add all attributes and other info
124
return "<if-regexp field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
125     }
126     public String JavaDoc expandedString(MethodContext methodContext) {
127         // TODO: something more than a stub/dummy
128
return this.rawString();
129     }
130 }
131
Popular Tags