1 package org.andromda.cartridges.jsf.component;
2
3 import java.io.InputStream;
4 import java.util.Properties;
5
6 import javax.faces.component.UIComponentBase;
7 import javax.faces.el.ValueBinding;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13
14
15
16 public class BinaryFile
17 extends UIComponentBase
18 {
19 private static final Log LOGGER = LogFactory.getLog(BinaryFile.class);
20
21
22
23 public static final String COMPONENT_TYPE = BinaryFile.class.getName();
24
25
26
27 public static final String RENDERER_TYPE = "org.andromda.cartridges.jsf.BinaryFile";
28
29
30
31
32 public BinaryFile()
33 {
34 super();
35 this.setRendererType(RENDERER_TYPE);
36 }
37
38
39
40
41 public String getFamily()
42 {
43 return RENDERER_TYPE;
44 }
45
46
47
48
49 public static final String VALUE_ATTRIBUTE = "value";
50
51
52
53
54 private Object value;
55
56
57
58
59
60
61 public Object getValue()
62 {
63 if (this.value == null)
64 {
65 final ValueBinding binding = this.getValueBinding(VALUE_ATTRIBUTE);
66 if (binding != null)
67 {
68 this.value = binding.getValue(this.getFacesContext());
69 }
70 }
71 return this.value;
72 }
73
74
75
76
77 public static final String FILE_NAME_ATTRIBUTE = "fileName";
78
79
80
81
82 private String fileName;
83
84
85
86
87
88
89 public void setFileName(final String fileName)
90 {
91 this.fileName = fileName;
92 }
93
94
95
96
97
98 public String getFileName()
99 {
100 if (this.fileName == null)
101 {
102 final ValueBinding binding = this.getValueBinding(FILE_NAME_ATTRIBUTE);
103 if (binding != null)
104 {
105 this.fileName = (String)binding.getValue(this.getFacesContext());
106 }
107 }
108 return this.fileName;
109 }
110
111
112
113
114 public static final String CONTENT_TYPE_ATTRIBUTE = "contentType";
115
116
117
118
119 private String contentType;
120
121
122
123
124
125
126 public String getContentType()
127 {
128 if (this.contentType == null)
129 {
130 final ValueBinding binding = this.getValueBinding(CONTENT_TYPE_ATTRIBUTE);
131 if (binding != null)
132 {
133 this.contentType = (String)binding.getValue(this.getFacesContext());
134 }
135
136
137 if (this.contentType == null)
138 {
139 final String fileName = this.getFileName();
140 if (StringUtils.isNotBlank(fileName))
141 {
142 int lastDotIndex = fileName.lastIndexOf('.');
143 if (lastDotIndex != -1)
144 {
145 final String extension = fileName.substring(
146 lastDotIndex,
147 fileName.length());
148 this.contentType = contentTypes.getProperty(extension);
149 }
150 }
151 }
152 }
153 return this.contentType;
154 }
155
156
157
158
159
160
161 public void setContentType(final String contentType)
162 {
163 this.contentType = contentType;
164 }
165
166
167
168
169 public static final String ENCODING_TYPE_ATTRIBUTE = "encoding";
170
171
172
173
174 private String encoding;
175
176
177
178
179
180
181 public String getEncoding()
182 {
183 if (this.encoding == null)
184 {
185 final ValueBinding binding = this.getValueBinding(ENCODING_TYPE_ATTRIBUTE);
186 if (binding != null)
187 {
188 this.encoding = (String)binding.getValue(this.getFacesContext());
189 }
190 }
191 return this.encoding;
192 }
193
194
195
196
197
198
199 public void setEncoding(final String encoding)
200 {
201 this.encoding = encoding;
202 }
203
204
205
206
207 public static final String PROMPT_ATTRIBUTE = "prompt";
208
209
210
211
212 private Boolean prompt;
213
214
215
216
217
218
219 public boolean isPrompt()
220 {
221 if (this.prompt == null)
222 {
223 final ValueBinding binding = this.getValueBinding(CONTENT_TYPE_ATTRIBUTE);
224 if (binding != null)
225 {
226 this.prompt = (Boolean)binding.getValue(this.getFacesContext());
227 }
228 }
229 return this.prompt != null ? this.prompt.booleanValue() : false;
230 }
231
232
233
234
235
236
237 public void setPrompt(final boolean prompt)
238 {
239 this.prompt = Boolean.valueOf(prompt);
240 }
241
242
243
244
245 static final Properties contentTypes = new Properties();
246
247
248
249
250 static
251 {
252 final String fileName = "contenttypes.properties";
253 final InputStream stream = BinaryFile.class.getResourceAsStream(fileName);
254 if (stream == null)
255 {
256 LOGGER.error("Could not load file from '" + fileName + '\'');
257 }
258 try
259 {
260 contentTypes.load(stream);
261 }
262 catch (final Throwable throwable)
263 {
264 LOGGER.error(throwable);
265 }
266 try
267 {
268 if (stream != null) {stream.close(); }
269 }
270 catch (final Throwable throwable)
271 {
272
273 }
274 }
275 }