KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xml > DocumentConstructor


1 // Copyright (c) 2003 Per M.A. Bothner
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.xml;
5 import gnu.mapping.*;
6 import gnu.mapping.Location; // As opposed to gnu.bytecode.Location
7
import gnu.lists.*;
8 import gnu.expr.*;
9 import gnu.bytecode.*;
10
11 public class DocumentConstructor extends NodeConstructor
12 {
13   public static final DocumentConstructor documentConstructor
14     = new DocumentConstructor();
15
16   public void apply (CallContext ctx)
17   {
18     Consumer saved = ctx.consumer;
19     Consumer out = pushNodeContext(ctx);
20     try
21       {
22     Object JavaDoc endMarker = Location.UNBOUND;
23     out.beginDocument();
24     for (;;)
25       {
26         Object JavaDoc arg = ctx.getNextArg(endMarker);
27         if (arg == endMarker)
28           break;
29         if (arg instanceof Consumable)
30           ((Consumable) arg).consume(out);
31         else
32           out.writeObject(arg);
33       }
34     out.endDocument();
35       }
36     finally
37       {
38     popNodeContext(saved, ctx);
39       }
40   }
41
42   public void compileToNode (ApplyExp exp, Compilation comp,
43                       ConsumerTarget target)
44   {
45     Variable consumer = target.getConsumerVariable();
46     Expression[] args = exp.getArgs();
47     int nargs = args.length;
48     CodeAttr code = comp.getCode();
49     code.emitLoad(consumer);
50     code.emitInvokeInterface(beginDocumentMethod);
51     for (int i = 0; i < nargs; i++)
52       compileChild(args[i], comp, target);
53     code.emitLoad(consumer);
54     code.emitInvokeInterface(endDocumentMethod);
55   }
56
57   static final Method beginDocumentMethod
58     = Compilation.typeConsumer.getDeclaredMethod("beginDocument", 0);
59   static final Method endDocumentMethod
60     = Compilation.typeConsumer.getDeclaredMethod("endDocument", 0);
61
62 }
63
Popular Tags