EqNode | Line # 54 | 6 | 2 | 80% |
0.8
|
EqInterpreter | Line # 108 | 7 | 4 | 43.8% |
0.4375
|
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 EQ node of the IF-statement. Its functionality is | |
36 | * very much determined by OperationNode and EqInterpreter, which are | |
37 | * configured to deliver the following semantics: | |
38 | * | |
39 | * <p>This operation expects two and only two Terms of the same type, and | |
40 | * returns a | |
41 | * value of type Types.BOOLEAN_TYPE. | |
42 | * The evaluation result is "true", if and only if calling equals() method on | |
43 | * the first term returns boolean "true". | |
44 | * | |
45 | * <p>It is possible to register equality interpreters for specific | |
46 | * combinations of Terms, which will be used in preference to the default | |
47 | * EqInterpreter. Use registerInterpreter method to do that. | |
48 | * | |
49 | * @see #registerInterpreter | |
50 | * | |
51 | * @author A.Otenko | |
52 | */ | |
53 | ||
54 | public class EqNode extends OperationNode { | |
55 | public static final String EQ_NODE = "EQ"; | |
56 | ||
57 | /** | |
58 | * Call this method to register the EqNode with the XMLPolicyParser. It also | |
59 | * registers the default EqInterpreter. | |
60 | */ | |
61 | 21 | public static void register(){ |
62 | 21 | try{ |
63 | 21 | issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(EQ_NODE, EqNode.class); |
64 | }catch (NoSuchMethodException nsme){ | |
65 | 0 | nsme.printStackTrace(); |
66 | } | |
67 | ||
68 | 21 | registerInterpreter(new EqInterpreter()); |
69 | } | |
70 | ||
71 | /** | |
72 | * This method maintains a register of interpreters for specific combinations | |
73 | * of Terms. If no special interpreters are registered, the default one is | |
74 | * used, as described above. | |
75 | * | |
76 | * @param i - the Interpreter instance that can evaluate equality expression | |
77 | * for some combination of Terms | |
78 | */ | |
79 | 84 | public static void registerInterpreter(Interpreter i){ |
80 | 84 | OperationNode.registerInterpreterForNode(EQ_NODE, i); |
81 | } | |
82 | ||
83 | 0 | protected EqNode() { |
84 | } | |
85 | ||
86 | /** | |
87 | * This constructor builds an EqNode given the XMLPolicyParser and the set of | |
88 | * attributes of this XML element. It expects that there are two and only two | |
89 | * child nodes. | |
90 | * | |
91 | * @param p - the XMLPolicyParser that builds this EqNode | |
92 | * @param attr - the attributes of this XML element | |
93 | */ | |
94 | 15 | public EqNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attrs){ |
95 | 15 | super(EQ_NODE, attrs, 2, 2); |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | /** | |
101 | * This is a default equality interpreter. It can interpret any two terms of | |
102 | * the same type | |
103 | * and will return the equality match as the corresponding Java object equals() | |
104 | * method determines; that is, leftHandSideTerm.equals(rightHandSideTerm) | |
105 | * | |
106 | * @author A.Otenko | |
107 | */ | |
108 | class EqInterpreter implements Interpreter { | |
109 | 21 | public EqInterpreter(){} |
110 | ||
111 | /** | |
112 | * This method returns null only if the types of the terms are different or | |
113 | * there is more than | |
114 | * two terms. | |
115 | */ | |
116 | 15 | public String canEvaluate(Term [] t){ |
117 | 15 | if (t.length==2){ |
118 | 15 | if (t[0].getType().intern()==t[1].getType().intern()){ |
119 | 15 | return Types.BOOLEAN_TYPE; |
120 | } | |
121 | } | |
122 | ||
123 | 0 | return null; |
124 | } | |
125 | ||
126 | 0 | public Object evaluate(Environment env, Term [] t) throws EvaluationException{ |
127 | 0 | if (canEvaluate(t)==null){ |
128 | 0 | throw new EvaluationException("Cannot evaluate expression"); |
129 | } | |
130 | ||
131 | 0 | return new Boolean(t[0].evaluate(env).equals(t[1].evaluate(env))); |
132 | } | |
133 | } |
|