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.modules.xml.tree.completion;

import org.netbeans.modules.xml.api.model.HintContext;
import org.w3c.dom.*;
import org.netbeans.tax.dom.Wrapper;
import org.netbeans.tax.TreeObject;

/**
 *
 * @author  Rich Unger 
 */
public abstract class TreeHintContext implements HintContext
{
    public static TreeHintContext getContext(
            TreeObject treeObject, String sPrefix)
    { 
        Node wrapperNode = Wrapper.wrap(treeObject);

        if( wrapperNode instanceof Element )
        { 
            return new ElementTreeHintContext( wrapperNode, sPrefix );
        } 
        else if( wrapperNode instanceof Attr )
        { 
            Attr attr = (Attr)wrapperNode;
            return new AttrTreeHintContext( wrapperNode, attr.getOwnerElement(), sPrefix );
        } 
        else
        { 
            throw new RuntimeException("TreeHintContext:: expected node of type Attr or Element, but got: " // NOI18N
                    + wrapperNode.getClass().getName());
        } 
    } 

    public static TreeHintContext getContext(
            Node peerNode, Node parentNode, String sPrefix)
    { 
        if( peerNode instanceof Element )
        { 
            return new ElementTreeHintContext( peerNode, sPrefix );
        } 
        else if( peerNode instanceof Attr )
        { 
            return new AttrTreeHintContext( peerNode, parentNode, sPrefix );
        } 
        else
        { 
            throw new RuntimeException("TreeHintContext:: expected node of type Attr or Element, but got: " // NOI18N
                    + peerNode.getClass().getName());
        } 
    } 

    protected TreeHintContext( Node peer, String sPrefix )
    { 
        m_peer = peer;
        m_sPrefix = sPrefix;
    } 

    public String getCurrentPrefix()
    { 
        return m_sPrefix;
    } 

    /////////////////// Node interface

    public String getNodeName()
    { 
        return m_peer.getNodeName();
    } 

    public String getNodeValue() throws DOMException
    { 
        return m_peer.getNodeValue();
    } 

    public void setNodeValue( String nodeValue ) throws DOMException
    { 
        m_peer.setNodeValue(nodeValue);
    } 

    public short getNodeType()
    { 
        return m_peer.getNodeType();
    } 

    public Node getParentNode()
    { 
        if( m_bVirtual )
        { 
            return m_parent;
        } 
        else
        { 
            return m_peer.getParentNode();
        } 
    } 

    public NodeList getChildNodes()
    { 
        return m_peer.getChildNodes();
    } 

    public Node getFirstChild()
    { 
        return m_peer.getFirstChild();
    } 

    public Node getLastChild()
    { 
        return m_peer.getLastChild();
    } 

    public Node getPreviousSibling()
    { 
        if( m_bVirtual )
        { 
            return m_previous;
        } 
        else
        { 
            return m_peer.getPreviousSibling();
        } 
    } 

    public Node getNextSibling()
    { 
        if( m_bVirtual )
        { 
            return m_next;
        } 
        else
        { 
            return m_peer.getNextSibling();
        }
    } 

    public NamedNodeMap getAttributes()
    { 
        return m_peer.getAttributes();
    } 

    public Document getOwnerDocument()
    { 
        return m_peer.getOwnerDocument();
    } 

    public Node insertBefore(Node newChild, Node refChild)
        throws DOMException
    { 
        return m_peer.insertBefore(newChild, refChild);
    } 

    public Node replaceChild(Node newChild, Node oldChild)
        throws DOMException
    { 
        return m_peer.replaceChild(newChild, oldChild);
    } 

    public Node removeChild(Node oldChild)
        throws DOMException
    { 
        return m_peer.removeChild(oldChild);
    } 

    public Node appendChild(Node newChild)
        throws DOMException
    { 
        return m_peer.appendChild(newChild);
    } 

    public boolean hasChildNodes()
    { 
        return m_peer.hasChildNodes();
    } 

    public Node cloneNode(boolean deep)
    { 
        return m_peer.cloneNode(deep);
    } 

    public void normalize()
    { 
        m_peer.normalize();
    } 

    public boolean isSupported(String feature, String version)
    { 
        return m_peer.isSupported(feature, version);
    } 

    public String getNamespaceURI()
    { 
        return m_peer.getNamespaceURI();
    } 

    public String getPrefix()
    { 
        return m_peer.getPrefix();
    } 

    public void setPrefix(String prefix)
        throws DOMException
    { 
        m_peer.setPrefix(prefix);
    } 

    public String getLocalName()
    { 
        return m_peer.getLocalName();
    } 

    public boolean hasAttributes()
    { 
        return m_peer.hasAttributes();
    } 

    /////////////////// Attr interface

    static class AttrTreeHintContext extends TreeHintContext implements Attr
    { 
        protected AttrTreeHintContext( Node peer, Node parent, String sPrefix )
        { 
            super(peer, sPrefix);
            m_parent = parent;
        } 

        public String getName()
        { 
            return ((Attr)m_peer).getName();
        } 

        public boolean getSpecified()
        { 
            return ((Attr)m_peer).getSpecified();
        } 

        public String getValue()
        { 
            return ((Attr)m_peer).getValue();
        } 

        public void setValue(String str)
            throws DOMException
        { 
            ((Attr)m_peer).setValue(str);
        } 

        public Element getOwnerElement()
        { 
            return (Element)m_parent;
        } 
    }

    /////////////////// Element interface

    static class ElementTreeHintContext extends TreeHintContext implements Element
    { 
        protected ElementTreeHintContext( Node peer, String sPrefix )
        { 
            super(peer, sPrefix);

            // make the peer a child of the context, to shoe-horn
            // into the GrammarQuery notion of replacing the current node.
            this.m_parent = peer;
            this.m_previous = getPreviousSibling();
            this.m_next = getNextSibling();
            this.m_bVirtual = true;
        } 

        public String getTagName()
        { 
            return ((Element)m_peer).getTagName();
        } 

        public String getAttribute(String name)
        { 
            return ((Element)m_peer).getAttribute(name);
        } 

        public void setAttribute(String name, String value)
            throws DOMException
        { 
            ((Element)m_peer).setAttribute(name, value);
        } 

        public void removeAttribute(String name)
            throws DOMException
        { 
            ((Element)m_peer).removeAttribute(name);
        } 

        public Attr getAttributeNode(String name)
        { 
            return ((Element)m_peer).getAttributeNode(name);
        } 

        public Attr setAttributeNode(Attr newAttr)
            throws DOMException
        { 
            return ((Element)m_peer).setAttributeNode(newAttr);
        } 

        public Attr removeAttributeNode(Attr oldAttr)
            throws DOMException
        { 
            return ((Element)m_peer).removeAttributeNode(oldAttr);
        } 

        public NodeList getElementsByTagName(String name)
        { 
            return ((Element)m_peer).getElementsByTagName(name);
        } 

        public String getAttributeNS(String namespaceURI, String localName)
        { 
            return ((Element)m_peer).getAttributeNS(namespaceURI, localName);
        } 

        public void setAttributeNS(String namespaceURI, String qualifiedName, String value)
            throws DOMException
        { 
            ((Element)m_peer).setAttributeNS(namespaceURI, qualifiedName, value);
        } 

        public void removeAttributeNS(String namespaceURI, String localName)
            throws DOMException
        { 
            ((Element)m_peer).removeAttributeNS(namespaceURI, localName);
        } 

        public Attr getAttributeNodeNS(String namespaceURI, String localName)
        { 
            return ((Element)m_peer).getAttributeNodeNS(namespaceURI, localName);
        } 

        public Attr setAttributeNodeNS(Attr newAttr)
            throws DOMException
        { 
            return ((Element)m_peer).setAttributeNodeNS(newAttr);
        } 

        public NodeList getElementsByTagNameNS(String namespaceURI, String localName)
        { 
            return ((Element)m_peer).getElementsByTagNameNS(namespaceURI, localName);
        } 

        public boolean hasAttribute(String name)
        { 
            return ((Element)m_peer).hasAttribute(name);
        } 

        public boolean hasAttributeNS(String namespaceURI, String localName)
        { 
            return ((Element)m_peer).hasAttributeNS(namespaceURI, localName);
        } 

    }


    protected Node m_parent, m_previous, m_next;
    protected boolean m_bVirtual = false;

    protected String m_sPrefix;
    protected Node m_peer;
}
... 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.