PresentNode | Line # 49 | 9 | 4 | 26.7% |
0.26666668
|
PresentInterpreter | Line # 109 | 8 | 6 | 5.3% |
0.05263158
|
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 Present node of the IF-statement. Its functionality is | |
36 | * very much determined by OperationNode and PresentInterpreter, which are | |
37 | * configured to deliver the following semantics: | |
38 | * | |
39 | * <p>This operation expects one and only one Term, which must either be | |
40 | * an ArgNode or EnvironmentNode and returns a | |
41 | * value of type Types.BOOLEAN_TYPE. | |
42 | * The evaluation result is "true", if and only if the Term refers to an | |
43 | * argument or environmental variable that is present in the current | |
44 | * Environment. | |
45 | * | |
46 | * @author A.Otenko | |
47 | */ | |
48 | ||
49 | public class PresentNode extends OperationNode { | |
50 | ||
51 | public static final String PRESENT_NODE = "Present"; | |
52 | ||
53 | /** | |
54 | * Call this method to register the PresentNode with the XMLPolicyParser. It | |
55 | * also | |
56 | * registers the default PresentInterpreter. | |
57 | */ | |
58 | 21 | public static void register(){ |
59 | 21 | try{ |
60 | 21 | issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(PRESENT_NODE, PresentNode.class); |
61 | }catch (NoSuchMethodException nsme){ | |
62 | 0 | nsme.printStackTrace(); |
63 | } | |
64 | ||
65 | 21 | TermNode.registerInterpreterForNode(PRESENT_NODE, new PresentInterpreter()); |
66 | } | |
67 | ||
68 | 0 | protected PresentNode() { |
69 | } | |
70 | ||
71 | /** | |
72 | * This constructor builds a PresentNode given the XMLPolicyParser and the | |
73 | * set of | |
74 | * attributes of this XML element. It expects that there is one and only one | |
75 | * child node. | |
76 | * | |
77 | * @param p - the XMLPolicyParser that builds this PresentNode | |
78 | * @param attrs - the attributes of this XML element | |
79 | */ | |
80 | 0 | public PresentNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attrs){ |
81 | 0 | super(PRESENT_NODE, attrs, 1, 1); |
82 | } | |
83 | ||
84 | /** | |
85 | * This method checks that there is only one child element in XML, and | |
86 | * that it is of type ArgNode or EnvironmentNode to ensure the semantic | |
87 | * correctness of XML policy. | |
88 | */ | |
89 | 0 | public void construct() throws issrg.pba.rbac.PolicyParsingException { |
90 | 0 | super.construct(); // this has ensured there is a node, the only one. |
91 | ||
92 | 0 | Object o = getChildren().get(0); // there should be one |
93 | 0 | if (!(o instanceof ArgNode) && !(o instanceof EnvironmentNode)){ |
94 | 0 | throw new issrg.pba.rbac.PolicyParsingException(PRESENT_NODE+" can have only "+ArgNode.ARG_NODE+" or "+EnvironmentNode.ENVIRONMENT_NODE+" as a child"); |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | /** | |
101 | * This is the default Interpreter for the Present node. It ensures that | |
102 | * it is asked to | |
103 | * interpret the right terms and returns the boolean result: true, if the Arg | |
104 | * or Environment | |
105 | * parameter is present in the environmental settings; false otherwise. | |
106 | * | |
107 | * @author A.Otenko | |
108 | */ | |
109 | class PresentInterpreter implements Interpreter{ | |
110 | 21 | public PresentInterpreter(){} |
111 | ||
112 | /** | |
113 | * This method checks that the array of Terms has one and only one element, | |
114 | * and that it is ArgNode or EnvironmentNode. | |
115 | * | |
116 | * @param t - the array of Terms | |
117 | * | |
118 | * @return Types.BOOLEAN_TYPE, if the array of Terms has only one element, | |
119 | * and that its class is either ArgNode or EnvironmentNode; otherwise null | |
120 | */ | |
121 | 0 | public String canEvaluate(Term [] t){ |
122 | 0 | if (t.length==1 && ((t[0] instanceof ArgNode) || (t[0] instanceof EnvironmentNode))){ |
123 | 0 | return Types.BOOLEAN_TYPE; |
124 | }else{ | |
125 | 0 | return null; |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
130 | * This method verifies that the expression can be evaluated and returns a | |
131 | * java.lang.Boolean with value set to | |
132 | * "true", if the specified Arg or Environment parameter is present in the | |
133 | * given Environment. | |
134 | * | |
135 | * @param env - the evaluation Environment with environmental parameters and | |
136 | * action arguments in it | |
137 | * @param t - the array of actual Terms | |
138 | * | |
139 | * @return java.lang.Boolean object with the value set to "true", if the | |
140 | * argument or environmental parameter named by the first Term in the array | |
141 | * is present in it; otherwise it is set to "false" | |
142 | */ | |
143 | 0 | public Object evaluate(Environment env, Term [] t) throws EvaluationException { |
144 | 0 | java.util.Map m = t[0] instanceof ArgNode ? env.getArgs(): env.getEnv(); |
145 | ||
146 | 0 | if (canEvaluate(t)==null || m==null){ |
147 | 0 | throw new EvaluationException("Cannot evaluate expression"); |
148 | } | |
149 | ||
150 | 0 | String s = t[0] instanceof ArgNode ? ((ArgNode)t[0]).parameter_name :((EnvironmentNode)t[0]).parameter_name; |
151 | ||
152 | 0 | return new Boolean(m.get(s)!=null); |
153 | } | |
154 | } |
|