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.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.logging.Level;
25  import java.util.logging.Logger;
26  
27  import javax.faces.application.ProjectStage;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIGraphic;
30  import javax.faces.component.behavior.ClientBehavior;
31  import javax.faces.component.behavior.ClientBehaviorHolder;
32  import javax.faces.context.FacesContext;
33  import javax.faces.context.ResponseWriter;
34  
35  import org.apache.myfaces.shared.renderkit.JSFAttr;
36  import org.apache.myfaces.shared.renderkit.RendererUtils;
37  import org.apache.myfaces.shared.renderkit.html.util.JavascriptUtils;
38  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
39  
40  
41  /**
42   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
43   * @author Thomas Spiegl
44   * @author Anton Koinov
45   * @version $Revision: 1349291 $ $Date: 2012-06-12 12:20:24 +0000 (Tue, 12 Jun 2012) $
46   */
47  public class HtmlImageRendererBase
48          extends HtmlRenderer
49  {
50      //private static final Log log = LogFactory.getLog(HtmlImageRendererBase.class);
51      private static final Logger log = Logger.getLogger(HtmlImageRendererBase.class.getName());
52      
53      public void decode(FacesContext context, UIComponent component)
54      {
55          //check for npe
56          super.decode(context, component);
57          
58          HtmlRendererUtils.decodeClientBehaviors(context, component);
59      }
60  
61      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
62              throws IOException
63      {
64          org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(
65                  facesContext, uiComponent, UIGraphic.class);
66  
67          ResponseWriter writer = facesContext.getResponseWriter();
68          
69          Map<String, List<ClientBehavior>> behaviors = null;
70          if (uiComponent instanceof ClientBehaviorHolder)
71          {
72              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
73              if (!behaviors.isEmpty())
74              {
75                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
76              }
77          }
78          
79          writer.startElement(HTML.IMG_ELEM, uiComponent);
80  
81          if (uiComponent instanceof ClientBehaviorHolder 
82                  && JavascriptUtils.isJavascriptAllowed(facesContext.getExternalContext())
83                  && !behaviors.isEmpty())
84          {
85              HtmlRendererUtils.writeIdAndName(writer, uiComponent, facesContext);
86          }
87          else
88          {
89              HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
90          }
91  
92          final String url = RendererUtils.getIconSrc(facesContext, uiComponent, JSFAttr.URL_ATTR);
93          if (url != null)
94          {
95              writer.writeURIAttribute(HTML.SRC_ATTR, url,JSFAttr.VALUE_ATTR);
96          }
97          else
98          {
99            if (facesContext.isProjectStage(ProjectStage.Development) && log.isLoggable(Level.WARNING))
100           {
101               log.warning("Component UIGraphic " + uiComponent.getClientId(facesContext) 
102                       + " has no attribute url, value, name or attribute resolves to null. Path to component " 
103                       + RendererUtils.getPathToComponent(uiComponent));
104           }
105         }
106 
107         /* 
108          * Warn the user if the ALT attribute is missing.
109          */                
110         if (uiComponent.getAttributes().get(HTML.ALT_ATTR) == null) 
111         {
112             if(facesContext.isProjectStage(ProjectStage.Development) && log.isLoggable(Level.WARNING))
113             {
114                 log.warning("Component UIGraphic " + uiComponent.getClientId(facesContext) 
115                         + " has no attribute alt or attribute resolves to null. Path to component " 
116                         + RendererUtils.getPathToComponent(uiComponent));
117             }
118         }
119 
120         if (uiComponent instanceof ClientBehaviorHolder && JavascriptUtils.isJavascriptAllowed(
121                 facesContext.getExternalContext()))
122         {
123             if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext))
124             {
125                 CommonPropertyUtils.renderEventProperties(writer, 
126                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
127             }
128             else
129             {
130                 if (isCommonEventsOptimizationEnabled(facesContext))
131                 {
132                     CommonEventUtils.renderBehaviorizedEventHandlers(facesContext, writer, 
133                            CommonPropertyUtils.getCommonPropertiesMarked(uiComponent),
134                            CommonEventUtils.getCommonEventsMarked(uiComponent), uiComponent, behaviors);
135                 }
136                 else
137                 {
138                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
139                 }
140             }
141             if (isCommonPropertiesOptimizationEnabled(facesContext))
142             {
143                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_ATTRIBUTES);
144                 CommonPropertyUtils.renderCommonPassthroughPropertiesWithoutEvents(writer, 
145                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
146             }
147             else
148             {
149                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
150                         HTML.IMG_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
151             }
152         }
153         else
154         {
155             if (isCommonPropertiesOptimizationEnabled(facesContext))
156             {
157                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_ATTRIBUTES);
158                 CommonPropertyUtils.renderCommonPassthroughProperties(writer, 
159                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
160             }
161             else
162             {
163                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
164                         HTML.IMG_PASSTHROUGH_ATTRIBUTES);
165             }
166         }
167 
168         writer.endElement(org.apache.myfaces.shared.renderkit.html.HTML.IMG_ELEM);
169 
170     }
171 
172 }