KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > operation > ValidateMethod


1 /*
2  * $Id: ValidateMethod.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.operation;
25
26 import java.util.*;
27 import java.lang.reflect.*;
28
29 import org.w3c.dom.*;
30
31 import org.ofbiz.base.util.*;
32
33 /**
34  * A string operation that calls a validation method
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.0
39  */

40 public class ValidateMethod extends SimpleMapOperation {
41     
42     public static final String JavaDoc module = ValidateMethod.class.getName();
43     
44     String JavaDoc methodName;
45     String JavaDoc className;
46
47     public ValidateMethod(Element element, SimpleMapProcess simpleMapProcess) {
48         super(element, simpleMapProcess);
49         this.methodName = element.getAttribute("method");
50         this.className = element.getAttribute("class");
51     }
52
53     public void exec(Map inMap, Map results, List messages, Locale locale, ClassLoader JavaDoc loader) {
54         Object JavaDoc obj = inMap.get(fieldName);
55
56         String JavaDoc fieldValue = null;
57
58         try {
59             fieldValue = (String JavaDoc) ObjectType.simpleTypeConvert(obj, "String", null, locale);
60         } catch (GeneralException e) {
61             messages.add("Could not convert field value for comparison: " + e.getMessage());
62             return;
63         }
64
65         if (loader == null) {
66             loader = Thread.currentThread().getContextClassLoader();
67         }
68
69         Class JavaDoc[] paramTypes = new Class JavaDoc[] {String JavaDoc.class};
70         Object JavaDoc[] params = new Object JavaDoc[] {fieldValue};
71
72         Class JavaDoc valClass;
73
74         try {
75             valClass = loader.loadClass(className);
76         } catch (ClassNotFoundException JavaDoc cnfe) {
77             String JavaDoc msg = "Could not find validation class: " + className;
78
79             messages.add(msg);
80             Debug.logError("[ValidateMethod.exec] " + msg, module);
81             return;
82         }
83
84         Method valMethod;
85
86         try {
87             valMethod = valClass.getMethod(methodName, paramTypes);
88         } catch (NoSuchMethodException JavaDoc cnfe) {
89             String JavaDoc msg = "Could not find validation method: " + methodName + " of class " + className;
90
91             messages.add(msg);
92             Debug.logError("[ValidateMethod.exec] " + msg, module);
93             return;
94         }
95
96         Boolean JavaDoc resultBool = Boolean.FALSE;
97
98         try {
99             resultBool = (Boolean JavaDoc) valMethod.invoke(null, params);
100         } catch (Exception JavaDoc e) {
101             String JavaDoc msg = "Error in validation method " + methodName + " of class " + className + ": " + e.getMessage();
102
103             messages.add(msg);
104             Debug.logError("[ValidateMethod.exec] " + msg, module);
105             return;
106         }
107
108         if (!resultBool.booleanValue()) {
109             addMessage(messages, loader, locale);
110         }
111     }
112 }
113
Popular Tags