01: // Copyright (c) 2003 Per M.A. Bothner
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.xml;
05:
06: import gnu.mapping.*;
07: import gnu.mapping.Location; // As opposed to gnu.bytecode.Location
08: import gnu.lists.*;
09: import gnu.expr.*;
10: import gnu.bytecode.*;
11:
12: public class DocumentConstructor extends NodeConstructor {
13: public static final DocumentConstructor documentConstructor = new DocumentConstructor();
14:
15: public void apply(CallContext ctx) {
16: Consumer saved = ctx.consumer;
17: Consumer out = pushNodeContext(ctx);
18: try {
19: Object endMarker = Location.UNBOUND;
20: out.startDocument();
21: for (;;) {
22: Object arg = ctx.getNextArg(endMarker);
23: if (arg == endMarker)
24: break;
25: if (arg instanceof Consumable)
26: ((Consumable) arg).consume(out);
27: else
28: out.writeObject(arg);
29: }
30: out.endDocument();
31: } finally {
32: popNodeContext(saved, ctx);
33: }
34: }
35:
36: public void compileToNode(ApplyExp exp, Compilation comp,
37: ConsumerTarget target) {
38: Variable consumer = target.getConsumerVariable();
39: Expression[] args = exp.getArgs();
40: int nargs = args.length;
41: CodeAttr code = comp.getCode();
42: code.emitLoad(consumer);
43: code.emitInvokeInterface(startDocumentMethod);
44: for (int i = 0; i < nargs; i++)
45: compileChild(args[i], comp, target);
46: code.emitLoad(consumer);
47: code.emitInvokeInterface(endDocumentMethod);
48: }
49:
50: static final Method startDocumentMethod = Compilation.typeConsumer
51: .getDeclaredMethod("startDocument", 0);
52: static final Method endDocumentMethod = Compilation.typeConsumer
53: .getDeclaredMethod("endDocument", 0);
54:
55: }
|