KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xml > TreeScanner


1 // Copyright (c) 2003 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.xml;
5 import gnu.bytecode.Type;
6 import gnu.lists.*;
7 import gnu.mapping.*;
8 import gnu.expr.*;
9 import java.io.*;
10
11 /** Abstract class that scans part of a node tree.
12  * Takes a node argument, and writes matching "relative" nodes
13  * out to a PositionConsumer as a sequence of position pairs.
14  * This is uses to implement "path expressions" as in XPath/XSLT/XQuery.
15  * For example, the ChildAxis sub-class writes out all child nodes
16  * of the argument that match the 'type' NodePredicate.
17  */

18
19 public abstract class TreeScanner extends MethodProc
20   implements Externalizable, CanInline
21 {
22   public NodePredicate type;
23
24   public NodePredicate getNodePredicate () { return type; }
25
26   public abstract void scan (AbstractSequence seq, int ipos,
27                  PositionConsumer out);
28
29   public int numArgs() { return 0x1001; }
30
31   public void apply (CallContext ctx) throws Throwable JavaDoc
32   {
33     PositionConsumer out = (PositionConsumer) ctx.consumer;
34     Object JavaDoc node = ctx.getNextArg();
35     ctx.lastArg();
36     KNode spos = (KNode) node;
37     scan(spos.sequence, spos.getPos(), out);
38   }
39
40   public void writeExternal(ObjectOutput out) throws IOException
41   {
42     out.writeObject(type);
43   }
44
45   public void readExternal(ObjectInput in)
46     throws IOException, ClassNotFoundException JavaDoc
47   {
48     type = (NodePredicate) in.readObject();
49   }
50
51   public String JavaDoc toString ()
52   {
53     return "#<" + getClass().getName() + ' ' + type + '>';
54   }
55
56   public Expression inline (ApplyExp exp, ExpWalker walker)
57   {
58     if (exp.getTypeRaw() == null && type instanceof Type)
59       exp.setType(NodeSetType.getInstance((Type) type));
60     return exp;
61   }
62 }
63
Popular Tags