01: // Copyright (c) 2004 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.lists.*;
08: import gnu.bytecode.*;
09: import gnu.expr.*;
10: import gnu.xml.TextUtils;
11:
12: public class MakeProcInst extends NodeConstructor {
13: public static final MakeProcInst makeProcInst = new MakeProcInst();
14:
15: public int numArgs() {
16: return 0x2002;
17: }
18:
19: public static void procInst$C(Object target, Object content,
20: Consumer out) {
21: target = KNode.atomicValue(target);
22: if (!(target instanceof String || target instanceof UntypedAtomic))
23: throw new ClassCastException(
24: "invalid type of processing-instruction target [XPTY0004]");
25:
26: if (!(out instanceof XConsumer))
27: return;
28: StringBuffer sbuf = new StringBuffer();
29: if (content instanceof Values) {
30: Object[] vals = ((Values) content).getValues();
31: for (int i = 0; i < vals.length; i++) {
32: if (i > 0)
33: sbuf.append(' ');
34: TextUtils.stringValue(vals[i], sbuf);
35: }
36: } else
37: TextUtils.stringValue(content, sbuf);
38: int length = sbuf.length();
39: int start = 0;
40: while (start < length
41: && Character.isWhitespace(sbuf.charAt(start)))
42: start++;
43: char[] chars = new char[length - start];
44: sbuf.getChars(start, length, chars, 0);
45: ((XConsumer) out).writeProcessingInstruction(target.toString(),
46: chars, 0, chars.length);
47: }
48:
49: public static void procInst$X(Object target, Object content,
50: CallContext ctx) {
51: Consumer saved = ctx.consumer;
52: Consumer out = NodeConstructor.pushNodeContext(ctx);
53: try {
54: procInst$C(target, content, out);
55: } finally {
56: NodeConstructor.popNodeContext(saved, ctx);
57: }
58: }
59:
60: public void apply(CallContext ctx) {
61: procInst$X(ctx.getNextArg(null), ctx.getNextArg(null), ctx);
62: }
63:
64: public void compileToNode(ApplyExp exp, Compilation comp,
65: ConsumerTarget target) {
66: CodeAttr code = comp.getCode();
67: Expression[] args = exp.getArgs();
68: args[0].compile(comp, Target.pushObject);
69: args[1].compile(comp, Target.pushObject);
70: code.emitLoad(target.getConsumerVariable());
71: code.emitInvokeStatic(ClassType.make(
72: "gnu.kawa.xml.MakeProcInst").getDeclaredMethod(
73: "procInst$C", 3));
74: }
75: }
|