KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > mailer > SetRecipientTag


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
17 package org.apache.taglibs.mailer;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.JspTagException JavaDoc;
21 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
22 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
23
24 /**
25  * SetRecipientTag - JSP tag <b>setrecipient</b> is used to set any type of
26  * recipient to an e-mail. Two attributes are required, type and
27  * setress. Type may be either "to", "cc", or "bcc" and address
28  * should be a string representation of the recipients e-mail
29  * address.
30  *
31  * <tag>
32  * <name>setrecipient</name>
33  * <tagclass>org.apache.taglibs.mailer.SetRecipientTag</tagclass>
34  * <bodycontent>JSP</bodycontent>
35  * <info>
36  * Append a recipient to the current recipients of the e-mail.
37  * </info>
38  *
39  * <attribute>
40  * <name>type</name>
41  * <required>true</required>
42  * <rtexprvalue>false</rtexprvalue>
43  * </attribute>
44  * <attribute>
45  * <name>address</name>
46  * <required>false</required>
47  * <rtexprvalue>false</rtexprvalue>
48  * </attribute>
49  * </tag>
50  *
51  * @author Rich Catlett Jayson Falkner
52  *
53  * @version 1.0
54  *
55  */

56
57 public class SetRecipientTag extends BodyTagSupport JavaDoc {
58
59     /**
60      * The type of address to add either to, cc, or bcc
61      */

62     private String JavaDoc type_string = null;
63     private int type = 0;
64
65     /**
66      * The address to be added
67      */

68     private String JavaDoc address = null;
69
70     /**
71      * implementation of the method from the tag interface that tells the JSP
72      * page what to do at the start of this tag
73      *
74      * @throws JspTagException thrown when an error occurs while processing the
75      * body of this method
76      *
77      * @return SKIP_BODY int telling the tag handler to not evaluate the body
78      * of this tag
79      * EVAL_BODY_TAG int telling the tag handler to evaluate the body
80      * of this tag
81      *
82      */

83     public int doStartTag() throws JspException JavaDoc {
84         if (address != null && address.length() > 0 ) {
85             addToParent(address);
86             return SKIP_BODY;
87         }
88         return EVAL_BODY_TAG;
89     }
90
91
92     /**
93      * implementation of the method from the tag interface that tells the JSP
94      * page what to do after the body of this tag
95      *
96      * @throws JspException thrown when an error occurs while processing the
97      * body of this method
98      *
99      * @return SKIP_BODY int telling the tag handler to not evaluate the body
100      * of this tag again
101      *
102      */

103     public int doAfterBody() throws JspException JavaDoc {
104         BodyContent JavaDoc body = getBodyContent();
105         String JavaDoc addr = body.getString();
106         // Clear the body since we only used it as input for the email address
107
body.clearBody();
108         if (addr != null && addr.length() > 0 ) {
109             addToParent(addr);
110             return SKIP_BODY;
111         } else {
112             throw new JspException JavaDoc("addrecpient tag could not find an email address. set " +
113                              " the address attribute, or place the address in" +
114                              " the body of the tag.");
115         }
116     }
117
118     /**
119      * set the type of recipient for the address
120      *
121      * @param type string that is the type of the address either
122      * "to", "cc", or "bcc".
123      */

124     public void setType(String JavaDoc type) throws JspTagException JavaDoc {
125       this.type_string = type.trim();
126       if (type_string.equalsIgnoreCase(MailTag.TO_ADDRESS_STRING)) {
127           this.type = MailTag.TO_ADDRESS;
128       } else if (type_string.equalsIgnoreCase(MailTag.CC_ADDRESS_STRING)) {
129           this.type = MailTag.CC_ADDRESS;
130       } else if (type_string.equalsIgnoreCase(MailTag.BCC_ADDRESS_STRING)) {
131           this.type = MailTag.BCC_ADDRESS;
132       } else {
133           throw new JspTagException JavaDoc
134              ("setrecipient tag type attribute must be \"to\", \"cc\", or \"bcc\"");
135       }
136     }
137
138     /**
139      * set the value for an address to be later added to the email
140      *
141      * @param address string that is an address to be added to the
142      * "to", "cc", or "bcc" lists of addresses.
143      */

144     public void setAddress(String JavaDoc address) {
145       this.address = address.trim();
146     }
147
148     protected void addToParent(String JavaDoc addr) throws JspTagException JavaDoc {
149       // parent tag must be a MailTag, gives access to methods in parent
150
MailTag myparent =
151           (MailTag)findAncestorWithClass
152               (this, MailTag.class);
153
154       if (myparent == null) {
155           throw new JspTagException JavaDoc("setrecipient tag not nested within mail tag");
156       }
157
158       // Make sure type is set..either "to", "cc", or "bcc"
159
switch (type) {
160           case MailTag.TO_ADDRESS:
161               // set to in the parent tag
162
myparent.resetTo(addr);
163               break;
164           case MailTag.CC_ADDRESS:
165               // set cc in the parent tag
166
myparent.resetCc(addr);
167               break;
168           case MailTag.BCC_ADDRESS:
169               // set bcc in the parent tag
170
myparent.resetBcc(addr);
171               break;
172           default:
173               throw new JspTagException JavaDoc("setrecipient tag type attribute is not set. " +
174                            " Specify either \"to\", \"cc\", or \"bcc\".");
175       }
176     }
177 }
178
179
Popular Tags