Attributes.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.kawa.xml;
  4. import gnu.mapping.*;
  5. import gnu.lists.*;
  6. public class Attributes extends MethodProc
  7. {
  8. public static final Attributes attributes = new Attributes();
  9. private Attributes() {
  10. applyToConsumerMethod =
  11. Procedure.lookupApplyHandle(Attributes.class, "applyToConsumer");
  12. }
  13. public int numArgs() { return 0x1001; }
  14. public static void attributes (TreeList tlist, int index, Consumer consumer)
  15. {
  16. int attr = tlist.gotoAttributesStart(index);
  17. System.out.print("Attributes called, at:"+attr+" "); tlist.dump();
  18. while (attr >= 0)
  19. {
  20. int ipos = attr << 1;
  21. int kind = tlist.getNextKind(ipos);
  22. if (kind != Sequence.ATTRIBUTE_VALUE)
  23. break;
  24. // if kind is CHAR_VALUE return text node. FIXME
  25. int next = tlist.nextDataIndex(attr);
  26. if (consumer instanceof PositionConsumer)
  27. ((PositionConsumer) consumer).writePosition(tlist, ipos);
  28. else
  29. tlist.consumeIRange(attr, next, consumer);
  30. attr = next;
  31. }
  32. }
  33. public static void attributes (Object node, Consumer consumer)
  34. {
  35. if (node instanceof TreeList)
  36. {
  37. attributes((TreeList) node, 0, consumer);
  38. }
  39. else if (node instanceof SeqPosition && ! (node instanceof TreePosition))
  40. {
  41. SeqPosition pos = (SeqPosition) node;
  42. if (pos.sequence instanceof TreeList)
  43. attributes((TreeList) pos.sequence, pos.ipos >> 1, consumer);
  44. }
  45. }
  46. public static Object applyToConsumer(Procedure proc, CallContext ctx) throws Throwable {
  47. Consumer consumer = ctx.consumer;
  48. Object node = ctx.getNextArg();
  49. ctx.lastArg();
  50. if (node instanceof Values)
  51. {
  52. TreeList tlist = (TreeList) node;
  53. int index = 0;
  54. for (;;)
  55. {
  56. int kind = tlist.getNextKind(index << 1);
  57. if (kind == Sequence.EOF_VALUE)
  58. break;
  59. if (kind == Sequence.OBJECT_VALUE)
  60. attributes(tlist.getPosNext(index << 1), consumer);
  61. else
  62. attributes(tlist, index, consumer);
  63. index = tlist.nextDataIndex(index);
  64. }
  65. }
  66. else
  67. attributes(node, consumer);
  68. return null;
  69. }
  70. }