001: /*
002: * $Id: RegexpCondition.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.conditional;
025:
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.oro.text.regex.MalformedPatternException;
031: import org.apache.oro.text.regex.Pattern;
032: import org.apache.oro.text.regex.PatternCompiler;
033: import org.apache.oro.text.regex.PatternMatcher;
034: import org.apache.oro.text.regex.Perl5Compiler;
035: import org.apache.oro.text.regex.Perl5Matcher;
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.GeneralException;
038: import org.ofbiz.base.util.ObjectType;
039: import org.ofbiz.minilang.SimpleMethod;
040: import org.ofbiz.minilang.method.ContextAccessor;
041: import org.ofbiz.minilang.method.MethodContext;
042: import org.w3c.dom.Element;
043:
044: /**
045: * Implements compare to a constant condition.
046: *
047: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
048: * @version $Revision: 1.1 $
049: * @since 2.1
050: */
051: public class RegexpCondition implements Conditional {
052:
053: public static final String module = RegexpCondition.class.getName();
054:
055: SimpleMethod simpleMethod;
056:
057: static PatternMatcher matcher = new Perl5Matcher();
058: static PatternCompiler compiler = new Perl5Compiler();
059:
060: List subOps = new LinkedList();
061: List elseSubOps = null;
062:
063: ContextAccessor mapAcsr;
064: ContextAccessor fieldAcsr;
065:
066: Pattern pattern = null;
067: String expr;
068:
069: public RegexpCondition(Element element, SimpleMethod simpleMethod) {
070: this .simpleMethod = simpleMethod;
071:
072: this .mapAcsr = new ContextAccessor(element
073: .getAttribute("map-name"));
074: this .fieldAcsr = new ContextAccessor(element
075: .getAttribute("field-name"));
076:
077: this .expr = element.getAttribute("expr");
078: try {
079: pattern = compiler.compile(expr);
080: } catch (MalformedPatternException e) {
081: Debug.logError(e, module);
082: }
083: }
084:
085: public boolean checkCondition(MethodContext methodContext) {
086: String fieldString = null;
087: Object fieldVal = null;
088:
089: if (!mapAcsr.isEmpty()) {
090: Map fromMap = (Map) mapAcsr.get(methodContext);
091: if (fromMap == null) {
092: if (Debug.infoOn())
093: Debug.logInfo("Map not found with name " + mapAcsr
094: + ", using empty string for comparison",
095: module);
096: } else {
097: fieldVal = fieldAcsr.get(fromMap, methodContext);
098: }
099: } else {
100: // no map name, try the env
101: fieldVal = fieldAcsr.get(methodContext);
102: }
103:
104: if (fieldVal != null) {
105: try {
106: fieldString = (String) ObjectType.simpleTypeConvert(
107: fieldVal, "String", null, null);
108: } catch (GeneralException e) {
109: Debug
110: .logError(
111: e,
112: "Could not convert object to String, using empty String",
113: module);
114: }
115: }
116: // always use an empty string by default
117: if (fieldString == null)
118: fieldString = "";
119:
120: if (matcher.matches(fieldString, pattern)) {
121: return true;
122: } else {
123: return false;
124: }
125: }
126: }
|