Object result; // what we'll store in scope:var
// determine the value by...
if (value != null) {
// ... reading our attribute
result = value;
} else if (valueSpecified) {
// ... accepting an explicit null
result = null;
} else {
// ... retrieving and trimming our body
if (bodyContent == null || bodyContent.getString() == null)
result = "";
else
result = bodyContent.getString().trim();
}
// decide what to do with the result
if (var != null) {
/*
* Store the result, letting an IllegalArgumentException
* propagate back if the scope is invalid (e.g., if an attempt
* is made to store something in the session without any
* HttpSession existing).
*/
if (result != null) {
if (result instanceof ValueExpression) {
if (scope != PageContext.PAGE_SCOPE) {
throw new JspException(
Resources.getMessage("SET_BAD_SCOPE_DEFERRED"));
}
VariableMapper vm =
pageContext.getELContext().getVariableMapper();
if (vm != null) {
vm.setVariable(var, (ValueExpression)result);
}
} else {
// Make sure to clear any previous mapping for this
// variable in the variable mapper.
if (scope == PageContext.PAGE_SCOPE) {
VariableMapper vm =
pageContext.getELContext().getVariableMapper();
if (vm != null) {
vm.setVariable(var, null);
}
}
pageContext.setAttribute(var, result, scope);
}
} else {
if (scopeSpecified)
pageContext.removeAttribute(var, scope);
else
pageContext.removeAttribute(var);
if (scope == PageContext.PAGE_SCOPE) {
VariableMapper vm =
pageContext.getELContext().getVariableMapper();
if (vm != null) {
vm.setVariable(var, null);
}
}
}
} else if (target != null) {
// save the result to target.property
if (target instanceof Map) {
// ... treating it as a Map entry
if (result == null)
((Map) target).remove(property);
else
((Map) target).put(property, result);
} else {
// ... treating it as a bean property
try {
PropertyDescriptor pd[] =
Introspector.getBeanInfo(target.getClass())
.getPropertyDescriptors();
boolean succeeded = false;
for (int i = 0; i < pd.length; i++) {
if (pd[i].getName().equals(property)) {
Method m = pd[i].getWriteMethod();
if (m == null) {
throw new JspException(
Resources.getMessage("SET_NO_SETTER_METHOD",
property));
}
if (result != null) {
try {
m.invoke(target,
new Object[] {
convertToExpectedType(result, m.getParameterTypes()[0])});
} catch (javax.el.ELException ex) {
throw new JspTagException(ex);
}
} else {
m.invoke(target, new Object[] { null });
}
succeeded = true;
}
}
if (!succeeded) {
throw new JspTagException(
Resources.getMessage("SET_INVALID_PROPERTY",
property));
}
} catch (IllegalAccessException ex) {
throw new JspException(ex);
} catch (IntrospectionException ex) {
throw new JspException(ex);
} catch (InvocationTargetException ex) {
throw new JspException(ex);
}
}
} else {
// should't ever occur because of validation in TLV and setters
throw new JspTagException();
}
return EVAL_PAGE;