1 22 23 package org.xquark.mediator.plan.primitivefunctions; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.mediator.DOMUtils.EvaluationVisitor; 28 import org.xquark.mediator.DOMUtils.Tuple; 29 import org.xquark.mediator.plan.*; 30 import org.xquark.mediator.runtime.MediatorException; 31 import org.xquark.xpath.datamodel.TypedNode; 32 import org.xquark.xquery.parser.*; 33 34 public class OpFuncDISTINCT_VALUES extends OpUn { 35 private static final String RCSRevision = "$Revision: 1.10 $"; 39 private static final String RCSName = "$Name: $"; 40 46 public OpFuncDISTINCT_VALUES(ExecutionPlan plan, XQueryExpression expression, Operator childexp) throws MediatorException { 47 super(plan, expression, childexp); 48 if (paths != null) 50 paths.clear(); 51 if (paths == null) 53 paths = new ArrayList (); 54 getRetPath(((FunctionCall) expression).getArgument(0), paths); 55 if (paths.isEmpty() || paths.size() > 1) 56 throw new MediatorException("Could not find paths for distinct-values function"); 57 size = 1; 59 idsize = 0; 60 } 61 62 private void getRetPath(XQueryExpression expr, ArrayList reslist) { 64 if (expr instanceof XQueryExpressionSequence) { 65 for (int i = 0; i < ((XQueryExpressionSequence) expr).getSubExpressions().size(); i++) { 66 getRetPath((XQueryExpression) ((XQueryExpressionSequence) expr).getSubExpressions().get(i), reslist); 67 } 68 } 69 if (expr instanceof LocatedExpression) { 70 reslist.add(expr); 71 } else if (expr instanceof Variable) { 72 reslist.add(expr); 73 } else if (expr instanceof FLWRExpression) { 74 getRetPath(((FLWRExpression) expr).getReturnClause(), reslist); 75 } else if (expr instanceof FunctionCall) { 76 getRetPath(((FunctionCall) expr).getArgument(0), reslist); 77 } 78 } 79 85 protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException { 86 return new FuncDISTINCT_VALUESResultSet(this, context, child); 87 } 88 89 98 100 106 } 107 108 class FuncDISTINCT_VALUESResultSet extends UnResultSet { 109 private static final String RCSRevision = "$Revision: 1.10 $"; 113 private static final String RCSName = "$Name: $"; 114 118 public FuncDISTINCT_VALUESResultSet(OpFuncDISTINCT_VALUES operator, DynamicContext context, OperatorRunnable child) throws MediatorException { 122 super(operator, context, child); 123 } 124 125 136 protected void evaluate(boolean non_blocking) throws MediatorException { 137 138 140 if (resultset == null) { 142 try { 143 ArrayList tuples = context.getCurrentTuples(); 144 if (tuples != null && !tuples.isEmpty()) { 145 EvaluationVisitor visitor = new EvaluationVisitor(operator.getPlan().getSchemaManager()); 146 visitor.reset(tuples); 147 visitor.setReturnType(EvaluationVisitor.NODE_TYPE); 148 if (expression != null) { 149 expression.accept(visitor); 150 ArrayList nodes = visitor.getResNodes(); 151 if (nodes != null) { 152 for (int i = 0; i < nodes.size(); i++) { 153 Tuple newtuple = buftuples.newTuple(); 154 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i)); 155 newtuple.fillIdentifiers(); 157 buftuples.add(newtuple); 158 } 159 } 160 } 161 } 162 setFinishedWhenEmpty(); 163 return; 164 } catch (XQueryException xe) { 165 throw new MediatorException(xe.getMessage(), xe); 166 } 167 } 168 169 if (!resultset.hasNext()) { 171 resultset.close(); 172 setFinishedWhenEmpty(); 173 return; 174 } 175 176 if (!resultset.hasNext()) { 177 } else { 178 String path = ((XQueryExpression) operator.getPaths().get(0)).getStringValue(); 179 ArrayList tmpNodeList = new ArrayList (); 180 ArrayList tmpValueList = new ArrayList (); 181 while (resultset.hasNext()) { 183 Tuple tuple = resultset.next(); 184 int index = tuple.getParent().getPathIndex(path); 185 if (index == -1) 186 index = 0; 187 ArrayList list = tuple.getNodesAtIndex(index); 188 if (list == null || list.size() != 1) 189 continue; 190 191 TypedNode node = (TypedNode) list.get(0); 192 if (node.getTypedValue() == null) { 193 if (node.getChildNodes().getLength() == 1) 194 node = (TypedNode) node.getFirstChild(); 195 else 196 node = null; 197 } 198 if (node != null && node.getTypedValue() != null) { 199 if (!tmpValueList.contains(node.getTypedValue())) { 200 tmpValueList.add(node.getTypedValue()); 201 tmpNodeList.add(node); 202 } 203 } 204 } 205 for (int i = 0; i < tmpNodeList.size(); i++) { 206 Tuple newtuple = buftuples.newTuple(); 207 newtuple.addNodeAtIndex(0, (TypedNode) tmpNodeList.get(i)); 208 newtuple.fillIdentifiers(); 209 buftuples.add(newtuple); 210 } 211 } 212 resultset.close(); 213 setFinishedWhenEmpty(); 214 } 215 } 216 | Popular Tags |