Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
36   154   11   7.2
6   55   0.42   5
5     3  
1    
 
 
  StringInterpreter       Line # 49 36 11 29.8% 0.29787233
 
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 interpreter for comparisons of strings in the IF-statements. It
36    * provides the following semantics:
37    * <ul>
38    * <li>EQ is a case-sensitive test for equality of strings two,</li>
39    * <li>GE, GT, LE, LT - case-sensitive test of vocabulary order of two strings
40    * ("greater" meaning "later in vocabulary order", "less" meaning "earlier in
41    * vocabulary order")</li>
42    * <li>Substrings - checking if the first string is met anywhere in the second
43    * string (case sensitive).</li>
44    * </ul>
45    *
46    * @author A.Otenko
47    */
48   
 
49    public class StringInterpreter implements Interpreter {
50   
51    protected int mode;
52    public final static int EQ_MODE=0;
53    public final static int GE_MODE=1;
54    public final static int LE_MODE=2;
55    public final static int GT_MODE=3;
56    public final static int LT_MODE=4;
57    public final static int SUBSTRINGS_MODE=5;
58   
59    /**
60    * Call this method to register the IntegerInterpreter with the relevant
61    * expression nodes: EqNode, GeNode, GtNode, LeNode, LtNode, SubstringsNode.
62    * The interpreter
63    * provides the conventional semantics of vocabulary order of strings
64    * for those operations, as explained above.
65    */
 
66  21 toggle public static void register(){
67  21 EqNode.registerInterpreter(new StringInterpreter(EQ_MODE));
68  21 GeNode.registerInterpreter(new StringInterpreter(GE_MODE));
69  21 LeNode.registerInterpreter(new StringInterpreter(LE_MODE));
70  21 GtNode.registerInterpreter(new StringInterpreter(GT_MODE));
71  21 LtNode.registerInterpreter(new StringInterpreter(LT_MODE));
72    }
73   
 
74  0 toggle protected StringInterpreter() {
75    }
76   
77    /**
78    * This is the constructor used to build the interpreter for different types
79    * of comparison operation: an interpreter for
80    * Substrings, GE, LE, GT, LT and EQ correspondingly. The evaluate method
81    * will return the
82    * comparison result corresponding to this mode.
83    *
84    * @param mode stands for the mode of operation; it can be one of
85    * SUBSTRINGS_MODE,
86    * EQ_MODE, GE_MODE, LE_MODE, GT_MODE or LT_MODE
87    */
 
88  126 toggle protected StringInterpreter(int mode){
89  126 this.mode = mode;
90    }
91   
92    /**
93    * This method tells whether this interpreter can evaluate the expression,
94    * which is only if there are only two Terms, and both are of type
95    * Types.STRING_TYPE.
96    *
97    * @param t - the array of Terms to be evaluated
98    *
99    * @return Types.BOOLEAN_TYPE, if the expression can be evaluated; null
100    * otherwise
101    */
 
102  66 toggle public String canEvaluate(Term [] t){
103  66 if (t.length==2 &&
104    t[0].getType().intern()==Types.STRING_TYPE &&
105    t[0].getType().intern()==t[1].getType().intern()){
106  2 return Types.BOOLEAN_TYPE;
107    }
108   
109  64 return null;
110    }
111   
112    /**
113    * This method evaluates the comparison expression, depending on the mode
114    * it was given at construction time. If the expression can be evaluated,
115    * it returns a java.lang.Boolean object; otherwise a EvaluationException is
116    * thrown.
117    *
118    * @param env - the Environment in which the evaluation occurs
119    * @param t - the array of Terms to be evaluated; should contain two and only
120    * two Terms, each of type Types.STRING_TYPE
121    *
122    * @return java.lang.Boolean object that contains the boolean result of
123    * evaluation, as determined by the mode provided at construction time
124    */
 
125  0 toggle public Object evaluate(Environment env, Term [] t) throws EvaluationException{
126  0 if (canEvaluate(t)==null){
127  0 throw new EvaluationException("Cannot evaluate expression");
128    }
129   
130  0 String s1=(String)t[0].evaluate(env);
131  0 String s2=(String)t[1].evaluate(env);
132  0 int r=mode<SUBSTRINGS_MODE ? 0 : s1.compareTo(s2);
133  0 boolean b=false;
134   
135  0 switch(mode){
136  0 case EQ_MODE: b=r==0;
137  0 break;
138  0 case GE_MODE: b=r>=0;
139  0 break;
140  0 case LE_MODE: b=r<=0;
141  0 break;
142  0 case GT_MODE: b=r>0;
143  0 break;
144  0 case LT_MODE: b=r<0;
145  0 break;
146  0 case SUBSTRINGS_MODE: b=s2.indexOf(s1)>=0; // s1 is met somewhere in s2
147  0 break;
148  0 default:
149    }
150   
151  0 return new Boolean(b);
152    }
153    }
154