KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > CmsFile


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsFile.java,v $
3  * Date : $Date: 2006/03/27 14:52:41 $
4  * Version: $Revision: 1.25 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file;
33
34 import org.opencms.main.CmsException;
35 import org.opencms.util.CmsUUID;
36
37 import java.io.Serializable JavaDoc;
38
39 /**
40  * A file resource in the OpenCms VFS.<p>
41  *
42  * A file resource is a CmsResource that contains an additional byte[] array
43  * of binary data, which is the file content.
44  * A file object is not allowed to have sub-resources.<p>
45  *
46  * @author Alexander Kandzior
47  * @author Michael Emmerich
48  *
49  * @version $Revision: 1.25 $
50  *
51  * @since 6.0.0
52  */

53 public class CmsFile extends CmsResource implements Cloneable JavaDoc, Serializable JavaDoc, Comparable JavaDoc {
54
55     /** Serial version UID required for safe serialization. */
56     private static final long serialVersionUID = -5201022482708455620L;
57
58     /** The id of the content database record. */
59     private CmsUUID m_contentId;
60
61     /** The content of this file. */
62     private byte[] m_fileContent;
63
64     /**
65      * Constructor, creates a new CmsFile Object from the given CmsResource with
66      * an empty byte array as file content.<p>
67      *
68      * @param resource the base resource object to create a file from
69      */

70     public CmsFile(CmsResource resource) {
71
72         this(
73             resource.getStructureId(),
74             resource.getResourceId(),
75             CmsUUID.getNullUUID(),
76             resource.getRootPath(),
77             resource.getTypeId(),
78             resource.getFlags(),
79             resource.getProjectLastModified(),
80             resource.getState(),
81             resource.getDateCreated(),
82             resource.getUserCreated(),
83             resource.getDateLastModified(),
84             resource.getUserLastModified(),
85             resource.getDateReleased(),
86             resource.getDateExpired(),
87             resource.getSiblingCount(),
88             0,
89             new byte[0]);
90
91         if (resource instanceof CmsFile) {
92             // the resource already was a file, keep contents that might have been read already
93
m_fileContent = ((CmsFile)resource).getContents();
94             m_contentId = ((CmsFile)resource).getContentId();
95             if (m_fileContent == null) {
96                 m_fileContent = new byte[0];
97                 m_contentId = CmsUUID.getNullUUID();
98             }
99         }
100     }
101
102     /**
103      * Constructor, creates a new CmsFile object.<p>
104      * @param structureId the id of this resources structure record
105      * @param resourceId the id of this resources resource record
106      * @param contentId the id of this resources content record
107      * @param path the filename of this resouce
108      * @param type the type of this resource
109      * @param flags the flags of this resource
110      * @param projectId the project id this resource was last modified in
111      * @param state the state of this resource
112      * @param dateCreated the creation date of this resource
113      * @param userCreated the id of the user who created this resource
114      * @param dateLastModified the date of the last modification of this resource
115      * @param userLastModified the id of the user who did the last modification of this resource
116      * @param dateReleased the release date of this resource
117      * @param dateExpired the expiration date of this resource
118      * @param linkCount the count of all siblings of this resource
119      * @param length the size of the file content of this resource
120      * @param content the binary content data of this file
121      */

122     public CmsFile(
123         CmsUUID structureId,
124         CmsUUID resourceId,
125         CmsUUID contentId,
126         String JavaDoc path,
127         int type,
128         int flags,
129         int projectId,
130         int state,
131         long dateCreated,
132         CmsUUID userCreated,
133         long dateLastModified,
134         CmsUUID userLastModified,
135         long dateReleased,
136         long dateExpired,
137         int linkCount,
138         int length,
139         byte[] content) {
140
141         // create the CmsResource.
142
super(
143             structureId,
144             resourceId,
145             path,
146             type,
147             false,
148             flags,
149             projectId,
150             state,
151             dateCreated,
152             userCreated,
153             dateLastModified,
154             userLastModified,
155             dateReleased,
156             dateExpired,
157             linkCount,
158             length);
159
160         // set content, id and length
161
m_contentId = contentId;
162         m_fileContent = content;
163     }
164
165     /**
166      * Utility method to upgrade a CmsResource to a CmsFile.<p>
167      *
168      * Sometimes a CmsResource might already be a (casted) CmsFile that
169      * also has the contents read. This methods tries to optimize
170      * read access to the VFS by "upgrading" the CmsResource to a CmsFile
171      * first. If this fails, the CmsFile is read from the VFS.<p>
172      *
173      * @param resource the resource to upgrade
174      * @param cms permission context for accessing the VFS
175      * @return the upgraded (or read) file
176      * @throws CmsException if something goes wrong
177      */

178     public static CmsFile upgrade(CmsResource resource, CmsObject cms) throws CmsException {
179         
180         // test if we already have a file
181
if (resource instanceof CmsFile) {
182             // resource is already a file
183
CmsFile file = (CmsFile)resource;
184             if ((file.getContents() != null) && (file.getContents().length > 0)) {
185                 // file has the contents already available
186
return file;
187             }
188         } else if (resource instanceof CmsBackupResource) {
189             // resource is a backup resource (extends file)
190
CmsFile file = (CmsFile)resource;
191             if ((file.getContents() != null) && (file.getContents().length > 0)) {
192                 // backup resource has the contents already available
193
return file;
194             } else {
195                 // no content available in backup resource
196
CmsBackupResource backupResource = (CmsBackupResource)resource;
197                 backupResource = cms.readBackupFile(backupResource.getRootPath(), backupResource.getVersionId());
198                 return backupResource;
199             }
200         }
201         
202         // resource is no file, or contents are not available
203
String JavaDoc filename = cms.getSitePath(resource);
204         // read and return the file
205
return cms.readFile(filename, CmsResourceFilter.IGNORE_EXPIRATION);
206     }
207
208     /**
209      * Returns a clone of this Objects instance.<p>
210      *
211      * @return a clone of this instance
212      */

213     public Object JavaDoc clone() {
214
215         byte[] newContent = new byte[this.getContents().length];
216         System.arraycopy(getContents(), 0, newContent, 0, getContents().length);
217
218         CmsFile clone = new CmsFile(
219             getStructureId(),
220             getResourceId(),
221             getContentId(),
222             getRootPath(),
223             getTypeId(),
224             getFlags(),
225             getProjectLastModified(),
226             getState(),
227             getDateCreated(),
228             getUserCreated(),
229             getDateLastModified(),
230             getUserLastModified(),
231             getDateReleased(),
232             getDateExpired(),
233             getSiblingCount(),
234             getLength(),
235             newContent);
236
237         if (isTouched()) {
238             clone.setDateLastModified(getDateLastModified());
239         }
240
241         return clone;
242     }
243
244     /**
245      * Returns the id of the content database entry.<p>
246      *
247      * @return the id of the content database entry
248      */

249     public CmsUUID getContentId() {
250
251         return m_contentId;
252     }
253
254     /**
255      * Returns the content of this file.<p>
256      *
257      * @return the content of this file
258      */

259     public byte[] getContents() {
260
261         return m_fileContent;
262     }
263
264     /**
265      * @see org.opencms.file.CmsResource#getLength()
266      */

267     public int getLength() {
268
269         return m_length;
270     }
271
272     /**
273      * @see org.opencms.file.CmsResource#isFile()
274      */

275     public boolean isFile() {
276
277         return true;
278     }
279
280     /**
281      * @see org.opencms.file.CmsResource#isFolder()
282      */

283     public boolean isFolder() {
284
285         return false;
286     }
287
288     /**
289      * Sets the contents of this file.<p>
290      *
291      * @param value the content of this file
292      */

293     public void setContents(byte[] value) {
294
295         m_fileContent = value;
296         if (m_fileContent.length > 0) {
297             m_length = m_fileContent.length;
298         } else {
299             m_length = 0;
300         }
301     }
302 }
Popular Tags