001    package org.apache.myfaces.tobago.taglib.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.apt.annotation.Tag;
021    import org.apache.myfaces.tobago.apt.annotation.BodyContent;
022    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
023    import org.apache.myfaces.tobago.component.ComponentUtil;
024    import org.apache.myfaces.tobago.component.UICommand;
025    import org.apache.myfaces.tobago.TobagoConstants;
026    
027    import javax.servlet.jsp.tagext.TagSupport;
028    import javax.servlet.jsp.JspException;
029    import javax.faces.webapp.UIComponentTag;
030    import javax.faces.component.UIComponent;
031    import javax.faces.component.EditableValueHolder;
032    import javax.faces.component.ValueHolder;
033    import javax.faces.el.ValueBinding;
034    import javax.faces.context.FacesContext;
035    
036    /*
037     * Date: Oct 14, 2006
038     * Time: 1:47:13 PM
039     */
040    
041    /**
042     * Add an attribute on the UIComponent
043     * associated with the closest parent UIComponent custom action.
044     */
045    @Tag(name = "attribute", bodyContent = BodyContent.EMPTY)
046    public class AttributeTag  extends TagSupport {
047    
048      private static final long serialVersionUID = 6231531736083277631L;
049    
050      /**
051       * <p>The name of the attribute</p>
052       */
053      private String name;
054    
055      /**
056       * <p>The value of the attribute</p>
057       */
058      private String value;
059    
060      /**
061       * The name of a attribute.
062       * @param name  A attribute name
063       */
064      @TagAttribute(required = true)
065      public void setName(String name) {
066        this.name = name;
067      }
068    
069      /**
070       * The value of a attribute
071       * @param value A attribute value
072       */
073      @TagAttribute(required = true)
074      public void setValue(String value) {
075        this.value = value;
076      }
077    
078      /**
079       * @throws javax.servlet.jsp.JspException if a JSP error occurs
080       */
081      public int doStartTag() throws JspException {
082    
083        // Locate our parent UIComponentTag
084        UIComponentTag tag =
085            UIComponentTag.getParentUIComponentTag(pageContext);
086        if (tag == null) {
087          // TODO Message resource i18n
088          throw new JspException("Not nested in faces tag");
089        }
090    
091        if (!tag.getCreated()) {
092          return (SKIP_BODY);
093        }
094    
095        UIComponent component = tag.getComponentInstance();
096        if (component == null) {
097          // TODO Message resource i18n
098          throw new JspException("Component Instance is null");
099        }
100        String attributeName = name;
101    
102        if (UIComponentTag.isValueReference(name)) {
103          ValueBinding valueBinding = ComponentUtil.createValueBinding(name);
104          if (valueBinding != null) {
105            attributeName = (String) valueBinding.getValue(FacesContext.getCurrentInstance());
106          } else {
107             // TODO Message resource i18n
108            throw new JspException("Can not get ValueBinding for attribute name " + name);
109          }
110        }
111        if (component instanceof EditableValueHolder
112            && TobagoConstants.ATTR_VALIDATOR.equals(attributeName)) {
113          ComponentUtil.setValidator((EditableValueHolder) component, value);
114        } else if (component instanceof ValueHolder
115            && TobagoConstants.ATTR_CONVERTER.equals(attributeName)) {
116          ComponentUtil.setConverter((ValueHolder) component, value);
117        } else if (TobagoConstants.ATTR_STYLE_CLASS.equals(attributeName)) {
118          ComponentUtil.setStyleClasses(component, value);
119        } else if (TobagoConstants.ATTR_RENDERED_PARTIALLY.equals(attributeName)
120            && component instanceof UICommand) {
121          ComponentUtil.setRenderedPartially((UICommand) component, value);
122        } else if (UIComponentTag.isValueReference(value)) {
123          ValueBinding valueBinding = ComponentUtil.createValueBinding(value);
124          if (valueBinding != null) {
125            component.setValueBinding(name, valueBinding);
126          } else {
127             // TODO Message resource i18n
128            throw new JspException("Can not get ValueBinding for attribute value " + value);
129          }
130        } else {
131          component.getAttributes().put(attributeName, value);
132        }
133    
134        return (SKIP_BODY);
135      }
136    
137    
138      /**
139       * <p>Release references to any acquired resources.
140       */
141      public void release() {
142        super.release();
143        this.name = null;
144        this.value = null;
145      }
146    }