package org.apache.commons.digester3;
import static java.lang.String.format;
import org.xml.sax.Attributes;
final class PatternRuleMatcher
implements RuleMatcher
{
private final String pattern;
private String namespaceURI;
public PatternRuleMatcher( String pattern )
{
this( pattern, null );
}
public PatternRuleMatcher( String pattern, String namespaceURI )
{
if ( pattern == null )
{
throw new IllegalArgumentException( "Input pattern must be not null" );
}
this.pattern = pattern;
this.namespaceURI = namespaceURI;
}
public boolean match( String namespace, String pattern, String name, Attributes attributes )
{
if ( namespaceURI != null && !namespace.equals( namespaceURI ) )
{
return false;
}
return this.pattern.equals( pattern );
}
public String getPattern()
{
return pattern;
}
public String getNamespaceURI()
{
return namespaceURI;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ( ( namespaceURI == null ) ? 0 : namespaceURI.hashCode() );
result = prime * result + pattern.hashCode();
return result;
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null )
{
return false;
}
if ( getClass() != obj.getClass() )
{
return false;
}
PatternRuleMatcher other = (PatternRuleMatcher) obj;
if ( namespaceURI == null )
{
if ( other.getNamespaceURI() != null )
{
return false;
}
}
else if ( !namespaceURI.equals( other.getNamespaceURI() ) )
{
return false;
}
if ( !pattern.equals( other.getPattern() ) )
{
return false;
}
return true;
}
@Override
public String toString()
{
return format( "%s (%s)", pattern, namespaceURI );
}
}