001: /*
002: * Copyright 2004-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.taglib.core;
017:
018: import javax.el.ELContext;
019: import javax.el.ValueExpression;
020: import javax.faces.application.Application;
021: import javax.faces.context.FacesContext;
022: import javax.faces.validator.Validator;
023: import javax.faces.webapp.ValidatorELTag;
024: import javax.servlet.jsp.JspException;
025:
026: /**
027: * Basic Validator implementation.
028: *
029: * @author Andreas Berger (latest modification by $Author: baranda $)
030: * @version $Revision: 467963 $ $Date: 2006-10-26 13:33:25 +0200 (Do, 26 Okt 2006) $
031: * @since 1.2
032: */
033: public class ValidatorTag extends ValidatorELTag {
034: private ValueExpression _validatorId;
035: private ValueExpression _binding;
036: private String _validatorIdString = null;
037:
038: public void setValidatorId(ValueExpression validatorId) {
039: _validatorId = validatorId;
040: }
041:
042: public void setBinding(ValueExpression binding) {
043: _binding = binding;
044: }
045:
046: /**
047: * Use this method to specify the validatorId programmatically.
048: *
049: * @param validatorIdString
050: */
051: public void setValidatorIdString(String validatorIdString) {
052: _validatorIdString = validatorIdString;
053: }
054:
055: public void release() {
056: super .release();
057: _validatorId = null;
058: _binding = null;
059: _validatorIdString = null;
060: }
061:
062: protected Validator createValidator()
063: throws javax.servlet.jsp.JspException {
064: FacesContext facesContext = FacesContext.getCurrentInstance();
065: ELContext elContext = facesContext.getELContext();
066: if (null != _binding) {
067: Object validator;
068: try {
069: validator = _binding.getValue(elContext);
070: } catch (Exception e) {
071: throw new JspException(
072: "Error while creating the Validator", e);
073: }
074: if (validator instanceof Validator) {
075: return (Validator) validator;
076: }
077: }
078: Application application = facesContext.getApplication();
079: Validator validator = null;
080: try {
081: // first check if an ValidatorId was set by a method
082: if (null != _validatorIdString) {
083: validator = application
084: .createValidator(_validatorIdString);
085: } else if (null != _validatorId) {
086: String validatorId = (String) _validatorId
087: .getValue(elContext);
088: validator = application.createValidator(validatorId);
089: }
090: } catch (Exception e) {
091: throw new JspException(
092: "Error while creating the Validator", e);
093: }
094:
095: if (null != validator) {
096: if (null != _binding) {
097: _binding.setValue(elContext, validator);
098: }
099: return validator;
100: }
101: throw new JspException(
102: "validatorId and/or binding must be specified");
103: }
104:
105: }
|