1 22 23 package org.xquark.mediator.DOMUtils; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.mediator.algebra.AlgFLWR; 28 import org.xquark.mediator.algebra.Algebra; 29 import org.xquark.schema.SchemaManager; 30 import org.xquark.xpath.datamodel.TypedNode; 31 import org.xquark.xquery.parser.*; 32 import org.xquark.xquery.typing.*; 33 34 35 public class FillExprWithTupleVisitor extends DefaultParserVisitor { 36 private static final String RCSRevision = "$Revision: 1.10 $"; 40 private static final String RCSName = "$Name: $"; 41 private ArrayList tuples = null; 46 private XQueryExpression resultExpr = null; 47 private SchemaManager schemamanager = null; 48 private TypeVisitor typevisitor = null; 49 55 public FillExprWithTupleVisitor(SchemaManager schemamanager, TypeVisitor typevisitor, Tuple tuple) { 56 this.tuples = new ArrayList (1); 57 tuples.add(tuple); 58 this.schemamanager = schemamanager; 59 this.typevisitor = typevisitor; 60 } 61 62 public FillExprWithTupleVisitor(SchemaManager schemamanager, TypeVisitor typevisitor, ArrayList tuples) { 63 this.tuples = tuples; 64 this.schemamanager = schemamanager; 65 this.typevisitor = typevisitor; 66 } 67 68 public XQueryExpression getResultExpr() { return resultExpr; } 69 70 75 public void visit(Algebra arg) throws XQueryException { 76 arg.getExpression().accept(this); 77 if (resultExpr != null) 78 arg.setExpression(resultExpr); 79 ArrayList children = arg.getChildren(); 80 if (children != null) 81 for (int i = 0; i < children.size(); i++) 82 visit((Algebra) children.get(i)); 83 if (arg.isQDB()) { 84 AlgFLWR tmpdepmem = (AlgFLWR) arg; 85 if (tmpdepmem.getDependencyList() != null) 86 for (int i = 0; i < tmpdepmem.getDependencyList().size(); i++) 87 visit((AlgFLWR) tmpdepmem.getDependencyList().get(i)); 88 } 89 arg.getVarsDependingOn().clear(); 90 resultExpr = null; 91 } 92 93 public void visit(XQueryExpression arg) throws XQueryException { 97 resultExpr = null; 98 } 99 100 public void visit(FunctionCall arg) throws XQueryException { 101 ArrayList arguments = arg.getArguments(); 102 if (arguments != null) { 103 for (int i = 0; i < arguments.size(); i++) { 104 XQueryExpression argi = (XQueryExpression) arguments.get(i); 105 argi.accept(this); 106 if (resultExpr != null) 107 arguments.set(i, resultExpr); 108 } 109 } 110 resultExpr = null; 111 } 112 113 public void visit(FLWRExpression arg) throws XQueryException { 114 for (int i = 0; i < arg.getVariables().size(); i++) { 115 Variable vari = (Variable) arg.getVariables().get(i); 116 vari.getExpression().accept(this); 117 if (resultExpr != null) 118 vari.setExpression(resultExpr,false); 119 } 120 if (arg.getWhereClause() != null) { 121 arg.getWhereClause().accept(this); 122 if (resultExpr != null) 123 arg.setWhereClause(resultExpr); 124 } 125 resultExpr = null; 129 } 130 131 public void visit(QuantifiedExpression arg) throws XQueryException { 132 for (int i = 0; i < arg.getVariables().size(); i++) { 133 Variable vari = (Variable) arg.getVariables().get(i); 134 vari.getExpression().accept(this); 135 if (resultExpr != null) 136 vari.setExpression(resultExpr,false); 137 } 138 arg.getConstraintExpresson().accept(this); 139 if (resultExpr != null) 140 arg.setConstraintExpresson(resultExpr); 141 resultExpr = null; 142 } 143 144 public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException { 145 arg.getExpression1().accept(this); 146 if (resultExpr != null) 147 arg.setExpression1(resultExpr); 148 arg.getExpression2().accept(this); 149 if (resultExpr != null) 150 arg.setExpression2(resultExpr); 151 resultExpr = null; 152 } 153 154 public void visit(LocatedExpression arg) throws XQueryException { 155 resultExpr = null; 156 XQueryExpression step0expr = arg.getExpression(); 157 if (step0expr instanceof Variable) { 158 159 EvaluationVisitor visitor = null; 160 for (int j = 0; j < tuples.size(); j++) { 161 162 if (j == 0) 163 visitor = new EvaluationVisitor(this.schemamanager, (Tuple) tuples.get(j)); 164 else 165 visitor.reset((Tuple) tuples.get(j)); 166 167 arg.accept(visitor); 168 ArrayList tmplist = visitor.getResNodes(); 169 if (tmplist != null && !tmplist.isEmpty()) { 170 if (tmplist.size() == 1) { 171 TypedNode tmpnode = (TypedNode) tmplist.get(0); 172 Object strvalue = tmpnode.getTypedValue(); 173 if (strvalue == null) { 174 strvalue = DOMUtils.getTextLeaf(tmpnode); 175 } 176 if (strvalue == null) 177 throw new XQueryException("Unexpected value encountered while replacing variable"); 178 resultExpr = DOMUtils.getXQueryExpression(strvalue); 179 if (resultExpr == null) 180 throw new XQueryException("Unexpected type encountered while replacing variable"); 181 } else { 182 ArrayList seqlist = new ArrayList (); 183 for (int i = 0; i < tmplist.size(); i++) { 184 TypedNode tmpnode = (TypedNode) tmplist.get(i); 185 Object strvalue = tmpnode.getTypedValue(); 186 if (strvalue == null) { 187 strvalue = DOMUtils.getTextLeaf(tmpnode); 188 } 189 if (strvalue == null) 190 throw new XQueryException("Unexpected value encountered while replacing variable"); 191 XQueryExpression tmpExpr = DOMUtils.getXQueryExpression(strvalue); 192 if (tmpExpr == null) 193 throw new XQueryException("Unexpected type encountered while replacing variable"); 194 seqlist.add(tmpExpr); 195 } 196 resultExpr = new XQueryExpressionSequence(seqlist, arg.getParentModule()); 197 resultExpr.setParenthesis(true); 198 } 199 break; 200 } 201 } 202 } 203 } 204 205 public void visit(Variable arg) throws XQueryException { 206 for (int j = 0; j < tuples.size(); j++) { 207 ArrayList tmplist = ((Tuple) tuples.get(j)).getPath(arg.getStringValue()); 208 if (tmplist != null) { 209 if (tmplist.size() == 1) { 210 TypedNode tmpnode = (TypedNode) tmplist.get(0); 211 212 Object strvalue = tmpnode.getTypedValue(); 213 if (strvalue == null) { 214 strvalue = DOMUtils.getTextLeaf(tmpnode); 215 } 216 if (strvalue == null) 217 throw new XQueryException("Unexpected value encountered while replacing variable"); 218 resultExpr = DOMUtils.getXQueryExpression(strvalue); 219 225 if (resultExpr == null) 226 throw new XQueryException("Unexpected type encountered while replacing variable"); 227 } else { 228 throw new XQueryException("Replacing variable by more than one value -> not implemented yet"); 229 } 230 break; 231 } 232 } 233 } 234 235 } 236 | Popular Tags |