1 4 package gnu.kawa.xml; 5 import gnu.mapping.*; 6 import gnu.lists.*; 7 import gnu.expr.*; 8 import gnu.bytecode.*; 9 import gnu.xml.*; 10 11 public class MakeText extends NodeConstructor 12 { 13 public static final MakeText makeText = new MakeText(); 14 15 public int numArgs() { return 0x1001; } 16 17 public Object apply1 (Object arg) 18 { 19 if (arg == null || (arg instanceof Values && ((Values) arg).isEmpty())) 20 return arg; 21 NodeTree node = new NodeTree(); 22 TextUtils.textValue(arg, new XMLFilter(node)); 23 return KText.make(node); 24 } 25 26 public static void text$X (Object arg, CallContext ctx) 27 { 28 if (arg == null || (arg instanceof Values && ((Values) arg).isEmpty())) 29 return; 30 Consumer saved = ctx.consumer; 31 Consumer out = NodeConstructor.pushNodeContext(ctx); 32 try 33 { 34 TextUtils.textValue(arg, out); 35 } 36 finally 37 { 38 NodeConstructor.popNodeContext(saved, ctx); 39 } 40 } 41 42 public void apply (CallContext ctx) 43 { 44 text$X(ctx.getNextArg(null), ctx); 45 } 46 47 public void compile (ApplyExp exp, Compilation comp, Target target) 48 { 49 ApplyExp.compile(exp, comp, target); 52 } 53 54 public void compileToNode (ApplyExp exp, Compilation comp, 55 ConsumerTarget target) 56 { 57 CodeAttr code = comp.getCode(); 59 Expression[] args = exp.getArgs(); 60 Expression texp = args[0]; 61 Variable cvar = target.getConsumerVariable(); 62 if (texp instanceof QuoteExp) 63 { 64 Object tval = ((QuoteExp) texp).getValue(); 65 if (tval instanceof String ) 66 { 67 String str = (String ) tval; 68 String segments = CodeAttr.calculateSplit(str); 69 int numSegments = segments.length(); 70 ClassType ctype = (ClassType) cvar.getType(); 71 Method writer = ctype.getMethod("write", 72 new Type[] { Type.string_type }); 73 int segStart = 0; 74 for (int seg = 0; seg < numSegments; seg++) 75 { 76 code.emitLoad(cvar); 77 int segEnd = segStart + (int) segments.charAt(seg); 78 code.emitPushString(str.substring(segStart, segEnd)); 79 code.emitInvoke(writer); 80 segStart = segEnd; 81 } 82 return; 83 } 84 } 85 texp.compile(comp, Target.pushObject); 86 code.emitLoad(cvar); 87 code.emitInvokeStatic(ClassType.make("gnu.xml.TextUtils") 88 .getDeclaredMethod("textValue", 2)); 89 } 90 } 91 | Popular Tags |