1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.api.registry.ObjectRef; 23 import org.netbeans.spi.registry.BasicContext; 24 import org.netbeans.spi.registry.SpiUtils; 25 import org.openide.filesystems.FileObject; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.Text ; 29 30 import java.io.IOException ; 31 32 36 final class ObjectRefBinding { 37 private static final String OBJECTREF_EXTENSION = "xml"; private static final String OBJECTREF_NAMESPACE = "http://www.netbeans.org/ns/registry"; 39 private static final String OBJECTREF_ELEMENT = "link"; 40 41 static final ObjectBinding.Reader READER = new ReaderImpl(); 42 static final ObjectBinding.Writer WRITER = new WriterImpl(); 43 44 private ObjectRefBinding() { 45 } 46 47 private static class ReaderImpl extends ObjectBinding.Reader { 48 boolean canRead(FileObject fo) { 49 boolean isRightExtension = fo.getExt().equalsIgnoreCase(OBJECTREF_EXTENSION); 50 Document d = (isRightExtension == true) ? DocumentUtils.DocumentRef.getDOM(fo) : null; 51 52 return (isRightExtension && d != null && d.getDocumentElement() != null && d.getDocumentElement().getNamespaceURI() != null 53 && d.getDocumentElement().getNamespaceURI().equals(OBJECTREF_NAMESPACE) && 54 d.getDocumentElement().getNodeName().equals(OBJECTREF_ELEMENT)); 55 } 56 57 String getFileExtension() { 58 return OBJECTREF_EXTENSION; 59 } 60 61 ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) { 62 return new ObjectBindingImpl(ctx, fo); 63 } 64 } 65 66 private static class WriterImpl extends ObjectBinding.Writer { 67 boolean canWrite(Object obj) { 68 return (obj instanceof ObjectRef); 69 } 70 71 void write(FileObject fo, Object obj) throws IOException { 72 Document doc = org.netbeans.core.registry.DocumentUtils.createDocument(); 73 Element e = doc.createElementNS(OBJECTREF_NAMESPACE, OBJECTREF_ELEMENT); 74 Text t = doc.createTextNode(((ObjectRef) obj).getContextAbsoluteName() + "/" + ((ObjectRef) obj).getBindingName()); 75 e.appendChild(t); 76 doc.appendChild(e); 77 org.netbeans.core.registry.DocumentUtils.writeDocument(fo, doc); 78 } 79 80 String getFileExtension() { 81 return OBJECTREF_EXTENSION; 82 } 83 } 84 85 private static final class ObjectBindingImpl extends ObjectBinding { 86 private DocumentUtils.DocumentRef docRef; 87 private BasicContext context; 88 89 ObjectBindingImpl(BasicContext context, FileObject fo) { 90 super(fo); 91 this.context = context; 92 docRef = new DocumentUtils.DocumentRef(); 93 } 94 95 public Object createInstance() throws IOException { 96 Object retVal = null; 97 Document d = docRef.getDocument(getFile()); 98 String target = DocumentUtils.getTextValue(d.getDocumentElement()); 99 target = target.trim(); 100 int i = target.lastIndexOf('/'); 101 String contextName = target.substring(0, i); 102 String bindingName = target.substring(i + 1); 103 retVal = SpiUtils.createObjectRef(((ContextImpl) context).getRootContextImpl(), contextName, bindingName); 104 return retVal; 105 } 106 107 public boolean isEnabled() { 108 return (getFile() != null && getFile().isValid()); 109 } 110 } 111 112 } 113 | Popular Tags |