1 |
| package org.drools.smf; |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| import java.io.IOException; |
44 |
| import java.io.InputStream; |
45 |
| import java.net.URL; |
46 |
| import java.util.Enumeration; |
47 |
| import java.util.Properties; |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| public class SemanticsReader |
73 |
| { |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| private static final SemanticsReader INSTANCE = new SemanticsReader( ); |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
0
| public static SemanticsReader getInstance()
|
91 |
| { |
92 |
0
| return INSTANCE;
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
18
| public SemanticsReader()
|
103 |
| { |
104 |
| |
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
0
| public SemanticModule read(URL url) throws IOException, SemanticsReaderException
|
112 |
| { |
113 |
0
| return read(url, Thread.currentThread( ).getContextClassLoader( ));
|
114 |
| } |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
12
| public SemanticModule read(URL url, ClassLoader cl) throws IOException,
|
126 |
| SemanticsReaderException |
127 |
| { |
128 |
12
| InputStream in = url.openStream( );
|
129 |
| |
130 |
12
| try
|
131 |
| { |
132 |
12
| return read( in, cl );
|
133 |
| } |
134 |
| finally |
135 |
| { |
136 |
12
| in.close( );
|
137 |
| } |
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
12
| public SemanticModule read(InputStream in, ClassLoader cl) throws IOException, SemanticsReaderException
|
154 |
| { |
155 |
12
| Properties props = new Properties( );
|
156 |
| |
157 |
12
| props.load( in );
|
158 |
| |
159 |
12
| String uri = props.getProperty( "module.uri" );
|
160 |
| |
161 |
12
| if ( uri == null || uri.trim( ).equals( "" ) )
|
162 |
| { |
163 |
0
| throw new SemanticsReaderException( "module.uri must be specified" );
|
164 |
| } |
165 |
| |
166 |
12
| SimpleSemanticModule module = new SimpleSemanticModule( uri.trim( ) );
|
167 |
| |
168 |
12
| for ( Enumeration propNames = props.propertyNames( ); propNames.hasMoreElements( ); )
|
169 |
| { |
170 |
80
| String key = (String) propNames.nextElement( );
|
171 |
| |
172 |
80
| if ( key.equals( "module.uri" ) )
|
173 |
| { |
174 |
12
| continue;
|
175 |
| } |
176 |
| |
177 |
68
| String className = props.getProperty( key );
|
178 |
| |
179 |
68
| Class factoryClass;
|
180 |
68
| try
|
181 |
| { |
182 |
68
| factoryClass = cl.loadClass( className );
|
183 |
| } |
184 |
| catch ( ClassNotFoundException e ) |
185 |
| { |
186 |
0
| throw new SemanticsReaderException( e );
|
187 |
| } |
188 |
68
| if ( key.indexOf( "(" ) < 0 || key.indexOf( ")" ) < 0 )
|
189 |
| { |
190 |
0
| throw new SemanticsReaderException( "invalid key: " + key );
|
191 |
| } |
192 |
| |
193 |
68
| String type = parseType( key );
|
194 |
| |
195 |
68
| if ( type == null || type.equals( "" ) )
|
196 |
| { |
197 |
0
| throw new SemanticsReaderException( "no type specified" );
|
198 |
| } |
199 |
| |
200 |
68
| String componentName = parseName( key );
|
201 |
| |
202 |
68
| if ( componentName == null || componentName.equals( "" ) )
|
203 |
| { |
204 |
0
| throw new SemanticsReaderException( "no component name specified" );
|
205 |
| } |
206 |
| |
207 |
68
| try
|
208 |
| { |
209 |
68
| if ( "Rule".equals( type ) )
|
210 |
| { |
211 |
7
| RuleFactory factory = (RuleFactory) factoryClass.newInstance( );
|
212 |
| |
213 |
7
| module.addRuleFactory( componentName,
|
214 |
| factory ); |
215 |
| } |
216 |
61
| else if ( "ObjectType".equals( type ) )
|
217 |
| { |
218 |
23
| ObjectTypeFactory factory = (ObjectTypeFactory) factoryClass.newInstance( );
|
219 |
| |
220 |
23
| module.addObjectTypeFactory( componentName,
|
221 |
| factory ); |
222 |
| } |
223 |
38
| else if ( "Condition".equals( type ) )
|
224 |
| { |
225 |
5
| ConditionFactory factory = (ConditionFactory) factoryClass.newInstance( );
|
226 |
| |
227 |
5
| module.addConditionFactory( componentName,
|
228 |
| factory ); |
229 |
| } |
230 |
33
| else if ( "Consequence".equals( type ) )
|
231 |
| { |
232 |
5
| ConsequenceFactory factory = (ConsequenceFactory) factoryClass.newInstance( );
|
233 |
| |
234 |
5
| module.addConsequenceFactory( componentName,
|
235 |
| factory ); |
236 |
| } |
237 |
28
| else if ( "Duration".equals( type ) )
|
238 |
| { |
239 |
6
| DurationFactory factory = (DurationFactory) factoryClass.newInstance( );
|
240 |
| |
241 |
6
| module.addDurationFactory( componentName,
|
242 |
| factory ); |
243 |
| } |
244 |
22
| else if ( "ImportEntry".equals( type ) )
|
245 |
| { |
246 |
11
| ImportEntryFactory factory = (ImportEntryFactory) factoryClass.newInstance( );
|
247 |
| |
248 |
11
| module.addImportEntryFactory( componentName,
|
249 |
| factory ); |
250 |
| } |
251 |
11
| else if ( "ApplicationData".equals( type ) )
|
252 |
| { |
253 |
6
| ApplicationDataFactory factory = (ApplicationDataFactory) factoryClass.newInstance( );
|
254 |
| |
255 |
6
| module.addApplicationDataFactory( componentName,
|
256 |
| factory ); |
257 |
| } |
258 |
5
| else if ( "Functions".equals( type ) )
|
259 |
| { |
260 |
5
| FunctionsFactory factory = (FunctionsFactory) factoryClass.newInstance( );
|
261 |
| |
262 |
5
| module.addFunctionsFactory( componentName,
|
263 |
| factory ); |
264 |
| } |
265 |
| else |
266 |
| { |
267 |
0
| throw new SemanticsReaderException( "unknown type '" + type + "'" );
|
268 |
| } |
269 |
| } |
270 |
| catch ( InstantiationException e ) |
271 |
| { |
272 |
0
| throw new SemanticsReaderException( e );
|
273 |
| } |
274 |
| catch ( IllegalAccessException e ) |
275 |
| { |
276 |
0
| throw new SemanticsReaderException( e );
|
277 |
| } |
278 |
| } |
279 |
| |
280 |
12
| return module;
|
281 |
| } |
282 |
| |
283 |
68
| protected String parseType(String key)
|
284 |
| { |
285 |
68
| int leftParen = key.indexOf( "(" );
|
286 |
| |
287 |
68
| if ( leftParen < 0 )
|
288 |
| { |
289 |
0
| return null;
|
290 |
| } |
291 |
| |
292 |
68
| return key.substring( 0,
|
293 |
| leftParen ).trim( ); |
294 |
| } |
295 |
| |
296 |
68
| protected String parseName(String key)
|
297 |
| { |
298 |
68
| int leftParen = key.indexOf( "(" );
|
299 |
68
| int rightParen = key.indexOf( ")" );
|
300 |
| |
301 |
68
| if ( leftParen < 0 || rightParen < 0 )
|
302 |
| { |
303 |
0
| return null;
|
304 |
| } |
305 |
| |
306 |
68
| return key.substring( leftParen + 1,
|
307 |
| rightParen ).trim( ); |
308 |
| } |
309 |
| } |