001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.taglib.core;
017:
018: import javax.servlet.jsp.tagext.TagSupport;
019: import javax.servlet.jsp.JspException;
020: import javax.faces.webapp.UIComponentClassicTagBase;
021: import javax.faces.webapp.UIComponentELTag;
022: import javax.faces.component.UIComponent;
023: import javax.faces.context.FacesContext;
024: import javax.el.ValueExpression;
025: import javax.el.ELContext;
026:
027: /**
028: * @author Manfred Geiler (latest modification by $Author: matzew $)
029: * @author Bruno Aranda (JSR-252)
030: * @version $Revision: 521679 $ $Date: 2007-03-23 12:36:40 +0100 (Fr, 23 Mrz 2007) $
031: */
032: public class AttributeTag extends TagSupport {
033: private static final long serialVersionUID = 31476300171678632L;
034: private ValueExpression _nameExpression;
035: private ValueExpression _valueExpression;
036:
037: /**
038: * @param nameExpression
039: */
040: public void setName(ValueExpression nameExpression) {
041: _nameExpression = nameExpression;
042: }
043:
044: /**
045: * @param valueExpression
046: */
047: public void setValue(ValueExpression valueExpression) {
048: _valueExpression = valueExpression;
049: }
050:
051: public int doStartTag() throws JspException {
052: UIComponentClassicTagBase componentTag = UIComponentELTag
053: .getParentUIComponentClassicTagBase(pageContext);
054: if (componentTag == null) {
055: throw new JspException("no parent UIComponentTag found");
056: }
057: UIComponent component = componentTag.getComponentInstance();
058: if (component == null) {
059: throw new JspException(
060: "parent UIComponentTag has no UIComponent");
061: }
062:
063: FacesContext facesContext = FacesContext.getCurrentInstance();
064: ELContext elContext = facesContext.getELContext();
065:
066: String name = null;
067: Object value = null;
068: boolean isLiteral = false;
069:
070: if (_nameExpression != null) {
071: name = (String) _nameExpression.getValue(elContext);
072: }
073:
074: if (_valueExpression != null) {
075: isLiteral = _valueExpression.isLiteralText();
076: value = _valueExpression.getValue(elContext);
077: }
078:
079: if (name != null) {
080: if (component.getAttributes().get(name) == null) {
081: if (isLiteral) {
082: component.getAttributes().put(name, value);
083: } else {
084: component
085: .setValueExpression(name, _valueExpression);
086: }
087: }
088: }
089:
090: return SKIP_BODY;
091: }
092:
093: /**
094: * @deprecated
095: */
096: public void release() {
097: super.release();
098: _nameExpression = null;
099: _valueExpression = null;
100: }
101:
102: }
|