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