KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > FileEntry


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.io.*;
23 import org.openide.filesystems.*;
24 import org.openide.util.Lookup;
25 import org.openide.util.NbBundle;
26
27 /** Entry that works with plain files. Copies, moves,
28 * renames and deletes them without any modification.
29 *
30 * @author Jaroslav Tulach
31 */

32 public class FileEntry extends MultiDataObject.Entry {
33     /** generated Serialized Version UID */
34     static final long serialVersionUID = 5972727204237511983L;
35
36     /** Creates new file entry initially attached to a given file object.
37     * @param obj the data object this entry belongs to
38     * @param fo the file object for the entry
39     */

40     public FileEntry(MultiDataObject obj, FileObject fo) {
41         obj.super (fo);
42     }
43
44     /* Makes a copy to given folder.
45     * @param f the folder to copy to
46     * @param suffix the suffix to add to the name of original file
47     */

48     public FileObject copy (FileObject f, String JavaDoc suffix) throws IOException {
49         FileObject fo = getFile();
50         String JavaDoc newName = fo.getName() + suffix;
51         return fo.copy (f, newName, fo.getExt ());
52     }
53
54     /* Renames underlying fileobject. This implementation return the
55     * same file.
56     *
57     * @param name new name
58     * @return file object with renamed file
59     */

60     public FileObject rename (String JavaDoc name) throws IOException {
61         boolean locked = isLocked ();
62         
63         FileLock lock = takeLock();
64         try {
65             getFile().rename(lock, name, getFile().getExt());
66         } finally {
67             if (!locked)
68                 lock.releaseLock();
69         }
70         return getFile ();
71     }
72
73     /* Moves file to another folder
74     * @param f the folder
75     * @param suffix the suffix to append to original name of the file
76     * @return new file object for the file
77     */

78     public FileObject move (FileObject f, String JavaDoc suffix) throws IOException {
79         boolean locked = isLocked ();
80         
81         FileObject fo = getFile();
82         FileLock lock = takeLock ();
83         try {
84             String JavaDoc newName = fo.getName() + suffix;
85             FileObject dest = fo.move (lock, f, newName, fo.getExt ());
86             return dest;
87         } finally {
88             if (!locked)
89                 lock.releaseLock ();
90         }
91     }
92
93     /* Deletes file object
94     */

95     public void delete () throws IOException {
96         /* JST: This fixes bug 4660. But I am not sure whether this will not
97         * create another or open some old bug.
98             
99             if (isLocked())
100               throw new IOException(NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess"));
101         */

102         boolean locked = isLocked ();
103
104         FileLock lock = takeLock();
105         try {
106             getFile().delete(lock);
107         }
108         finally {
109             if (!locked)
110                 lock.releaseLock();
111         }
112     }
113
114     /* Creates dataobject from template. Copies the file
115     * @param f the folder to create instance in
116     * @param name name of the file or null if it should be choosen automaticly
117     */

118     public FileObject createFromTemplate (FileObject f, String JavaDoc name) throws IOException {
119         if (name == null) {
120             name = FileUtil.findFreeFileName(
121                        f,
122                        getFile ().getName (), getFile ().getExt ()
123                    );
124         }
125         
126         
127         FileObject fo = null;
128         for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) {
129             if (h.accept(getFile())) {
130                 fo = h.createFromTemplate(getFile(), f, name, DataObject.CreateAction.findParameters(name));
131                 assert fo != null;
132                 break;
133             }
134         }
135         
136         if (fo == null) {
137             fo = getFile().copy (f, name, getFile().getExt ());
138         }
139         
140         
141         // unmark template state
142
DataObject.setTemplate (fo, false);
143
144         return fo;
145     }
146
147     /** Specialized entry that simplifies substitution when a file entry
148     * is created from template.
149     * Subclasses must implement
150     * {@link #createFormat} and return a valid text format that
151     * will be used for converting the lines of the original file
152     * to lines in the newly created one.
153     */

154     public abstract static class Format extends FileEntry {
155         static final long serialVersionUID =8896750589709521197L;
156         /** Create a new entry initially attached to a given file object.
157         * @param obj the data object this entry belongs to
158         * @param fo the file object for the entry
159         */

160         public Format (MultiDataObject obj, FileObject fo) {
161             super (obj, fo);
162         }
163
164         /* Creates dataobject from template. Copies the file and applyes substitutions
165         * provided by the createFormat method.
166         *
167         * @param f the folder to create instance in
168         * @param name name of the file or null if it should be choosen automaticly
169         */

170         public FileObject createFromTemplate (FileObject f, String JavaDoc name) throws IOException {
171             String JavaDoc ext = getFile ().getExt ();
172
173             if (name == null) {
174                 name = FileUtil.findFreeFileName(
175                            f,
176                            getFile ().getName (), ext
177                        );
178             }
179             
180             
181             FileObject fo = null;
182             for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) {
183                 if (h.accept(getFile())) {
184                     fo = h.createFromTemplate(getFile(), f, name, DataObject.CreateAction.findParameters(name));
185                     assert fo != null;
186                     break;
187                 }
188             }
189
190             if (fo != null) {
191                 // unmark template state
192
DataObject.setTemplate (fo, false);
193                 return fo;
194             }
195             
196             fo = f.createData (name, ext);
197
198             java.text.Format JavaDoc frm = createFormat (f, name, ext);
199
200             BufferedReader r = new BufferedReader (new InputStreamReader (getFile ().getInputStream ()));
201             try {
202                 FileLock lock = fo.lock ();
203                 try {
204                     BufferedWriter w = new BufferedWriter (new OutputStreamWriter (fo.getOutputStream (lock)));
205
206                     try {
207                         String JavaDoc current;
208                         while ((current = r.readLine ()) != null) {
209                             w.write (frm.format (current));
210                             // Cf. #7061.
211
w.newLine ();
212                         }
213                     } finally {
214                         w.close ();
215                     }
216                 } finally {
217                     lock.releaseLock ();
218                 }
219             } finally {
220                 r.close ();
221             }
222
223             // copy attributes
224
FileUtil.copyAttributes (getFile (), fo);
225
226             // unmark template state
227
DataObject.setTemplate (fo, false);
228
229             return fo;
230         }
231
232         /** Provide a suitable format for
233         * substitution of lines.
234         *
235         * @param target the target folder of the installation
236         * @param n the name the file will have
237         * @param e the extension the file will have
238         * @return a format to use for formatting lines
239         */

240         protected abstract java.text.Format JavaDoc createFormat (FileObject target, String JavaDoc n, String JavaDoc e);
241
242     }
243
244
245     /** Simple file entry variant. It does nearly nothing.
246     * When a file is copied, it does nothing. If it is moved
247     * or renamed it deletes the file.
248     * <P>
249     * Useful for representing useless files.
250     */

251     public final static class Numb extends MultiDataObject.Entry {
252         /** generated Serialized Version UID */
253         static final long serialVersionUID = -6572157492885890612L;
254
255         /**
256          * Create a dummy entry.
257          * @param obj the data object this entry belongs to
258          * @param fo the file object to create an entry for
259          */

260         public Numb (MultiDataObject obj, FileObject fo) {
261             obj.super (fo);
262         }
263         
264         /** Is not important at all.
265         * @return false
266         */

267         public boolean isImportant () {
268             return false;
269         }
270
271         /** Does nothing.
272         * @param f ignored
273         * @param suffix ignored
274         * @return <code>null</code>
275         */

276         public FileObject copy (FileObject f, String JavaDoc suffix) {
277             return null;
278         }
279
280         /** Removes file.
281          * @param name ignored
282         * @return <code>null</code>
283          * @throws IOException in case of problem
284         */

285         public FileObject rename (String JavaDoc name) throws IOException {
286             stdBehaving();
287             return null;
288         }
289
290         /** Removes file.
291          * @param f ignored
292          * @param suffix ignored
293         * @return <code>null</code>
294          * @throws IOException in case of problem
295         */

296         public FileObject move (FileObject f, String JavaDoc suffix) throws IOException {
297             stdBehaving();
298             return null;
299         }
300
301         /** Removes file.
302          * @throws IOException in case of problem
303          */

304         public void delete () throws IOException {
305             stdBehaving();
306         }
307
308         /** Removes file.
309          * @throws IOException in case of problem
310          */

311         private void stdBehaving () throws IOException {
312             if (getFile() == null)
313                 return;
314
315             if (isLocked())
316                 throw new IOException (NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess"));
317
318             FileLock lock = takeLock();
319             try {
320                 getFile().delete(lock);
321             } finally {
322                 if (lock != null)
323                     lock.releaseLock();
324             }
325         }
326
327         /** Does nothing.
328          * @param f ignored
329          * @param name ignored
330          * @return <code>null</code>
331          */

332         public FileObject createFromTemplate (FileObject f, String JavaDoc name) {
333             return null;
334         }
335     }
336     /** Simple entry for handling folders, on copy, move and createFromTemplate
337      * it creates new empty folder and copies attributes of source folder.
338      * Operation on children should be performed explicitly by DataObject
339      * using this entry.
340      * @since 1.13
341      */

342     public final static class Folder extends MultiDataObject.Entry {
343
344         /** Creates new FolderEntry */
345         public Folder (MultiDataObject obj, FileObject fo) {
346             obj.super (fo);
347         }
348
349         /** Creates new folder and copies attributes.
350          * @param f the folder to create this entry in
351          * @param suffix suffix appended to the new name to use
352          * @return the copied <code>FileObject</code> or <code>null</code> if it cannot be copied
353          * @exception IOException when the operation fails
354          */

355         public FileObject copy (FileObject f, String JavaDoc suffix) throws IOException {
356             String JavaDoc add = suffix + ((getFile ().getExt ().length () > 0) ? "." + getFile ().getExt () : "");
357
358             FileObject fo = FileUtil.createFolder (f, getFile ().getName () + add);
359             FileUtil.copyAttributes (getFile (), fo);
360
361             return fo;
362         }
363
364         /** Nearly the same like {@link #copy (FileObject f, String suffix)}.
365          * @param f the folder to move this entry to
366          * @param suffix suffix appended to the new name to use
367          * @return the moved <code>FileObject</code> or <code>null</code> if it has been deleted
368          * @exception IOException when the operation fails
369          */

370         public FileObject move (FileObject f, String JavaDoc suffix) throws IOException {
371             return copy (f, suffix);
372         }
373
374         /** Creates new folder and copies attributes, the template flag is cleared.
375          * @param f the folder to create this entry in
376          * @param name the new name to use
377          * @return the copied <code>FileObject</code> or <code>null</code> if it cannot be copied
378          * @exception IOException when the operation fails
379          */

380         public FileObject createFromTemplate (FileObject f, String JavaDoc name) throws IOException {
381             if (name == null) {
382                 name = FileUtil.findFreeFileName(
383                            f,
384                            getFile ().getName (), getFile ().getExt ()
385                        );
386             }
387             FileObject fo = FileUtil.createFolder (f, name);
388
389             FileUtil.copyAttributes (getFile (), fo);
390             DataObject.setTemplate (fo, false);
391
392             return fo;
393         }
394
395         /** Renames folder.
396          * @param name the new name
397          * @return the renamed <code>FileObject</code> or <code>null</code> if it has been deleted
398          * @exception IOException when the operation fails
399          */

400         public FileObject rename (String JavaDoc name) throws IOException {
401             boolean locked = isLocked ();
402             FileLock lock = takeLock ();
403             try {
404                 getFile ().rename (lock, name, null);
405             } finally {
406                 if (!locked)
407                     lock.releaseLock ();
408             }
409             return getFile ();
410         }
411
412         /** Deletes folder associated with entry. Although filesystems delete
413          * folders recursively, it is better to delete children DataObjects before
414          * the {@link #FileEntry.Folder} entry is deleted.
415          * @exception IOException when the operation fails
416          */

417         public void delete () throws IOException {
418             boolean locked = isLocked ();
419             FileLock lock = takeLock ();
420             try {
421                 getFile ().delete (lock);
422             } finally {
423                 if (!locked)
424                     lock.releaseLock();
425             }
426         }
427
428     }
429 }
430
Popular Tags