1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.api.convertor.ConvertorDescriptor; 23 import org.netbeans.api.convertor.Convertors; 24 import org.netbeans.spi.registry.BasicContext; 25 import org.openide.filesystems.FileObject; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 29 import java.io.IOException ; 30 31 35 final class ConvertorBinding { 36 private static final String CONVERTOR_EXTENSION = "xml"; static final ObjectBinding.Reader READER = new ReaderImpl(); 38 static final ObjectBinding.Writer WRITER = new WriterImpl(); 39 40 private ConvertorBinding() { 41 } 42 43 private static final class ReaderImpl extends ObjectBinding.Reader { 44 final boolean canRead(final FileObject fo) { 45 boolean isRightExtension = fo.getExt().equalsIgnoreCase(getFileExtension()); 46 Document dom = (isRightExtension == true) ? DocumentUtils.DocumentRef.getDOM(fo) : null; 47 48 final Element documentElement = (dom != null) ? dom.getDocumentElement() : null; 49 return isRightExtension && documentElement != null && (documentElement.getNamespaceURI() != null); 50 } 51 52 final String getFileExtension() { 53 return CONVERTOR_EXTENSION; 54 } 55 56 final ObjectBinding getObjectBinding(final BasicContext ctx, final FileObject fo) { 57 return new ObjectBindingImpl(fo); 58 } 59 } 60 61 private static final class WriterImpl extends ObjectBinding.Writer { 62 final boolean canWrite(final Object obj) { 63 return Convertors.canWrite(obj); 64 } 65 66 final void write(final FileObject fo, final Object obj) throws IOException { 67 final Document doc = DocumentUtils.createDocument(); 68 final Element e = Convertors.write(doc, obj); 69 doc.appendChild(e); 70 DocumentUtils.writeDocument(fo, doc); 71 } 72 73 final String getFileExtension() { 74 return CONVERTOR_EXTENSION; 75 } 76 } 77 78 private static final class ObjectBindingImpl extends ObjectBinding { 79 private final DocumentUtils.DocumentRef docRef; 80 81 ObjectBindingImpl(final FileObject fo) { 82 super(fo); 83 docRef = new DocumentUtils.DocumentRef(); 84 } 85 86 public final Object createInstance() throws IOException { 87 return Convertors.read(docRef.getDocument(getFile()).getDocumentElement()); 88 } 89 90 public final Object getModuleDescriptor() { 91 return new Object () { 92 public boolean equals(final Object obj) { 93 if (obj instanceof ConvertorDescriptor) { 94 final ConvertorDescriptor cd = (ConvertorDescriptor) obj; 95 return cd.getElementName().equals(getElement()) && cd.getNamespace().equals(getNamespace()); 96 } 97 return false; 98 } 99 }; 100 } 101 102 public final boolean isEnabled() { 103 return Convertors.canRead(getNamespace(), getElement()); 104 } 105 106 private String getNamespace() { 107 return docRef.getDocument(getFile()).getDocumentElement().getNamespaceURI(); 108 } 109 110 private String getElement() { 111 return docRef.getDocument(getFile()).getDocumentElement().getNodeName(); 112 } 113 114 } 115 116 } 117 | Popular Tags |