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