⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeinterpreter.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 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;

import issrg.pba.rbac.xmlpolicy.ifstatement.*;

/**
 * This class interprets comparisons of Time values in IF-statements.
 */
public class TimeInterpreter implements Interpreter {
	private int mode;

	private final static int EQ_MODE=0;
	private final static int GE_MODE=1;
	private final static int LE_MODE=2;
	private final static int GT_MODE=3;
	private final static int LT_MODE=4;

	/**
	 * This method registers as an interpreter for specific comparison
	 * operators.
	 */
	public static void register(){
		EqNode.registerInterpreter(new TimeInterpreter(EQ_MODE));
		GeNode.registerInterpreter(new TimeInterpreter(GE_MODE));
		LeNode.registerInterpreter(new TimeInterpreter(LE_MODE));
		GtNode.registerInterpreter(new TimeInterpreter(GT_MODE));
		LtNode.registerInterpreter(new TimeInterpreter(LT_MODE));
	}

	protected TimeInterpreter(int mode){
		this.mode=mode;
	}

	/**
	 * This method tells if the Terms can be evaluated by this interpreter.
	 *
	 * @param t - the array of Terms to evaluate
	 *
	 * @return Types.BOOLEAN_TYPE, if the array of Terms contains only two 
	 *   elements, and both are of type Time.TIME_TYPE; otherwise returns 
	 *   null, i.e. can't evaluate
	 */
	public String canEvaluate(Term [] t){
		if (t!=null && t.length==2 &&
			t[0].getType().intern()==Time.TIME_TYPE &&
			t[1].getType().intern()==t[0].getType().intern()
			){
			return Types.BOOLEAN_TYPE;
		}

		return null;
	}

	/**
	 * This method evaluates the expression specified by an array of Terms,
	 * given the Environment.
	 *
	 * @param env - the Environment of evaluation
	 * @param t - the array of Terms, each containing the actual value;
	 *   it must contain only 2 elements, both of type Time.TIME_TYPE
	 *
	 * @return Boolean, the value of which tells whether the result is
	 *   true or false
	 *
	 * @throws EvaluationException, if the expression cannot be evaluated
	 */
	public Object evaluate(Environment env, Term [] t) throws EvaluationException{
		if (canEvaluate(t)==null){
			throw new EvaluationException("Cannot evaluate expression: valid time expression expected");
		}

		//System.out.println("\t\tevaluating time now");

		//System.out.print("Left operand: "+t[0]);
		//System.out.println("\t"+t[0].evaluate(env));
		//System.out.print("Right operand: "+t[1]);
		//System.out.println("\t"+t[1].evaluate(env));

		int [] r0=((Time)(t[0].evaluate(env))).getEvaluationTime();
		int [] r1=((Time)(t[1].evaluate(env))).getEvaluationTime();

		boolean b=true;
		boolean complete=false; // comparison is complete for GE and LE, if the operand in the sequence is strictly GT or LT

		for (int i=0; i<r0.length; i++){
			// System.out.println(r0[i]+" cmp "+r1[i]);

			if (r0[i]!=-1 && r1[i]!=-1){
				switch(mode){ // the mode is specified at construction time - see the registration procedure
				case EQ_MODE:
					b=r0[i]==r1[i];
					break;
				case GE_MODE:
					complete=r0[i]>r1[i];
					b=r0[i]>=r1[i];
					break;
				case LE_MODE:
					complete=r0[i]<r1[i];
					b=r0[i]<=r1[i];
					break;
				case GT_MODE:
					b=r0[i]>r1[i];
					complete=b;
					break;
				case LT_MODE:
					b=r0[i]<r1[i];
					complete=b;
					break;
				}

				if (complete || !b){
					break;
				}
			}
		}

		//System.out.println(b);

		return new Boolean(b);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -