1 /*
2 * $Id: AttributeTag.java,v 1.19 2007/04/27 22:00:11 ofung Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.webapp;
42
43
44 import javax.el.ELContext;
45 import javax.el.ExpressionFactory;
46 import javax.faces.component.UIComponent;
47 import javax.faces.context.FacesContext;
48 import javax.servlet.jsp.JspException;
49 import javax.servlet.jsp.tagext.TagSupport;
50
51
52 /**
53 * <p>Tag implementation that adds an attribute with a specified name
54 * and String value to the component whose tag it is nested inside,
55 * if the component does not already contain an attribute with the
56 * same name. This tag creates no output to the page currently
57 * being created.</p>
58 *
59 * @deprecated The Faces implementation must now provide the
60 * implementation for this class.
61 */
62
63 public class AttributeTag extends TagSupport {
64
65
66 // ---------------------------------------------------------- Static Members
67
68
69 private static final long serialVersionUID = -7782950243436672334L;
70
71
72 // ------------------------------------------------------------- Attributes
73
74
75 /**
76 * <p>The name of the attribute to be created, if not already present.
77 */
78 private String name = null;
79
80
81 /**
82 * <p>Set the attribute name.</p>
83 *
84 * @param name The new attribute name
85 */
86 public void setName(String name) {
87
88 this.name = name;
89
90 }
91
92
93 /**
94 * <p>The value to be associated with this attribute, if it is created.</p>
95 */
96 private String value = null;
97
98
99
100 /**
101 * <p>Set the attribute value.</p>
102 *
103 * @param value The new attribute value
104 */
105 public void setValue(String value) {
106
107 this.value = value;
108
109 }
110
111
112 // --------------------------------------------------------- Public Methods
113
114
115 /**
116 * <p>Register the specified attribute name and value with the
117 * {@link UIComponent} instance associated with our most immediately
118 * surrounding {@link UIComponentTag} instance, if this {@link UIComponent}
119 * does not already have a value for the specified attribute name.</p>
120 *
121 * @throws JspException if a JSP error occurs
122 */
123 public int doStartTag() throws JspException {
124
125 // Locate our parent UIComponentTag
126 UIComponentClassicTagBase tag =
127 UIComponentClassicTagBase.getParentUIComponentClassicTagBase(pageContext);
128 if (tag == null) { // PENDING - i18n
129 throw new JspException("Not nested in a UIComponentTag");
130 }
131
132 // Add this attribute if it is not already defined
133 UIComponent component = tag.getComponentInstance();
134 if (component == null) { // PENDING - i18n
135 throw new JspException("No component associated with UIComponentTag");
136 }
137
138 FacesContext context = FacesContext.getCurrentInstance();
139 ExpressionFactory exprFactory =
140 context.getApplication().getExpressionFactory();
141 ELContext elContext = context.getELContext();
142
143 String nameVal = (String)
144 exprFactory.createValueExpression(elContext, name, String.class)
145 .getValue(elContext);
146 Object valueVal =
147 exprFactory.createValueExpression(elContext, value, Object.class)
148 .getValue(elContext);
149
150 if (component.getAttributes().get(nameVal) == null) {
151 component.getAttributes().put(nameVal, valueVal);
152 }
153 return (SKIP_BODY);
154
155 }
156
157
158 public int doEndTag() throws JspException {
159 this.release();
160 return (EVAL_PAGE);
161 }
162
163
164 /**
165 * <p>Release references to any acquired resources.
166 */
167 public void release() {
168
169 this.name = null;
170 this.value = null;
171 }
172
173 }