1 mars 1.1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
3 xmlns:o="http://www.o-xml.org/lang/">
|
4 mars 1.5 <xsl:param name="classname" select="'test'"/>
|
5 mars 1.1
6 <xsl:output method="text" encoding="UTF-8"/>
|
7 mars 1.3 <xsl:strip-space elements="*"/>
|
8 mars 1.1
|
9 mars 1.2
10 <xsl:template name="imports">
11 import org.oXML.type.*;
|
12 mars 1.3 import org.oXML.util.Log;
13 import org.oXML.ObjectBoxException;
14 import org.oXML.xpath.Expression;
15 import org.oXML.engine.RuntimeContext;
|
16 mars 1.2 import org.oXML.xpath.parser.Parser;
17 </xsl:template>
18
|
19 mars 1.5
|
20 mars 1.2 <xsl:template match="o:program">
21 <xsl:call-template name="imports"/>
22
23 public class <xsl:value-of select="$classname"/> extends Program {
24
25 public static Expression[] expressions = new Expression[<xsl:value-of select="count(//o:expression)"/>];
26
27 <!-- program initialiser -->
28 public void init()
|
29 mars 1.3 throws ObjectBoxException {
|
30 mars 1.5 super.init();
|
31 mars 1.2 Program program = this;
32 <xsl:apply-templates select="o:type"/>
33 <xsl:apply-templates select="o:function"/>
34 <xsl:apply-templates select="//o:expression" mode="parse"/>
35 }
36
37 public Program program()
|
38 mars 1.3 throws ObjectBoxException {
|
39 mars 1.2 return new <xsl:value-of select="$classname"/>();
40 };
41
|
42 mars 1.3 public Node run(RuntimeContext rc)
43 throws ObjectBoxException {
|
44 mars 1.2 <xsl:apply-templates select="o:param" mode="initialise"/>
45 <xsl:apply-templates mode="instruction"/>
46 return null;
47 }
|
48 mars 1.1
|
49 mars 1.3 public static void main(String[] args)
50 throws ObjectBoxException {
51 <xsl:value-of select="$classname"/> program = new <xsl:value-of select="$classname"/>();
52 program.init();
|
53 mars 1.2 program.run();
|
54 mars 1.1 }
|
55 mars 1.2 }
|
56 mars 1.1 </xsl:template>
57
|
58 mars 1.2 <!-- initialise program parameter -->
59 <xsl:template match="o:param" mode="initialise">
60 rc.setVariable(<xsl:apply-templates select="@name" mode="name"/>
61 , program.getParameterValue(<xsl:apply-templates select="@name" mode="name"/>));
|
62 mars 1.1 </xsl:template>
63
64 <xsl:template match="o:type">
65 <xsl:if test="not(preceding-sibling::o:type)">Type </xsl:if>
|
66 mars 1.3 type = new Type(<xsl:apply-templates select="@name" mode="name"/>);
67 program.addType(type);
|
68 mars 1.6 <xsl:apply-templates select="o:parent"/>
69 <xsl:apply-templates select="o:variable"/>
|
70 mars 1.1 <xsl:apply-templates select="o:function"/>
71 </xsl:template>
72
|
73 mars 1.6 <xsl:template match="o:parent">
74 type.addParent(<xsl:apply-templates select="@name" mode="type"/>);
75 </xsl:template>
76
77 <xsl:template match="o:variable">
78 type.addVariable(<xsl:apply-templates select="@name" mode="name"/>);
79 </xsl:template>
80
|
81 mars 1.1 <!-- type constructor function -->
82 <xsl:template match="o:function[ancestor::o:type and @name = ancestor::o:type/@name]">
83 <xsl:if test="not(preceding::o:function)">Function </xsl:if>
|
84 mars 1.6 <xsl:text>function = new ConstructorFunction(</xsl:text>
85 <xsl:apply-templates select="@name" mode="type"/>
86 <xsl:text>, new Type[]{</xsl:text>
87 <xsl:apply-templates select="o:param" mode="signature"/>}){
88
|
89 mars 1.3 public Node invoke(Node[] args, RuntimeContext rc)
90 throws ObjectBoxException{
|
91 mars 1.8 rc.hide();
92 try{
|
93 mars 1.6 Node target = new Node(<xsl:apply-templates select="ancestor::o:type/@name" mode="type"/>);
94 <!-- initialise type variables - unless they are set by function args -->
95 <xsl:apply-templates select="ancestor::o:type/o:variable[@name != current()/o:param/@name]" mode="initialise"/>
96 <!-- set type variables as variables in the current context -->
97 <xsl:apply-templates select="ancestor::o:type/o:variable" mode="set"/>
98 <!-- function body -->
|
99 mars 1.2 <xsl:apply-templates select="o:do" mode="function"/>
|
100 mars 1.6 return target;
|
101 mars 1.8 }finally{
102 rc.unhide();
103 }
|
104 mars 1.2 }
|
105 mars 1.3 };
106 program.addFunction(function);
107 type.addConstructor(function);
|
108 mars 1.2 </xsl:template>
109
|
110 mars 1.1 <!-- type function -->
111 <!-- Function function = new TypeFunction(type, name, signature){ ... def ... }; -->
112 <xsl:template match="o:function[ancestor::o:type and @name != ancestor::o:type/@name]">
113 <xsl:if test="not(preceding::o:function)">Function </xsl:if>
|
114 mars 1.3 <xsl:text>function = new TypeFunction(</xsl:text>
115 <xsl:apply-templates select="ancestor::o:type/@name" mode="type"/>,
116 <xsl:apply-templates select="@name" mode="name"/>
117 <xsl:text>, new Type[]{</xsl:text>
118 <xsl:apply-templates select="o:param" mode="signature"/>}){
119 public Node invoke(Node target, Node[] args, RuntimeContext rc)
120 throws ObjectBoxException{
|
121 mars 1.8 rc.hide();
122 try{
|
123 mars 1.6 <!-- set type variables as variables in the current context -->
124 <xsl:apply-templates select="ancestor::o:type/o:variable" mode="set"/>
|
125 mars 1.2 <xsl:apply-templates select="o:do" mode="function"/>
|
126 mars 1.6 <!-- to be done - check if non-conditional return? -->
127 <!-- if last instruction is not <o:return>, add a null return stmt -->
128 <o:if test="*[last()][not(self::o:return]">return new NodesetNode();</o:if>
|
129 mars 1.8 }finally{
130 rc.unhide();
131 }
|
132 mars 1.3 }
133 };
|
134 mars 1.6 type.addFunction(function);
|
135 mars 1.1 </xsl:template>
136
137 <!-- top level (static/standalone) function -->
138 <xsl:template match="o:function">
139 <xsl:if test="not(preceding::o:function)">Function </xsl:if>
|
140 mars 1.3 <xsl:text>function = new Function(</xsl:text>
141 <xsl:apply-templates select="@name" mode="name"/>
|
142 mars 1.1 <xsl:text>, new Type[]{</xsl:text>
143 <xsl:apply-templates select="o:param" mode="signature"/>}){
|
144 mars 1.3 public Node invoke(Node[] args, RuntimeContext rc)
145 throws ObjectBoxException{
|
146 mars 1.8 rc.hide();
147 try{
|
148 mars 1.2 <xsl:apply-templates select="o:do" mode="function"/>
|
149 mars 1.6 <!-- if last instruction is not <o:return>, add a null return stmt -->
|
150 mars 1.9 <o:if test="o:*[last()][not(self::o:return]">return new NodesetNode();</o:if>
|
151 mars 1.8 }finally{
152 rc.unhide();
153 }
|
154 mars 1.3 }
155 public Node invoke(Node target, Node[] args, RuntimeContext rc)
156 throws ObjectBoxException{
157 throw new ObjectBoxException("cannot invoke static function with target");
158 }
159 };
160 program.addFunction(function);
161 </xsl:template>
162
|
163 mars 1.6 <!-- initialise type variable with default value -->
164 <xsl:template match="o:variable[o:select]" mode="initialise">
165 target.getVariable(<xsl:apply-templates select="@name" mode="name"/>).setValue(<xsl:apply-templates select="o:select/o:expression"/>);
166 </xsl:template>
167
168 <!-- initialise type variable without default value -->
169 <xsl:template match="o:variable[o:select]" mode="initialise">
170 target.getVariable(<xsl:apply-templates select="@name" mode="name"/>).setValue(new NodesetNode());
171 </xsl:template>
172
|
173 mars 1.5 <!-- set type variable values as variables -->
174 <xsl:template match="o:variable" mode="set">
|
175 mars 1.6 rc.setVariable(target.getVariable(<xsl:apply-templates select="@name" mode="name"/>));
|
176 mars 1.5 </xsl:template>
177
178 <!-- set function parameter values as variables -->
179 <xsl:template match="o:param" mode="set">
180 rc.setVariable(<xsl:apply-templates select="@name" mode="name"/>
181 , args[<xsl:value-of select="position() -1"/>]);
182 </xsl:template>
183
|
184 mars 1.3 <!-- function body -->
185 <xsl:template match="o:do" mode="function">
|
186 mars 1.5 <!-- set arguments as variables -->
187 <xsl:apply-templates select="ancestor::o:function/o:param" mode="set"/>
188
189 <!-- function instructions -->
190 <xsl:apply-templates select="*" mode="instruction"/>
|
191 mars 1.3
|
192 mars 1.1 </xsl:template>
193
|
194 mars 1.3
|
195 mars 1.2 <xsl:template match="node()|@*" mode="name">
|
196 mars 1.3 <xsl:text>new Name("</xsl:text>
197 <xsl:value-of select="."/>
198 <xsl:text>")</xsl:text>
|
199 mars 1.1 </xsl:template>
200
|
201 mars 1.2 <xsl:template match="node()|@*" mode="type">
|
202 mars 1.1 getType(<xsl:apply-templates select="." mode="name"/>)
203 </xsl:template>
204
205 <xsl:template match="o:param" mode="signature">
|
206 mars 1.5 <xsl:apply-templates select="@type" mode="type"/>
|
207 mars 1.1 <xsl:if test="following-sibling::o:param">,</xsl:if>
208 </xsl:template>
209
|
210 mars 1.2 <xsl:template match="o:expression" mode="parse">
|
211 mars 1.4 <!-- make call to Parser to parse expression -->
|
212 mars 1.3 <xsl:text> expressions[</xsl:text>
|
213 mars 1.4 <xsl:value-of select="position() - 1"/>
|
214 mars 1.3 <xsl:text>] = Parser.parse("</xsl:text>
215 <xsl:value-of select="."/>
216 <xsl:text>"); </xsl:text>
217 <!-- bind expression to namespace context -->
218 <xsl:text> expressions[</xsl:text>
219 <xsl:value-of select="position() - 1"/>
220 <xsl:text>].bind(new org.oXML.xpath.SimpleResolver(new String[][]{</xsl:text>
221 <xsl:for-each select="namespace::node()">
222 <xsl:text>new String[]{"</xsl:text>
223 <xsl:value-of select="name(.)"/>
224 <xsl:text>","</xsl:text>
225 <xsl:value-of select="."/>
|
226 mars 1.4 <xsl:text>"},</xsl:text>
|
227 mars 1.3 </xsl:for-each>
228 <xsl:text>})); </xsl:text>
|
229 mars 1.2 </xsl:template>
230
|
231 mars 1.1 <xsl:template match="o:expression">
|
232 mars 1.9 <xsl:value-of select="$classname"/>.expressions[<xsl:value-of select="count(preceding::o:expression)"/>].evaluate(rc)
|
233 mars 1.1 </xsl:template>
234
235
236 <!-- instruction templates follow -->
237
238
239 <xsl:template match="o:if" mode="instruction">
240 if(<xsl:apply-templates select="o:test/o:expression"/>.booleanValue()){
241 <xsl:apply-templates mode="instruction"/>
242 }
243 </xsl:template>
244
245 <xsl:template match="o:while" mode="instruction">
246 do{
247 <xsl:apply-templates mode="instruction"/>
248 }while(<xsl:apply-templates select="o:test/o:expression"/>
|
249 mars 1.9 <xsl:text>.booleanValue());</xsl:text>
|
250 mars 1.1 </xsl:template>
251
252 <xsl:template match="o:variable[o:select]" mode="instruction">
253 rc.setVariable(<xsl:apply-templates select="@name" mode="name"/>
|
254 mars 1.9 ,<xsl:apply-templates select="o:select/o:expression"/>);
|
255 mars 1.1 </xsl:template>
256
257 <xsl:template match="o:variable" mode="instruction">
258 rc.setOutputContextNode(new NodesetNode());
259 try{
260 <xsl:apply-templates mode="instruction"/>
261 rc.setVariable(<xsl:apply-templates select="@name" mode="name"/>
262 , rc.getOutputContextNode());
263 }finally{
264 rc.resetOutputContextNode();
265 }
266 </xsl:template>
267
268 <xsl:template match="o:eval" mode="instruction">
|
269 mars 1.9 rc.push(<xsl:apply-templates select="o:select/o:expression"/>);
|
270 mars 1.1 rc.pop();
271 </xsl:template>
272
|
273 mars 1.2 <xsl:template match="o:log[o:select]" mode="instruction">
|
274 mars 1.9 Log.info(<xsl:apply-templates select="o:select/o:expression"/>.stringValue());
|
275 mars 1.2 </xsl:template>
276
|
277 mars 1.1 <xsl:template match="o:log[o:msg]" mode="instruction">
|
278 mars 1.2 <!-- to be done! evaluate mixed expression -->
|
279 mars 1.9 Log.info(<xsl:apply-templates select="o:msg/o:expression"/>.stringValue());
|
280 mars 1.1 </xsl:template>
281
282 <xsl:template match="o:log" mode="instruction">
283 rc.setOutputContextNode(new NodesetNode());
284 try{
285 <xsl:apply-templates mode="instruction"/>
286 System.println(rc.getOutputContextNode().stringValue());
287 }finally{
288 rc.resetOutputContextNode();
289 }
290 </xsl:template>
291
|
292 mars 1.7 <xsl:template match="o:choose" mode="instruction">
293 <xsl:apply-templates select="o:when" mode="instruction"/>
294 <xsl:apply-templates select="o:otherwise" mode="instruction"/>
295 </xsl:template>
296
297 <xsl:template match="o:when" mode="instruction">
298 <xsl:if test="preceding-sibling::o:when">else</xsl:if>
299 <xsl:text> if(</xsl:text>
|
300 mars 1.9 <xsl:apply-templates select="o:test/o:expression"/>.booleanValue()){
|
301 mars 1.7 <xsl:apply-templates mode="instruction"/>
302 }
303 </xsl:template>
304
305 <xsl:template match="o:otherwise" mode="instruction">
306 <xsl:text> else{</xsl:text>
307 <xsl:apply-templates mode="instruction"/>
308 }
309 </xsl:template>
310
|
311 mars 1.1 <xsl:template match="o:do" mode="instruction">
312 <xsl:apply-templates mode="instruction"/>
|
313 mars 1.2 </xsl:template>
314
315 <xsl:template match="o:return[o:select]" mode="instruction">
|
316 mars 1.6 if(true) <!-- oddity to avoid 'unreaachable block' compilation error -->
|
317 mars 1.9 return <xsl:apply-templates select="o:select/o:expression"/>;
318 </xsl:template>
319
320 <xsl:template match="o:element" mode="instruction">
321 rc.push(new ElementNode(<xsl:apply-templates select="@name" mode="name"/>));
322 <xsl:apply-templates mode="instruction"/>
323 rc.pop();
324 </xsl:template>
325
326 <xsl:template match="o:attribute[o:select]" mode="instruction">
327 rc.push(new AttributeNode(<xsl:apply-templates select="@name" mode="name"/>
328 , <xsl:apply-templates select="o:select/o:expression"/>.stringValue()));
329 rc.pop();
330 </xsl:template>
331
332 <xsl:template match="o:processing-instruction" mode="instruction">
333 <xsl:message>warning: no instruction template for <xsl:value-of select="name(.)"/></xsl:message>
334 </xsl:template>
335
336 <xsl:template match="o:comment" mode="instruction">
337 <xsl:message>warning: no instruction template for <xsl:value-of select="name(.)"/></xsl:message>
338 mars 1.9 </xsl:template>
339
340 <xsl:template match="o:text" mode="instruction">
341 <xsl:message>warning: no instruction template for <xsl:value-of select="name(.)"/></xsl:message>
342 </xsl:template>
343
344 <xsl:template match="o:for-each[o:select]" mode="instruction">
345 <xsl:message>warning: no instruction template for <xsl:value-of select="name(.)"/></xsl:message>
346 </xsl:template>
347
348 <xsl:template match="o:for-each[o:to]" mode="instruction">
349 <xsl:variable name="from">
350 <xsl:if test="o:from">
351 <xsl:apply-templates select="o:to/o:expression"/>
352 <xsl:text>.numberValue();</xsl:text>
353 </xsl:if>
354 <xsl:if test="not(o:from)">0</xsl:if>
355 </xsl:variable>
356 <xsl:variable name="to">
357 <xsl:apply-templates select="o:to/o:expression"/>
358 <xsl:text>.numberValue();</xsl:text>
359 mars 1.9 </xsl:variable>
360 <xsl:variable name="step">
361 <xsl:if test="o:step">
362 <xsl:apply-templates select="o:step/o:expression"/>
363 <xsl:text>.numberValue();</xsl:text>
364 </xsl:if>
365 <xsl:if test="not(o:step)">1</xsl:if>
366 </xsl:variable>
367 <!-- tbd: try to not evaluate 'to' expression more than once! -->
368 <xsl:text>for(double d=</xsl:text>
369 <xsl:value-of select="$from"/>
370 <xsl:text>;i<</xsl:text>
371 <xsl:value-of select="$to"/>
372 <xsl:text>;i+=</xsl:text>
373 <xsl:value-of select="$step"/>
374 <xsl:text>){ </xsl:text>
375 <xsl:choose>
376 <xsl:when test="@name">
377 rc.setVariable(<xsl:apply-templates select="@name" mode="name"/>, new NumberNode(d));
378 </xsl:when>
379 <xsl:otherwise>
380 mars 1.9 rc.setContextNode(new NumberNode(d));
381 </xsl:otherwise>
382 </xsl:choose>
383 <xsl:apply-templates mode="instruction"/>
384 }
|
385 mars 1.1 </xsl:template>
386
387 <xsl:template match="o:*" mode="instruction">
388 <xsl:message>warning: no instruction template for <xsl:value-of select="name(.)"/></xsl:message>
389 </xsl:template>
390
391 <!-- text literal -->
392 <xsl:template match="text()" mode="instruction">
393 <xsl:if test="normalize-space(.)">
394 rc.push(new StringNode("<xsl:value-of select="normalize-space(.)"/>"));
395 rc.pop();
396 </xsl:if>
397 </xsl:template>
398
399 <!-- element literal -->
400 <xsl:template match="*" mode="instruction">
401 rc.push(new ElementNode(new Name("<xsl:value-of select="name(.)"/>")));
402 <xsl:apply-templates mode="instruction"/>
403 rc.pop();
404 </xsl:template>
405
|
406 mars 1.9 <xsl:template match="o:function|o:type" mode="instruction"/>
407 <xsl:template match="o:test|o:select|o:to|o:from|o:step" mode="instruction"/>
|
408 mars 1.1
409 </xsl:stylesheet>
|