KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > taglib > ValidatorTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.faces.taglib;
17
18 import org.apache.cocoon.taglib.TagSupport;
19
20 import org.apache.cocoon.faces.FacesUtils;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.faces.component.EditableValueHolder;
25 import javax.faces.validator.Validator;
26
27 /**
28  * Base class for validator tags.
29  * Can be extended to implement custom validators.
30  *
31  * @version CVS $Id: ValidatorTag.java 53791 2004-10-05 13:16:15Z vgritsenko $
32  */

33 public class ValidatorTag extends TagSupport {
34
35     private String JavaDoc validatorId;
36
37     public String JavaDoc getValidatorId() {
38         return this.validatorId;
39     }
40
41     public void setValidatorId(String JavaDoc validatorId) {
42         this.validatorId = validatorId;
43     }
44
45     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
46     throws SAXException JavaDoc {
47         UIComponentTag tag = FacesUtils.findParentUIComponentTag(this);
48         if (tag == null) {
49             throw new SAXException JavaDoc("Tag <" + getClass().getName() + "> have to be nested within a UIComponentTag");
50         }
51
52         if (!tag.getCreated()) {
53             return 0;
54         }
55
56         Validator validator = createValidator();
57         ((EditableValueHolder)tag.getComponentInstance()).addValidator(validator);
58
59         return SKIP_BODY;
60     }
61
62     /**
63      * Override to create custom validator
64      */

65     protected Validator createValidator() {
66         final UIComponentTag tag = FacesUtils.findParentUIComponentTag(this);
67         String JavaDoc validatorIdVal = (String JavaDoc) tag.evaluate(validatorId);
68         return tag.getApplication().createValidator(validatorIdVal);
69     }
70
71     public void recycle() {
72         super.recycle();
73         this.validatorId = null;
74     }
75 }
76
Popular Tags