1 19 20 package org.netbeans.modules.masterfs.filebasedfs.naming; 21 22 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo; 23 24 import java.io.File ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.WeakReference ; 27 import java.util.*; 28 import org.netbeans.modules.masterfs.providers.ProvidedExtensions; 29 30 33 public final class NamingFactory { 34 private static final Map nameMap = new WeakHashMap(); 35 36 public static synchronized FileNaming fromFile(final File file) { 37 final LinkedList list = new LinkedList(); 38 File current = file; 39 while (current != null) { 40 list.addFirst(current); 41 current = current.getParentFile(); 42 } 43 44 FileNaming fileName = null; 45 for (int i = 0; i < list.size(); i++) { 46 fileName = NamingFactory.registerInstanceOfFileNaming(fileName, (File ) list.get(i)); 47 } 48 49 return fileName; 50 } 51 52 public static synchronized int getSize () { 53 return nameMap.size(); 54 } 55 56 public static synchronized FileNaming fromFile(final FileNaming parentFn, final File file) { 57 return NamingFactory.registerInstanceOfFileNaming(parentFn, file); 58 } 59 60 public static synchronized void checkCaseSensitivity(final FileNaming childName, final File f) { 61 if (!childName.getFile().getName().equals(f.getName())) { 62 boolean isCaseSensitive = !new File (f,"a").equals(new File (f,"A")); if (!isCaseSensitive) { 64 NamingFactory.rename(childName,f.getName()); 65 } 66 } 67 } 68 69 private static synchronized FileNaming[] rename (FileNaming fNaming, String newName) { 70 return rename(fNaming, newName, null); 71 } 72 73 public static synchronized FileNaming[] rename (FileNaming fNaming, String newName, ProvidedExtensions.IOHandler handler) { 74 final ArrayList all = new ArrayList(); 75 boolean retVal = false; 76 remove(fNaming, null); 77 retVal = fNaming.rename(newName, handler); 78 all.add(fNaming); 79 NamingFactory.registerInstanceOfFileNaming(fNaming.getParent(), fNaming.getFile(), fNaming); 80 renameChildren(all); 81 return (retVal) ? ((FileNaming[])all.toArray(new FileNaming[all.size()])) : null; 82 } 83 84 private static void renameChildren(final ArrayList all) { 85 HashMap toRename = new HashMap (); 86 for (Iterator iterator = nameMap.entrySet().iterator(); iterator.hasNext();) { 87 Map.Entry entry = (Map.Entry) iterator.next(); 88 Integer id = (Integer )entry.getKey(); 89 90 List list = new ArrayList(); 91 92 Object value = entry.getValue(); 94 if (value instanceof Reference ) { 95 list.add(value); 96 } else if (value instanceof List) { 97 list.addAll((List) value); 98 } 99 100 for (int i = 0; i < list.size(); i++) { 101 FileNaming fN = (FileNaming)((Reference ) list.get(i)).get(); 102 if (fN == null) continue; 103 Integer computedId = NamingFactory.createID(fN.getFile()); 104 105 boolean isRenamed = (!computedId.equals(id)); 106 if (isRenamed) { 107 toRename.put(id, fN); 108 } 109 } 110 } 111 112 for (Iterator iterator = toRename.entrySet().iterator(); iterator.hasNext();) { 113 Map.Entry entry = (Map.Entry) iterator.next(); 114 Integer id = (Integer )entry.getKey(); 115 FileNaming fN = (FileNaming)entry.getValue(); 116 all.add(fN); 117 remove(fN, id); 118 fN.getId(true); 119 NamingFactory.registerInstanceOfFileNaming(fN.getParent(), fN.getFile(), fN); 120 } 121 } 122 123 private static void remove(final FileNaming fNaming, Integer id) { 124 id = (id != null) ? id : fNaming.getId(); 125 Object value = NamingFactory.nameMap.get(id); 126 if (value instanceof List) { 127 Reference ref = NamingFactory.getReference((List) value, fNaming.getFile()); 128 if (ref != null) { 129 ((List) value).remove(ref); 130 } 131 } else { 132 NamingFactory.nameMap.remove(id); 133 } 134 } 135 136 public static Integer createID(final File file) { 137 return new Integer (file.hashCode()); 138 } 139 private static FileNaming registerInstanceOfFileNaming(final FileNaming parentName, final File file) { 140 return NamingFactory.registerInstanceOfFileNaming(parentName, file, null); 141 } 142 143 private static FileNaming registerInstanceOfFileNaming(final FileNaming parentName, final File file, final FileNaming newValue) { 144 FileNaming retVal; 145 146 final Object value = NamingFactory.nameMap.get(new Integer (file.hashCode())); 147 Reference ref = (Reference ) (value instanceof Reference ? value : null); 148 ref = (ref == null && value instanceof List ? NamingFactory.getReference((List) value, file) : ref); 149 150 final FileNaming cachedElement = (ref != null) ? (FileNaming) ref.get() : null; 151 152 if (cachedElement != null && cachedElement.getFile().compareTo(file) == 0) { 153 retVal = cachedElement; 154 } else { 155 retVal = (newValue == null) ? NamingFactory.createFileNaming(file, parentName) : newValue; 156 final WeakReference refRetVal = new WeakReference (retVal); 157 158 final boolean isList = (value instanceof List); 159 if (cachedElement != null || isList) { 160 if (isList) { 162 ((List) value).add(refRetVal); 163 } else { 164 final List l = new ArrayList(); 165 l.add(refRetVal); 166 NamingFactory.nameMap.put(retVal.getId(), l); 167 } 168 } else { 169 Reference r = (Reference )NamingFactory.nameMap.put(retVal.getId(), refRetVal); 171 assert r == null || r.get() == null; 172 } 173 } 174 175 assert retVal != null; 176 177 return retVal; 178 } 179 180 private static Reference getReference(final List list, final File f) { 181 Reference retVal = null; 182 for (int i = 0; retVal == null && i < list.size(); i++) { 183 final Reference ref = (Reference ) list.get(i); 184 final FileNaming cachedElement = (ref != null) ? (FileNaming) ref.get() : null; 185 if (cachedElement != null && cachedElement.getFile().compareTo(f) == 0) { 186 retVal = ref; 187 } 188 } 189 return retVal; 190 } 191 192 private static FileNaming createFileNaming(final File f, final FileNaming parentName) { 193 FileName retVal = null; 194 final FileInfo fInfo = new FileInfo(f); 196 197 if (f.isFile()) { 198 retVal = new FileName(parentName, f); 199 } else { 200 if (f.isDirectory()) { 201 retVal = new FolderName(parentName, f); 202 } else { 203 if (fInfo.isUNCFolder()) { 204 retVal = new UNCName(parentName, f); 205 } 206 } 207 } 208 209 if (retVal == null ) { 210 retVal = new FileName(parentName, f) { 212 public boolean isDirectory() { 213 return false; 214 } 215 216 public boolean isFile() { 217 return false; 218 } 219 }; 220 221 } 222 223 assert retVal != null : f.getAbsolutePath() + " isDirectory: " + f.isDirectory() + " isFile: " + f.isFile() + " exists: " + f.exists(); return retVal; 225 } 226 227 } 228 | Popular Tags |