Method from javax.faces.component.UIInput Detail: |
public void addValidator(Validator validator) {
if (validator == null) {
throw new NullPointerException();
}
if (validators == null) {
validators = new AttachedObjectListHolder< Validator >();
}
validators.add(validator);
}
|
public void addValueChangeListener(ValueChangeListener listener) {
addFacesListener(listener);
}
|
public void clearInitialState() {
if (initialStateMarked()) {
super.clearInitialState();
if (validators != null) {
validators.clearInitialState();
}
}
}
|
protected boolean compareValues(Object previous,
Object value) {
if (previous == null) {
return (value != null);
} else if (value == null) {
return (true);
} else {
return (!(previous.equals(value)));
}
}
|
public void decode(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
// Force validity back to "true"
setValid(true);
super.decode(context);
}
|
protected Object getConvertedValue(FacesContext context,
Object newSubmittedValue) throws ConverterException {
Renderer renderer = getRenderer(context);
Object newValue;
if (renderer != null) {
newValue = renderer.getConvertedValue(context, this,
newSubmittedValue);
} else if (newSubmittedValue instanceof String) {
// If there's no Renderer, and we've got a String,
// run it through the Converter (if any)
Converter converter = getConverterWithType(context);
if (converter != null) {
newValue = converter.getAsObject(context, this,
(String) newSubmittedValue);
} else {
newValue = newSubmittedValue;
}
} else {
newValue = newSubmittedValue;
}
return newValue;
}
Convert the submitted value into a "local value" of the
appropriate data type, if necessary. Employ the following
algorithm to do so:
- If a
Renderer is present, call
getConvertedValue() to convert the submitted
value.
- If no
Renderer is present, and the submitted
value is a String, locate a Converter as follows:
- If
getConverter() returns a non-null Converter ,
use that instance.
- Otherwise, if a value binding for
value exists,
call getType() on it.
- If this call returns
null , assume the output
type is String and perform no conversion.
- Otherwise, call
Application.createConverter(Class)
to locate any registered Converter capable of
converting data values of the specified type.
- If a Converter instance was located, call its
getAsObject() method to perform the conversion.
If conversion fails:
- Enqueue an appropriate error message by calling the
addMessage() method on the
FacesContext .
- Set the
valid property
on this component to false
- Otherwise, use the submitted value without any conversion
This method can be overridden by subclasses for more specific
behavior.
|
public String getConverterMessage() {
return (String) getStateHelper().eval(PropertyKeys.converterMessage);
}
|
public String getFamily() {
return (COMPONENT_FAMILY);
}
|
public String getRequiredMessage() {
return (String) getStateHelper().eval(PropertyKeys.requiredMessage);
}
|
public Object getSubmittedValue() {
return (this.submittedValue);
}
Return the submittedValue value of this UIInput component.
This method should only be used by the decode() and
validate() method of this component, or
its corresponding Renderer .
|
public MethodBinding getValidator() {
MethodBinding result = null;
Validator[] curValidators = getValidators();
// go through our lisetners list and find the one and only
// MethodBindingValidator instance, if present.
if (null != curValidators) {
for (int i = 0; i < curValidators.length; i++) {
// We are guaranteed to have at most one instance of
// MethodBindingValidator in the curValidators list.
if (MethodBindingValidator.class ==
curValidators[i].getClass()) {
result = ((MethodBindingValidator) curValidators[i]).
getWrapped();
break;
}
}
}
return result;
} Deprecated! { - @link #getValidators} should be used instead.
Return a MethodBinding pointing at a
method that will be called during Process Validations
phase of the request processing lifecycle, to validate the current
value of this component.
|
public String getValidatorMessage() {
return (String) getStateHelper().eval(PropertyKeys.validatorMessage);
}
|
public Validator[] getValidators() {
return ((validators != null) ? validators.asArray(Validator.class) : EMPTY_VALIDATOR);
}
Return the set of registered Validator s for this
UIInput instance. If there are no registered validators,
a zero-length array is returned.
|
public MethodBinding getValueChangeListener() {
MethodBinding result = null;
ValueChangeListener[] curListeners = getValueChangeListeners();
// go through our lisetners list and find the one and only
// MethodBindingValueChangeListener instance, if present.
if (null != curListeners) {
for (int i = 0; i < curListeners.length; i++) {
// We are guaranteed to have at most one instance of
// MethodBindingValueChangeListener in the curListeners list.
if (MethodBindingValueChangeListener.class ==
curListeners[i].getClass()) {
result = ((MethodBindingValueChangeListener) curListeners[i]).
getWrapped();
break;
}
}
}
return result;
}
|
public ValueChangeListener[] getValueChangeListeners() {
return (ValueChangeListener[]) getFacesListeners(ValueChangeListener.class);
}
Return the set of registered ValueChangeListener s for this
UIInput instance. If there are no registered listeners,
a zero-length array is returned.
|
public static boolean isEmpty(Object value) {
if (value == null) {
return (true);
} else if ((value instanceof String) &&
(((String) value).length() < 1)) {
return (true);
} else if (value.getClass().isArray()) {
if (0 == java.lang.reflect.Array.getLength(value)) {
return (true);
}
} else if (value instanceof List) {
if (((List) value).isEmpty()) {
return (true);
}
}
return (false);
}
|
public boolean isImmediate() {
return (Boolean) getStateHelper().eval(PropertyKeys.immediate, false);
}
|
public boolean isLocalValueSet() {
return (Boolean) getStateHelper().eval(PropertyKeys.localValueSet, false);
}
Return the "local value set" state for this component.
Calls to setValue() automatically reset
this property to true . |
public boolean isRequired() {
return (Boolean) getStateHelper().eval(PropertyKeys.required, false);
}
|
public boolean isValid() {
return (Boolean) getStateHelper().eval(PropertyKeys.valid, true);
}
|
public void markInitialState() {
super.markInitialState();
if (validators != null) {
validators.markInitialState();
}
}
|
public void processDecodes(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
// Skip processing if our rendered flag is false
if (!isRendered()) {
return;
}
super.processDecodes(context);
if (isImmediate()) {
executeValidate(context);
}
}
Specialized decode behavior on top of that provided by the
superclass. In addition to the standard
processDecodes behavior inherited from UIComponentBase , calls validate() if the the
immediate property is true; if the component is
invalid afterwards or a RuntimeException is thrown,
calls FacesContext#renderResponse .
|
public void processUpdates(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
// Skip processing if our rendered flag is false
if (!isRendered()) {
return;
}
super.processUpdates(context);
try {
updateModel(context);
} catch (RuntimeException e) {
context.renderResponse();
throw e;
}
if (!isValid()) {
context.renderResponse();
}
}
|
public void processValidators(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
// Skip processing if our rendered flag is false
if (!isRendered()) {
return;
}
super.processValidators(context);
if (!isImmediate()) {
executeValidate(context);
}
}
In addition to the standard processValidators behavior
inherited from UIComponentBase , calls validate()
if the immediate property is false (which is the
default); if the component is invalid afterwards, calls
FacesContext#renderResponse .
If a RuntimeException is thrown during
validation processing, calls FacesContext#renderResponse
and re-throw the exception.
|
public void removeValidator(Validator validator) {
if (validator == null) {
return;
}
if (validators != null) {
validators.remove(validator);
}
}
Remove a Validator instance from the set associated with
this UIInput , if it was previously associated.
Otherwise, do nothing.
|
public void removeValueChangeListener(ValueChangeListener listener) {
removeFacesListener(listener);
}
|
public void resetValue() {
this.setValue(null);
this.setSubmittedValue(null);
this.setLocalValueSet(false);
this.setValid(true);
}
Convenience method to reset this component's value to the
un-initialized state. This method does the following:
Call #setValue passing null .
Call #setSubmittedValue passing null .
Call #setLocalValueSet passing false .
Call #setValid passing true .
Upon return from this call if the instance had a
ValueBinding associated with it for the "value"
property, this binding is evaluated when UIOutput#getValue is called. Otherwise, null is
returned from getValue() .
|
public void restoreState(FacesContext context,
Object state) {
if (context == null) {
throw new NullPointerException();
}
if (state == null) {
return;
}
values = (Object[]) state;
super.restoreState(context, values[0]);
emptyStringIsNull = (Boolean) values[1];
validateEmptyFields = (Boolean) values[2];
if (values[3] != null) {
if (validators == null) {
validators = new AttachedObjectListHolder< Validator >();
}
validators.restoreState(context, values[3]);
}
}
|
public Object saveState(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
if (values == null) {
values = new Object[4];
}
values[0] = super.saveState(context);
values[1] = emptyStringIsNull;
values[2] = validateEmptyFields;
values[3] = ((validators != null) ? validators.saveState(context) : null);
return (values);
}
|
public void setConverterMessage(String message) {
getStateHelper().put(PropertyKeys.converterMessage, message);
}
Override any ValueExpression set for the "converterMessage"
with the literal argument provided to this method. Subsequent calls
to #getConverterMessage will return this value;
|
public void setImmediate(boolean immediate) {
getStateHelper().put(PropertyKeys.immediate, immediate);
}
|
public void setLocalValueSet(boolean localValueSet) {
getStateHelper().put(PropertyKeys.localValueSet, localValueSet);
}
Sets the "local value set" state for this component. |
public void setRequired(boolean required) {
getStateHelper().put(PropertyKeys.required, required);
}
|
public void setRequiredMessage(String message) {
getStateHelper().put(PropertyKeys.requiredMessage, message);
}
Override any ValueExpression set for the "requiredMessage"
with the literal argument provided to this method. Subsequent calls
to #getRequiredMessage will return this value;
|
public void setSubmittedValue(Object submittedValue) {
this.submittedValue = submittedValue;
}
Set the submittedValue value of this UIInput component.
This method should only be used by the decode() and
validate() method of this component, or
its corresponding Renderer .
|
public void setValid(boolean valid) {
getStateHelper().put(PropertyKeys.valid, valid);
}
|
public void setValidator(MethodBinding validatorBinding) {
Validator[] curValidators = getValidators();
// see if we need to null-out, or replace an existing validator
if (null != curValidators) {
for (int i = 0; i < curValidators.length; i++) {
// if we want to remove the validatorBinding
if (null == validatorBinding) {
// We are guaranteed to have at most one instance of
// MethodBindingValidator in the curValidators
// list.
if (MethodBindingValidator.class ==
curValidators[i].getClass()) {
removeValidator(curValidators[i]);
return;
}
}
// if we want to replace the validatorBinding
else //noinspection ObjectEquality
if (validatorBinding == curValidators[i]) {
removeValidator(curValidators[i]);
break;
}
}
}
addValidator(new MethodBindingValidator(validatorBinding));
} Deprecated! Use - #addValidator instead, obtaining the
argument Validator by creating an instance of javax.faces.validator.MethodExpressionValidator .
Set a MethodBinding pointing at a
method that will be called during Process Validations
phase of the request processing lifecycle, to validate the current
value of this component.
Any method referenced by such an expression must be public, with
a return type of void , and accept parameters of type
FacesContext , UIComponent , and Object .
|
public void setValidatorMessage(String message) {
getStateHelper().put(PropertyKeys.validatorMessage, message);
}
Override any ValueExpression set for the "validatorMessage"
with the literal argument provided to this method. Subsequent calls
to #getValidatorMessage will return this value;
|
public void setValue(Object value) {
super.setValue(value);
// Mark the local value as set.
setLocalValueSet(true);
}
|
public void setValueChangeListener(MethodBinding valueChangeListener) {
ValueChangeListener[] curListeners = getValueChangeListeners();
// see if we need to null-out, or replace an existing listener
if (null != curListeners) {
for (int i = 0; i < curListeners.length; i++) {
// if we want to remove the valueChangeListener
if (null == valueChangeListener) {
// We are guaranteed to have at most one instance of
// MethodBindingValueChangeListener in the curListeners
// list.
if (MethodBindingValueChangeListener.class ==
curListeners[i].getClass()) {
removeFacesListener(curListeners[i]);
return;
}
}
// if we want to replace the valueChangeListener
else //noinspection ObjectEquality
if (valueChangeListener == curListeners[i]) {
removeFacesListener(curListeners[i]);
break;
}
}
}
addValueChangeListener(new MethodBindingValueChangeListener(valueChangeListener));
} Deprecated! Use - #addValueChangeListener instead, obtaining the
argument ValueChangeListener by creating an instance of javax.faces.event.MethodExpressionValueChangeListener .
|
public void updateModel(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
if (!isValid() || !isLocalValueSet()) {
return;
}
ValueExpression ve = getValueExpression("value");
if (ve != null) {
Throwable caught = null;
FacesMessage message = null;
try {
ve.setValue(context.getELContext(), getLocalValue());
setValue(null);
setLocalValueSet(false);
} catch (ELException e) {
caught = e;
String messageStr = e.getMessage();
Throwable result = e.getCause();
while (null != result &&
result.getClass().isAssignableFrom(ELException.class)) {
messageStr = result.getMessage();
result = result.getCause();
}
if (null == messageStr) {
message =
MessageFactory.getMessage(context, UPDATE_MESSAGE_ID,
MessageFactory.getLabel(
context, this));
} else {
message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
messageStr,
messageStr);
}
setValid(false);
} catch (Exception e) {
caught = e;
message =
MessageFactory.getMessage(context, UPDATE_MESSAGE_ID,
MessageFactory.getLabel(
context, this));
setValid(false);
}
if (caught != null) {
assert(message != null);
@SuppressWarnings({"ThrowableInstanceNeverThrown"})
UpdateModelException toQueue =
new UpdateModelException(message, caught);
ExceptionQueuedEventContext eventContext =
new ExceptionQueuedEventContext(context,
toQueue,
this,
PhaseId.UPDATE_MODEL_VALUES);
context.getApplication().publishEvent(context,
ExceptionQueuedEvent.class,
eventContext);
}
}
}
Perform
the following algorithm to update the model data
associated with this UIInput , if any, as appropriate.
- If the
valid property of this component is
false , take no further action.
- If the
localValueSet property of this component is
false , take no further action.
- If no ValueExpression for
value exists,
take no further action.
- Call
setValue() method of the ValueExpression
to update the value that the ValueExpression points at.
- If the
setValue() method returns successfully:
- Clear the local value of this UIInput .
- Set the
localValueSet property of this
UIInput to false.
- If the
setValue() method throws an Exception:
The exception must not be re-thrown. This enables tree traversal
to continue for this lifecycle phase, as in all the other lifecycle
phases.
|
public void validate(FacesContext context) {
if (context == null) {
throw new NullPointerException();
}
// Submitted value == null means "the component was not submitted
// at all".
Object submittedValue = getSubmittedValue();
if (submittedValue == null) {
return;
}
// If non-null, an instanceof String, and we're configured to treat
// zero-length Strings as null:
// call setSubmittedValue(null)
if ((considerEmptyStringNull(context)
&& submittedValue instanceof String
&& ((String) submittedValue).length() == 0)) {
setSubmittedValue(null);
submittedValue = null;
}
Object newValue = null;
try {
newValue = getConvertedValue(context, submittedValue);
}
catch (ConverterException ce) {
addConversionErrorMessage(context, ce);
setValid(false);
}
validateValue(context, newValue);
// If our value is valid, store the new value, erase the
// "submitted" value, and emit a ValueChangeEvent if appropriate
if (isValid()) {
Object previous = getValue();
setValue(newValue);
setSubmittedValue(null);
if (compareValues(previous, newValue)) {
queueEvent(new ValueChangeEvent(this, previous, newValue));
}
}
}
Perform the
following algorithm to validate the local value of this UIInput .
- Retrieve the submitted value with #getSubmittedValue .
If this returns
null , exit without further
processing. (This indicates that no value was submitted for this
component.)
- If the
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
context parameter value is true (ignoring case), and
getSubmittedValue() returns a zero-length
String call #setSubmittedValue ,
passing null as the argument and continue processing
using null as the current submitted
value.
- Convert the submitted value into a "local value" of the
appropriate data type by calling #getConvertedValue .
- Validate the property by calling #validateValue .
- If the
valid property of this component is still
true , retrieve the previous value of the component
(with getValue() ), store the new local value using
setValue() , and reset the submitted value to
null. If the local value is different from
the previous value of this component, fire a
ValueChangeEvent to be broadcast to all interested
listeners.
Application components implementing UIInput that wish to
perform validation with logic embedded in the component should perform
their own correctness checks, and then call the
super.validate() method to perform the standard
processing described above.
|
protected void validateValue(FacesContext context,
Object newValue) {
// If our value is valid, enforce the required property if present
if (isValid() && isRequired() && isEmpty(newValue)) {
String requiredMessageStr = getRequiredMessage();
FacesMessage message;
if (null != requiredMessageStr) {
message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
requiredMessageStr,
requiredMessageStr);
} else {
message =
MessageFactory.getMessage(context, REQUIRED_MESSAGE_ID,
MessageFactory.getLabel(
context, this));
}
context.addMessage(getClientId(context), message);
setValid(false);
}
// If our value is valid and not empty or empty w/ validate empty fields enabled, call all validators
if (isValid() && (!isEmpty(newValue) || validateEmptyFields(context))) {
if (validators != null) {
Validator[] validators = this.validators.asArray(Validator.class);
for (Validator validator : validators) {
try {
validator.validate(context, this, newValue);
}
catch (ValidatorException ve) {
// If the validator throws an exception, we're
// invalid, and we need to add a message
setValid(false);
FacesMessage message;
String validatorMessageString = getValidatorMessage();
if (null != validatorMessageString) {
message =
new FacesMessage(FacesMessage.SEVERITY_ERROR,
validatorMessageString,
validatorMessageString);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
} else {
Collection< FacesMessage > messages = ve.getFacesMessages();
if (null != messages) {
message = null;
String cid = getClientId(context);
for (FacesMessage m : messages) {
context.addMessage(cid, m);
}
} else {
message = ve.getFacesMessage();
}
}
if (message != null) {
context.addMessage(getClientId(context), message);
}
}
}
}
}
}
|