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.taglib;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import javax.el.MethodExpression;
25  import javax.el.ValueExpression;
26  import javax.faces.component.UIComponent;
27  import javax.faces.webapp.UIComponentELTag;
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: skitching $)
35   * @version $Revision: 673827 $ $Date: 2008-07-03 16:46:23 -0500 (Thu, 03 Jul 2008) $
36   */
37  public abstract class UIComponentBodyELTagBase extends UIComponentELTag
38  {
39      private static final Log log = LogFactory.getLog(UIComponentBodyTagBase.class);
40  
41      public int doEndTag() throws JspException
42      {
43          if (log.isWarnEnabled())
44          {
45              UIComponent component = getComponentInstance();
46              if (component != null &&
47                  component.getRendersChildren() &&
48                  !isBodyContentEmpty())
49              {
50                  log.warn("Component with id '" + component.getClientId(getFacesContext()) +
51                           "' (" + getClass().getName() +
52                           " tag) and path : "+org.apache.myfaces.shared.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." +
53                           "\n BodyContent:\n" + getBodyContent().getString().trim());
54              }
55          }
56          return super.doEndTag();
57      }
58  
59      /***
60       * TODO: Ignore <!-- --> comments
61       */
62      private boolean isBodyContentEmpty()
63      {
64          BodyContent bodyContent = getBodyContent();
65          if (bodyContent == null)
66          {
67              return true;
68          }
69          try
70          {
71              Reader reader = bodyContent.getReader();
72              int c;
73              while ((c = reader.read()) != -1)
74              {
75                  if (!Character.isWhitespace((char)c))
76                  {
77                      return false;
78                  }
79              }
80              return true;
81          }
82          catch (IOException e)
83          {
84              log.error("Error inspecting BodyContent", e);
85              return false;
86          }
87      }
88  
89      //-------- rest is identical to UIComponentTagBase ------------------
90  
91      //UIComponent attributes
92      private ValueExpression _forceId;
93      private ValueExpression _forceIdIndex;
94      private static final Boolean DEFAULT_FORCE_ID_INDEX_VALUE = Boolean.TRUE;
95  
96      //Special UIComponent attributes (ValueHolder, ConvertibleValueHolder)
97      private ValueExpression _value;
98      private ValueExpression _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, org.apache.myfaces.shared.renderkit.JSFAttr.FORCE_ID_ATTR, _forceId);
106         setBooleanProperty(component, org.apache.myfaces.shared.renderkit.JSFAttr.FORCE_ID_INDEX_ATTR, _forceIdIndex, DEFAULT_FORCE_ID_INDEX_VALUE);
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(ValueExpression 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(ValueExpression aForceIdIndex)
133     {
134         _forceIdIndex = aForceIdIndex;
135     }
136 
137     public void setValue(ValueExpression value)
138     {
139         _value = value;
140     }
141 
142     public void setConverter(ValueExpression converter)
143     {
144         _converter = converter;
145     }
146 
147 
148 
149     // sub class helpers
150     
151     protected void setIntegerProperty(UIComponent component, String propName, ValueExpression value)
152     {
153         UIComponentELTagUtils.setIntegerProperty(component, propName, value);
154     }
155     
156     @Deprecated
157     protected void setIntegerProperty(UIComponent component, String propName, String value)
158     {
159         UIComponentTagUtils.setIntegerProperty(getFacesContext(), component, propName, value);
160     }
161     
162     protected void setStringProperty(UIComponent component, String propName, ValueExpression value)
163     {
164         UIComponentELTagUtils.setStringProperty(component, propName, value);
165     }
166 
167     @Deprecated
168     protected void setStringProperty(UIComponent component, String propName, String value)
169     {
170         UIComponentTagUtils.setStringProperty(getFacesContext(), component, propName, value);
171     }
172     
173     protected void setBooleanProperty(UIComponent component, String propName, ValueExpression value)
174     {
175         UIComponentELTagUtils.setBooleanProperty(component, propName, value);
176     }
177     
178     protected void setBooleanProperty(UIComponent component, String propName, ValueExpression value, Boolean defaultValue)
179     {
180         UIComponentELTagUtils.setBooleanProperty(component, propName, value, defaultValue);
181     }
182 
183     @Deprecated
184     protected void setBooleanProperty(UIComponent component, String propName, String value)
185     {
186         UIComponentTagUtils.setBooleanProperty(getFacesContext(), component, propName, value);
187     }
188     
189     private void setValueProperty(UIComponent component, ValueExpression value)
190     {
191         UIComponentELTagUtils.setValueProperty(getFacesContext(), component, value);
192     }
193 
194     @Deprecated
195     protected void setValueProperty(UIComponent component, String value)
196     {
197         UIComponentTagUtils.setValueProperty(getFacesContext(), component, value);
198     }
199     
200     private void setConverterProperty(UIComponent component, ValueExpression value)
201     {
202         UIComponentELTagUtils.setConverterProperty(getFacesContext(), component, value);
203     }
204 
205 
206     @Deprecated
207     private void setConverterProperty(UIComponent component, String value)
208     {
209         UIComponentTagUtils.setConverterProperty(getFacesContext(), component, value);
210     }
211     
212     protected void addValidatorProperty(UIComponent component, MethodExpression value)
213     {
214         UIComponentELTagUtils.addValidatorProperty(getFacesContext(), component, value);
215     }
216 
217     @Deprecated
218     protected void setValidatorProperty(UIComponent component, String value)
219     {
220         UIComponentTagUtils.setValidatorProperty(getFacesContext(), component, value);
221     }
222     
223     protected void setActionProperty(UIComponent component, MethodExpression action)
224     {
225         UIComponentELTagUtils.setActionProperty(getFacesContext(), component, action);
226     }
227 
228     @Deprecated
229     protected void setActionProperty(UIComponent component, String action)
230     {
231         UIComponentTagUtils.setActionProperty(getFacesContext(), component, action);
232     }
233     
234     protected void setActionListenerProperty(UIComponent component, MethodExpression actionListener)
235     {
236         UIComponentELTagUtils.addActionListenerProperty(getFacesContext(), component, actionListener);
237     }
238 
239     @Deprecated
240     protected void setActionListenerProperty(UIComponent component, String actionListener)
241     {
242         UIComponentTagUtils.setActionListenerProperty(getFacesContext(), component, actionListener);
243     }
244     
245     protected void addValueChangedListenerProperty(UIComponent component, MethodExpression valueChangedListener)
246     {
247         UIComponentELTagUtils.addValueChangedListenerProperty(getFacesContext(), component, valueChangedListener);
248     }
249 
250     @Deprecated
251     protected void setValueChangedListenerProperty(UIComponent component, String valueChangedListener)
252     {
253         UIComponentTagUtils.setValueChangedListenerProperty(getFacesContext(), component, valueChangedListener);
254     }
255 
256     protected void setValueBinding(UIComponent component,
257             String propName,
258             ValueExpression value)
259     {
260         UIComponentELTagUtils.setValueBinding(getFacesContext(), component, propName, value);
261     }
262 
263 }