OrNode | Line # 48 | 5 | 2 | 75% |
0.75
|
OrInterpreter | 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 Or node of the IF-statement. Its functionality is | |
36 | * very much determined by OperationNode and OrInterpreter, 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 there is at least one Term | |
42 | * of the OrNode that | |
43 | * evaluates to boolean "true". | |
44 | * | |
45 | * @author A.Otenko | |
46 | */ | |
47 | ||
48 | public class OrNode extends OperationNode { | |
49 | ||
50 | public static final String OR_NODE = "OR"; | |
51 | ||
52 | /** | |
53 | * Call this method to register the node with the XMLPolicyParser. This | |
54 | * method also registers the default OrInterpreter. | |
55 | */ | |
56 | 21 | public static void register(){ |
57 | 21 | try{ |
58 | 21 | issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(OR_NODE, OrNode.class); |
59 | }catch (NoSuchMethodException nsme){ | |
60 | 0 | nsme.printStackTrace(); |
61 | } | |
62 | ||
63 | 21 | issrg.pba.rbac.xmlpolicy.ifstatement.OperationNode.registerInterpreterForNode(OR_NODE, new OrInterpreter()); |
64 | } | |
65 | ||
66 | 0 | protected OrNode() { |
67 | } | |
68 | ||
69 | /** | |
70 | * This constructor builds an OrNode, given a XMLPolicyParser and the | |
71 | * set of attributes of this XML element. | |
72 | * | |
73 | * @param p - the XMLPolicyParser that builds this OrNode | |
74 | * @param attrs - the attributes of this XML element | |
75 | */ | |
76 | 12 | public OrNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attrs){ |
77 | 12 | super(OR_NODE, attrs, -1, 1); |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * This is the default OrInterpreter 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 there is at least one Term that evaluates | |
85 | * to boolean "true". | |
86 | */ | |
87 | class OrInterpreter implements Interpreter { | |
88 | 21 | public OrInterpreter(){} |
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 | 47 | public String canEvaluate(Term [] t){ |
100 | 47 | if (t==null || t.length==0){ |
101 | 0 | return null; |
102 | } | |
103 | ||
104 | 145 | for (int i=0; i<t.length; i++){ |
105 | 98 | if (t[i].getType().intern()!=Types.BOOLEAN_TYPE){ // all of the terms must be a boolean |
106 | 0 | return null; |
107 | } | |
108 | } | |
109 | ||
110 | 47 | return Types.BOOLEAN_TYPE; |
111 | } | |
112 | ||
113 | /** | |
114 | * This method evaluates the given expression, so that a boolean "true" is | |
115 | * returned if at least one of the Terms evaluates to boolean "true". Note | |
116 | * that not all of the Terms may be evaluated as the result (once a Term that | |
117 | * evaluates to "true" 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 "true" if there is at least one Term that evaluates to "true"; | |
125 | * otherwise it is set to "false" | |
126 | */ | |
127 | 35 | public Object evaluate(Environment env, Term [] t) throws EvaluationException { |
128 | 35 | if (canEvaluate(t)==null){ |
129 | 0 | throw new EvaluationException("Cannot evaluate expression"); |
130 | } | |
131 | ||
132 | 35 | Object result=null; |
133 | 85 | for (int i=0; i<t.length; i++){ // the loop will always be executed at least once. |
134 | 70 | result = t[i].evaluate(env); |
135 | ||
136 | // it should be Boolean | |
137 | 70 | if (((Boolean)result).booleanValue()){ |
138 | 20 | break; // no point to evaluate further |
139 | } | |
140 | } | |
141 | ||
142 | 35 | return result; |
143 | } | |
144 | } |
|