001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.conditional;
019:
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.oro.text.regex.MalformedPatternException;
025: import org.apache.oro.text.regex.Pattern;
026: import org.apache.oro.text.regex.PatternCompiler;
027: import org.apache.oro.text.regex.PatternMatcher;
028: import org.apache.oro.text.regex.Perl5Compiler;
029: import org.apache.oro.text.regex.Perl5Matcher;
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.GeneralException;
032: import org.ofbiz.base.util.ObjectType;
033: import org.ofbiz.base.util.string.FlexibleStringExpander;
034: import org.ofbiz.minilang.SimpleMethod;
035: import org.ofbiz.minilang.method.ContextAccessor;
036: import org.ofbiz.minilang.method.MethodContext;
037: import org.w3c.dom.Element;
038:
039: /**
040: * Implements compare to a constant condition.
041: */
042: public class RegexpCondition implements Conditional {
043:
044: public static final String module = RegexpCondition.class.getName();
045:
046: SimpleMethod simpleMethod;
047:
048: static PatternMatcher matcher = new Perl5Matcher();
049: static PatternCompiler compiler = new Perl5Compiler();
050:
051: List subOps = new LinkedList();
052: List elseSubOps = null;
053:
054: ContextAccessor mapAcsr;
055: ContextAccessor fieldAcsr;
056:
057: FlexibleStringExpander exprExdr;
058:
059: public RegexpCondition(Element element, SimpleMethod simpleMethod) {
060: this .simpleMethod = simpleMethod;
061:
062: this .mapAcsr = new ContextAccessor(element
063: .getAttribute("map-name"));
064: this .fieldAcsr = new ContextAccessor(element
065: .getAttribute("field-name"));
066:
067: this .exprExdr = new FlexibleStringExpander(element
068: .getAttribute("expr"));
069: }
070:
071: public boolean checkCondition(MethodContext methodContext) {
072: String fieldString = getFieldString(methodContext);
073:
074: Pattern pattern = null;
075: try {
076: pattern = compiler.compile(methodContext
077: .expandString(this .exprExdr));
078: } catch (MalformedPatternException e) {
079: Debug.logError(e, "Regular Expression [" + this .exprExdr
080: + "] is mal-formed: " + e.toString(), module);
081: }
082:
083: if (matcher.matches(fieldString, pattern)) {
084: //Debug.logInfo("The string [" + fieldString + "] matched the pattern expr [" + pattern.getPattern() + "]", module);
085: return true;
086: } else {
087: //Debug.logInfo("The string [" + fieldString + "] did NOT match the pattern expr [" + pattern.getPattern() + "]", module);
088: return false;
089: }
090: }
091:
092: protected String getFieldString(MethodContext methodContext) {
093: String fieldString = null;
094: Object fieldVal = null;
095:
096: if (!mapAcsr.isEmpty()) {
097: Map fromMap = (Map) mapAcsr.get(methodContext);
098: if (fromMap == null) {
099: if (Debug.infoOn())
100: Debug.logInfo("Map not found with name " + mapAcsr
101: + ", using empty string for comparison",
102: module);
103: } else {
104: fieldVal = fieldAcsr.get(fromMap, methodContext);
105: }
106: } else {
107: // no map name, try the env
108: fieldVal = fieldAcsr.get(methodContext);
109: }
110:
111: if (fieldVal != null) {
112: try {
113: fieldString = (String) ObjectType.simpleTypeConvert(
114: fieldVal, "String", null, null);
115: } catch (GeneralException e) {
116: Debug
117: .logError(
118: e,
119: "Could not convert object to String, using empty String",
120: module);
121: }
122: }
123: // always use an empty string by default
124: if (fieldString == null)
125: fieldString = "";
126:
127: return fieldString;
128: }
129:
130: public void prettyPrint(StringBuffer messageBuffer,
131: MethodContext methodContext) {
132: messageBuffer.append("regexp[");
133: messageBuffer.append("[");
134: if (!this .mapAcsr.isEmpty()) {
135: messageBuffer.append(this .mapAcsr);
136: messageBuffer.append(".");
137: }
138: messageBuffer.append(this .fieldAcsr);
139: messageBuffer.append("=");
140: messageBuffer.append(getFieldString(methodContext));
141: messageBuffer.append("] matches ");
142: messageBuffer.append(methodContext.expandString(this .exprExdr));
143: messageBuffer.append("]");
144: }
145: }
|