1 19 package org.netbeans.modules.xml.core.lib; 20 21 import java.io.IOException ; 22 23 import org.openide.loaders.DataObject; 24 import org.openide.filesystems.*; 25 26 34 public final class FileUtilities { 35 36 41 public static DataObject createDataObject (final FileObject folder, final String name, final String ext, final boolean overwrite) throws IOException { 42 DataObject dataObject = null; 43 String normalized = ext == null ? "" : "".equals(ext) ? "" : "." + ext; 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 56 public static FileObject createFileObject (FileObject folder, String nameExt, boolean overwrite) throws IOException { 57 59 while ( nameExt.startsWith ("../") ) { nameExt = nameExt.substring (3); 61 if ( folder.isRoot() == false ) { 62 folder = folder.getParent(); 63 } 64 } 65 66 FileObject fo = null; 67 68 73 String 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); } else { 80 name = nameExt; 81 ext = ""; } 83 84 fo = createFileObject (folder, name, ext, overwrite, true, false); 85 86 return fo; 87 } 88 89 90 96 99 102 105 private static FileObject createFileObject (final FileObject folder, final String name, final String ext, final boolean overwrite, boolean askForOverwrite, final boolean makeCopy) throws IOException { 106 if ( Util.THIS.isLoggable() ) { 107 Util.THIS.debug ("[FileUtilities.createFileObject]"); Util.THIS.debug (" folder = " + folder); Util.THIS.debug (" name = " + name); Util.THIS.debug (" .ext = " + ext); } 112 113 FileObject file = folder.getFileObject (name + ext); 114 115 if (file == null) { 117 file = FileUtil.createData (folder, name + ext); 118 119 } else if ( file.isVirtual() || overwrite) { 120 127 FileSystem fs = folder.getFileSystem(); 128 final FileObject tempFile = file; 129 130 fs.runAtomicAction (new FileSystem.AtomicAction () { 131 public void run () throws IOException { 132 133 if ( ( makeCopy == true ) && 134 ( overwrite == true ) && 135 ( tempFile.isVirtual() == false ) ) { 136 138 for (int i = 1; true; i++) { 139 if (folder.getFileObject(name + ext + i) == null) { 140 tempFile.copy (folder, name + ext + i, ""); break; 142 } 143 } 144 } 145 146 if ( ( makeCopy == false ) && 147 ( tempFile.isVirtual() ) ) { 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 |