KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > plan > DynamicContext


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2004 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mediator.plan;
24
25 import java.util.ArrayList JavaDoc;
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 JavaDoc currentTuples = new ArrayList JavaDoc();
44     private ExecutionPlan plan = null;
45     private EvaluationVisitor evalVisitor = null;
46
47     // other element can be added later like TimeZone, currentDate ...
48

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 JavaDoc getCurrentTuples() {
67         return this.currentTuples;
68     }
69
70     public SchemaManager getSchemaManager() {
71         return plan.getTypeVisitor().getSchemaManager();
72     }
73
74     public String JavaDoc getBaseURI() {
75         return statement.getBaseURI();
76     }
77
78 // public boolean hasPreparedXMLStatement(OpSource opSource) {
79
// return statement.hasPreparedXMLStatement(opSource);
80
// }
81

82     public PreparedXMLStatement getPreparedXMLStatement(OpSource opSource, String JavaDoc sourceName, String JavaDoc requestStr) throws XMLDBCException {
83         return statement.getPreparedXMLStatement(opSource, sourceName, requestStr);
84     }
85
86     public Object JavaDoc getVariableValue(Variable var) throws XQueryException {
87         Object JavaDoc 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 JavaDoc 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 JavaDoc seqlist = new ArrayList JavaDoc();
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 JavaDoc clone() {
127         DynamicContext cloned = new DynamicContext(this.plan,this.statement);
128         cloned.currentTuples = (ArrayList JavaDoc)currentTuples.clone();
129         return cloned;
130     }
131
132 }
133
Popular Tags