1 22 23 package org.xquark.mediator.plan; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.mediator.DOMUtils.DOMUtils; 28 import org.xquark.mediator.DOMUtils.EvaluationVisitor; 29 import org.xquark.mediator.DOMUtils.Tuple; 30 import org.xquark.mediator.runtime.MediatorStatement; 31 import org.xquark.mediator.runtime.PreparedMediatorStatement; 32 import org.xquark.mediator.runtime._MediatorConnection; 33 import org.xquark.schema.SchemaManager; 34 import org.xquark.xml.xdbc.PreparedXMLStatement; 35 import org.xquark.xml.xdbc.XMLDBCException; 36 import org.xquark.xpath.datamodel.TypedNode; 37 import org.xquark.xquery.parser.Variable; 38 import org.xquark.xquery.parser.XQueryException; 39 40 public class DynamicContext { 41 42 private MediatorStatement statement = null; 43 private ArrayList currentTuples = new ArrayList (); 44 private ExecutionPlan plan = null; 45 private EvaluationVisitor evalVisitor = null; 46 47 49 public DynamicContext(ExecutionPlan plan, MediatorStatement statement) { 50 this.statement = statement; 51 this.plan = plan; 52 this.evalVisitor = new EvaluationVisitor(plan.getSchemaManager()); 53 } 54 55 public _MediatorConnection getConnection() { 56 return statement.getMediatorConnection(); 57 } 58 public void addCurrentTuple(Tuple tuple) { 59 this.currentTuples.add(0, tuple); 60 } 61 62 public void deleteCurrentTuple(Tuple tuple) { 63 this.currentTuples.remove(tuple); 64 } 65 66 public ArrayList getCurrentTuples() { 67 return this.currentTuples; 68 } 69 70 public SchemaManager getSchemaManager() { 71 return plan.getTypeVisitor().getSchemaManager(); 72 } 73 74 public String getBaseURI() { 75 return statement.getBaseURI(); 76 } 77 78 82 public PreparedXMLStatement getPreparedXMLStatement(OpSource opSource, String sourceName, String requestStr) throws XMLDBCException { 83 return statement.getPreparedXMLStatement(opSource, sourceName, requestStr); 84 } 85 86 public Object getVariableValue(Variable var) throws XQueryException { 87 Object strvalue = null; 88 if (var.getExpression() == null) { 89 90 return ((PreparedMediatorStatement) statement).getVariables().get(var.getStringValue()); 91 } 92 for (int j = 0; j < currentTuples.size(); j++) { 93 evalVisitor.reset((Tuple) currentTuples.get(j)); 94 95 var.getExpression().accept(evalVisitor); 96 ArrayList tmplist = evalVisitor.getResNodes(); 97 if (tmplist != null && !tmplist.isEmpty()) { 98 if (tmplist.size() == 1) { 99 TypedNode tmpnode = (TypedNode) tmplist.get(0); 100 strvalue = tmpnode.getTypedValue(); 101 if (strvalue == null) { 102 strvalue = DOMUtils.getTextLeaf(tmpnode); 103 } 104 } else { 105 ArrayList seqlist = new ArrayList (); 106 for (int k = 0; k < tmplist.size(); k++) { 107 TypedNode tmpnodek = (TypedNode) tmplist.get(k); 108 strvalue = tmpnodek.getTypedValue(); 109 if (strvalue == null) { 110 strvalue = DOMUtils.getTextLeaf(tmpnodek); 111 } 112 if (strvalue != null) 113 seqlist.add(strvalue); 114 } 115 if (!seqlist.isEmpty()) 116 strvalue = seqlist; 117 else 118 strvalue = null; 119 } 120 break; 121 } 122 } 123 return strvalue; 124 } 125 126 public Object clone() { 127 DynamicContext cloned = new DynamicContext(this.plan,this.statement); 128 cloned.currentTuples = (ArrayList )currentTuples.clone(); 129 return cloned; 130 } 131 132 } 133 | Popular Tags |