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.renderkit.html;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIGraphic;
25  import javax.faces.component.html.HtmlGraphicImage;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.myfaces.shared_orchestra.renderkit.JSFAttr;
32  import org.apache.myfaces.shared_orchestra.renderkit.html.HTML;
33  
34  
35  /**
36   * @author Manfred Geiler (latest modification by $Author: grantsmith $)
37   * @author Thomas Spiegl
38   * @author Anton Koinov
39   * @version $Revision$ $Date: 2005-05-11 18:45:06 +0200 (Wed, 11 May 2005) $
40   */
41  public class HtmlImageRendererBase
42          extends HtmlRenderer
43  {
44       private static final Log log = LogFactory.getLog(HtmlImageRendererBase.class);
45  
46          public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
47                  throws IOException
48          {
49              org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, UIGraphic.class);
50  
51              ResponseWriter writer = facesContext.getResponseWriter();
52  
53              String url;
54              if (uiComponent instanceof HtmlGraphicImage)
55              {
56                  url = ((HtmlGraphicImage)uiComponent).getUrl();
57              }
58              else
59              {
60                  url = (String)uiComponent.getAttributes().get(JSFAttr.URL_ATTR);
61              }
62  
63              writer.startElement(HTML.IMG_ELEM, uiComponent);
64  
65              HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
66  
67              if ((url != null) && (url.length() > 0))
68              {
69  
70                  String src = facesContext.getApplication()
71                                  .getViewHandler().getResourceURL(facesContext, url);
72                  writer.writeURIAttribute(HTML.SRC_ATTR,
73                                           facesContext.getExternalContext().encodeResourceURL(src),
74                                           null);
75              }
76              else
77              {
78                  if (log.isWarnEnabled()) log.warn("Graphic with id " + uiComponent.getClientId(facesContext) + " has no value (url).");
79              }
80              
81              /* 
82               * Warn the user if the ALT attribute is missing.
83               */                
84              if (uiComponent.getAttributes().get(HTML.ALT_ATTR) == null) 
85              {
86                  log.warn("ALT attribute is missing for : " + uiComponent.getId());
87              }            
88              
89              HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_PASSTHROUGH_ATTRIBUTES);
90              
91              writer.endElement(org.apache.myfaces.shared_orchestra.renderkit.html.HTML.IMG_ELEM);
92          }
93  
94  }