| 1 package gnu.kawa.xml; 2 import gnu.mapping.*; 3 import gnu.lists.*; 4 5 8 9 public class NodeCompare extends Procedure2 10 { 11 static final int RESULT_GRT = 1; 13 static final int RESULT_EQU = 0; 14 static final int RESULT_LSS = -1; 15 16 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 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 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 apply2 (Object arg1, Object 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 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 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 |