OperationNode | Line # 51 | 15 | 7 | 67.9% |
0.6785714
|
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 an abstract class which is a base to other operations. The | |
36 | * sub-classes would only | |
37 | * need to have a valid constructor, so the XMLParser will be able to | |
38 | * instantiate a working | |
39 | * copy of it, and perhaps a static registration method to update the collection | |
40 | * of interpreters. | |
41 | * | |
42 | * <p>This class maintains a registry of Interpreters for each type of | |
43 | * operation. During evaluation time an Interpreter will be found in the | |
44 | * registry for the | |
45 | * required type of operation and it will be used to perform the evaluation. | |
46 | * If no Interpreter can be found for the specific set of Terms, | |
47 | * EvaluationException occurs. | |
48 | * | |
49 | * @see Interpreter | |
50 | */ | |
51 | public class OperationNode extends TermNode { | |
52 | protected String type; | |
53 | protected Interpreter interpreter; | |
54 | ||
55 | 0 | protected OperationNode() {} |
56 | ||
57 | /** | |
58 | * This constructor builds an OperationNode given the name and the attributes | |
59 | * of the XML element, and the acceptable numbers of operands. | |
60 | * | |
61 | * @param nodeName - the name of the XML element this OperationNode represents | |
62 | * @param attrs - the attributes of this XML element | |
63 | * @param maxOperandCount - the maximum acceptable number of operands; if | |
64 | * "-1", any number of operands is acceptable | |
65 | * @param minOperandCount - the minimum acceptable number of operands | |
66 | */ | |
67 | 144 | public OperationNode(String nodeName, org.xml.sax.Attributes attrs, int maxOperandCount, int minOperandCount){ |
68 | 144 | super(nodeName, attrs, maxOperandCount, minOperandCount); |
69 | } | |
70 | ||
71 | /** | |
72 | * The method builds a usual TermNode and then checks if an interpreter can | |
73 | * be found that | |
74 | * would agree to interpret the relevant terms (children nodes) and deliver a | |
75 | * definite result. | |
76 | * If no such interpreter is found, a PolicyParsingException is thrown; | |
77 | * otherwise, the | |
78 | * interpreter will be used by the evaluate method, and the result type | |
79 | * reported by the canEvaluate | |
80 | * method of the interpreter will be returned by the getType method. | |
81 | */ | |
82 | 144 | public void construct() throws issrg.pba.rbac.PolicyParsingException { |
83 | 144 | super.construct(); |
84 | ||
85 | 144 | Interpreter[] ints = TermNode.getInterpretersForNode(getName()); |
86 | 144 | if (ints!=null){ |
87 | 272 | for (int i=0; i<ints.length; i++){ |
88 | 0 | if ((type=ints[i].canEvaluate(terms))!=null){ // can evaluate; terms has been set by the super.construct(); this automatically sets the type of the result, too |
89 | 144 | interpreter=ints[i]; |
90 | 144 | break; |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | 144 | if (interpreter==null){ |
96 | 0 | throw new issrg.pba.rbac.PolicyParsingException("No interpreter for such combination of operands has been found for node "+getName()); |
97 | } | |
98 | } | |
99 | ||
100 | 323 | public String getType(){ |
101 | 323 | return type; // evaluated during the call to construct() |
102 | } | |
103 | ||
104 | /** | |
105 | * This method evaluates the expression given the environment. It uses the | |
106 | * Interpreter found during the call to construct(); | |
107 | * | |
108 | * @param env - the Environment with the variables in it | |
109 | * | |
110 | * @return the Interpreter-specific object, guaranteed to be of type returned | |
111 | * by getType() | |
112 | * | |
113 | * @throws EvaluationException, if there was a problem evaluating the | |
114 | * expression | |
115 | */ | |
116 | 229 | public Object evaluate(Environment env) throws EvaluationException{ |
117 | 229 | try{ |
118 | 229 | return interpreter.evaluate(env, terms); |
119 | }catch (EvaluationException ee){ | |
120 | 0 | throw ee; |
121 | }catch (Throwable th){ | |
122 | 0 | throw new EvaluationException("Run-time error", th); |
123 | } | |
124 | } | |
125 | } |
|