1 61 62 63 64 package org.jaxen.expr; 65 66 import java.util.ArrayList ; 67 import java.util.Collections ; 68 import java.util.HashSet ; 69 import java.util.Iterator ; 70 import java.util.List ; 71 import java.util.Set ; 72 import org.jaxen.Context; 73 import org.jaxen.JaxenException; 74 import org.jaxen.XPathSyntaxException; 75 76 public class DefaultUnionExpr extends DefaultBinaryExpr implements UnionExpr 77 { 78 public DefaultUnionExpr(Expr lhs, 79 Expr rhs) 80 { 81 super( lhs, 82 rhs ); 83 } 84 85 public String getOperator() 86 { 87 return "|"; 88 } 89 90 public String toString() 91 { 92 return "[(DefaultUnionExpr): " + getLHS() + ", " + getRHS() + "]"; 93 } 94 95 public Object evaluate(Context context) throws JaxenException 96 { 97 List results = new ArrayList (); 98 99 try { 100 List lhsResults = (List ) getLHS().evaluate( context ); 101 List rhsResults = (List ) getRHS().evaluate( context ); 102 103 Set unique = new HashSet (); 104 105 results.addAll( lhsResults ); 106 unique.addAll( lhsResults ); 107 108 Iterator rhsIter = rhsResults.iterator(); 109 110 while ( rhsIter.hasNext() ) 111 { 112 Object each = rhsIter.next(); 113 114 if ( ! unique.contains( each ) ) 115 { 116 results.add( each ); 117 unique.add( each ); 118 } 119 } 120 121 Collections.sort(results, new NodeComparator(context.getNavigator())); 122 123 return results; 124 } 125 catch (ClassCastException e) { 126 throw new XPathSyntaxException(this.getText(), context.getPosition(), "Unions are only allowed over node-sets"); 127 } 128 } 129 130 public void accept(Visitor visitor) 131 { 132 visitor.visit(this); 133 } 134 135 } 136 137 | Popular Tags |