Types | Line # 46 | 13 | 4 | 77.8% |
0.7777778
|
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 class maintains a registry of known types, so that the string encoding | |
36 | * of the values of these types can be decoded from the XML Policy. When | |
37 | * registering the type, the impelementing class should have a constructor with | |
38 | * a single String parameter to it. | |
39 | * | |
40 | * <p>By default there are three types known: BOOLEAN_TYPE, INTEGER_TYPE and | |
41 | * STRING_TYPE ("Boolean", "Integer" and "String" respectively). | |
42 | * | |
43 | * @author A.Otenko | |
44 | */ | |
45 | ||
46 | public final class Types { | |
47 | protected final static java.util.Map knownTypes=new java.util.Hashtable(); | |
48 | ||
49 | public final static String INTEGER_TYPE = "Integer"; | |
50 | public final static String DOUBLE_TYPE = "Double"; | |
51 | public final static String BOOLEAN_TYPE = "Boolean"; | |
52 | public final static String STRING_TYPE = "String"; | |
53 | ||
54 | 21 | static{ |
55 | 21 | registerType(INTEGER_TYPE, Integer.class); |
56 | 21 | registerType(DOUBLE_TYPE, Double.class); |
57 | 21 | registerType(BOOLEAN_TYPE, Boolean.class); |
58 | 21 | registerType(STRING_TYPE, String.class); |
59 | } | |
60 | ||
61 | /** | |
62 | * This method registers a class to be used to instantiate values of the | |
63 | * given type from String encoding. | |
64 | * | |
65 | * @param type - the type of the value | |
66 | * @param c - the Class that represents the values of that type; it must | |
67 | * have a public constructor with a single String argument | |
68 | */ | |
69 | 105 | public static void registerType(String type, Class c){ |
70 | 105 | try{ |
71 | 105 | knownTypes.put(type, c.getConstructor(new Class[]{String.class})); |
72 | }catch (NoSuchMethodException nsme){ | |
73 | 0 | nsme.printStackTrace(); |
74 | } | |
75 | } | |
76 | ||
77 | /** | |
78 | * This is the method for building the objects of known types out of their | |
79 | * String encoding. | |
80 | * | |
81 | * @param type is the type of the object, as it appears in the XML Policy | |
82 | * @param value is the String encoding of the object | |
83 | * | |
84 | * @return the constructed type-specific Object | |
85 | * | |
86 | * @throws EvaluationException if the constructor is not found or the value | |
87 | * is not | |
88 | * properly encoded | |
89 | */ | |
90 | 89 | public static Object construct(String type, String value) throws EvaluationException{ |
91 | 89 | try{ |
92 | 89 | java.lang.reflect.Constructor c = (java.lang.reflect.Constructor)(knownTypes.get(type)); |
93 | 0 | if (c==null) throw new EvaluationException("Could not build a value of type "+type+" out of \""+value+"\": no implementing Class found for this type"); |
94 | ||
95 | 89 | return c.newInstance(new Object[]{value}); |
96 | }catch (Throwable th){ | |
97 | 0 | throw new EvaluationException("Could not build a value of type "+type+" out of \""+value+"\"", th); |
98 | } | |
99 | } | |
100 | } |
|