KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > DOMUtils > GetUnknownPathsVisitor


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

23
24 package org.xquark.mediator.DOMUtils;
25
26 import java.util.ArrayList JavaDoc;
27
28 import org.xquark.schema.SchemaManager;
29 import org.xquark.xquery.parser.*;
30 import org.xquark.xquery.parser.util.Constants;
31 import org.xquark.xquery.typing.TypeVisitor;
32
33
34 public class GetUnknownPathsVisitor extends DefaultParserVisitor {
35     // **********************************************************************
36
// * VERSIONING
37
// **********************************************************************
38
private static final String JavaDoc RCSRevision = "$Revision: 1.11 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40     // **********************************************************************
41
// * CLASS VARIABLES
42
// **********************************************************************
43
private XQueryExpression resultExpr = null;
44     private SchemaManager schemamanager = null;
45     private TypeVisitor typevisitor = null;
46     private ArrayList JavaDoc newVars = new ArrayList JavaDoc();
47     private ArrayList JavaDoc knownVars = new ArrayList JavaDoc();
48     // **********************************************************************
49
// * INITIALIZATION
50
// **********************************************************************
51
/**
52      *
53      */

54     public GetUnknownPathsVisitor(SchemaManager schemamanager, TypeVisitor typevisitor) {
55         this.schemamanager = schemamanager;
56         this.typevisitor = typevisitor;
57     }
58
59     public ArrayList JavaDoc getNewVariables() {
60         if (newVars.isEmpty())
61             return null;
62         return newVars;
63     }
64     
65     // **********************************************************************
66
// * VISITATION
67
// **********************************************************************
68
public void visit(XQueryExpression arg) throws XQueryException {
69         resultExpr = null;
70     }
71
72     public void visit(FunctionCall arg) throws XQueryException {
73         ArrayList JavaDoc arguments = arg.getArguments();
74         if (arguments != null) {
75             for (int i = 0; i < arguments.size(); i++) {
76                 XQueryExpression argi = (XQueryExpression) arguments.get(i);
77                 argi.accept(this);
78                 if (resultExpr != null)
79                     arguments.set(i, resultExpr);
80             }
81         }
82         resultExpr = null;
83     }
84
85     public void visit(FLWRExpression arg) throws XQueryException {
86         // add all vars to those known
87
knownVars.addAll(arg.getVariables());
88         for (int i = 0; i < arg.getVariables().size(); i++) {
89             Variable vari = (Variable) arg.getVariables().get(i);
90             vari.getExpression().accept(this);
91             if (resultExpr != null)
92                 vari.setExpression(resultExpr, false);
93         }
94         // get where clause
95
if (arg.getWhereClause() != null) {
96             arg.getWhereClause().accept(this);
97             if (resultExpr != null)
98                 arg.setWhereClause(resultExpr);
99         }
100 // arg.getReturnClause().accept(this);
101
// if (resultExpr != null)
102
// arg.setReturnClause(resultExpr);
103
// resultExpr = null;
104
// retrieve all vars to those known
105
knownVars.removeAll(arg.getVariables());
106     }
107
108     public void visit(QuantifiedExpression arg) throws XQueryException {
109         // add varibales to those known
110
knownVars.addAll(arg.getVariables());
111         for (int i = 0; i < arg.getVariables().size(); i++) {
112             Variable vari = (Variable) arg.getVariables().get(i);
113             vari.getExpression().accept(this);
114             if (resultExpr != null)
115                 vari.setExpression(resultExpr, false);
116         }
117         arg.getConstraintExpresson().accept(this);
118         if (resultExpr != null)
119             arg.setConstraintExpresson(resultExpr);
120         // retrieve all vars to those known
121
knownVars.removeAll(arg.getVariables());
122     }
123     
124     public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException {
125         arg.getExpression1().accept(this);
126         if (resultExpr != null)
127             arg.setExpression1(resultExpr);
128         arg.getExpression2().accept(this);
129         if (resultExpr != null)
130             arg.setExpression2(resultExpr);
131         resultExpr = null;
132     }
133
134     public void visit(LocatedExpression arg) throws XQueryException {
135         resultExpr = null;
136         if (arg.getStepNum(0).getExpression() instanceof Variable) {
137             Variable var = (Variable)arg.getStepNum(0).getExpression();
138             if (!knownVars.contains(var)) {
139                 resultExpr = typevisitor.getStaticContext().createVariable(Constants.DECLARATION_BINDINGTYPE, arg, Variable.CAN_CREATOR);
140                 newVars.add(resultExpr);
141             }
142         }
143     }
144
145     public void visit(Variable arg) throws XQueryException {
146         resultExpr = null;
147         if (!knownVars.contains(arg)) {
148             resultExpr = typevisitor.getStaticContext().createVariable(Constants.DECLARATION_BINDINGTYPE, arg, Variable.CAN_CREATOR);
149             newVars.add(resultExpr);
150         }
151     }
152
153     public void visit(ExternalVariable arg) throws XQueryException {
154         resultExpr = null;
155         if (!knownVars.contains(arg)) {
156             resultExpr = typevisitor.getStaticContext().createVariable(Constants.DECLARATION_BINDINGTYPE, arg, Variable.CAN_CREATOR);
157             newVars.add(resultExpr);
158         }
159     }
160
161 }
162
Popular Tags