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.convertor.ConvertorDescriptor;
import org.netbeans.api.convertor.Convertors;
import org.netbeans.spi.registry.BasicContext;
import org.openide.filesystems.FileObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

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 ConvertorBinding {
    private static final String CONVERTOR_EXTENSION = "xml";//NOI18N    
    static final ObjectBinding.Reader READER = new ReaderImpl();
    static final ObjectBinding.Writer WRITER = new WriterImpl();

    private ConvertorBinding() {
    }

    private static final class ReaderImpl extends ObjectBinding.Reader {
        final boolean canRead(final FileObject fo) {
            boolean isRightExtension = fo.getExt().equalsIgnoreCase(getFileExtension());
            Document dom = (isRightExtension == true) ? DocumentUtils.DocumentRef.getDOM(fo) : null;  
            
            final Element documentElement = (dom != null) ? dom.getDocumentElement() : null;            
            return isRightExtension && documentElement != null && (documentElement.getNamespaceURI() != null);/*Convertors.canRead(documentElement)*/
        }

        final String getFileExtension() {
            return CONVERTOR_EXTENSION;
        }

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

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

        final void write(final FileObject fo, final Object obj) throws IOException {
            final Document doc = DocumentUtils.createDocument();
            final Element e = Convertors.write(doc, obj);
            doc.appendChild(e);
            DocumentUtils.writeDocument(fo, doc);
        }

        final String getFileExtension() {
            return CONVERTOR_EXTENSION;
        }
    }

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

        ObjectBindingImpl(final FileObject fo) {
            super(fo);
            docRef = new DocumentUtils.DocumentRef();
        }

        public final Object createInstance() throws IOException {
            return Convertors.read(docRef.getDocument(getFile()).getDocumentElement());
        }

        public final Object getModuleDescriptor() {
            return new Object() {
                public boolean equals(final Object obj) {
                    if (obj instanceof ConvertorDescriptor) {
                        final ConvertorDescriptor cd = (ConvertorDescriptor) obj;
                        return cd.getElementName().equals(getElement()) && cd.getNamespace().equals(getNamespace());
                    }
                    return false;
                }
            };
        }

        public final boolean isEnabled() {
            return Convertors.canRead(getNamespace(), getElement());
        }

        private String getNamespace() {
            return docRef.getDocument(getFile()).getDocumentElement().getNamespaceURI();
        }

        private String getElement() {
            return docRef.getDocument(getFile()).getDocumentElement().getNodeName();
        }

    }

}
... 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.