KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > model > ValidatorTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.model;
8
9
10 import java.io.IOException JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
14
15 import com.inversoft.verge.mvc.validator.ValidatorConstants;
16 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
17 import com.inversoft.verge.mvc.view.jsp.html.FormTag;
18
19
20 /**
21  * <p>
22  * This class is the validator tag. This tag sets up a
23  * validator so that the form is validated. This tag must
24  * be inside a form tag that extends {@link
25  * com.inversoft.verge.mvc.view.jsp.html.FormTag FormTag}.
26  * </p>
27  *
28  * <p>
29  * These can only be a single validation tag per form.
30  * </p>
31  *
32  * @author Brian Pontarelli
33  * @since 2.0
34  * @version 2.0
35  */

36 public class ValidatorTag extends TagSupport JavaDoc {
37
38     /**
39      * The key the validator tag is stored under in the form tag
40      */

41     public static final String JavaDoc VALIDATOR_KEY = "validator";
42
43
44     private String JavaDoc validator;
45
46
47     /**
48      * Construsts a new <code>ValidatorTag</code>
49      */

50     public ValidatorTag() {
51     }
52
53
54     /**
55      * Gets the class name of the validator
56      *
57      * @return The class name of the validator
58      */

59     public String JavaDoc getValidator() {
60         return validator;
61     }
62
63     /**
64      * Sets the class name of the validator
65      *
66      * @param validator The new class name of the validator
67      */

68     public void setValidator(String JavaDoc validator) {
69         this.validator = validator;
70     }
71
72
73     /**
74      * Outputs the meta data needed for the Validator. This throws an exception
75      * if there was already a validator on the form or if there is no form
76      *
77      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
78      */

79     public int doStartTag() throws JspException JavaDoc {
80         
81         FormTag parent = (FormTag) findAncestorWithClass(this, FormTag.class);
82         if (parent == null) {
83             throw new JspException JavaDoc("The validator tag must be in a form");
84         }
85         
86         ValidatorTag prev = (ValidatorTag) parent.getAttribute(VALIDATOR_KEY);
87         if (prev == null) {
88             parent.setAttribute(VALIDATOR_KEY, this);
89         } else {
90             throw new JspException JavaDoc("Only one validator tag per form");
91         }
92
93         try {
94             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
95             HtmlViewToolkit.appendHiddenTag(buf,
96                 ValidatorConstants.VALIDATOR_PARAMETER, validator);
97             pageContext.getOut().write(buf.toString());
98         } catch (IOException JavaDoc ioe) {
99             throw new JspException JavaDoc(ioe);
100         }
101         
102         return EVAL_PAGE;
103     }
104 }
Popular Tags