KickJava   Java API By Example, From Geeks To Geeks.

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


1 package gnu.kawa.xml;
2 import gnu.mapping.*;
3 import gnu.lists.*;
4
5 /** Compare nodes for document order.
6  * Implements the XQuery operators '<<', '>>', 'is', 'isnot'.
7  */

8
9 public class NodeCompare extends Procedure2
10 {
11   // Return codes from Numeric.compare:
12
static final int RESULT_GRT = 1;
13   static final int RESULT_EQU = 0;
14   static final int RESULT_LSS = -1;
15
16   // One flag bit for each of the above RESULT_XXX codes:
17
static final int TRUE_IF_GRT = 1 << (RESULT_GRT + 3);
18   static final int TRUE_IF_EQU = 1 << (RESULT_EQU + 3);
19   static final int TRUE_IF_LSS = 1 << (RESULT_LSS + 3);
20   int flags;
21
22   // The funny name of the static fields and methods correspond to
23
// the name mangling scheme defined in Compilation.mangleName.
24
// They can therefor be statically resolved by the compiler.
25

26   public static final NodeCompare $Eq = make("is",TRUE_IF_EQU);
27   public static final NodeCompare $Ne = make("isnot",TRUE_IF_LSS|TRUE_IF_GRT);
28   public static final NodeCompare $Gr = make(">>",TRUE_IF_GRT);
29   public static final NodeCompare $Ls = make("<<",TRUE_IF_LSS);
30
31   public static NodeCompare make(String JavaDoc name, int flags)
32   {
33     NodeCompare proc = new NodeCompare();
34     proc.setName(name);
35     proc.flags = flags;
36     return proc;
37   }
38
39   public Object JavaDoc apply2 (Object JavaDoc arg1, Object JavaDoc arg2)
40   {
41     if (arg1 == null || arg2 == null)
42       return null;
43     if (arg1 == Values.empty)
44       return arg1;
45     if (arg2 == Values.empty)
46       return arg2;
47
48     AbstractSequence seq1, seq2;
49     int ipos1, ipos2;
50     if (arg1 instanceof AbstractSequence)
51       {
52     seq1 = (AbstractSequence) arg1;
53     ipos1 = seq1.startPos();
54       }
55     else
56       {
57     try
58       {
59         SeqPosition spos = (SeqPosition) arg1;
60         seq1 = spos.sequence;
61         ipos1 = spos.getPos();
62       }
63     catch (ClassCastException JavaDoc ex)
64       {
65         throw WrongType.make(ex, this, 1, arg1);
66       }
67       }
68     if (arg2 instanceof AbstractSequence)
69       {
70     seq2 = (AbstractSequence) arg2;
71     ipos2 = seq2.startPos();
72       }
73     else
74       {
75     try
76       {
77         SeqPosition spos = (SeqPosition) arg2;
78         seq2 = spos.sequence;
79         ipos2 = spos.getPos();
80       }
81     catch (ClassCastException JavaDoc ex)
82       {
83         throw WrongType.make(ex, this, 2, arg2);
84       }
85       }
86
87     int comp;
88     if (seq1 == seq2)
89       comp = seq1.compare(ipos1, ipos2);
90     else if (this == $Eq)
91       return Boolean.FALSE;
92     else if (this == $Ne)
93       return Boolean.TRUE;
94     else
95       comp = seq1.stableCompare(seq2);
96     if ((1 << (3 + comp) & flags) != 0)
97       return Boolean.TRUE;
98     else
99       return Boolean.FALSE;
100   }
101 }
102
Popular Tags