TreeScanner.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2003 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.kawa.xml;
  4. import gnu.lists.*;
  5. import gnu.mapping.*;
  6. import java.io.*;
  7. /* #ifdef use:java.lang.invoke */
  8. import java.lang.invoke.*;
  9. /* #else */
  10. // import gnu.mapping.CallContext.MethodHandle;
  11. /* #endif */
  12. /** Abstract class that scans part of a node tree.
  13. * Takes a node argument, and writes matching "relative" nodes
  14. * out to a PositionConsumer as a sequence of position pairs.
  15. * This is uses to implement "path expressions" as in XPath/XSLT/XQuery.
  16. * For example, the ChildAxis sub-class writes out all child nodes
  17. * of the argument that match the 'type' NodePredicate.
  18. */
  19. public abstract class TreeScanner extends MethodProc
  20. implements Externalizable
  21. {
  22. public static final MethodHandle applyToConsumerTS =
  23. Procedure.lookupApplyHandle(TreeScanner.class, "applyToConsumerTS");
  24. TreeScanner() {
  25. applyToConsumerMethod = applyToConsumerTS;
  26. setProperty(Procedure.validateApplyKey,
  27. "gnu.kawa.xml.CompileXmlFunctions:validateApplyTreeScanner");
  28. }
  29. public NodePredicate type;
  30. public NodePredicate getNodePredicate () { return type; }
  31. public abstract void scan (AbstractSequence seq, int ipos,
  32. PositionConsumer out);
  33. public int numArgs() { return 0x1001; }
  34. //public void apply (CallContext ctx) throws Throwable
  35. public static Object applyToConsumerTS(Procedure proc, CallContext ctx) throws Throwable {
  36. TreeScanner tproc = (TreeScanner) proc;
  37. PositionConsumer out = (PositionConsumer) ctx.consumer;
  38. Object node = ctx.getNextArg();
  39. ctx.lastArg();
  40. KNode spos;
  41. try {
  42. spos = (KNode) node;
  43. } catch (ClassCastException ex) {
  44. throw new WrongType(tproc.getDesc(), WrongType.ARG_CAST,
  45. node, "node()");
  46. }
  47. tproc.scan(spos.sequence, spos.getPos(), out);
  48. return null;
  49. }
  50. public void writeExternal(ObjectOutput out) throws IOException
  51. {
  52. out.writeObject(type);
  53. }
  54. public void readExternal(ObjectInput in)
  55. throws IOException, ClassNotFoundException
  56. {
  57. type = (NodePredicate) in.readObject();
  58. }
  59. public String getDesc ()
  60. {
  61. String thisName = getClass().getName();
  62. int dot = thisName.lastIndexOf('.');
  63. if (dot > 0)
  64. thisName = thisName.substring(dot+1);
  65. return thisName+"::"+type;
  66. }
  67. public String toString ()
  68. {
  69. return "#<" + getClass().getName() + ' ' + type + '>';
  70. }
  71. }