1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
47
48
49
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
66
67
68
69
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
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
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
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
120
121
122
123
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
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
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 }