001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.el.fmt;
040:
041: import javax.servlet.jsp.JspException;
042: import javax.servlet.jsp.jstl.fmt.LocalizationContext;
043:
044: import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
045: import org.apache.taglibs.standard.tag.common.fmt.MessageSupport;
046:
047: /**
048: * <p>A handler for <message> that accepts attributes as Strings
049: * and evaluates them as expressions at runtime.</p>
050: *
051: * @author Jan Luehe
052: */
053:
054: public class MessageTag extends MessageSupport {
055:
056: //*********************************************************************
057: // Private state (implementation details)
058:
059: private String key_; // stores EL-based property
060: private String bundle_; // stores EL-based property
061:
062: //*********************************************************************
063: // Constructor
064:
065: /**
066: * Constructs a new MessageTag. As with TagSupport, subclasses
067: * should not provide other constructors and are expected to call
068: * the superclass constructor
069: */
070: public MessageTag() {
071: super ();
072: init();
073: }
074:
075: //*********************************************************************
076: // Tag logic
077:
078: // evaluates expression and chains to parent
079: public int doStartTag() throws JspException {
080:
081: // evaluate any expressions we were passed, once per invocation
082: evaluateExpressions();
083:
084: // chain to the parent implementation
085: return super .doStartTag();
086: }
087:
088: // Releases any resources we may have (or inherit)
089: public void release() {
090: super .release();
091: init();
092: }
093:
094: //*********************************************************************
095: // Accessor methods
096:
097: // for EL-based attribute
098: public void setKey(String key_) {
099: this .key_ = key_;
100: this .keySpecified = true;
101: }
102:
103: // for EL-based attribute
104: public void setBundle(String bundle_) {
105: this .bundle_ = bundle_;
106: this .bundleSpecified = true;
107: }
108:
109: //*********************************************************************
110: // Private (utility) methods
111:
112: // (re)initializes state (during release() or construction)
113: private void init() {
114: // null implies "no expression"
115: key_ = bundle_ = null;
116: }
117:
118: // Evaluates expressions as necessary
119: private void evaluateExpressions() throws JspException {
120: /*
121: * Note: we don't check for type mismatches here; we assume
122: * the expression evaluator will return the expected type
123: * (by virtue of knowledge we give it about what that type is).
124: * A ClassCastException here is truly unexpected, so we let it
125: * propagate up.
126: */
127:
128: if (keySpecified) {
129: keyAttrValue = (String) ExpressionEvaluatorManager
130: .evaluate("key", key_, String.class, this ,
131: pageContext);
132: }
133:
134: if (bundleSpecified) {
135: bundleAttrValue = (LocalizationContext) ExpressionEvaluatorManager
136: .evaluate("bundle", bundle_,
137: LocalizationContext.class, this,
138: pageContext);
139: }
140: }
141: }
|