1 package com.icl.saxon.expr; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.Controller; 4 import com.icl.saxon.om.*; 5 6 7 import java.util.Hashtable ; 8 9 12 13 14 public class DistinctEnumeration implements NodeEnumeration { 15 16 private NodeEnumeration p1; 17 private NodeEnumeration e1; 18 private Hashtable lookup = new Hashtable (); 19 private Context context; 20 private Expression expression; 21 private Controller controller; 22 23 NodeInfo nextNode = null; 24 25 29 30 public DistinctEnumeration(NodeEnumeration p1, Controller controller) throws XPathException { 31 this.p1 = p1; 32 this.context = null; 33 this.expression = null; 34 this.controller = controller; 35 e1 = p1; 36 if (!e1.isSorted()) { 37 e1 = (new NodeSetExtent(e1, controller)).sort().enumerate(); 39 } 40 41 43 if (e1.hasMoreElements()) { 44 nextNode = e1.nextElement(); 45 advance(); 46 } 47 } 48 49 public DistinctEnumeration(Context c, NodeEnumeration p1, Expression exp) 50 throws XPathException { 51 this.p1 = p1; 52 this.context = c.newContext(); 53 this.expression = exp; 54 this.controller = c.getController(); 55 e1 = p1; 56 if (!e1.isSorted()) { 57 e1 = (new NodeSetExtent(e1, controller)).sort().enumerate(); 58 } 59 60 62 if (e1.hasMoreElements()) { 63 nextNode = e1.nextElement(); 64 advance(); 65 } 66 } 67 68 public boolean hasMoreElements() { 69 return nextNode!=null; 70 } 71 72 public NodeInfo nextElement() throws XPathException { 73 NodeInfo current = nextNode; 74 advance(); 75 return current; 76 } 77 78 private void advance() throws XPathException { 79 80 while (nextNode != null) { 81 String val; 82 if (expression==null) { 83 val = nextNode.getStringValue(); 84 } else { 85 context.setContextNode(nextNode); 86 context.setPosition(1); 87 context.setLast(1); 88 val = expression.evaluateAsString(context); 89 } 90 if (lookup.get(val)==null) { 91 lookup.put(val, nextNode); 92 return; 93 } else { 94 if (e1.hasMoreElements()) { 95 nextNode = e1.nextElement(); 96 } else { 97 nextNode = null; 98 } 99 } 100 } 101 102 } 103 104 public boolean isSorted() { 105 return true; 106 } 107 108 public boolean isReverseSorted() { 109 return false; 110 } 111 112 public boolean isPeer() { 113 return false; 114 } 115 116 117 } 118 119 | Popular Tags |