001: package org.andromda.cartridges.jsf.component;
002:
003: import java.io.InputStream;
004:
005: import java.util.Properties;
006:
007: import javax.faces.component.UIComponentBase;
008: import javax.faces.el.ValueBinding;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: public class BinaryFile extends UIComponentBase {
014: private static final Log logger = LogFactory
015: .getLog(BinaryFile.class);
016: public static final String COMPONENT_TYPE = BinaryFile.class
017: .getName();
018: public static final String RENDERER_TYPE = "org.andromda.cartridges.jsf.BinaryFile";
019:
020: public BinaryFile() {
021: super ();
022: this .setRendererType(RENDERER_TYPE);
023: }
024:
025: /**
026: * @see javax.faces.component.UIComponent#getFamily()
027: */
028: public String getFamily() {
029: return RENDERER_TYPE;
030: }
031:
032: /**
033: * Stores the name of the attriubte that holds the value.
034: */
035: public static final String VALUE_ATTRIBUTE = "value";
036:
037: /**
038: * Stores the value of this binary file.
039: */
040: private Object value;
041:
042: /**
043: * Gets the current value of this binary file.
044: *
045: * @return the value of this binary file.
046: */
047: public Object getValue() {
048: if (this .value == null) {
049: final ValueBinding binding = this
050: .getValueBinding(VALUE_ATTRIBUTE);
051: if (binding != null) {
052: this .value = binding.getValue(this .getFacesContext());
053: }
054: }
055: return this .value;
056: }
057:
058: /**
059: * Stores the name of the attriubte that holds the fileName.
060: */
061: public static final String FILE_NAME_ATTRIBUTE = "fileName";
062:
063: /**
064: * The name of the file to render.
065: */
066: private String fileName;
067:
068: /**
069: * Sets the file name for this component.
070: *
071: * @param fileName the name of the binary file to be rendered.
072: */
073: public void setFileName(final String fileName) {
074: this .fileName = fileName;
075: }
076:
077: /**
078: * Gets the file name for rending the binary file.
079: * @return the name of the file to render.
080: */
081: public String getFileName() {
082: if (this .fileName == null) {
083: final ValueBinding binding = this
084: .getValueBinding(FILE_NAME_ATTRIBUTE);
085: if (binding != null) {
086: this .fileName = (String) binding.getValue(this
087: .getFacesContext());
088: }
089: }
090: return this .fileName;
091: }
092:
093: /**
094: * The name of the attribute that stores the content type.
095: */
096: public static final String CONTENT_TYPE_ATTRIBUTE = "contentType";
097:
098: /**
099: * The content type to use when rendering the file.
100: */
101: private String contentType;
102:
103: /**
104: * Gets the explicity content type to render the file in.
105: *
106: * @return Returns the contentType.
107: */
108: public String getContentType() {
109: if (this .contentType == null) {
110: final ValueBinding binding = this
111: .getValueBinding(CONTENT_TYPE_ATTRIBUTE);
112: if (binding != null) {
113: this .contentType = (String) binding.getValue(this
114: .getFacesContext());
115: }
116:
117: // - if the content type is still null, lets guess
118: if (this .contentType == null) {
119: final String fileName = this .getFileName();
120: if (fileName != null && fileName.trim().length() > 0) {
121: int lastDotIndex = fileName.lastIndexOf('.');
122: if (lastDotIndex != -1) {
123: final String extension = fileName.substring(
124: lastDotIndex, fileName.length());
125: this .contentType = contentTypes
126: .getProperty(extension);
127: }
128: }
129: }
130: }
131: return this .contentType;
132: }
133:
134: /**
135: * Sets the explicit content type in which to render the file.
136: *
137: * @param contentType The contentType to set.
138: */
139: public void setContentType(final String contentType) {
140: this .contentType = contentType;
141: }
142:
143: /**
144: * Whether or not we should be prompted to save the file when its rendered.
145: */
146: public static final String PROMPT_ATTRIBUTE = "prompt";
147:
148: /**
149: * Stores the 'prompt' value.
150: */
151: private Boolean prompt;
152:
153: /**
154: * Gets whether or not the prompt should be rendered.
155: *
156: * @return Returns the prompt.
157: */
158: public boolean isPrompt() {
159: if (this .prompt == null) {
160: final ValueBinding binding = this
161: .getValueBinding(CONTENT_TYPE_ATTRIBUTE);
162: if (binding != null) {
163: this .prompt = (Boolean) binding.getValue(this
164: .getFacesContext());
165: }
166: }
167: return this .prompt != null ? this .prompt.booleanValue() : false;
168: }
169:
170: /**
171: * Sets whether or not the prompt should be rendered.
172: *
173: * @param prompt The prompt to set.
174: */
175: public void setPrompt(final boolean prompt) {
176: this .prompt = Boolean.valueOf(prompt);
177: }
178:
179: /**
180: * Stores the default content types.
181: */
182: final static Properties contentTypes = new Properties();
183:
184: /**
185: * Load up the default content types
186: */
187: static {
188: final String fileName = "contenttypes.properties";
189: final InputStream stream = BinaryFile.class
190: .getResourceAsStream(fileName);
191: if (stream == null) {
192: logger.error("Could not load file from '" + fileName + "'");
193: }
194: try {
195: contentTypes.load(stream);
196: } catch (final Throwable throwable) {
197: logger.error(throwable);
198: }
199: try {
200: stream.close();
201: } catch (final Throwable throwable) {
202: // - ignore
203: }
204: }
205: }
|