AndNode | Line # 47 | 5 | 2 | 75% |
0.75
|
AndInterpreter | Line # 87 | 14 | 8 | 79.3% |
0.79310346
|
No Tests | |||
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | package issrg.pba.rbac.xmlpolicy.ifstatement; | |
33 | ||
34 | /** | |
35 | * This is the class for And node of the IF-statement. Its functionality is | |
36 | * very much determined by OperationNode and AndInterpreter, which are | |
37 | * configured to deliver the following semantics: | |
38 | * | |
39 | * <p>This operation expects one or more Terms, each returning a | |
40 | * Types.BOOLEAN_TYPE type, and returns a value of type Types.BOOLEAN_TYPE. | |
41 | * The evaluation result is "true", if and only if all Terms of the AndNode | |
42 | * evaluate to boolean "true". | |
43 | * | |
44 | * @author A.Otenko | |
45 | */ | |
46 | ||
47 | public class AndNode extends OperationNode { | |
48 | ||
49 | public static final String AND_NODE = "AND"; | |
50 | ||
51 | /** | |
52 | * Call this method to register the node with the XMLPolicyParser. This | |
53 | * method also registers the default AndInterpreter. | |
54 | */ | |
55 | 21 | public static void register(){ |
56 | 21 | try{ |
57 | 21 | issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(AND_NODE, AndNode.class); |
58 | }catch (NoSuchMethodException nsme){ | |
59 | 0 | nsme.printStackTrace(); |
60 | } | |
61 | ||
62 | 21 | issrg.pba.rbac.xmlpolicy.ifstatement.OperationNode.registerInterpreterForNode(AND_NODE, new AndInterpreter()); |
63 | } | |
64 | ||
65 | 0 | protected AndNode() { |
66 | } | |
67 | ||
68 | /** | |
69 | * This constructor builds an AndNode, given a XMLPolicyParser and the | |
70 | * set of attributes of this XML element. | |
71 | * | |
72 | * @param p - the XMLPolicyParser that builds this AndNode | |
73 | * @param attrs - the attributes of this XML element | |
74 | */ | |
75 | 35 | public AndNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attrs){ |
76 | 35 | super(AND_NODE, attrs, -1, 1); |
77 | } | |
78 | } | |
79 | ||
80 | ||
81 | /** | |
82 | * This is the default AndInterpreter that can evaluate the expression | |
83 | * consisting of any number of Terms of type Types.BOOLEAN_TYPE. It returns | |
84 | * a boolean "true" if and only if all of the Terms evaluate | |
85 | * to boolean "true". | |
86 | */ | |
87 | class AndInterpreter implements Interpreter { | |
88 | 21 | public AndInterpreter(){} |
89 | ||
90 | /** | |
91 | * This method returns Types.BOOLEAN_TYPE if the array length is non-zero, and | |
92 | * each Term is of type Types.BOOLEAN_TYPE. | |
93 | * | |
94 | * @param t - the array of Terms; must have non-zero length | |
95 | * | |
96 | * @return Types.BOOLEAN_TYPE if the array length is non-zero and all Terms | |
97 | * in it return Types.BOOLEAN_TYPE on call to getType() | |
98 | */ | |
99 | 72 | public String canEvaluate(Term [] t){ |
100 | 72 | if (t==null || t.length==0){ |
101 | 0 | return null; |
102 | } | |
103 | ||
104 | 219 | for (int i=0; i<t.length; i++){ |
105 | 147 | if (t[i].getType().intern()!=Types.BOOLEAN_TYPE){ // all of the terms must be a boolean |
106 | 0 | return null; |
107 | } | |
108 | } | |
109 | ||
110 | 72 | return Types.BOOLEAN_TYPE; |
111 | } | |
112 | ||
113 | /** | |
114 | * This method evaluates the given expression, so that a boolean "true" is | |
115 | * returned if all of the Terms evaluate to boolean "true". Note | |
116 | * that not all of the Terms may be evaluated as the result (once a Term that | |
117 | * evaluates to "false" is found, evaluation of any further Terms is | |
118 | * pointless). | |
119 | * | |
120 | * @param env - the Environment of evaluation | |
121 | * @param t - the array of Terms | |
122 | * | |
123 | * @return java.lang.Boolean object with the result of evaluation; it is | |
124 | * set to "false" if there is at least one Term that evaluates to "false"; | |
125 | * otherwise it is set to "true" | |
126 | */ | |
127 | 37 | public Object evaluate(Environment env, Term [] t) throws EvaluationException { |
128 | 37 | if (canEvaluate(t)==null){ |
129 | 0 | throw new EvaluationException("Cannot evaluate expression"); |
130 | } | |
131 | ||
132 | 37 | Object result=null; |
133 | 65 | for (int i=0; i<t.length; i++){ // the loop will always be executed at least once. |
134 | 52 | result = t[i].evaluate(env); |
135 | ||
136 | // it should be Boolean | |
137 | 52 | if (!((Boolean)result).booleanValue()){ |
138 | 24 | break; // no point to evaluate further |
139 | } | |
140 | } | |
141 | ||
142 | 37 | return result; |
143 | } | |
144 | } |
|