KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ifops > CheckId


1 /*
2  * $Id: CheckId.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.method.ifops;
25
26 import java.util.*;
27
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32
33 /**
34  * Iff the given ID field is not valid the fail-message
35  * or fail-property sub-elements are used to add a message to the error-list.
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class CheckId extends MethodOperation {
42     
43     public static final String JavaDoc module = CheckId.class.getName();
44     
45     String JavaDoc message = null;
46     String JavaDoc propertyResource = null;
47     boolean isProperty = false;
48
49     ContextAccessor fieldAcsr;
50     ContextAccessor mapAcsr;
51     ContextAccessor errorListAcsr;
52
53     public CheckId(Element element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
56         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
57         this.errorListAcsr = new ContextAccessor(element.getAttribute("error-list-name"), "error_list");
58
59         //note: if no fail-message or fail-property then message will be null
60
Element failMessage = UtilXml.firstChildElement(element, "fail-message");
61         Element failProperty = UtilXml.firstChildElement(element, "fail-property");
62
63         if (failMessage != null) {
64             this.message = failMessage.getAttribute("message");
65             this.isProperty = false;
66         } else if (failProperty != null) {
67             this.propertyResource = failProperty.getAttribute("resource");
68             this.message = failProperty.getAttribute("property");
69             this.isProperty = true;
70         }
71     }
72
73     public boolean exec(MethodContext methodContext) {
74         boolean isValid = true;
75
76         List messages = (List) errorListAcsr.get(methodContext);
77         if (messages == null) {
78             messages = new LinkedList();
79             errorListAcsr.put(methodContext, messages);
80         }
81
82         Object JavaDoc fieldVal = null;
83         if (!mapAcsr.isEmpty()) {
84             Map fromMap = (Map) mapAcsr.get(methodContext);
85
86             if (fromMap == null) {
87                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module);
88             } else {
89                 fieldVal = fieldAcsr.get(fromMap, methodContext);
90             }
91         } else {
92             // no map name, try the env
93
fieldVal = fieldAcsr.get(methodContext);
94         }
95         
96         String JavaDoc fieldStr = fieldVal.toString();
97         StringBuffer JavaDoc errorDetails = new StringBuffer JavaDoc();
98         
99         //check various illegal characters, etc for ids
100
if (fieldStr.indexOf(' ') >= 0) {
101             isValid = false;
102             errorDetails.append("[space found at position " + (fieldStr.indexOf(' ') + 1) + "]");
103         }
104         if (fieldStr.indexOf('"') >= 0) {
105             isValid = false;
106             errorDetails.append("[double-quote found at position " + (fieldStr.indexOf('"') + 1) + "]");
107         }
108         if (fieldStr.indexOf('\'') >= 0) {
109             isValid = false;
110             errorDetails.append("[single-quote found at position " + (fieldStr.indexOf('\'') + 1) + "]");
111         }
112         if (fieldStr.indexOf('&') >= 0) {
113             isValid = false;
114             errorDetails.append("[ampersand found at position " + (fieldStr.indexOf('&') + 1) + "]");
115         }
116         if (fieldStr.indexOf('?') >= 0) {
117             isValid = false;
118             errorDetails.append("[question mark found at position " + (fieldStr.indexOf('?') + 1) + "]");
119         }
120         if (fieldStr.indexOf('<') >= 0) {
121             isValid = false;
122             errorDetails.append("[less-than sign found at position " + (fieldStr.indexOf('<') + 1) + "]");
123         }
124         if (fieldStr.indexOf('>') >= 0) {
125             isValid = false;
126             errorDetails.append("[greater-than sign found at position " + (fieldStr.indexOf('>') + 1) + "]");
127         }
128         if (fieldStr.indexOf('\\') >= 0) {
129             isValid = false;
130             errorDetails.append("[back-slash found at position " + (fieldStr.indexOf('\\') + 1) + "]");
131         }
132         if (fieldStr.indexOf('/') >= 0) {
133             isValid = false;
134             errorDetails.append("[forward-slash found at position " + (fieldStr.indexOf('/') + 1) + "]");
135         }
136
137         if (!isValid) {
138             this.addMessage(messages, methodContext, "The ID value in the field [" + fieldAcsr + "] was not valid", ": " + errorDetails.toString());
139         }
140
141         return true;
142     }
143
144     public void addMessage(List messages, MethodContext methodContext, String JavaDoc defaultMessage, String JavaDoc errorDetails) {
145         ClassLoader JavaDoc loader = methodContext.getLoader();
146         
147         String JavaDoc message = methodContext.expandString(this.message);
148         String JavaDoc propertyResource = methodContext.expandString(this.propertyResource);
149         
150         if (!isProperty && message != null) {
151             messages.add(message + errorDetails);
152         } else if (isProperty && propertyResource != null && message != null) {
153             //String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message);
154
String JavaDoc propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale());
155
156             if (propMsg == null || propMsg.length() == 0) {
157                 messages.add(defaultMessage + errorDetails);
158             } else {
159                 messages.add(methodContext.expandString(propMsg) + errorDetails);
160             }
161         } else {
162             messages.add(defaultMessage + errorDetails);
163         }
164     }
165
166     public String JavaDoc rawString() {
167         // TODO: add all attributes and other info
168
return "<check-id field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
169     }
170     public String JavaDoc expandedString(MethodContext methodContext) {
171         // TODO: something more than a stub/dummy
172
return this.rawString();
173     }
174 }
175
Popular Tags