package javax.security.jacc;
class URLPattern
{
static final int DEFAULT = 0;
static final int THE_PATH_PREFIX = 1;
static final int PATH_PREFIX = 2;
static final int EXTENSION = 3;
static final int EXACT = 4;
private String pattern;
private String ext;
private int length;
private int type = -1;
URLPattern(String pattern)
{
this.pattern = pattern;
length = pattern.length();
if( pattern.equals("/") )
type = DEFAULT;
else if( pattern.startsWith("/*") )
type = THE_PATH_PREFIX;
else if( length > 0 && pattern.charAt(0) == '/' && pattern.endsWith("/*") )
type = PATH_PREFIX;
else if( pattern.startsWith("*.") )
{
type = EXTENSION;
ext = pattern.substring(1);
}
else
type = EXACT;
}
boolean matches(URLPattern url)
{
return matches(url.pattern);
}
boolean matches(String urlPattern)
{
if( type == DEFAULT || type == THE_PATH_PREFIX )
return true;
if( type == EXTENSION && urlPattern.endsWith(ext) )
return true;
if( type == PATH_PREFIX )
{
if( urlPattern.regionMatches(0,pattern, 0, length-2) )
{
int last = length - 2;
if( urlPattern.length() > last && urlPattern.charAt(last) != '/' )
return false;
return true;
}
return false;
}
if( pattern.equals(urlPattern) )
return true;
return false;
}
String getPattern()
{
return pattern;
}
boolean isDefault()
{
return type == DEFAULT;
}
boolean isExact()
{
return type == EXACT;
}
boolean isExtension()
{
return type == EXTENSION;
}
boolean isPrefix()
{
return type == THE_PATH_PREFIX || type == PATH_PREFIX;
}
public int hashCode()
{
return pattern.hashCode();
}
boolean equals(URLPattern p)
{
boolean equals = type == p.type;
if( equals )
{
equals = pattern.equals(p.pattern);
}
return equals;
}
}