KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > runtime > Document


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.runtime;
14
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19
20 import org.apache.commons.lang.StringUtils;
21
22
23 /**
24  * User: sameercharles Date: Apr 28, 2003 Time: 11:20:59 AM
25  * @author Sameer Charles
26  * @version 1.1
27  */

28 public class Document {
29
30     /**
31      * request parameter name.
32      */

33     private String JavaDoc atomName;
34
35     /**
36      * File name, without extension.
37      */

38     private String JavaDoc fileName;
39
40     /**
41      * File extension.
42      */

43     private String JavaDoc extension;
44
45     /**
46      * Mime type.
47      */

48     private String JavaDoc type;
49
50     /**
51      * Underlining file.
52      */

53     private java.io.File JavaDoc file;
54
55     /**
56      * A reference to the file input stream.
57      */

58     private FileInputStream JavaDoc inputStream;
59
60     /**
61      * package private constructor
62      */

63     Document() {
64     }
65
66     /**
67      * Sets the parameter name
68      * @param name parameter name
69      */

70     public void setAtomName(String JavaDoc name) {
71         this.atomName = name;
72     }
73
74     /**
75      * Returns the parameter name
76      * @return parameter name
77      */

78     public String JavaDoc getAtomName() {
79         return this.atomName;
80     }
81
82     /**
83      * Sets the file name without extension.
84      * @param name file name without extension
85      */

86     public void setFileName(String JavaDoc name) {
87         this.fileName = name;
88     }
89
90     /**
91      * Returns the file name without extension.
92      * @return file name
93      */

94     public String JavaDoc getFileName() {
95         return this.fileName;
96     }
97
98     /**
99      * Returns the full file name with extension (if existing).
100      * @return file name with extension
101      */

102     public String JavaDoc getFileNameWithExtension() {
103         if (StringUtils.isEmpty(this.extension)) {
104             return this.fileName;
105         }
106
107         return this.fileName + "." + this.extension; //$NON-NLS-1$
108
}
109
110     /**
111      * Sets the mime type for this file
112      * @param type mime type
113      */

114     public void setType(String JavaDoc type) {
115         this.type = type;
116     }
117
118     /**
119      * Returns the mime type for this file
120      * @return mime type
121      */

122     public String JavaDoc getType() {
123         return this.type;
124     }
125
126     /**
127      * Sets a reference to the uploaded file.
128      * @param in file
129      */

130     public void setFile(java.io.File JavaDoc in) {
131         this.file = in;
132     }
133
134     /**
135      * Sets the file extension.
136      * @param ext file extension
137      */

138     public void setExtention(String JavaDoc ext) {
139         this.extension = ext;
140     }
141
142     /**
143      * Returns the file extension.
144      * @return file extension
145      */

146     public String JavaDoc getExtension() {
147         return this.extension;
148     }
149
150     /**
151      * Returns the file length in bytes
152      * @return file length
153      */

154     public long getLength() {
155         return this.file.length();
156     }
157
158     /**
159      * Returns a stream from the uploaded file. Note that subsequent invocation will always return a reference to the
160      * same input stream.
161      * @return stream from the uploaded file
162      */

163     public InputStream JavaDoc getStream() {
164         try {
165             return (this.inputStream = (new FileInputStream JavaDoc(this.file)));
166         }
167         catch (FileNotFoundException JavaDoc e) {
168             return null;
169         }
170     }
171
172     /**
173      * Returns the uploaded file. Users should normally use getStream, but getFile() can be used when you need to
174      * repeatedly access the file. <strong>The obtained file should never be deleted by the caller</strong>
175      * @return a reference to the uploaded file.
176      */

177     public java.io.File JavaDoc getFile() {
178         return this.file;
179     }
180
181     /**
182      * Delete the file, taking care of closing an open input stream
183      */

184     public void delete() {
185         if (this.inputStream != null) {
186             try {
187                 this.inputStream.close();
188             }
189             catch (IOException JavaDoc e) {
190                 // ignore
191
}
192         }
193         this.file.delete();
194     }
195 }
196
Popular Tags