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