/*
* $Id: ImportPermission.java,v 1.3 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script;
import java.security.Permission;
/**
* class ImportPermission
*
* @author: Jani Lehtimki
*/
public final class ImportPermission extends Permission
{
private String _path;
private int _type = 0;
public ImportPermission(String path)
{
super(path);
setPath(path);
}
public ImportPermission(String path, String actions)
{
super(path);
setPath(path);
}
public int hashCode()
{
return _path.hashCode() ^ (_type+13);
}
public String getActions()
{
return "";
}
public boolean equals(Object o)
{
if (o instanceof ImportPermission) {
ImportPermission p = (ImportPermission)o;
return _path.equals(p._path) && (_type == p._type);
}
return false;
}
private void setPath(String path)
{
if (path.endsWith("/*")) {
_type = 1;
path = path.substring(0, path.length() - 1);
} else if (path.endsWith("/-")) {
_type = 2;
path = path.substring(0, path.length() - 1);
}
_path = path;
}
public boolean implies(Permission perm)
{
if (perm instanceof ImportPermission) {
String p = ((ImportPermission)perm)._path;
switch(_type) {
case 0:
{
return p.equals(_path);
}
case 1:
{
if (p.startsWith(_path)) {
int n = _path.length();
return (p.indexOf('/', n+1)==-1);
}
return false;
}
case 2:
{
return p.startsWith(_path);
}
}
}
return false;
}
public static boolean onSameDir(String source, String target)
{
int i = source.lastIndexOf('/');
source = source.substring(0, i+1);
if (target.startsWith(source)) {
if (target.indexOf('/', source.length()+1) == -1) {
return true;
}
}
return false;
}
}
|