1 19 20 package org.netbeans.modules.java; 21 22 import java.io.FileNotFoundException ; 23 import java.io.FilterInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.ObjectInput ; 27 import java.text.DateFormat ; 28 import java.util.Collection ; 29 import java.util.Date ; 30 import java.util.LinkedList ; 31 import java.util.Map ; 32 import org.netbeans.api.java.classpath.ClassPath; 33 import org.netbeans.modules.java.settings.JavaSettings; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.DataObjectExistsException; 37 import org.openide.loaders.FileEntry; 38 import org.openide.loaders.MultiDataObject; 39 import org.openide.loaders.MultiFileLoader; 40 import org.openide.util.NbBundle; 41 42 46 public class JavaDataLoader extends MultiFileLoader { 47 48 public static final String JAVA_EXTENSION = "java"; 50 public static final String PROP_PARSER_ENGINE = "parserEngine"; 52 static final long serialVersionUID =-6286836352608877232L; 53 54 57 public JavaDataLoader() { 58 this("org.netbeans.modules.java.JavaDataObject"); } 60 61 65 public JavaDataLoader(String recognizedObject) { 66 super(recognizedObject); 67 } 68 69 public JavaDataLoader(Class recognizedObject) { 70 super(recognizedObject); 71 } 72 73 protected String actionsContext () { 74 return "Loaders/text/x-java/Actions/"; } 76 77 protected String defaultDisplayName() { 78 return NbBundle.getMessage(JavaDataLoader.class, "PROP_JavaLoader_Name"); 79 } 80 81 88 protected MultiDataObject createMultiObject (FileObject primaryFile) 89 throws DataObjectExistsException, java.io.IOException { 90 return new JavaDataObject(primaryFile, this); 91 } 92 93 101 protected FileObject findPrimaryFile (FileObject fo) { 102 if (fo.isFolder()) return null; 104 if (fo.getExt().equals(JAVA_EXTENSION)) 105 return fo; 106 return null; 107 } 108 109 116 protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) { 117 return new JavaFileEntry(obj, primaryFile); 118 } 119 120 127 protected MultiDataObject.Entry createSecondaryEntry (MultiDataObject obj, FileObject secondaryFile) { 128 ErrorManager.getDefault().log ("Subclass of JavaDataLoader ("+this.getClass().getName() 131 +") has secondary entries but does not override createSecondaryEntries (MultidataObject, FileObject) method."); return new FileEntry.Numb(obj, secondaryFile); 133 } 134 135 145 protected Map createStringsMap() { 146 Map map = JavaSettings.getDefault().getReplaceableStringsProps(); 147 map.put("DATE", DateFormat.getDateInstance(DateFormat.LONG).format(new Date ())); map.put("TIME", DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date ())); return map; 150 } 151 152 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 153 super.readExternal(in); 154 if (in.available() > 0) { 155 int v; 156 157 v = in.readInt(); 158 if (v >= 1) 159 in.readObject(); 160 if (v >= 2) 161 in.readBoolean(); 162 } 163 } 164 165 169 public class JavaFileEntry extends IndentFileEntry { 170 static final long serialVersionUID =8244159045498569616L; 171 172 176 boolean disableInputStream; 177 178 181 private Collection activeReaders; 182 183 184 public JavaFileEntry(MultiDataObject obj, FileObject file) { 185 super(obj, file); 186 } 187 188 195 protected java.text.Format createFormat (FileObject target, String n, String e) { 196 Map map = createStringsMap(); 197 198 modifyMap(map, target, n, e); 199 200 JMapFormat format = new JMapFormat(map); 201 format.setLeftBrace("__"); format.setRightBrace("__"); format.setCondDelimiter("$"); format.setExactMatch(false); 205 return format; 206 } 207 208 216 protected void modifyMap(Map map, FileObject target, String n, String e) { 217 ClassPath cp = ClassPath.getClassPath(target, ClassPath.SOURCE); 218 String resourcePath = ""; 219 if (cp != null) { 220 resourcePath = cp.getResourceName(target); 221 } else { 222 ErrorManager.getDefault().log(ErrorManager.WARNING, "No classpath was found for folder: "+target); 223 } 224 map.put("NAME", n); map.put("PACKAGE", resourcePath.replace('/', '.')); map.put("PACKAGE_SLASHES", resourcePath); if (target.isRoot ()) { 230 map.put ("PACKAGE_AND_NAME", n); map.put ("PACKAGE_AND_NAME_SLASHES", n); } else { 233 map.put ("PACKAGE_AND_NAME", resourcePath.replace('/', '.') + '.' + n); map.put ("PACKAGE_AND_NAME_SLASHES", resourcePath + '/' + n); } 236 map.put("QUOTES","\""); } 241 242 public synchronized void addReader(InputStream r) { 243 if (activeReaders == null) { 244 activeReaders = new LinkedList (); 245 } 246 activeReaders.add(r); 247 } 248 249 public synchronized void removeReader(InputStream r) { 250 if (activeReaders == null) 251 return; 252 activeReaders.remove(r); 253 } 254 255 public void delete() throws IOException { 256 synchronized (this) { 257 if (activeReaders != null) { 258 int size=activeReaders.size(); 259 260 if (size>0) { 261 InputStream [] readers=(InputStream [])activeReaders.toArray(new InputStream [size]); 262 int i; 263 264 for (i=0; i<readers.length; i++) readers[i].close(); 265 } 266 } 267 activeReaders = null; 268 disableInputStream = true; 269 } 270 super.delete(); 271 } 272 273 public InputStream getInputStream() throws FileNotFoundException { 274 FileObject fob = getFile(); 275 synchronized (this) { 276 if (disableInputStream) { 277 throw new FileNotFoundException ("File is being deleted."); } 280 InputStream s = new NotifyInputStream(fob.getInputStream()); 281 addReader(s); 282 return s; 283 } 284 } 285 286 private class NotifyInputStream extends FilterInputStream { 287 public NotifyInputStream(InputStream is) { 288 super(is); 289 } 290 291 public void close() throws IOException { 292 super.close(); 293 removeReader(this); 294 } 295 } 296 } 297 } 298 | Popular Tags |