ConstantNode | Line # 47 | 13 | 4 | 72.7% |
0.72727275
|
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 that represents a Constant node and evaluates the contant | |
36 | * specified | |
37 | * in the policy XML. | |
38 | * | |
39 | * <p>If the policy contains the constant of type for which there is no | |
40 | * constructor provided | |
41 | * by the AEF (not registered with the Types), an EvaluationException will | |
42 | * occur at construction-time. | |
43 | * | |
44 | * @author A.Otenko | |
45 | */ | |
46 | ||
47 | public class ConstantNode extends TermNode { | |
48 | /** | |
49 | * This is the name of the node that represents the Constant - a static value specified in | |
50 | * the policy. | |
51 | */ | |
52 | public final static String CONSTANT_NODE = "Constant"; | |
53 | ||
54 | /** | |
55 | * This method should be called to register the node with the XML Parser | |
56 | */ | |
57 | 21 | public static void register(){ |
58 | 21 | try{ |
59 | 21 | issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(CONSTANT_NODE, ConstantNode.class); |
60 | }catch (NoSuchMethodException nsme){ | |
61 | 0 | nsme.printStackTrace(); |
62 | } | |
63 | } | |
64 | ||
65 | protected String type; | |
66 | protected Object value; | |
67 | ||
68 | 0 | protected ConstantNode() {} |
69 | ||
70 | /** | |
71 | * This constructor builds an ConstantNode, given a XMLPolicyParser and the | |
72 | * set of attributes of this XML element. It expects that a | |
73 | * issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE and | |
74 | * issrg.pba.rbac.xmlpolicy.XMLTags.VALUE_ATTRIBUTE ("Type" and "Value") are | |
75 | * present in this XML element. The value is converted from string into a | |
76 | * Java object using the Types.construct() method, so the type of the value | |
77 | * must be registered there. | |
78 | * | |
79 | * @param p - the XMLPolicyParser that builds this ConstantNode | |
80 | * @param attr - the attributes of this XML element | |
81 | * | |
82 | * @throws EvaluationException, if the value could not be decoded using | |
83 | * available types | |
84 | * @see Types#construct | |
85 | */ | |
86 | 89 | public ConstantNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attr) throws EvaluationException, issrg.pba.rbac.PolicyParsingException{ |
87 | 89 | super(CONSTANT_NODE, attr, -1, 0); |
88 | ||
89 | 89 | type=((String)getAttributes().get(issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE)).intern(); |
90 | 0 | if (type==null) throw new issrg.pba.rbac.PolicyParsingException(issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE+" attribute was expected in "+CONSTANT_NODE); |
91 | 89 | value = getAttributes().get(issrg.pba.rbac.xmlpolicy.XMLTags.VALUE_ATTRIBUTE); |
92 | 0 | if (value==null) throw new issrg.pba.rbac.PolicyParsingException(issrg.pba.rbac.xmlpolicy.XMLTags.VALUE_ATTRIBUTE+" attribute was expected in "+CONSTANT_NODE); |
93 | 89 | value = Types.construct(type, (String)value); |
94 | } | |
95 | ||
96 | /** | |
97 | * This method returns the Type of the constant as determined at construction | |
98 | * time. | |
99 | */ | |
100 | 211 | public String getType(){ |
101 | 211 | return type; |
102 | } | |
103 | ||
104 | /** | |
105 | * This method returns the value of the constant evaluated at construction | |
106 | * time. It doesn't actually throw EvaluationExceptions, but the clause is | |
107 | * kept for subclasses, if there will be any. | |
108 | */ | |
109 | 122 | public Object evaluate(Environment env) throws EvaluationException{ |
110 | 122 | return value; |
111 | } | |
112 | } |
|