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.custom.media;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  import javax.faces.render.Renderer;
27  
28  import org.apache.myfaces.custom.media.util.MediaConstants;
29  import org.apache.myfaces.custom.media.util.MediaUtil;
30  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
31  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
33  
34  /**
35   * 
36   * @JSFRenderer
37   *   renderKitId = "HTML_BASIC" 
38   *   family = "org.apache.myfaces.media"
39   *   type = "org.apache.myfaces.media"
40   *
41   * @author Hazem Saleh
42   *
43   */
44  public class MediaRenderer extends Renderer {
45  
46      public void encodeBegin(FacesContext context, UIComponent component)
47          throws IOException {
48  
49      MediaComponent mediaComponent = (MediaComponent) component;
50  
51      RendererUtils.checkParamValidity(context, component,
52          MediaComponent.class);
53  
54      generateMediaTag(context, mediaComponent);
55      }
56  
57      public void encodeEnd(FacesContext context, UIComponent component)
58          throws IOException {
59      super.encodeEnd(context, component);
60      }
61  
62      /*
63       * This helper method is used for generating the suitable media tag.
64       */
65      private void generateMediaTag(FacesContext context,
66          MediaComponent mediaComponent) throws IOException {
67      
68      String source = mediaComponent.getSource();    
69      
70      /* if the src uri is an image then use the <img> tag */
71      if(MediaUtil.isImage(source)) 
72      {
73          MediaUtil.generateImageTag(context, mediaComponent);
74      } 
75      else /* generate EMBED Tag */
76      {
77          MediaUtil.generateEmbedTag(context, mediaComponent);
78      }
79      
80      }    
81  }