View Javadoc
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       * BinaryFile.class.getName()
22       */
23      public static final String COMPONENT_TYPE = BinaryFile.class.getName();
24      /**
25       * org.andromda.cartridges.jsf.BinaryFile
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       * @see javax.faces.component.UIComponent#getFamily()
40       */
41      public String getFamily()
42      {
43          return RENDERER_TYPE;
44      }
45  
46      /**
47       * Stores the name of the attribute that holds the value.
48       */
49      public static final String VALUE_ATTRIBUTE = "value";
50  
51      /**
52       * Stores the value of this binary file.
53       */
54      private Object value;
55  
56      /**
57       * Gets the current value of this binary file.
58       *
59       * @return the value of this binary file.
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       * Stores the name of the attribute that holds the fileName.
76       */
77      public static final String FILE_NAME_ATTRIBUTE = "fileName";
78  
79      /**
80       * The name of the file to render.
81       */
82      private String fileName;
83  
84      /**
85       * Sets the file name for this component.
86       *
87       * @param fileName the name of the binary file to be rendered.
88       */
89      public void setFileName(final String fileName)
90      {
91          this.fileName = fileName;
92      }
93  
94      /**
95       * Gets the file name for rending the binary file.
96       * @return the name of the file to render.
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      * The name of the attribute that stores the content type.
113      */
114     public static final String CONTENT_TYPE_ATTRIBUTE = "contentType";
115 
116     /**
117      * The content type to use when rendering the file.
118      */
119     private String contentType;
120 
121     /**
122      * Gets the explicit content type to render the file in.
123      *
124      * @return Returns the contentType.
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             // - if the content type is still null, lets guess
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      * Sets the explicit content type in which to render the file.
158      *
159      * @param contentType The contentType to set.
160      */
161     public void setContentType(final String contentType)
162     {
163         this.contentType = contentType;
164     }
165 
166     /**
167      * The name of the attribute that stores the encoding.
168      */
169     public static final String ENCODING_TYPE_ATTRIBUTE = "encoding";
170 
171     /**
172      * The encoding to use when rendering the file.
173      */
174     private String encoding;
175 
176     /**
177      * Gets the encoding to render the file in.
178      *
179      * @return Returns the encoding.
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      * Sets the explicit encoding used to render the file.
196      *
197      * @param encoding The encoding to set.
198      */
199     public void setEncoding(final String encoding)
200     {
201         this.encoding = encoding;
202     }
203 
204     /**
205      * Whether or not we should be prompted to save the file when its rendered.
206      */
207     public static final String PROMPT_ATTRIBUTE = "prompt";
208 
209     /**
210      * Stores the 'prompt' value.
211      */
212     private Boolean prompt;
213 
214     /**
215      * Gets whether or not the prompt should be rendered.
216      *
217      * @return Returns the prompt.
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      * Sets whether or not the prompt should be rendered.
234      *
235      * @param prompt The prompt to set.
236      */
237     public void setPrompt(final boolean prompt)
238     {
239         this.prompt = Boolean.valueOf(prompt);
240     }
241 
242     /**
243      * Stores the default content types.
244      */
245     static final Properties contentTypes = new Properties();
246 
247     /**
248      * Load up the default content types
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             // - ignore
273         }
274     }
275 }