View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.shared_orchestra.taglib;
    20
    21  import org.apache.myfaces.shared_orchestra.renderkit.JSFAttr;
    22
    23  import org.apache.commons.logging.Log;
    24  import org.apache.commons.logging.LogFactory;
    25
    26  import javax.faces.component.UIComponent;
    27  import javax.faces.webapp.UIComponentBodyTag;
    28  import javax.servlet.jsp.JspException;
    29  import javax.servlet.jsp.tagext.BodyContent;
    30  import java.io.IOException;
    31  import java.io.Reader;
    32
    33  /**
    34   * @author Manfred Geiler (latest modification by $Author: grantsmith $)
    35   * @version $Revision: 472630 $ $Date: 2006-11-08 15:40:03 -0500 (Wed, 08 Nov 2006) $
    36   */
    37  public abstract class UIComponentBodyTagBase
    38          extends UIComponentBodyTag
    39  {
    40      private static final Log log = LogFactory.getLog(UIComponentBodyTagBase.class);
    41
    42      public int doEndTag() throws JspException
    43      {
    44          if (log.isWarnEnabled())
    45          {
    46              UIComponent component = getComponentInstance();
    47              if (component != null &&
    48                  component.getRendersChildren() &&
    49                  !isBodyContentEmpty())
    50              {
    51                  log.warn("Component with id '" + component.getClientId(getFacesContext()) +
    52                           "' (" + getClass().getName() +
    53                           " tag) and path : "+org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getPathToComponent(component)+"renders it's children, but has embedded JSP or HTML code. Use the <f:verbatim> tag for nested HTML. For comments use <%/* */%> style JSP comments instead of <!-- --> style HTML comments." +
    54                           "\n BodyContent:\n" + getBodyContent().getString().trim());
    55              }
    56          }
    57          return super.doEndTag();
    58      }
    59
    60      /**
    61       * TODO: Ignore <!-- --> comments
    62       */
    63      private boolean isBodyContentEmpty()
    64      {
    65          BodyContent bodyContent = getBodyContent();
    66          if (bodyContent == null)
    67          {
    68              return true;
    69          }
    70          try
    71          {
    72              Reader reader = bodyContent.getReader();
    73              int c;
    74              while ((c = reader.read()) != -1)
    75              {
    76                  if (!Character.isWhitespace((char)c))
    77                  {
    78                      return false;
    79                  }
    80              }
    81              return true;
    82          }
    83          catch (IOException e)
    84          {
    85              log.error("Error inspecting BodyContent", e);
    86              return false;
    87          }
    88      }
    89
    90      //-------- rest is identical to UIComponentTagBase ------------------
    91
    92      //UIComponent attributes
    93      private String _forceId;
    94      private String _forceIdIndex = "true";
    95
    96      //Special UIComponent attributes (ValueHolder, ConvertibleValueHolder)
    97      private String _value;
    98      private String _converter;
    99      //attributes id, rendered and binding are handled by UIComponentTag
    100
    101     protected void setProperties(UIComponent component)
    102     {
    103         super.setProperties(component);
    104
    105         setBooleanProperty(component, JSFAttr.FORCE_ID_ATTR, _forceId);
    106         setBooleanProperty(component, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.FORCE_ID_INDEX_ATTR, _forceIdIndex);
    107
    108         //rendererType already handled by UIComponentTag
    109
    110         setValueProperty(component, _value);
    111         setConverterProperty(component, _converter);
    112     }
    113
    114     /**
    115      * Sets the forceId attribute of the tag.  NOTE: Not every tag that extends this class will
    116      * actually make use of this attribute.  Check the TLD to see which components actually
    117      * implement it.
    118      *
    119      * @param aForceId The value of the forceId attribute.
    120      */
    121     public void setForceId(String aForceId)
    122     {
    123         _forceId = aForceId;
    124     }
    125
    126     /**
    127      * Sets the forceIdIndex attribute of the tag.  NOTE: Not every tag that extends this class will
    128      * actually make use of this attribute.  Check the TLD to see which components actually implement it.
    129      *
    130      * @param aForceIdIndex The value of the forceIdIndex attribute.
    131      */
    132     public void setForceIdIndex(String aForceIdIndex)
    133     {
    134         _forceIdIndex = aForceIdIndex;
    135     }
    136
    137     public void setValue(String value)
    138     {
    139         _value = value;
    140     }
    141
    142     public void setConverter(String converter)
    143     {
    144         _converter = converter;
    145     }
    146
    147
    148
    149     // sub class helpers
    150
    151     protected void setIntegerProperty(UIComponent component, String propName, String value)
    152     {
    153         UIComponentTagUtils.setIntegerProperty(getFacesContext(), component, propName, value);
    154     }
    155
    156     protected void setStringProperty(UIComponent component, String propName, String value)
    157     {
    158         UIComponentTagUtils.setStringProperty(getFacesContext(), component, propName, value);
    159     }
    160
    161     protected void setBooleanProperty(UIComponent component, String propName, String value)
    162     {
    163         UIComponentTagUtils.setBooleanProperty(getFacesContext(), component, propName, value);
    164     }
    165
    166     protected void setValueProperty(UIComponent component, String value)
    167     {
    168         UIComponentTagUtils.setValueProperty(getFacesContext(), component, value);
    169     }
    170
    171     private void setConverterProperty(UIComponent component, String value)
    172     {
    173         UIComponentTagUtils.setConverterProperty(getFacesContext(), component, value);
    174     }
    175
    176     protected void setValidatorProperty(UIComponent component, String value)
    177     {
    178         UIComponentTagUtils.setValidatorProperty(getFacesContext(), component, value);
    179     }
    180
    181     protected void setActionProperty(UIComponent component, String action)
    182     {
    183         UIComponentTagUtils.setActionProperty(getFacesContext(), component, action);
    184     }
    185
    186     protected void setActionListenerProperty(UIComponent component, String actionListener)
    187     {
    188         UIComponentTagUtils.setActionListenerProperty(getFacesContext(), component, actionListener);
    189     }
    190
    191     protected void setValueChangedListenerProperty(UIComponent component, String valueChangedListener)
    192     {
    193         UIComponentTagUtils.setValueChangedListenerProperty(getFacesContext(), component, valueChangedListener);
    194     }
    195
    196     protected void setValueBinding(UIComponent component,
    197                                    String propName,
    198                                    String value)
    199     {
    200         UIComponentTagUtils.setValueBinding(getFacesContext(), component, propName, value);
    201     }
    202
    203 }