001: /*
002: * $Id: CheckId.java,v 1.2 2003/10/10 09:55:53 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.ifops;
025:
026: import java.util.*;
027:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031: import org.ofbiz.minilang.method.*;
032:
033: /**
034: * Iff the given ID field is not valid the fail-message
035: * or fail-property sub-elements are used to add a message to the error-list.
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.2 $
039: * @since 2.0
040: */
041: public class CheckId extends MethodOperation {
042:
043: public static final String module = CheckId.class.getName();
044:
045: String message = null;
046: String propertyResource = null;
047: boolean isProperty = false;
048:
049: ContextAccessor fieldAcsr;
050: ContextAccessor mapAcsr;
051: ContextAccessor errorListAcsr;
052:
053: public CheckId(Element element, SimpleMethod simpleMethod) {
054: super (element, simpleMethod);
055: this .fieldAcsr = new ContextAccessor(element
056: .getAttribute("field-name"));
057: this .mapAcsr = new ContextAccessor(element
058: .getAttribute("map-name"));
059: this .errorListAcsr = new ContextAccessor(element
060: .getAttribute("error-list-name"), "error_list");
061:
062: //note: if no fail-message or fail-property then message will be null
063: Element failMessage = UtilXml.firstChildElement(element,
064: "fail-message");
065: Element failProperty = UtilXml.firstChildElement(element,
066: "fail-property");
067:
068: if (failMessage != null) {
069: this .message = failMessage.getAttribute("message");
070: this .isProperty = false;
071: } else if (failProperty != null) {
072: this .propertyResource = failProperty
073: .getAttribute("resource");
074: this .message = failProperty.getAttribute("property");
075: this .isProperty = true;
076: }
077: }
078:
079: public boolean exec(MethodContext methodContext) {
080: boolean isValid = true;
081:
082: List messages = (List) errorListAcsr.get(methodContext);
083: if (messages == null) {
084: messages = new LinkedList();
085: errorListAcsr.put(methodContext, messages);
086: }
087:
088: Object fieldVal = null;
089: if (!mapAcsr.isEmpty()) {
090: Map fromMap = (Map) mapAcsr.get(methodContext);
091:
092: if (fromMap == null) {
093: if (Debug.infoOn())
094: Debug.logInfo("Map not found with name " + mapAcsr
095: + ", running operations", module);
096: } else {
097: fieldVal = fieldAcsr.get(fromMap, methodContext);
098: }
099: } else {
100: // no map name, try the env
101: fieldVal = fieldAcsr.get(methodContext);
102: }
103:
104: String fieldStr = fieldVal.toString();
105: StringBuffer errorDetails = new StringBuffer();
106:
107: //check various illegal characters, etc for ids
108: if (fieldStr.indexOf(' ') >= 0) {
109: isValid = false;
110: errorDetails.append("[space found at position "
111: + (fieldStr.indexOf(' ') + 1) + "]");
112: }
113: if (fieldStr.indexOf('"') >= 0) {
114: isValid = false;
115: errorDetails.append("[double-quote found at position "
116: + (fieldStr.indexOf('"') + 1) + "]");
117: }
118: if (fieldStr.indexOf('\'') >= 0) {
119: isValid = false;
120: errorDetails.append("[single-quote found at position "
121: + (fieldStr.indexOf('\'') + 1) + "]");
122: }
123: if (fieldStr.indexOf('&') >= 0) {
124: isValid = false;
125: errorDetails.append("[ampersand found at position "
126: + (fieldStr.indexOf('&') + 1) + "]");
127: }
128: if (fieldStr.indexOf('?') >= 0) {
129: isValid = false;
130: errorDetails.append("[question mark found at position "
131: + (fieldStr.indexOf('?') + 1) + "]");
132: }
133: if (fieldStr.indexOf('<') >= 0) {
134: isValid = false;
135: errorDetails.append("[less-than sign found at position "
136: + (fieldStr.indexOf('<') + 1) + "]");
137: }
138: if (fieldStr.indexOf('>') >= 0) {
139: isValid = false;
140: errorDetails.append("[greater-than sign found at position "
141: + (fieldStr.indexOf('>') + 1) + "]");
142: }
143: if (fieldStr.indexOf('\\') >= 0) {
144: isValid = false;
145: errorDetails.append("[back-slash found at position "
146: + (fieldStr.indexOf('\\') + 1) + "]");
147: }
148: if (fieldStr.indexOf('/') >= 0) {
149: isValid = false;
150: errorDetails.append("[forward-slash found at position "
151: + (fieldStr.indexOf('/') + 1) + "]");
152: }
153:
154: if (!isValid) {
155: this .addMessage(messages, methodContext,
156: "The ID value in the field [" + fieldAcsr
157: + "] was not valid", ": "
158: + errorDetails.toString());
159: }
160:
161: return true;
162: }
163:
164: public void addMessage(List messages, MethodContext methodContext,
165: String defaultMessage, String errorDetails) {
166: ClassLoader loader = methodContext.getLoader();
167:
168: String message = methodContext.expandString(this .message);
169: String propertyResource = methodContext
170: .expandString(this .propertyResource);
171:
172: if (!isProperty && message != null) {
173: messages.add(message + errorDetails);
174: } else if (isProperty && propertyResource != null
175: && message != null) {
176: //String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message);
177: String propMsg = UtilProperties.getMessage(
178: propertyResource, message, methodContext
179: .getEnvMap(), methodContext.getLocale());
180:
181: if (propMsg == null || propMsg.length() == 0) {
182: messages.add(defaultMessage + errorDetails);
183: } else {
184: messages.add(methodContext.expandString(propMsg)
185: + errorDetails);
186: }
187: } else {
188: messages.add(defaultMessage + errorDetails);
189: }
190: }
191: }
|