~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/loaders/src/org/openide/loaders/FileEntry.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.openide.loaders;
 
43
 
 
44
import java.io.*;
 
45
import org.openide.filesystems.*;
 
46
import org.openide.util.Lookup;
 
47
import org.openide.util.NbBundle;
 
48
 
 
49
/** Entry that works with plain files. Copies, moves,
 
50
* renames and deletes them without any modification.
 
51
*
 
52
* @author Jaroslav Tulach
 
53
*/
 
54
public class FileEntry extends MultiDataObject.Entry {
 
55
    /** generated Serialized Version UID */
 
56
    static final long serialVersionUID = 5972727204237511983L;
 
57
 
 
58
    /** Creates new file entry initially attached to a given file object.
 
59
    * @param obj the data object this entry belongs to
 
60
    * @param fo the file object for the entry
 
61
    */
 
62
    public FileEntry(MultiDataObject obj, FileObject fo) {
 
63
        obj.super (fo);
 
64
    }
 
65
 
 
66
    /* Makes a copy to given folder.
 
67
    * @param f the folder to copy to
 
68
    * @param suffix the suffix to add to the name of original file
 
69
    */
 
70
    public FileObject copy (FileObject f, String suffix) throws IOException {
 
71
        FileObject fo = getFile();
 
72
        String newName = fo.getName() + suffix;
 
73
        return fo.copy (f, newName, fo.getExt ());
 
74
    }
 
75
 
 
76
    @Override
 
77
    public FileObject copyRename(FileObject f, String name, String ext) throws IOException {
 
78
        FileObject fo = getFile();
 
79
        return fo.copy (f, name, ext);
 
80
    }
 
81
    
 
82
    /* Renames underlying fileobject. This implementation return the
 
83
    * same file.
 
84
    *
 
85
    * @param name new name
 
86
    * @return file object with renamed file
 
87
    */
 
88
    public FileObject rename (String name) throws IOException {
 
89
        boolean locked = isLocked ();
 
90
        
 
91
        FileLock lock = takeLock();
 
92
        try {
 
93
            getFile().rename(lock, name, getFile().getExt());
 
94
        } finally {
 
95
            if (!locked)
 
96
                lock.releaseLock();
 
97
        }
 
98
        return getFile ();
 
99
    }
 
100
 
 
101
    /* Moves file to another folder
 
102
    * @param f the folder
 
103
    * @param suffix the suffix to append to original name of the file
 
104
    * @return new file object for the file
 
105
    */
 
106
    public FileObject move (FileObject f, String suffix) throws IOException {
 
107
        boolean locked = isLocked ();
 
108
        
 
109
        FileObject fo = getFile();
 
110
        FileLock lock = takeLock ();
 
111
        try {
 
112
            String newName = fo.getName() + suffix;
 
113
            FileObject dest = fo.move (lock, f, newName, fo.getExt ());
 
114
            return dest;
 
115
        } finally {
 
116
            if (!locked)
 
117
                lock.releaseLock ();
 
118
        }
 
119
    }
 
120
 
 
121
    /* Deletes file object
 
122
    */
 
123
    public void delete () throws IOException {
 
124
        /* JST: This fixes bug 4660. But I am not sure whether this will not
 
125
        * create another or open some old bug.
 
126
            
 
127
            if (isLocked())
 
128
              throw new IOException(NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess"));
 
129
        */
 
130
        boolean locked = isLocked ();
 
131
 
 
132
        FileLock lock = takeLock();
 
133
        try {
 
134
            getFile().delete(lock);
 
135
        }
 
136
        finally {
 
137
            if (!locked)
 
138
                lock.releaseLock();
 
139
        }
 
140
    }
 
141
 
 
142
    /* Creates dataobject from template. Copies the file
 
143
    * @param f the folder to create instance in
 
144
    * @param name name of the file or null if it should be choosen automaticly
 
145
    */
 
146
    public FileObject createFromTemplate (FileObject f, String name) throws IOException {
 
147
        if (name == null) {
 
148
            name = FileUtil.findFreeFileName(
 
149
                       f,
 
150
                       getFile ().getName (), getFile ().getExt ()
 
151
                   );
 
152
        }
 
153
        
 
154
        
 
155
        FileObject fo = null;
 
156
        for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) {
 
157
            if (h.accept(getFile())) {
 
158
                fo = h.createFromTemplate(getFile(), f, name,
 
159
                    DataObject.CreateAction.enhanceParameters(
 
160
                        DataObject.CreateAction.findParameters(name),
 
161
                        name, getFile().getExt()));
 
162
                assert fo != null;
 
163
                break;
 
164
            }
 
165
        }
 
166
        
 
167
        if (fo == null) {
 
168
            fo = getFile().copy (f, name, getFile().getExt ());
 
169
        }
 
170
        
 
171
        
 
172
        // unmark template state
 
173
        DataObject.setTemplate (fo, false);
 
174
 
 
175
        return fo;
 
176
    }
 
177
 
 
178
    /** Specialized entry that simplifies substitution when a file entry
 
179
    * is created from template.
 
180
    * Subclasses must implement
 
181
    * {@link #createFormat} and return a valid text format that
 
182
    * will be used for converting the lines of the original file
 
183
    * to lines in the newly created one.
 
184
    */
 
185
    public abstract static class Format extends FileEntry {
 
186
        static final long serialVersionUID =8896750589709521197L;
 
187
        /** Create a new entry initially attached to a given file object.
 
188
        * @param obj the data object this entry belongs to
 
189
        * @param fo the file object for the entry
 
190
        */
 
191
        public Format (MultiDataObject obj, FileObject fo) {
 
192
            super (obj, fo);
 
193
        }
 
194
 
 
195
        /* Creates dataobject from template. Copies the file and applyes substitutions
 
196
        * provided by the createFormat method.
 
197
        *
 
198
        * @param f the folder to create instance in
 
199
        * @param name name of the file or null if it should be choosen automaticly
 
200
        */
 
201
        public FileObject createFromTemplate (FileObject f, String name) throws IOException {
 
202
            String ext = getFile ().getExt ();
 
203
 
 
204
            if (name == null) {
 
205
                name = FileUtil.findFreeFileName(
 
206
                           f,
 
207
                           getFile ().getName (), ext
 
208
                       );
 
209
            }
 
210
            
 
211
            FileObject fo = null;
 
212
            for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) {
 
213
                if (h.accept(getFile())) {
 
214
                    fo = h.createFromTemplate(
 
215
                        getFile(), f, name,
 
216
                        DataObject.CreateAction.enhanceParameters(
 
217
                            DataObject.CreateAction.findParameters(name),
 
218
                            name, getFile().getExt()));
 
219
                    assert fo != null;
 
220
                    break;
 
221
                }
 
222
            }
 
223
 
 
224
            if (fo != null) {
 
225
                // unmark template state
 
226
                DataObject.setTemplate (fo, false);
 
227
                return fo;
 
228
            }
 
229
            
 
230
            fo = f.createData (name, ext);
 
231
 
 
232
            java.text.Format frm = createFormat (f, name, ext);
 
233
 
 
234
            BufferedReader r = new BufferedReader (new InputStreamReader (getFile ().getInputStream ()));
 
235
            try {
 
236
                FileLock lock = fo.lock ();
 
237
                try {
 
238
                    BufferedWriter w = new BufferedWriter (new OutputStreamWriter (fo.getOutputStream (lock)));
 
239
 
 
240
                    try {
 
241
                        String current;
 
242
                        while ((current = r.readLine ()) != null) {
 
243
                            w.write (frm.format (current));
 
244
                            // Cf. #7061.
 
245
                            w.newLine ();
 
246
                        }
 
247
                    } finally {
 
248
                        w.close ();
 
249
                    }
 
250
                } finally {
 
251
                    lock.releaseLock ();
 
252
                }
 
253
            } finally {
 
254
                r.close ();
 
255
            }
 
256
 
 
257
            // copy attributes
 
258
            FileUtil.copyAttributes (getFile (), fo);
 
259
 
 
260
            // unmark template state
 
261
            DataObject.setTemplate (fo, false);
 
262
 
 
263
            return fo;
 
264
        }
 
265
 
 
266
        /** Provide a suitable format for
 
267
        * substitution of lines.
 
268
        *
 
269
        * @param target the target folder of the installation
 
270
        * @param n the name the file will have
 
271
        * @param e the extension the file will have
 
272
        * @return a format to use for formatting lines
 
273
        */
 
274
        protected abstract java.text.Format createFormat (FileObject target, String n, String e);
 
275
 
 
276
    }
 
277
 
 
278
 
 
279
    /** Simple file entry variant. It does nearly nothing.
 
280
    * When a file is copied, it does nothing. If it is moved
 
281
    * or renamed it deletes the file.
 
282
    * <P>
 
283
    * Useful for representing useless files.
 
284
    */
 
285
    public final static class Numb extends MultiDataObject.Entry {
 
286
        /** generated Serialized Version UID */
 
287
        static final long serialVersionUID = -6572157492885890612L;
 
288
 
 
289
        /**
 
290
         * Create a dummy entry.
 
291
         * @param obj the data object this entry belongs to
 
292
         * @param fo the file object to create an entry for
 
293
         */
 
294
        public Numb (MultiDataObject obj, FileObject fo) {
 
295
            obj.super (fo);
 
296
        }
 
297
        
 
298
        /** Is not important at all.
 
299
        * @return false
 
300
        */
 
301
        public boolean isImportant () {
 
302
            return false;
 
303
        }
 
304
 
 
305
        /** Does nothing.
 
306
        * @param f ignored
 
307
        * @param suffix ignored
 
308
        * @return <code>null</code>
 
309
        */
 
310
        public FileObject copy (FileObject f, String suffix) {
 
311
            return null;
 
312
        }
 
313
 
 
314
        /** Removes file.
 
315
         * @param name ignored
 
316
        * @return <code>null</code>
 
317
         * @throws IOException in case of problem
 
318
        */
 
319
        public FileObject rename (String name) throws IOException {
 
320
            stdBehaving();
 
321
            return null;
 
322
        }
 
323
 
 
324
        /** Removes file.
 
325
         * @param f ignored
 
326
         * @param suffix ignored
 
327
        * @return <code>null</code>
 
328
         * @throws IOException in case of problem
 
329
        */
 
330
        public FileObject move (FileObject f, String suffix) throws IOException {
 
331
            stdBehaving();
 
332
            return null;
 
333
        }
 
334
 
 
335
        /** Removes file.
 
336
         * @throws IOException in case of problem
 
337
         */
 
338
        public void delete () throws IOException {
 
339
            stdBehaving();
 
340
        }
 
341
 
 
342
        /** Removes file.
 
343
         * @throws IOException in case of problem
 
344
         */
 
345
        private void stdBehaving () throws IOException {
 
346
            if (getFile() == null)
 
347
                return;
 
348
 
 
349
            if (isLocked())
 
350
                throw new IOException (NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess"));
 
351
 
 
352
            FileLock lock = takeLock();
 
353
            try {
 
354
                getFile().delete(lock);
 
355
            } finally {
 
356
                if (lock != null)
 
357
                    lock.releaseLock();
 
358
            }
 
359
        }
 
360
 
 
361
        /** Does nothing.
 
362
         * @param f ignored
 
363
         * @param name ignored
 
364
         * @return <code>null</code>
 
365
         */
 
366
        public FileObject createFromTemplate (FileObject f, String name) {
 
367
            return null;
 
368
        }
 
369
    }
 
370
    /** Simple entry for handling folders, on copy, move and createFromTemplate
 
371
     * it creates new empty folder and copies attributes of source folder.
 
372
     * Operation on children should be performed explicitly by DataObject 
 
373
     * using this entry.
 
374
     * @since 1.13
 
375
     */
 
376
    public final static class Folder extends MultiDataObject.Entry {
 
377
 
 
378
        /** Creates new FolderEntry */
 
379
        public Folder (MultiDataObject obj, FileObject fo) {
 
380
            obj.super (fo);
 
381
        }
 
382
 
 
383
        /** Creates new folder and copies attributes.
 
384
         * @param f the folder to create this entry in
 
385
         * @param suffix suffix appended to the new name to use
 
386
         * @return the copied <code>FileObject</code> or <code>null</code> if it cannot be copied
 
387
         * @exception IOException when the operation fails
 
388
         */
 
389
        public FileObject copy (FileObject f, String suffix) throws IOException {
 
390
            String add = suffix + ((getFile ().getExt ().length () > 0) ? "." + getFile ().getExt () : "");
 
391
 
 
392
            FileObject fo = FileUtil.createFolder (f, getFile ().getName () + add);
 
393
            FileUtil.copyAttributes (getFile (), fo);
 
394
 
 
395
            return fo;
 
396
        }
 
397
 
 
398
        /** Nearly the same like {@link #copy (FileObject f, String suffix)}.
 
399
         * @param f the folder to move this entry to
 
400
         * @param suffix suffix appended to the new name to use
 
401
         * @return the moved <code>FileObject</code> or <code>null</code> if it has been deleted
 
402
         * @exception IOException when the operation fails
 
403
         */
 
404
        public FileObject move (FileObject f, String suffix) throws IOException {
 
405
            return copy (f, suffix);
 
406
        }
 
407
 
 
408
        /** Creates new folder and copies attributes, the template flag is cleared.
 
409
         * @param f the folder to create this entry in
 
410
         * @param name the new name to use
 
411
         * @return the copied <code>FileObject</code> or <code>null</code> if it cannot be copied
 
412
         * @exception IOException when the operation fails
 
413
         */
 
414
        public FileObject createFromTemplate (FileObject f, String name) throws IOException {
 
415
            if (name == null) {
 
416
                name = FileUtil.findFreeFileName(
 
417
                           f,
 
418
                           getFile ().getName (), getFile ().getExt ()
 
419
                       );
 
420
            }
 
421
            FileObject fo = FileUtil.createFolder (f, name);
 
422
 
 
423
            FileUtil.copyAttributes (getFile (), fo);
 
424
            DataObject.setTemplate (fo, false);
 
425
 
 
426
            return fo;
 
427
        }
 
428
 
 
429
        /** Renames folder.
 
430
         * @param name the new name
 
431
         * @return the renamed <code>FileObject</code> or <code>null</code> if it has been deleted
 
432
         * @exception IOException when the operation fails
 
433
         */
 
434
        public FileObject rename (String name) throws IOException {
 
435
            boolean locked = isLocked ();
 
436
            FileLock lock = takeLock ();
 
437
            try {
 
438
                getFile ().rename (lock, name, null);
 
439
            } finally {
 
440
                if (!locked)
 
441
                    lock.releaseLock ();
 
442
            }
 
443
            return getFile ();
 
444
        }
 
445
 
 
446
        /** Deletes folder associated with entry. Although filesystems delete
 
447
         * folders recursively, it is better to delete children DataObjects before
 
448
         * the {@link #FileEntry.Folder} entry is deleted.
 
449
         * @exception IOException when the operation fails
 
450
         */
 
451
        public void delete () throws IOException {
 
452
            boolean locked = isLocked ();
 
453
            FileLock lock = takeLock ();
 
454
            try {
 
455
                getFile ().delete (lock);
 
456
            } finally {
 
457
                if (!locked)
 
458
                    lock.releaseLock();
 
459
            }
 
460
        }
 
461
 
 
462
    }
 
463
}