1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.core.registry.oldformats.InstanceUtils; 23 import org.netbeans.spi.registry.BasicContext; 24 import org.openide.filesystems.FileObject; 25 26 import java.io.IOException ; 27 28 29 33 final class InstanceBinding { 34 private static final String INSTANCE_EXTENSION = "instance"; static final ObjectBinding.Reader READER = new ObjectBinding.Reader() { 36 boolean canRead(FileObject fo) { 37 return fo.getExt().equalsIgnoreCase(INSTANCE_EXTENSION); 38 } 39 40 String getFileExtension() { 41 return INSTANCE_EXTENSION; 42 } 43 44 ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) { 45 return new ObjectBindingImpl(fo); 46 } 47 }; 48 49 private InstanceBinding() { 50 } 51 52 private static class ObjectBindingImpl extends ObjectBinding { 53 protected ObjectBindingImpl(FileObject fo) { 54 super(fo); 55 } 56 57 public Object createInstance() throws IOException { 58 try { 59 return read(getFile()); 60 } catch (ClassNotFoundException e) { 61 throw new IOException (e.getLocalizedMessage()); 62 } catch (UnsupportedOperationException e) { 63 throw new IOException (e.getLocalizedMessage()); 64 } 65 } 66 67 public boolean isEnabled() { 68 return (getFile() != null && getFile().isValid()); 69 } 70 } 71 72 private static Object read(FileObject fo) 73 throws ClassNotFoundException , UnsupportedOperationException { 74 Object o = fo.getAttribute("instanceCreate"); 75 if (o != null) { 76 return o; 77 } 78 79 o = fo.getAttribute("instanceClass"); 80 if (o != null) { 81 if (o instanceof String ) { 82 return InstanceUtils.newValue((String ) o); 83 } else { 84 return o; 85 } 86 } 87 88 String s = fo.getName(); 89 if (s.indexOf('[') >= 0) { 90 s = s.substring(s.indexOf('[')+1, s.indexOf(']')); 91 } 92 s = s.replace('-', '.'); 93 return InstanceUtils.newValue(s); 94 } 95 96 } 97 | Popular Tags |