alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.registry;

import org.netbeans.api.registry.ObjectRef;
import org.netbeans.spi.registry.BasicContext;
import org.netbeans.spi.registry.SpiUtils;
import org.openide.filesystems.FileObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import java.io.IOException;

/**
 * Provides ObjectBinding.Reader and ObjectBinding.Writer for ObjectBinding implementation.
 * (copy & pasted & refactored from original org.netbeans.core.registry.ObjectBinding)
 */
final class ObjectRefBinding {
    private static final String OBJECTREF_EXTENSION = "xml";//NOI18N
    private static final String OBJECTREF_NAMESPACE = "http://www.netbeans.org/ns/registry";
    private static final String OBJECTREF_ELEMENT = "link";

    static final ObjectBinding.Reader READER = new ReaderImpl();
    static final ObjectBinding.Writer WRITER = new WriterImpl();

    private ObjectRefBinding() {
    }

    private static class ReaderImpl extends ObjectBinding.Reader {
        boolean canRead(FileObject fo) {
            boolean isRightExtension = fo.getExt().equalsIgnoreCase(OBJECTREF_EXTENSION);
            Document d = (isRightExtension == true) ? DocumentUtils.DocumentRef.getDOM(fo) : null;  
            
            return (isRightExtension && d != null && d.getDocumentElement() != null && d.getDocumentElement().getNamespaceURI() != null
                    && d.getDocumentElement().getNamespaceURI().equals(OBJECTREF_NAMESPACE) &&
                    d.getDocumentElement().getNodeName().equals(OBJECTREF_ELEMENT));
        }

        String getFileExtension() {
            return OBJECTREF_EXTENSION;
        }

        ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) {
            return new ObjectBindingImpl(ctx, fo);
        }
    }

    private static class WriterImpl extends ObjectBinding.Writer {
        boolean canWrite(Object obj) {
            return (obj instanceof ObjectRef);
        }

        void write(FileObject fo, Object obj) throws IOException {
            Document doc = org.netbeans.core.registry.DocumentUtils.createDocument();
            Element e = doc.createElementNS(OBJECTREF_NAMESPACE, OBJECTREF_ELEMENT);
            Text t = doc.createTextNode(((ObjectRef) obj).getContextAbsoluteName() + "/" + ((ObjectRef) obj).getBindingName());
            e.appendChild(t);
            doc.appendChild(e);
            org.netbeans.core.registry.DocumentUtils.writeDocument(fo, doc);
        }

        String getFileExtension() {
            return OBJECTREF_EXTENSION;
        }
    }

    private static final class ObjectBindingImpl extends ObjectBinding {
        private DocumentUtils.DocumentRef docRef;
        private BasicContext context;

        ObjectBindingImpl(BasicContext context, FileObject fo) {
            super(fo);
            this.context = context;
            docRef = new DocumentUtils.DocumentRef();
        }

        public Object createInstance() throws IOException {
            Object retVal = null;
            Document d = docRef.getDocument(getFile());
            String target = DocumentUtils.getTextValue(d.getDocumentElement());
            target = target.trim();
            int i = target.lastIndexOf('/');
            String contextName = target.substring(0, i);
            String bindingName = target.substring(i + 1);
            retVal = SpiUtils.createObjectRef(((ContextImpl) context).getRootContextImpl(), contextName, bindingName);
            return retVal;
        }

        public boolean isEnabled() {
            return (getFile() != null && getFile().isValid());
        }
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.