You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2005/10/21 18:40:19 UTC

DO NOT REPLY [Bug 37199] New: - if-Variable for any Validator rule

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=37199>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=37199

           Summary: if-Variable for any Validator rule
           Product: Struts
           Version: 1.2.7
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: minor
          Priority: P2
         Component: Validator Framework
        AssignedTo: dev@struts.apache.org
        ReportedBy: t.jacob@s2neueinformatik.de


I'm am proposing an extension to the Validator of Struts.
It adds the feature to declare a variable 'if' in the validation.xml.

The value of this variable is an EL expression.
If and only if this EL expression evaluates to true,
the field's rules are considered.

The EL expression may contain a number of variables,
including the current form bean, the property's value,
and the request, session, and application scopes.

Using EL, and providing this context, enables a much more
flexible approach to switching rules on and off dynamically.

Currently, I am extending the FieldChecks class, calling a
switching method in each rule's method. A nicer integration
of my source code would be very much appreciated.
The implementation uses Commons EL.

People told be that this contribution is generic enough
to be part of Struts. Perhaps you get to the same conclusion :-)

This is the code snippet to switch:

protected static boolean skipValidation(final Object bean, final ValidatorAction 
action, final Field field,
	final HttpServletRequest request)
{
	Var ifVariable = field.getVar("if");
	if (ifVariable == null)
	{
		return false;
	}

	String ifExpression = ifVariable.getValue();

	ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
	VariableResolver resolver = new VariableResolver()
	{
		public Object resolveVariable(String name) throws ELException
		{
			HttpSession session = request.getSession();
			ServletContext context = session.getServletContext();

			if (name.equals("form"))
			{
				return bean;
			}
			else if (name.equals("requestScope"))
			{
				Map requestMap = new HashMap(request.getParameterMap());
				Enumeration enumeration = request.getAttributeNames();
				while (enumeration.hasMoreElements())
				{
					String attributeName = (String) enumeration.
nextElement();
					Object attributeValue = request.
getAttribute(attributeName);
					requestMap.put(attributeName, attributeValue);
				}
				return requestMap;
			}
			else if (name.equals("sessionScope"))
			{
				Map sessionMap = new HashMap();
				Enumeration enumeration = session.getAttributeNames();
				while (enumeration.hasMoreElements())
				{
					String attributeName = (String) enumeration.
nextElement();
					Object attributeValue = session.
getAttribute(attributeName);
					sessionMap.put(attributeName, attributeValue);
				}
				return sessionMap;
			}
			else if (name.equals("applicationScope"))
			{
				Map contextMap = new HashMap();
				Enumeration enumeration = context.getAttributeNames();
				while (enumeration.hasMoreElements())
				{
					String attributeName = (String) enumeration.
nextElement();
					Object attributeValue = context.
getAttribute(attributeName);
					contextMap.put(attributeName, attributeValue);
				}
				return contextMap;
			}
			else if (name.equals("value"))
			{
				return ValidatorUtils.getValueAsString(bean, field.
getProperty());
			}
			else
			{
				Object bean = request.getAttribute(name);
				if (bean != null)
				{
					return bean;
				}

				bean = session.getAttribute(name);
				if (bean != null)
				{
					return bean;
				}

				return context.getAttribute(name);
			}
		}
	};

	try
	{
		Boolean result = (Boolean) evaluator.evaluate("${" + ifVariable.
getValue() + "}", Boolean.class, resolver,
			null);
		return result != null && !result.booleanValue();
	}
	catch (ELException exception)
	{
		throw new IllegalArgumentException("Invalid validator 'if' EL 
expression '" + ifExpression + "': " + exception);
	}
}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org