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.util;
20  
21  import java.io.IOException;
22  
23  import javax.faces.context.FacesContext;
24  import javax.faces.context.ResponseWriter;
25  
26  import org.apache.myfaces.custom.media.MediaComponent;
27  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
28  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
29  
30  /**
31   * This class acts as a helper for the Media component. 
32   */
33  public class MediaUtil {
34      
35      final static String[] IMAGES_EXTENSIONS = new String[] {
36      "jpg",
37      "jpeg",
38      "png",
39      "mng",
40      "bmp",
41      "gif",
42      "dxf"
43      };
44      
45      /**
46       * This method checks whether the passed uri contains
47       * image.
48       * @param uri
49       * @return boolean.
50       */
51      public static boolean isImage(String uri) {
52      
53      for(int i = 0; i < IMAGES_EXTENSIONS.length; ++i) 
54      {
55          if(uri.endsWith(IMAGES_EXTENSIONS[i])) 
56          {
57          return true;
58          }
59      }
60      
61      return false;
62      }
63      
64      /**
65       * This method is used for generating the EMBED Tag 
66       * from the mediaComponent attributes.
67       * @param context
68       * @param mediaComponent
69       * @throws IOException
70       */
71      public static void generateEmbedTag(FacesContext context,
72          MediaComponent mediaComponent) throws IOException {
73  
74      ResponseWriter writer = context.getResponseWriter();
75      String source = mediaComponent.getSource();
76      String contentType = mediaComponent.getContentType();
77      String width = mediaComponent.getWidth();
78      String height = mediaComponent.getHeight();
79  
80      /* start writing the media component */
81      writer.startElement(MediaConstants.EMBED_ELEM, mediaComponent);
82  
83      writer.writeAttribute(HTML.ID_ATTR,
84          mediaComponent.getClientId(context), null);
85      writer.writeAttribute(HTML.NAME_ATTR, mediaComponent.getId(),
86          JSFAttr.ID_ATTR);
87  
88      writer.writeAttribute(HTML.SRC_ATTR, source, null);
89  
90      writer.writeAttribute(HTML.TYPE_ATTR, contentType, null);
91  
92      /* write the rest of attributes */
93  
94      if (width != null && !"".equals(width)) {
95          writer.writeAttribute(HTML.WIDTH_ATTR, width, null);
96      }
97  
98      if (height != null && !"".equals(height)) {
99          writer.writeAttribute(HTML.HEIGHT_ATTR, height, null);
100     }
101 
102     writer.writeAttribute(MediaConstants.PLUGINSPAGE_ATTR,
103         MediaConstants.DEFAULT_MEDIA_PLUGIN_PAGE, null);
104     writer.writeAttribute(MediaConstants.SHOW_GOTO_BAR_ATTR, "true", null);
105     writer.writeAttribute(MediaConstants.SHOW_DISPLAY_ATTR, "true", null);
106     writer
107         .writeAttribute(MediaConstants.SHOW_STATUS_BAR_ATTR, "true",
108             null);
109 
110     /* if no EMBED not supported */
111     writer.startElement(MediaConstants.NO_EMBED_ELEM, mediaComponent);
112     writer.write("<a href=\"" + 
113             source + 
114             "\">EMBED is not supported, Click here to see the resource</a>");
115     writer.endElement(MediaConstants.NO_EMBED_ELEM);
116     }
117     
118     /**
119      * This method is used for generating the IMG tag 
120      * from the mediaComponent attributes. 
121      * @param context
122      * @param mediaComponent
123      * @throws IOException
124      */
125     public static void generateImageTag(FacesContext context,
126         MediaComponent mediaComponent) throws IOException {
127 
128     ResponseWriter writer = context.getResponseWriter();
129     String source = mediaComponent.getSource();
130     String width = mediaComponent.getWidth();
131     String height = mediaComponent.getHeight();
132 
133     /* start writing the media component */
134     writer.startElement(HTML.IMG_ELEM, mediaComponent);
135 
136     writer.writeAttribute(HTML.ID_ATTR, mediaComponent.getClientId(context), null);
137     writer.writeAttribute(HTML.NAME_ATTR, mediaComponent.getId(), JSFAttr.ID_ATTR);
138 
139     writer.writeAttribute(HTML.SRC_ATTR, source, null);
140 
141     /* write the rest of attributes */
142 
143     if (width != null && !"".equals(width)) 
144     {
145         writer.writeAttribute(HTML.WIDTH_ATTR, width, null);
146     }
147 
148     if (height != null && !"".equals(height)) 
149     {
150         writer.writeAttribute(HTML.HEIGHT_ATTR, height, null);
151     }
152 
153     writer.endElement(HTML.IMG_ELEM);
154     }    
155     
156 }