01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.minilang.operation;
19:
20: import java.util.*;
21:
22: import org.apache.oro.text.regex.*;
23: import org.w3c.dom.*;
24:
25: import org.ofbiz.base.util.*;
26:
27: /**
28: * Validates the current field using a regular expression
29: */
30: public class Regexp extends SimpleMapOperation {
31:
32: public static final String module = Regexp.class.getName();
33:
34: static PatternMatcher matcher = new Perl5Matcher();
35: static PatternCompiler compiler = new Perl5Compiler();
36: Pattern pattern = null;
37: String expr;
38:
39: public Regexp(Element element, SimpleMapProcess simpleMapProcess) {
40: super (element, simpleMapProcess);
41: expr = element.getAttribute("expr");
42: try {
43: pattern = compiler.compile(expr);
44: } catch (MalformedPatternException e) {
45: Debug.logError(e, module);
46: }
47: }
48:
49: public void exec(Map inMap, Map results, List messages,
50: Locale locale, ClassLoader loader) {
51: Object obj = inMap.get(fieldName);
52:
53: String fieldValue = null;
54:
55: try {
56: fieldValue = (String) ObjectType.simpleTypeConvert(obj,
57: "String", null, locale);
58: } catch (GeneralException e) {
59: messages
60: .add("Could not convert field value for comparison: "
61: + e.getMessage());
62: return;
63: }
64:
65: if (pattern == null) {
66: messages.add("Could not compile regular expression \""
67: + expr + "\" for validation");
68: return;
69: }
70:
71: if (!matcher.matches(fieldValue, pattern)) {
72: addMessage(messages, loader, locale);
73: }
74: }
75: }
|