TermNode | Line # 44 | 25 | 9 | 82.5% |
0.825
|
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 | import issrg.pba.rbac.PolicyParsingException; | |
35 | import issrg.pba.rbac.xmlpolicy.PolicyXMLNode; | |
36 | ||
37 | /** | |
38 | * This abstract class represents XML elements that are Terms of IF-expressions. | |
39 | * It maintains a registry of Interpreters for each type of operation, which is | |
40 | * used indirectly by OperationNode and other subclasses. | |
41 | * | |
42 | * @author A.Otenko | |
43 | */ | |
44 | public abstract class TermNode extends PolicyXMLNode implements Term { | |
45 | protected int maxOperand; | |
46 | protected int minOperand; | |
47 | protected Term [] terms; | |
48 | ||
49 | private final static java.util.Map interpreters = new java.util.Hashtable(); | |
50 | ||
51 | /** | |
52 | * This method registers an interpreter for a given type of operation. | |
53 | * | |
54 | * @param nodeName - the name of the XML element that represents the | |
55 | * operation | |
56 | * @param i - the Interpreter for that operation | |
57 | */ | |
58 | 441 | protected static void registerInterpreterForNode(String nodeName, Interpreter i){ |
59 | 441 | java.util.Vector v = (java.util.Vector)interpreters.get(nodeName); |
60 | 441 | if (v==null){ |
61 | 210 | v=new java.util.Vector(); |
62 | 210 | interpreters.put(nodeName, v); |
63 | } | |
64 | ||
65 | 441 | v.add(i); |
66 | } | |
67 | ||
68 | /** | |
69 | * This method retrieves an array of interpreters for a particular kind of | |
70 | * operation. | |
71 | * | |
72 | * @param nodeName - the name of the XML element that represents the | |
73 | * operation | |
74 | * | |
75 | * @return an array of Interpreters for this kind of operation; can be null | |
76 | * or empty if no Interpreters were registered for that kind of operation | |
77 | */ | |
78 | 144 | protected static Interpreter[] getInterpretersForNode(String nodeName){ |
79 | 144 | java.util.Vector v = (java.util.Vector)interpreters.get(nodeName); |
80 | 144 | Interpreter [] result = null; |
81 | 144 | if (v!=null){ |
82 | 144 | Object [] o = v.toArray(); |
83 | 144 | result = new Interpreter[o.length]; |
84 | 144 | System.arraycopy(o, 0, result, 0, result.length); |
85 | } | |
86 | ||
87 | 144 | return result; |
88 | } | |
89 | ||
90 | 0 | protected TermNode(){ |
91 | 0 | super(); |
92 | } | |
93 | ||
94 | /** | |
95 | * This constructor builds a TermNode object that will check that the child | |
96 | * nodes are | |
97 | * Terms and that there are not more than maxOperandCount of them and not | |
98 | * less than minOperandCount. | |
99 | * The negative values of these two parameters mean no restriction on the | |
100 | * amount of children. | |
101 | * | |
102 | * @param name is the name of the XML element for this operator | |
103 | * @param attr is the set of attributes it has got | |
104 | * @param maxOperandCount is the maximum amount of children allowed; -1 means | |
105 | * "don't care" | |
106 | * @param minOperandCouns is the minimum amount of children allowed; -1 means | |
107 | * "don't care" | |
108 | */ | |
109 | 322 | public TermNode(String name, org.xml.sax.Attributes attr, int maxOperandCount, int minOperandCount){ |
110 | 322 | super(name, attr); |
111 | ||
112 | 322 | maxOperand=maxOperandCount; |
113 | 322 | minOperand=minOperandCount; |
114 | } | |
115 | ||
116 | /** | |
117 | * This method ensures the children are Terms and that there are not more | |
118 | * than maxOperandCount | |
119 | * and not less than minOperandCount of them, as specified in the | |
120 | * constructor. It also creates | |
121 | * an array of Terms that can be used by the sub-classes in their | |
122 | * evaluations. | |
123 | * | |
124 | * @throws PolicyParsingException if one of the above-stated conditions is | |
125 | * violated | |
126 | */ | |
127 | 322 | public void construct() throws PolicyParsingException { |
128 | 322 | java.util.Vector c = getChildren(); |
129 | ||
130 | 322 | if ((minOperand>=0 && c.size()<minOperand) || (maxOperand>=0 && c.size()>maxOperand)){ |
131 | 0 | throw new PolicyParsingException("Illegal number of operands (child nodes) for "+getName()+" node"); |
132 | } | |
133 | ||
134 | 322 | terms = new Term[c.size()]; |
135 | ||
136 | 609 | for (int i=0; i<c.size(); i++){ |
137 | 287 | if (c.get(i) instanceof Term){ |
138 | 287 | terms[i]=(Term)c.get(i); |
139 | 287 | continue; |
140 | } | |
141 | ||
142 | 0 | throw new PolicyParsingException("Operand ("+i+") "+((PolicyXMLNode)c.get(i)).getName()+" is not a valid term for "+getName()+" node"); |
143 | } | |
144 | } | |
145 | } |
|