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.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026:
027: /**
028: * Iff the given ID field is not valid the fail-message
029: * or fail-property sub-elements are used to add a message to the error-list.
030: */
031: public class CheckId extends MethodOperation {
032:
033: public static final String module = CheckId.class.getName();
034:
035: String message = null;
036: String propertyResource = null;
037: boolean isProperty = false;
038:
039: ContextAccessor fieldAcsr;
040: ContextAccessor mapAcsr;
041: ContextAccessor errorListAcsr;
042:
043: public CheckId(Element element, SimpleMethod simpleMethod) {
044: super (element, simpleMethod);
045: this .fieldAcsr = new ContextAccessor(element
046: .getAttribute("field-name"));
047: this .mapAcsr = new ContextAccessor(element
048: .getAttribute("map-name"));
049: this .errorListAcsr = new ContextAccessor(element
050: .getAttribute("error-list-name"), "error_list");
051:
052: //note: if no fail-message or fail-property then message will be null
053: Element failMessage = UtilXml.firstChildElement(element,
054: "fail-message");
055: Element failProperty = UtilXml.firstChildElement(element,
056: "fail-property");
057:
058: if (failMessage != null) {
059: this .message = failMessage.getAttribute("message");
060: this .isProperty = false;
061: } else if (failProperty != null) {
062: this .propertyResource = failProperty
063: .getAttribute("resource");
064: this .message = failProperty.getAttribute("property");
065: this .isProperty = true;
066: }
067: }
068:
069: public boolean exec(MethodContext methodContext) {
070: boolean isValid = true;
071:
072: List messages = (List) errorListAcsr.get(methodContext);
073: if (messages == null) {
074: messages = new LinkedList();
075: errorListAcsr.put(methodContext, messages);
076: }
077:
078: Object fieldVal = null;
079: if (!mapAcsr.isEmpty()) {
080: Map fromMap = (Map) mapAcsr.get(methodContext);
081:
082: if (fromMap == null) {
083: if (Debug.infoOn())
084: Debug.logInfo("Map not found with name " + mapAcsr
085: + ", running operations", module);
086: } else {
087: fieldVal = fieldAcsr.get(fromMap, methodContext);
088: }
089: } else {
090: // no map name, try the env
091: fieldVal = fieldAcsr.get(methodContext);
092: }
093:
094: String fieldStr = fieldVal.toString();
095: StringBuffer errorDetails = new StringBuffer();
096:
097: //check various illegal characters, etc for ids
098: if (fieldStr.indexOf(' ') >= 0) {
099: isValid = false;
100: errorDetails.append("[space found at position "
101: + (fieldStr.indexOf(' ') + 1) + "]");
102: }
103: if (fieldStr.indexOf('"') >= 0) {
104: isValid = false;
105: errorDetails.append("[double-quote found at position "
106: + (fieldStr.indexOf('"') + 1) + "]");
107: }
108: if (fieldStr.indexOf('\'') >= 0) {
109: isValid = false;
110: errorDetails.append("[single-quote found at position "
111: + (fieldStr.indexOf('\'') + 1) + "]");
112: }
113: if (fieldStr.indexOf('&') >= 0) {
114: isValid = false;
115: errorDetails.append("[ampersand found at position "
116: + (fieldStr.indexOf('&') + 1) + "]");
117: }
118: if (fieldStr.indexOf('?') >= 0) {
119: isValid = false;
120: errorDetails.append("[question mark found at position "
121: + (fieldStr.indexOf('?') + 1) + "]");
122: }
123: if (fieldStr.indexOf('<') >= 0) {
124: isValid = false;
125: errorDetails.append("[less-than sign found at position "
126: + (fieldStr.indexOf('<') + 1) + "]");
127: }
128: if (fieldStr.indexOf('>') >= 0) {
129: isValid = false;
130: errorDetails.append("[greater-than sign found at position "
131: + (fieldStr.indexOf('>') + 1) + "]");
132: }
133: if (fieldStr.indexOf('\\') >= 0) {
134: isValid = false;
135: errorDetails.append("[back-slash found at position "
136: + (fieldStr.indexOf('\\') + 1) + "]");
137: }
138: if (fieldStr.indexOf('/') >= 0) {
139: isValid = false;
140: errorDetails.append("[forward-slash found at position "
141: + (fieldStr.indexOf('/') + 1) + "]");
142: }
143:
144: if (!isValid) {
145: this .addMessage(messages, methodContext,
146: "The ID value in the field [" + fieldAcsr
147: + "] was not valid", ": "
148: + errorDetails.toString());
149: }
150:
151: return true;
152: }
153:
154: public void addMessage(List messages, MethodContext methodContext,
155: String defaultMessage, String errorDetails) {
156:
157: String message = methodContext.expandString(this .message);
158: String propertyResource = methodContext
159: .expandString(this .propertyResource);
160:
161: if (!isProperty && message != null) {
162: messages.add(message + errorDetails);
163: } else if (isProperty && propertyResource != null
164: && message != null) {
165: //String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message);
166: String propMsg = UtilProperties.getMessage(
167: propertyResource, message, methodContext
168: .getEnvMap(), methodContext.getLocale());
169:
170: if (propMsg == null || propMsg.length() == 0) {
171: messages.add(defaultMessage + errorDetails);
172: } else {
173: messages.add(methodContext.expandString(propMsg)
174: + errorDetails);
175: }
176: } else {
177: messages.add(defaultMessage + errorDetails);
178: }
179: }
180:
181: public String rawString() {
182: // TODO: add all attributes and other info
183: return "<check-id field-name=\"" + this .fieldAcsr
184: + "\" map-name=\"" + this .mapAcsr + "\"/>";
185: }
186:
187: public String expandedString(MethodContext methodContext) {
188: // TODO: something more than a stub/dummy
189: return this.rawString();
190: }
191: }
|