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 Logger log = Logger.getLogger(HtmlImageRendererBase.class.getName());
44      
45      @Override
46      public void decode(FacesContext context, UIComponent component)
47      {
48          super.decode(context, component);
49          
50          HtmlRendererUtils.decodeClientBehaviors(context, component);
51      }
52  
53      @Override
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 && !behaviors.isEmpty())
75          {
76              HtmlRendererUtils.writeIdAndName(writer, uiComponent, facesContext);
77          }
78          else
79          {
80              HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
81          }
82  
83          final String url = RendererUtils.getIconSrc(facesContext, uiComponent, JSFAttr.URL_ATTR);
84          if (url != null)
85          {
86              writer.writeURIAttribute(HTML.SRC_ATTR, url,JSFAttr.VALUE_ATTR);
87          }
88          else
89          {
90              if (facesContext.isProjectStage(ProjectStage.Development) && log.isLoggable(Level.WARNING))
91              {
92                  log.warning("Component UIGraphic " + uiComponent.getClientId(facesContext) 
93                          + " has no attribute url, value, name or attribute resolves to null. Path to component " 
94                          + RendererUtils.getPathToComponent(uiComponent));
95              }
96          }
97  
98          /* 
99           * Warn the user if the ALT attribute is missing.
100          */                
101         if (uiComponent.getAttributes().get(HTML.ALT_ATTR) == null) 
102         {
103             if(facesContext.isProjectStage(ProjectStage.Development) && log.isLoggable(Level.WARNING))
104             {
105                 log.warning("Component UIGraphic " + uiComponent.getClientId(facesContext) 
106                         + " has no attribute alt or attribute resolves to null. Path to component " 
107                         + RendererUtils.getPathToComponent(uiComponent));
108             }
109         }
110 
111         if (uiComponent instanceof ClientBehaviorHolder)
112         {
113             if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext))
114             {
115                 CommonPropertyUtils.renderEventProperties(writer, 
116                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
117             }
118             else
119             {
120                 if (isCommonEventsOptimizationEnabled(facesContext))
121                 {
122                     CommonEventUtils.renderBehaviorizedEventHandlers(facesContext, writer, 
123                            CommonPropertyUtils.getCommonPropertiesMarked(uiComponent),
124                            CommonEventUtils.getCommonEventsMarked(uiComponent), uiComponent, behaviors);
125                 }
126                 else
127                 {
128                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
129                 }
130             }
131             if (isCommonPropertiesOptimizationEnabled(facesContext))
132             {
133                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_ATTRIBUTES);
134                 CommonPropertyUtils.renderCommonPassthroughPropertiesWithoutEvents(writer, 
135                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
136             }
137             else
138             {
139                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
140                         HTML.IMG_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
141             }
142         }
143         else
144         {
145             if (isCommonPropertiesOptimizationEnabled(facesContext))
146             {
147                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_ATTRIBUTES);
148                 CommonPropertyUtils.renderCommonPassthroughProperties(writer, 
149                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
150             }
151             else
152             {
153                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
154                         HTML.IMG_PASSTHROUGH_ATTRIBUTES);
155             }
156         }
157 
158         writer.endElement(org.apache.myfaces.shared.renderkit.html.HTML.IMG_ELEM);
159 
160     }
161 
162 }