1 package org.lcsim.util.xml;
2
3 import gnu.jel.CompilationException;
4 import gnu.jel.CompiledExpression;
5 import gnu.jel.DVMap;
6 import gnu.jel.Evaluator;
7 import gnu.jel.Library;
8 import java.util.HashMap;
9 import java.util.Map;
10 import org.jdom.Attribute;
11 import org.jdom.DataConversionException;
12 import org.jdom.DefaultJDOMFactory;
13 import org.jdom.Namespace;
14
15
16
17
18
19 public class JDOMExpressionFactory extends DefaultJDOMFactory
20 {
21 private Object[] resolver = { new Resolver() };
22 private Library jelLibrary = setUpLibrary();
23 private Map<String,Double> constants = new HashMap<String,Double>();
24
25 public void addConstant(String name, double value)
26 {
27 constants.put(name,value);
28 }
29
30 public org.jdom.Attribute attribute(String name, String value, int type, Namespace namespace)
31 {
32 return new CustomAttribute(name,value,type,namespace);
33 }
34
35 public org.jdom.Attribute attribute(String name, String value, Namespace namespace)
36 {
37 return new CustomAttribute(name,value,namespace);
38 }
39
40 public org.jdom.Attribute attribute(String name, String value, int type)
41 {
42 return new CustomAttribute(name,value,type);
43 }
44
45 public org.jdom.Attribute attribute(String name, String value)
46 {
47 return new CustomAttribute(name,value);
48 }
49
50 private class CustomAttribute extends Attribute
51 {
52 CustomAttribute(String name, String value)
53 {
54 super(name,value);
55 }
56 CustomAttribute(String name, String value, int type)
57 {
58 super(name,value,type);
59 }
60 CustomAttribute(String name, String value, Namespace namespace)
61 {
62 super(name,value,namespace);
63 }
64 CustomAttribute(String name, String value, int type, Namespace namespace)
65 {
66 super(name,value,type,namespace);
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public long getLongValue() throws DataConversionException
86 {
87 String expression = super.getValue();
88 try
89 {
90 CompiledExpression expr = Evaluator.compile(expression,jelLibrary,Long.TYPE);
91 return expr.evaluate_long(resolver);
92 }
93 catch (Throwable x)
94 {
95 DataConversionException xx = new DataConversionException(expression,"long");
96 xx.initCause(x);
97 throw xx;
98 }
99 }
100
101 public int getIntValue() throws DataConversionException
102 {
103 String expression = super.getValue();
104 try
105 {
106 CompiledExpression expr = Evaluator.compile(expression,jelLibrary,Integer.TYPE);
107 return expr.evaluate_int(resolver);
108 }
109 catch (Throwable x)
110 {
111 DataConversionException xx = new DataConversionException(expression,"int");
112 xx.initCause(x);
113 throw xx;
114 }
115 }
116
117 public float getFloatValue() throws DataConversionException
118 {
119 String expression = super.getValue();
120 try
121 {
122 CompiledExpression expr = Evaluator.compile(expression,jelLibrary,Float.TYPE);
123 return expr.evaluate_float(resolver);
124 }
125 catch (Throwable x)
126 {
127 DataConversionException xx = new DataConversionException(expression,"float");
128 xx.initCause(x);
129 throw xx;
130 }
131 }
132
133 public double getDoubleValue() throws DataConversionException
134 {
135 String expression = super.getValue();
136 try
137 {
138 CompiledExpression expr = Evaluator.compile(expression,jelLibrary,Double.TYPE);
139 return expr.evaluate_double(resolver);
140 }
141 catch (Throwable x)
142 {
143 DataConversionException xx = new DataConversionException(expression,"double");
144 xx.initCause(x);
145 throw xx;
146 }
147 }
148 }
149
150 public double computeDouble(String expression)
151 {
152 try
153 {
154 CompiledExpression expr = Evaluator.compile(expression,jelLibrary,Double.TYPE);
155 return expr.evaluate_double(resolver);
156 }
157 catch (Throwable x)
158 {
159 throw new RuntimeException(x);
160 }
161 }
162
163 public float computeFloat(String expression)
164 {
165 try
166 {
167 CompiledExpression expr = Evaluator.compile(expression,jelLibrary,Float.TYPE);
168 return expr.evaluate_float(resolver);
169 }
170 catch (Throwable x)
171 {
172 throw new RuntimeException(x);
173 }
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 private Library setUpLibrary()
192 {
193 try
194 {
195 Class[] staticLib = { Math.class };
196 Class[] dynamicLib = { Resolver.class };
197 Library lib = new Library(staticLib,dynamicLib,null,(Resolver) resolver[0],null);
198 lib.markStateDependent("random",null);
199 return lib;
200 }
201 catch (CompilationException x)
202 {
203 throw new RuntimeException(x);
204 }
205 }
206
207 public class Resolver extends DVMap
208 {
209 public String getTypeName(String str)
210 {
211 if (constants.containsKey(str)) return "Double";
212 return null;
213 }
214 public double getDoubleProperty(String str)
215 {
216 return ((Number) constants.get(str)).doubleValue();
217 }
218 }
219 }