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