1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.core.registry.oldformats.SerialDataConvertor; 23 import org.netbeans.spi.registry.BasicContext; 24 import org.openide.filesystems.FileObject; 25 import org.w3c.dom.Document ; 26 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 30 34 final class SettingBinding { 35 private static final String SETTINGS_EXTENSION = "settings"; static final ObjectBinding.Reader READER = new ReaderImpl(); 37 static final ObjectBinding.Writer WRITER = new WriterImpl(); 38 39 private SettingBinding() { 40 } 41 42 private static class ReaderImpl extends ObjectBinding.Reader { 43 boolean canRead(FileObject fo) { 44 boolean isRightExtension = fo.getExt().equalsIgnoreCase(SETTINGS_EXTENSION) || fo.getExt().equalsIgnoreCase("xml"); 45 Document dom = (isRightExtension) ? DocumentUtils.DocumentRef.getDOM(fo) : null; 46 47 return isRightExtension && dom != null && SerialDataConvertor.isSettingsFormat(dom.getDocumentElement()); 48 } 49 50 String getFileExtension() { 51 return SETTINGS_EXTENSION; 52 } 53 54 ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) { 55 return new ObjectBindingImpl(fo); 56 } 57 } 58 59 private static class WriterImpl extends ObjectBinding.Writer { 60 boolean canWrite(Object obj) { 61 return (obj instanceof Serializable ); 62 } 63 64 void write(FileObject fo, Object obj) throws IOException { 65 SerialDataConvertor sdc = new SerialDataConvertor(); 66 sdc.write(fo, obj); 67 } 68 69 String getFileExtension() { 70 return SETTINGS_EXTENSION; 71 } 72 } 73 74 private static class ObjectBindingImpl extends ObjectBinding { 75 private DocumentUtils.DocumentRef docRef; 76 77 ObjectBindingImpl(FileObject fo) { 78 super(fo); 79 docRef = new DocumentUtils.DocumentRef(); 80 } 81 82 public Object createInstance() throws IOException { 83 Object retVal = null; 84 Document d = docRef.getDocument(getFile()); 85 try { 86 retVal = SerialDataConvertor.read(d.getDocumentElement(), getFile()); 87 } catch (ClassNotFoundException e) { 88 throw new IOException (e.getLocalizedMessage()); 89 } 90 91 return retVal; 92 } 93 94 public boolean isEnabled() { 95 String moduleName = getModuleName(); 96 return (moduleName == null) || StateUpdater.getDefault().isModuleEnabled(moduleName); 97 } 98 99 public Object getModuleDescriptor() { 100 return new Object () { 101 public boolean equals(Object obj) { 102 if (obj instanceof String ) { 103 String modName = (String )obj; 104 String moduleName = getModuleName(); 105 return moduleName == null ? false : moduleName.equals(modName); 106 } 107 return false; 108 } 109 }; 110 } 111 112 private String getModuleName() { 113 String moduleName = SerialDataConvertor.getModuleCodeName(docRef.getDocument(getFile()).getDocumentElement()); 114 return (moduleName != null) ? StateUpdater.cutOffVersion(moduleName) : null; 115 } 116 } 117 118 } 119 | Popular Tags |