KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > lib > FileUtilities


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 package org.netbeans.modules.xml.core.lib;
20
21 import java.io.IOException JavaDoc;
22
23 import org.openide.loaders.DataObject;
24 import org.openide.filesystems.*;
25
26 /**
27  * Main purpose it to unify handling of FileObject's isVirtual(), isValid(),
28  * canRead() and canWrite() aspects.
29  *
30  * @author Libor Kramolis
31  * @author Petr Kuzel, added support for empty extensions
32  * @version 0.1
33  */

34 public final class FileUtilities {
35
36     /*
37      * Create data object in given folder. Consider calling it from FS atomic action
38      * as empty files may confuse loaders.
39      * @return DataObject or null if the DataObject exist and should not be overwritten
40      */

41     public static DataObject createDataObject (final FileObject folder, final String JavaDoc name, final String JavaDoc ext, final boolean overwrite) throws IOException JavaDoc {
42         DataObject dataObject = null;
43         String JavaDoc normalized = ext == null ? "" : "".equals(ext) ? "" : "." + ext; // NOI18N
44
FileObject fileObject = createFileObject (folder, name, normalized, overwrite, false, true);
45         if ( fileObject != null ) {
46             dataObject = DataObject.find (fileObject);
47         }
48         return dataObject;
49     }
50
51     /**
52      * Create DataObject at relative path to given folder. Consider calling it from FS atomic action
53      * as empty files may confuse loaders.
54      * @return DataObject or null if the DataObject exist and should not be overwritten
55      */

56     public static FileObject createFileObject (FileObject folder, String JavaDoc nameExt, boolean overwrite) throws IOException JavaDoc {
57         // eliminate relative path
58

59         while ( nameExt.startsWith ("../") ) { // NOI18N
60
nameExt = nameExt.substring (3);
61             if ( folder.isRoot() == false ) {
62                 folder = folder.getParent();
63             }
64         }
65
66         FileObject fo = null;
67
68 // if ( ( overwrite == true ) ||
69
// ( GuiUtil.confirmAction (Util.THIS.getString ("PROP_replaceMsg", nameExt)) ) ) {
70
// fo = FileUtil.createData (folder, nameExt);
71
// }
72

73         String JavaDoc name, ext;
74         int dotIndex = nameExt.lastIndexOf ('.');
75         int slashIndex = nameExt.lastIndexOf ('/');
76         if (dotIndex != -1 && dotIndex > slashIndex) {
77             name = nameExt.substring (0, dotIndex);
78             ext = "." + nameExt.substring (dotIndex + 1); // NOI18N
79
} else {
80             name = nameExt;
81             ext = ""; // NOI18N
82
}
83
84         fo = createFileObject (folder, name, ext, overwrite, true, false);
85
86         return fo;
87     }
88
89
90 // /*
91
// * @return FileObject or null if the FileObject exist and should not be overwritten
92
// */
93
// public static FileObject createFileObject (final FileObject folder, String nameExt, final boolean overwrite) throws IOException {
94
// int dotIndex = nameExt.lastIndexOf ('.');
95

96 // String name = nameExt.substring (0, dotIndex);
97
// String ext = nameExt.substring (dotIndex + 1);
98

99 // return createFileObject (folder, name, ext, overwrite, true, false);
100
// }
101

102     /*
103      * @return FileObject or null if the FileObject exist and should not be overwritten
104      */

105     private static FileObject createFileObject (final FileObject folder, final String JavaDoc name, final String JavaDoc ext, final boolean overwrite, boolean askForOverwrite, final boolean makeCopy) throws IOException JavaDoc {
106         if ( Util.THIS.isLoggable() ) /* then */ {
107             Util.THIS.debug ("[FileUtilities.createFileObject]"); // NOI18N
108
Util.THIS.debug (" folder = " + folder); // NOI18N
109
Util.THIS.debug (" name = " + name); // NOI18N
110
Util.THIS.debug (" .ext = " + ext); //NOI18N
111
}
112
113         FileObject file = folder.getFileObject (name + ext);
114         
115         if (file == null) { // new one
116

117             file = FileUtil.createData (folder, name + ext);
118             
119         } else if ( file.isVirtual() || overwrite) {
120             // isVirtual:
121
// FileObject represents virtual file (not available),
122
// so it is important to delete such virtual file
123
// and create real one.
124
// overwrite:
125
// make backup of original file
126

127             FileSystem fs = folder.getFileSystem();
128             final FileObject tempFile = file;
129
130             fs.runAtomicAction (new FileSystem.AtomicAction () {
131                 public void run () throws IOException JavaDoc {
132
133                     if ( ( makeCopy == true ) &&
134                          ( overwrite == true ) &&
135                          ( tempFile.isVirtual() == false ) ) {
136                         // Make copy of original not virtual file
137

138                         for (int i = 1; true; i++) {
139                             if (folder.getFileObject(name + ext + i) == null) {
140                                 tempFile.copy (folder, name + ext + i, ""); // NOI18N
141
break;
142                             }
143                         }
144                     }
145                     
146                     if ( ( makeCopy == false ) &&
147                          ( tempFile.isVirtual() ) ) { // do not create new file object
148
tempFile.delete();
149                         FileUtil.createData (folder, name + ext);
150                     }
151                 }
152             });
153
154             file = folder.getFileObject (name + ext);
155         } else if ( askForOverwrite ) {
156
157             if (!!! GuiUtil.confirmAction (Util.THIS.getString ("PROP_replaceMsg", name + ext) ) ) {
158                 file = null;
159             }
160
161         } else {
162             file = null;
163         }
164         
165         return file;
166     }
167
168 }
169
Popular Tags