1 19 20 package org.netbeans.core.registry; 21 22 import org.openide.filesystems.FileObject; 23 import org.openide.loaders.DataObject; 24 import org.openide.loaders.DataObjectNotFoundException; 25 import org.openide.cookies.InstanceCookie; 26 import org.netbeans.spi.registry.BasicContext; 27 28 import java.io.IOException ; 29 30 36 37 final class XMLBasedBinding extends ObjectBinding.Reader { 38 static final ObjectBinding.Reader READER = new XMLBasedBinding (); 39 40 private XMLBasedBinding () {} 41 42 boolean canRead(FileObject fo) { 43 boolean retVal = (fo.getExt().equals(getFileExtension())); 44 45 if (retVal) { 46 try { 47 InstanceCookie ic = getInstanceCookie(fo); 48 retVal = (ic != null); 49 } catch (DataObjectNotFoundException e) { 50 retVal = false; 51 } 52 } 53 return retVal; 54 } 55 56 private static InstanceCookie getInstanceCookie(FileObject fo) throws DataObjectNotFoundException { 57 DataObject d = DataObject.find(fo); 58 return (InstanceCookie)d.getCookie(InstanceCookie.class); 59 } 60 61 String getFileExtension() { 62 return "xml"; } 64 65 ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) { 66 return new ObjectBindingImpl (fo); 67 } 68 69 private class ObjectBindingImpl extends ObjectBinding { 70 InstanceCookie ic; 71 ObjectBindingImpl(FileObject fo) { 72 super(fo); 73 } 74 75 public Object createInstance() throws IOException { 76 InstanceCookie icLocal = (ic != null) ? ic : getInstanceCookie(getFile()); 77 try { 78 return icLocal.instanceCreate(); 79 } catch (ClassNotFoundException e) { 80 throw new IOException (e.getLocalizedMessage()); 81 } 82 } 83 84 public boolean isEnabled() { 85 return (getFile() != null && getFile().isValid()); 86 } 87 } 88 } 89 | Popular Tags |