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 /** 049 * <p>The name of the attribute</p> 050 */ 051 private String name; 052 /** 053 * <p>The value of the attribute</p> 054 */ 055 private String value; 056 /** 057 * The name of a attribute. 058 * @param name A attribute name 059 */ 060 @TagAttribute(required = true) 061 public void setName(String name) { 062 this.name = name; 063 } 064 065 /** 066 * The value of a attribute 067 * @param value A attribute value 068 */ 069 @TagAttribute(required = true) 070 public void setValue(String value) { 071 this.value = value; 072 } 073 074 /** 075 * @throws javax.servlet.jsp.JspException if a JSP error occurs 076 */ 077 public int doStartTag() throws JspException { 078 079 // Locate our parent UIComponentTag 080 UIComponentTag tag = 081 UIComponentTag.getParentUIComponentTag(pageContext); 082 if (tag == null) { 083 // TODO Message resource i18n 084 throw new JspException("Not nested in faces tag"); 085 } 086 087 if (!tag.getCreated()) { 088 return (SKIP_BODY); 089 } 090 091 UIComponent component = tag.getComponentInstance(); 092 if (component == null) { 093 // TODO Message resource i18n 094 throw new JspException("Component Instance is null"); 095 } 096 String attributeName = name; 097 098 if (UIComponentTag.isValueReference(name)) { 099 ValueBinding valueBinding = ComponentUtil.createValueBinding(name); 100 if (valueBinding != null) { 101 attributeName = (String) valueBinding.getValue(FacesContext.getCurrentInstance()); 102 } else { 103 // TODO Message resource i18n 104 throw new JspException("Can not get ValueBinding for attribute name " + name); 105 } 106 } 107 if (component instanceof EditableValueHolder 108 && TobagoConstants.ATTR_VALIDATOR.equals(attributeName)) { 109 ComponentUtil.setValidator((EditableValueHolder) component, value); 110 } else if (component instanceof ValueHolder 111 && TobagoConstants.ATTR_CONVERTER.equals(attributeName)) { 112 ComponentUtil.setConverter((ValueHolder) component, value); 113 } else if (TobagoConstants.ATTR_STYLE_CLASS.equals(attributeName)) { 114 ComponentUtil.setStyleClasses(component, value); 115 } else if (TobagoConstants.ATTR_RENDERED_PARTIALLY.equals(attributeName) 116 && component instanceof UICommand) { 117 ComponentUtil.setRenderedPartially((UICommand) component, value); 118 } else if (UIComponentTag.isValueReference(value)) { 119 ValueBinding valueBinding = ComponentUtil.createValueBinding(value); 120 if (valueBinding != null) { 121 component.setValueBinding(name, valueBinding); 122 } else { 123 // TODO Message resource i18n 124 throw new JspException("Can not get ValueBinding for attribute value " + value); 125 } 126 } else { 127 component.getAttributes().put(attributeName, value); 128 } 129 130 return (SKIP_BODY); 131 } 132 133 134 /** 135 * <p>Release references to any acquired resources. 136 */ 137 public void release() { 138 super.release(); 139 this.name = null; 140 this.value = null; 141 } 142 }