1 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 41 public class CheckId extends MethodOperation { 42 43 public static final String module = CheckId.class.getName(); 44 45 String message = null; 46 String 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 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 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 fieldVal = fieldAcsr.get(methodContext); 94 } 95 96 String fieldStr = fieldVal.toString(); 97 StringBuffer errorDetails = new StringBuffer (); 98 99 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 defaultMessage, String errorDetails) { 145 ClassLoader loader = methodContext.getLoader(); 146 147 String message = methodContext.expandString(this.message); 148 String 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.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 rawString() { 167 return "<check-id field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 169 } 170 public String expandedString(MethodContext methodContext) { 171 return this.rawString(); 173 } 174 } 175 | Popular Tags |