001: /*
002: * $Id: IfRegexp.java,v 1.1 2003/08/17 06:06:13 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.ifops;
025:
026: import java.util.*;
027:
028: import org.apache.oro.text.regex.*;
029: import org.w3c.dom.*;
030:
031: import org.ofbiz.base.util.*;
032: import org.ofbiz.minilang.*;
033: import org.ofbiz.minilang.method.*;
034:
035: /**
036: * Iff the specified field complies with the pattern specified by the regular expression, process sub-operations
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.1 $
040: * @since 2.0
041: */
042: public class IfRegexp extends MethodOperation {
043:
044: public static final String module = IfRegexp.class.getName();
045:
046: static PatternMatcher matcher = new Perl5Matcher();
047: static PatternCompiler compiler = new Perl5Compiler();
048:
049: List subOps = new LinkedList();
050: List elseSubOps = null;
051:
052: ContextAccessor mapAcsr;
053: ContextAccessor fieldAcsr;
054:
055: Pattern pattern = null;
056: String expr;
057:
058: public IfRegexp(Element element, SimpleMethod simpleMethod) {
059: super (element, simpleMethod);
060: this .mapAcsr = new ContextAccessor(element
061: .getAttribute("map-name"));
062: this .fieldAcsr = new ContextAccessor(element
063: .getAttribute("field-name"));
064:
065: this .expr = element.getAttribute("expr");
066: try {
067: pattern = compiler.compile(expr);
068: } catch (MalformedPatternException e) {
069: Debug.logError(e, module);
070: }
071:
072: SimpleMethod.readOperations(element, subOps, simpleMethod);
073:
074: Element elseElement = UtilXml
075: .firstChildElement(element, "else");
076:
077: if (elseElement != null) {
078: elseSubOps = new LinkedList();
079: SimpleMethod.readOperations(elseElement, elseSubOps,
080: simpleMethod);
081: }
082: }
083:
084: public boolean exec(MethodContext methodContext) {
085: // if conditions fails, always return true; if a sub-op returns false
086: // return false and stop, otherwise return true
087:
088: String fieldString = null;
089: Object fieldVal = null;
090:
091: if (!mapAcsr.isEmpty()) {
092: Map fromMap = (Map) mapAcsr.get(methodContext);
093: if (fromMap == null) {
094: if (Debug.infoOn())
095: Debug.logInfo("Map not found with name " + mapAcsr
096: + ", using empty string for comparison",
097: module);
098: } else {
099: fieldVal = fieldAcsr.get(fromMap, methodContext);
100: }
101: } else {
102: // no map name, try the env
103: fieldVal = fieldAcsr.get(methodContext);
104: }
105:
106: if (fieldVal != null) {
107: try {
108: fieldString = (String) ObjectType.simpleTypeConvert(
109: fieldVal, "String", null, null);
110: } catch (GeneralException e) {
111: Debug
112: .logError(
113: e,
114: "Could not convert object to String, using empty String",
115: module);
116: }
117: }
118: // always use an empty string by default
119: if (fieldString == null)
120: fieldString = "";
121:
122: if (matcher.matches(fieldString, pattern)) {
123: return SimpleMethod.runSubOps(subOps, methodContext);
124: } else {
125: if (elseSubOps != null) {
126: return SimpleMethod
127: .runSubOps(elseSubOps, methodContext);
128: } else {
129: return true;
130: }
131: }
132: }
133: }
|