01: // Copyright (c) 2001, 2004 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.xml;
05:
06: import gnu.lists.*;
07: import gnu.mapping.*;
08: import gnu.expr.*;
09: import gnu.bytecode.*;
10:
11: public class MakeAttribute extends NodeConstructor {
12: public static final MakeAttribute makeAttribute = new MakeAttribute();
13: public static final QuoteExp makeAttributeExp = new QuoteExp(
14: makeAttribute);
15:
16: public int numArgs() {
17: return 0xFFFFF001;
18: }
19:
20: public static void startAttribute(Consumer out, Object type) {
21: out.startAttribute(type);
22: }
23:
24: public void apply(CallContext ctx) {
25: Consumer saved = ctx.consumer;
26: Consumer out = pushNodeContext(ctx);
27: try {
28: Object type = ctx.getNextArg();
29: startAttribute(out, type);
30: Object endMarker = Special.dfault;
31: for (;;) {
32: Object arg = ctx.getNextArg(endMarker);
33: if (arg == endMarker)
34: break;
35: if (arg instanceof Consumable)
36: ((Consumable) arg).consume(out);
37: else
38: ctx.writeValue(arg);
39: }
40: out.endAttribute();
41: } finally {
42: popNodeContext(saved, ctx);
43: }
44: }
45:
46: public void compileToNode(ApplyExp exp, Compilation comp,
47: ConsumerTarget target) {
48: Variable consumer = ((ConsumerTarget) target)
49: .getConsumerVariable();
50: Expression[] args = exp.getArgs();
51: int nargs = args.length;
52: CodeAttr code = comp.getCode();
53: code.emitLoad(consumer);
54: code.emitDup();
55: args[0].compile(comp, Target.pushObject);
56: // Stack: consumer, consumer, tagtype
57: code.emitInvokeStatic(startAttributeMethod);
58: for (int i = 1; i < nargs; i++)
59: compileChild(args[i], comp, target);
60: code.emitInvokeInterface(endAttributeMethod);
61: }
62:
63: static final ClassType typeMakeAttribute = ClassType
64: .make("gnu.kawa.xml.MakeAttribute");
65: static final Method startAttributeMethod = typeMakeAttribute
66: .getDeclaredMethod("startAttribute", 2);
67: static final Method endAttributeMethod = Compilation.typeConsumer
68: .getDeclaredMethod("endAttribute", 0);
69:
70: public Type getReturnType(Expression[] args) {
71: return Compilation.typeObject;
72: }
73: }
|