📄 presentnode.java
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the University of Salford nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package issrg.pba.rbac.xmlpolicy.ifstatement;
/**
* This is the class for Present node of the IF-statement. Its functionality is
* very much determined by OperationNode and PresentInterpreter, which are
* configured to deliver the following semantics:
*
* <p>This operation expects one and only one Term, which must either be
* an ArgNode or EnvironmentNode and returns a
* value of type Types.BOOLEAN_TYPE.
* The evaluation result is "true", if and only if the Term refers to an
* argument or environmental variable that is present in the current
* Environment.
*
* @author A.Otenko
*/
public class PresentNode extends OperationNode {
public static final String PRESENT_NODE = "Present";
/**
* Call this method to register the PresentNode with the XMLPolicyParser. It
* also
* registers the default PresentInterpreter.
*/
public static void register(){
try{
issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(PRESENT_NODE, PresentNode.class);
}catch (NoSuchMethodException nsme){
nsme.printStackTrace();
}
TermNode.registerInterpreterForNode(PRESENT_NODE, new PresentInterpreter());
}
protected PresentNode() {
}
/**
* This constructor builds a PresentNode given the XMLPolicyParser and the
* set of
* attributes of this XML element. It expects that there is one and only one
* child node.
*
* @param p - the XMLPolicyParser that builds this PresentNode
* @param attrs - the attributes of this XML element
*/
public PresentNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attrs){
super(PRESENT_NODE, attrs, 1, 1);
}
/**
* This method checks that there is only one child element in XML, and
* that it is of type ArgNode or EnvironmentNode to ensure the semantic
* correctness of XML policy.
*/
public void construct() throws issrg.pba.rbac.PolicyParsingException {
super.construct(); // this has ensured there is a node, the only one.
Object o = getChildren().get(0); // there should be one
if (!(o instanceof ArgNode) && !(o instanceof EnvironmentNode)){
throw new issrg.pba.rbac.PolicyParsingException(PRESENT_NODE+" can have only "+ArgNode.ARG_NODE+" or "+EnvironmentNode.ENVIRONMENT_NODE+" as a child");
}
}
}
/**
* This is the default Interpreter for the Present node. It ensures that
* it is asked to
* interpret the right terms and returns the boolean result: true, if the Arg
* or Environment
* parameter is present in the environmental settings; false otherwise.
*
* @author A.Otenko
*/
class PresentInterpreter implements Interpreter{
public PresentInterpreter(){}
/**
* This method checks that the array of Terms has one and only one element,
* and that it is ArgNode or EnvironmentNode.
*
* @param t - the array of Terms
*
* @return Types.BOOLEAN_TYPE, if the array of Terms has only one element,
* and that its class is either ArgNode or EnvironmentNode; otherwise null
*/
public String canEvaluate(Term [] t){
if (t.length==1 && ((t[0] instanceof ArgNode) || (t[0] instanceof EnvironmentNode))){
return Types.BOOLEAN_TYPE;
}else{
return null;
}
}
/**
* This method verifies that the expression can be evaluated and returns a
* java.lang.Boolean with value set to
* "true", if the specified Arg or Environment parameter is present in the
* given Environment.
*
* @param env - the evaluation Environment with environmental parameters and
* action arguments in it
* @param t - the array of actual Terms
*
* @return java.lang.Boolean object with the value set to "true", if the
* argument or environmental parameter named by the first Term in the array
* is present in it; otherwise it is set to "false"
*/
public Object evaluate(Environment env, Term [] t) throws EvaluationException {
java.util.Map m = t[0] instanceof ArgNode ? env.getArgs(): env.getEnv();
if (canEvaluate(t)==null || m==null){
throw new EvaluationException("Cannot evaluate expression");
}
String s = t[0] instanceof ArgNode ? ((ArgNode)t[0]).parameter_name :((EnvironmentNode)t[0]).parameter_name;
return new Boolean(m.get(s)!=null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -