001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.validator.validators;
006:
007: import com.opensymphony.xwork.ActionContext;
008: import com.opensymphony.xwork.util.OgnlValueStack;
009: import com.opensymphony.xwork.util.TextParseUtil;
010: import com.opensymphony.xwork.validator.*;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013:
014: /**
015: * Abstract implementation of the Validator interface suitable for subclassing.
016: *
017: * @author Jason Carreira
018: * @author tmjee
019: */
020: public abstract class ValidatorSupport implements Validator,
021: ShortCircuitableValidator {
022:
023: protected final Log log = LogFactory.getLog(this .getClass());
024: protected String defaultMessage = "";
025: protected String messageKey = null;
026: private ValidatorContext validatorContext;
027: private boolean shortCircuit;
028: private boolean parse = false;
029: private String type;
030:
031: public void setParse(boolean parse) {
032: this .parse = parse;
033: }
034:
035: public boolean getParse() {
036: return parse;
037: }
038:
039: public void setDefaultMessage(String message) {
040: this .defaultMessage = message;
041: }
042:
043: public String getDefaultMessage() {
044: return defaultMessage;
045: }
046:
047: public String getMessage(Object object) {
048: String message;
049: OgnlValueStack stack = ActionContext.getContext()
050: .getValueStack();
051: boolean pop = false;
052:
053: if (!stack.getRoot().contains(object)) {
054: stack.push(object);
055: pop = true;
056: }
057:
058: stack.push(this );
059:
060: if (messageKey != null) {
061: if ((defaultMessage == null)
062: || (defaultMessage.trim().equals(""))) {
063: defaultMessage = messageKey;
064: }
065: if (validatorContext == null) {
066: validatorContext = new DelegatingValidatorContext(
067: object);
068: }
069: message = validatorContext.getText(messageKey,
070: defaultMessage);
071: } else {
072: message = defaultMessage;
073: }
074:
075: message = TextParseUtil.translateVariables(message, stack);
076:
077: stack.pop();
078:
079: if (pop) {
080: stack.pop();
081: }
082:
083: return message;
084: }
085:
086: public void setMessageKey(String key) {
087: messageKey = key;
088: }
089:
090: public String getMessageKey() {
091: return messageKey;
092: }
093:
094: public void setShortCircuit(boolean shortcircuit) {
095: shortCircuit = shortcircuit;
096: }
097:
098: public boolean isShortCircuit() {
099: return shortCircuit;
100: }
101:
102: public void setValidatorContext(ValidatorContext validatorContext) {
103: this .validatorContext = validatorContext;
104: }
105:
106: public ValidatorContext getValidatorContext() {
107: return validatorContext;
108: }
109:
110: public void setValidatorType(String type) {
111: this .type = type;
112: }
113:
114: public String getValidatorType() {
115: return type;
116: }
117:
118: /**
119: * Parse <code>expression</code> passed in against value stack. Only parse
120: * when 'parse' param is set to true, else just returns the expression unparsed.
121: *
122: * @param expression
123: * @return
124: */
125: protected Object conditionalParse(String expression) {
126: if (parse) {
127: OgnlValueStack stack = ActionContext.getContext()
128: .getValueStack();
129: return TextParseUtil.translateVariables('$', expression,
130: stack);
131: }
132: return expression;
133: }
134:
135: /**
136: * Return the field value named <code>name</code> from <code>object</code>,
137: * <code>object</code> should have the appropriate getter/setter.
138: *
139: * @param name
140: * @param object
141: * @return
142: * @throws ValidationException
143: */
144: protected Object getFieldValue(String name, Object object)
145: throws ValidationException {
146: OgnlValueStack stack = ActionContext.getContext()
147: .getValueStack();
148:
149: boolean pop = false;
150:
151: if (!stack.getRoot().contains(object)) {
152: stack.push(object);
153: pop = true;
154: }
155:
156: Object retVal = stack.findValue(name);
157:
158: if (pop) {
159: stack.pop();
160: }
161:
162: return retVal;
163: }
164:
165: protected void addActionError(Object object) {
166: validatorContext.addActionError(getMessage(object));
167: }
168:
169: protected void addFieldError(String propertyName, Object object) {
170: validatorContext
171: .addFieldError(propertyName, getMessage(object));
172: }
173: }
|