// Copyright 2001 werken digital. All rights reserved.

package org.jaxen.expr;

import org.jaxen.ContextSupport;
import org.jaxen.Navigator;

import org.jaxen.expr.iter.IterableAxis;


class DefaultNameStep extends DefaultStep
{
    private String prefix;
    private String localName;
    private boolean matchesAnyName;
    private boolean matchesAnyNamespace;

    public DefaultNameStep(IterableAxis axis,
                           String prefix,
                           String localName)
    {
        super( axis );

        this.prefix    = prefix;
        this.localName = localName;
        this.matchesAnyName = "*".equals( localName );
        this.matchesAnyNamespace = matchesAnyName || prefix.equals( "*" );        
    }

    public String getPrefix()
    {
        return this.prefix;
    }

    public String getLocalName()
    {
        return this.localName;
    }

    public String getText()
    {
        if ( ( getPrefix() != null )
             &&
             ( ! getPrefix().equals("") ) )
        {
            return getAxisName() + "::" + getPrefix() + ":" + getLocalName() + super.getText();
        }

        return getAxisName() 
            + "::" + getLocalName()
            + super.getText();
    }

    public String toString()
    {
        return "[(DefaultNameStep): " + getPrefix() + ":" + getLocalName() + "[" + super.toString() + "]]";
    }

    public boolean matches(Object node,
                           ContextSupport contextSupport)
    {
        Navigator nav  = contextSupport.getNavigator();

        String nodeUri  = null;
        String nodeName = null;

        if (  nav.isElement( node ) )
        {
            nodeUri  = nav.getElementNamespaceUri( node );
            nodeName = nav.getElementName( node );

        }
        else if ( nav.isAttribute( node ) )
        {
            nodeUri  = nav.getAttributeNamespaceUri( node );
            nodeName = nav.getAttributeName( node );
        }
        else
        {
            return false;
        }

        if ( matchesAnyNamespace )
        {
            return matchesAnyName || getLocalName().equals( nodeName );
        }
        else
        {
            //String myUri = contextSupport.translateNamespacePrefixToUri( getPrefix() );
            String myUri = nav.translateNamespacePrefixToUri( getPrefix(), node );

            if ( matchesNamespaceURIs( myUri, nodeUri ) )
            {
                return matchesAnyName || getLocalName().equals( nodeName );
            }
        }

        return false;

        /*
        return ( testUri.equals( nodeUri )
                 &&
                 getLocalName().equals( nodeName ) );
        */
    }
    
    /** @return true if the two namespace URIs are equal
     * Note that we may wish to consider null being equal to ""
     */
    protected boolean matchesNamespaceURIs( String u1, String u2 ) {
        if ( u1 == u2 ) {
            return true;
        }
        if ( u1 != null && u2 != null ) {
            return u1.equals( u2 );
        }
        return false;
    }
    
}

@@ -2180,18 +2180,16 @@
         
-//
 String m
@@ -2193,28 +2193,72 @@
 g my
-Uri = contextSupport
+Prefix = getPrefix();            %0A            String myUri = nav
 .tra
@@ -2285,27 +2285,30 @@
 xToUri( 
-get
+my
 Prefix
-()
+, node
  );%0A    
@@ -2315,34 +2315,90 @@
         
-String myUri = nav
+if ( myUri == null ) %0A            %7B%0A                myUri = contextSupport
 .transla
@@ -2425,28 +2425,33 @@
 ri( 
-get
+my
 Prefix
-(), node );
+ );%0A            %7D
 %0A%0A  

Allow context support to provide default namespace mappings