1 19 package org.netbeans.modules.xml.core; 20 21 import java.io.*; 22 import java.util.*; 23 import java.text.DateFormat ; 24 25 import org.openide.filesystems.*; 26 import org.openide.loaders.*; 27 import org.openide.util.actions.SystemAction; 28 import org.openide.util.*; 29 import org.openide.actions.*; 30 import org.openide.ErrorManager; 31 32 import org.netbeans.modules.xml.core.actions.CollectXMLAction; 33 import org.netbeans.modules.xml.core.actions.XMLUpdateDocumentAction; 34 35 39 public class XMLDataLoader extends UniFileLoader { 40 private static final long serialVersionUID = 3824119075670384804L; 42 43 44 private static final String XML_EXT = "xml"; private static final String XMLINFO_EXT = "xmlinfo"; 47 48 public XMLDataLoader () { 49 super ("org.netbeans.modules.xml.core.XMLDataObject"); } 51 52 56 58 protected void initialize () { 59 super.initialize(); 60 61 ExtensionList ext = getExtensions(); 62 ext.addExtension (XML_EXT); 63 ext.addMimeType (org.netbeans.modules.xml.core.XMLDataObject.MIME_TYPE); 64 ext.addMimeType ("application/xml"); setExtensions (ext); 67 } 68 69 protected String actionsContext() { 70 return "Loaders/text/xml-mime/Actions/"; 71 } 72 73 76 protected String defaultDisplayName () { 77 return Util.THIS.getString ("PROP_XmlLoader_Name"); 78 } 79 80 86 protected FileObject findPrimaryFile (FileObject fo) { 87 if (fo.isFolder()) { 88 return null; 89 } 90 91 FileObject res = null; 92 if ( super.findPrimaryFile (fo) != null ) { 93 res = fo; 94 } else if ( XMLINFO_EXT.equals (fo.getExt()) ) { 95 res = FileUtil.findBrother (fo, XML_EXT); 96 } else if ( fo.getMIMEType().endsWith ("+xml") ) { res = fo; 99 } 100 101 try { 103 if ( ( res != null ) && 104 ( res.getFileSystem().isDefault() == true ) ) { if ( ( DataLoaderPool.getPreferredLoader (res) != this ) && ( isTemplate (res) == false ) ) { res = null; 108 } 109 } 110 } catch (FileStateInvalidException ex) { 111 } 113 114 return res; 115 } 116 117 118 121 private static boolean isTemplate (FileObject fo) { 122 Object o = fo.getAttribute (DataObject.PROP_TEMPLATE); 123 boolean ret = false; 124 if ( o instanceof Boolean ) { 125 ret = ((Boolean ) o).booleanValue(); 126 } 127 return ret; 128 } 129 130 131 139 protected MultiDataObject createMultiObject (FileObject primaryFile) 140 throws DataObjectExistsException { 141 try { 142 return new org.netbeans.modules.xml.core.XMLDataObject (primaryFile, this); 144 } catch (org.openide.loaders.DataObjectExistsException ex) { 145 throw ex; 148 } 149 } 150 151 156 protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) { 157 return new XMLFileEntry (obj, primaryFile); 158 } 159 160 166 protected MultiDataObject.Entry createSecondaryEntry (MultiDataObject obj, FileObject secondaryFile) { 167 return new FileEntry (obj, secondaryFile); 168 } 169 170 171 174 public static class XMLFileEntry extends FileEntry.Format { 175 176 177 private static final long serialVersionUID = -7300320795693949470L; 178 179 180 184 boolean disableInputStream; 185 186 189 private Collection activeReaders; 190 191 192 public XMLFileEntry (MultiDataObject obj, FileObject file) { 193 super (obj, file); 194 } 195 196 203 protected java.text.Format createFormat (FileObject target, String name, String ext) { 204 HashMap map = new HashMap(); 205 Date now = new Date(); 206 207 map.put ("NAME", name + "." + ext); 209 map.put ("ROOT", "root"); 219 map.put ("DATE", DateFormat.getDateInstance (DateFormat.LONG).format (now)); map.put ("TIME", DateFormat.getTimeInstance (DateFormat.SHORT).format (now)); map.put ("USER", System.getProperty ("user.name")); 223 MapFormat format = new MapFormat (map); 224 format.setLeftBrace ("__"); format.setRightBrace ("__"); format.setExactMatch (false); 227 return format; 228 } 229 230 235 236 237 243 public FileObject createFromTemplate (FileObject f, String name) throws IOException { 244 String ext = getFile ().getExt (); 245 246 if (name == null) { 247 name = FileUtil.findFreeFileName( 248 f, 249 getFile ().getName (), ext 250 ); 251 } 252 FileObject fo = f.createData (name, ext); 253 254 java.text.Format frm = createFormat (f, name, ext); 255 256 BufferedReader r = new BufferedReader (new InputStreamReader (getFile ().getInputStream (), "UTF8")); try { 258 FileLock lock = fo.lock (); 259 try { 260 BufferedWriter w = new BufferedWriter (new OutputStreamWriter (fo.getOutputStream (lock), "UTF8")); 262 try { 263 String line = null; 264 String current; 265 while ((current = r.readLine ()) != null) { 266 line = frm.format (current); 267 w.write (line); 268 w.newLine (); 269 } 270 } finally { 271 w.close (); 272 } 273 } finally { 274 lock.releaseLock (); 275 } 276 } finally { 277 r.close (); 278 } 279 280 FileUtil.copyAttributes (getFile (), fo); 282 283 try { 285 DataObject.find(fo).setTemplate (false); 286 } catch (DataObjectNotFoundException ex) { 287 ErrorManager.getDefault().notify(ex); 288 } 289 290 return fo; 291 } 292 293 296 private synchronized void addReader(InputStream r) { 297 if (activeReaders == null) { 298 activeReaders = new LinkedList(); 299 } 300 activeReaders.add(r); 301 } 302 303 private synchronized void removeReader(InputStream r) { 304 if (activeReaders == null) 305 return; 306 activeReaders.remove(r); 307 } 308 309 public void delete() throws IOException { 310 synchronized (this) { 311 if (activeReaders != null && activeReaders.size() > 0) { 312 for (Iterator it = activeReaders.iterator(); it.hasNext(); ) { 313 InputStream r = (InputStream)it.next(); 314 r.close(); 315 it.remove(); 316 } 317 } 318 activeReaders = null; 319 disableInputStream = true; 320 } 321 super.delete(); 322 } 323 324 public InputStream getInputStream() throws FileNotFoundException { 325 FileObject fob = getFile(); 326 synchronized (this) { 327 if (disableInputStream) { 328 throw new FileNotFoundException("File is being deleted."); } 331 InputStream s = new NotifyInputStream(fob.getInputStream()); 332 addReader(s); 333 return s; 334 } 335 } 336 337 private class NotifyInputStream extends FilterInputStream { 338 public NotifyInputStream(InputStream is) { 339 super(is); 340 } 341 342 public void close() throws IOException { 343 super.close(); 344 removeReader(this); 345 } 346 } 347 348 public FileLock takeLock() throws IOException { 350 FileLock lock = super.takeLock(); 351 352 if ( Util.THIS.isLoggable() ) Util.THIS.debug("XMLDataLoader.XMLEntry.takeLock()/" + getFile() + "=" + lock); 354 return lock; 355 } 356 357 } 358 359 } 360 | Popular Tags |