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