KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > el > fmt > MessageTag


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.standard.tag.el.fmt;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
21
22 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
23 import org.apache.taglibs.standard.tag.common.fmt.MessageSupport;
24
25 /**
26  * <p>A handler for &lt;message&gt; that accepts attributes as Strings
27  * and evaluates them as expressions at runtime.</p>
28  *
29  * @author Jan Luehe
30  */

31
32 public class MessageTag extends MessageSupport {
33
34     //*********************************************************************
35
// Private state (implementation details)
36

37     private String JavaDoc key_; // stores EL-based property
38
private String JavaDoc bundle_; // stores EL-based property
39

40
41     //*********************************************************************
42
// Constructor
43

44     /**
45      * Constructs a new MessageTag. As with TagSupport, subclasses
46      * should not provide other constructors and are expected to call
47      * the superclass constructor
48      */

49     public MessageTag() {
50         super();
51         init();
52     }
53
54
55     //*********************************************************************
56
// Tag logic
57

58     // evaluates expression and chains to parent
59
public int doStartTag() throws JspException JavaDoc {
60
61         // evaluate any expressions we were passed, once per invocation
62
evaluateExpressions();
63
64     // chain to the parent implementation
65
return super.doStartTag();
66     }
67
68     // Releases any resources we may have (or inherit)
69
public void release() {
70         super.release();
71         init();
72     }
73
74
75     //*********************************************************************
76
// Accessor methods
77

78     // for EL-based attribute
79
public void setKey(String JavaDoc key_) {
80         this.key_ = key_;
81     this.keySpecified = true;
82     }
83
84     // for EL-based attribute
85
public void setBundle(String JavaDoc bundle_) {
86         this.bundle_ = bundle_;
87         this.bundleSpecified = true;
88     }
89
90
91     //*********************************************************************
92
// Private (utility) methods
93

94     // (re)initializes state (during release() or construction)
95
private void init() {
96         // null implies "no expression"
97
key_ = bundle_ = null;
98     }
99
100     // Evaluates expressions as necessary
101
private void evaluateExpressions() throws JspException JavaDoc {
102         /*
103          * Note: we don't check for type mismatches here; we assume
104          * the expression evaluator will return the expected type
105          * (by virtue of knowledge we give it about what that type is).
106          * A ClassCastException here is truly unexpected, so we let it
107          * propagate up.
108          */

109
110     if (keySpecified) {
111         keyAttrValue = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
112             "key", key_, String JavaDoc.class, this, pageContext);
113     }
114
115     if (bundleSpecified) {
116         bundleAttrValue = (LocalizationContext)
117         ExpressionEvaluatorManager.evaluate(
118                 "bundle", bundle_, LocalizationContext.class, this,
119             pageContext);
120     }
121     }
122 }
123
Popular Tags