KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xml > xqueryevaluator > eval > EvalFilter


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 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.xml.xqueryevaluator.eval;
24
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32
33 import org.xml.sax.*;
34 import org.xquark.mediator.DOMUtils.BufferTuple;
35 import org.xquark.mediator.DOMUtils.EvaluationVisitor;
36 import org.xquark.mediator.plan.DynamicContext;
37 import org.xquark.mediator.runtime.MediatorException;
38 import org.xquark.schema.SchemaManager;
39 import org.xquark.schema.validation.SchemaValidationContext;
40 import org.xquark.schema.validation.ValidatingSchemaFilter;
41 import org.xquark.schema.validation.ValidatingSchemaHandler;
42 import org.xquark.xquery.parser.*;
43 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionDOC;
44
45 /**
46  *
47  */

48 public class EvalFilter {
49
50     // **********************************************************************
51
// * VERSIONING
52
// **********************************************************************
53

54     private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
55     private static final String JavaDoc RCSName = "$Name: $";
56     
57     // **********************************************************************
58
// * CLASS VARIABLES
59
// **********************************************************************
60

61     private static SAXParserFactory JavaDoc saxParserFactory = null;
62     static
63     {
64         // Create a JAXP SAXParserFactory and configure it
65
saxParserFactory = SAXParserFactory.newInstance();
66         saxParserFactory.setNamespaceAware(true);
67         saxParserFactory.setValidating(false);
68     }
69
70     private XMLReader saxReader = null;
71     private InputSource inputSource = null;
72     private XPPReader xppReader = null;
73     private BufferTuple buftuples = null;
74     private EvaluationVisitor evalVisitor = null;
75         
76     // **********************************************************************
77
// * INITIALIZATION
78
// **********************************************************************
79

80     public EvalFilter(BufferTuple buftuples, DynamicContext context, ArrayList JavaDoc paths, FLWRExpression flwr) throws MediatorException {
81         this(buftuples, context, paths, flwr, true);
82     }
83     
84     public EvalFilter(BufferTuple buftuples, DynamicContext context, ArrayList JavaDoc paths, FLWRExpression flwr, boolean useXPP) throws MediatorException {
85         this.buftuples = buftuples;
86         try {
87             SchemaValidationContext svc = new SchemaValidationContext(new SchemaManager(context.getSchemaManager()));
88             SAX2TypedDOM handler = new SAX2TypedDOM(flwr,buftuples,context,paths,svc);
89             Variable var = (Variable)flwr.getVariables().get(0);
90             String JavaDoc doc = null;
91             XQueryExpression arg = null;
92             if (var.getExpression() instanceof LocatedExpression)
93                 arg = ((FunctionDOC)((Step)var.getExpression().getSteps().get(0)).getExpression()).getArgument(0);
94             else if (var.getExpression() instanceof FunctionDOC)
95                 arg = ((FunctionDOC)var.getExpression()).getArgument(0);
96                 
97             if (arg instanceof ValueString)
98                 doc = ((ValueString)arg).getStringValue();
99             else {
100                 if (evalVisitor == null)
101                     evalVisitor = new EvaluationVisitor(context.getSchemaManager(),context.getCurrentTuples());
102                 else
103                     evalVisitor.reset(context.getCurrentTuples());
104                 evalVisitor.setReturnType(EvaluationVisitor.VALUE_TYPE);
105                 try {
106                     arg.accept(evalVisitor);
107                 } catch (XQueryException xqe) {
108                     throw new MediatorException("Incorrect argument of function doc : " + arg.getStringValue());
109                 }
110                 Comparable JavaDoc comp = evalVisitor.getResValue();
111                 if (comp != null)
112                     doc = comp.toString();
113             }
114             if (doc == null)
115                 throw new MediatorException("Incorrect argument of function doc : " + arg.getStringValue());
116             if (doc.startsWith("*:")) doc = doc.substring(2);
117             URL JavaDoc url;
118             if (context.getBaseURI() != null) url = new URL JavaDoc(new URL JavaDoc(context.getBaseURI()),doc);
119             else url = new URL JavaDoc(doc);
120             if (useXPP) {
121                 xppReader = new XPPReader();
122                 ValidatingSchemaHandler filter = new ValidatingSchemaHandler(svc, false);
123                 filter.setContentHandler(handler);
124                 filter.setLexicalHandler(handler);
125                 filter.setErrorHandler(
126                     new ErrorHandler() {
127                         public void warning(SAXParseException ex) throws SAXException {
128                         }
129
130                         public void error(SAXParseException ex) throws SAXException {
131                             throw ex;
132                         }
133
134                         public void fatalError(SAXParseException ex) throws SAXException {
135                             throw ex;
136                         }
137                     });
138                 xppReader.setContentHandler(filter);
139                 xppReader.setLexicalHandler(filter);
140                 xppReader.setErrorHandler(filter);
141                 xppReader.setInput(url.toString(), url.openStream());
142             } else {
143                 XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
144                 ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, svc, false);
145                 filter.setContentHandler(handler);
146                 filter.setLexicalHandler(handler);
147                 filter.setErrorHandler(
148                     new ErrorHandler() {
149                         public void warning(SAXParseException ex) throws SAXException {
150                         }
151
152                         public void error(SAXParseException ex) throws SAXException {
153                             throw ex;
154                         }
155
156                         public void fatalError(SAXParseException ex) throws SAXException {
157                             throw ex;
158                         }
159                     });
160                 saxReader = filter;
161                 inputSource = new InputSource(url.toString());
162             }
163         } catch (SAXException e) {
164             throw new MediatorException(e.getMessage());
165         } catch (MalformedURLException JavaDoc e) {
166             throw new MediatorException(e.getMessage());
167         } catch (IOException JavaDoc e) {
168             throw new MediatorException(e.getMessage());
169         } catch (ParserConfigurationException JavaDoc e) {
170             throw new MediatorException(e.getMessage());
171         }
172     }
173     // **********************************************************************
174
// * ACCESS
175
// **********************************************************************
176

177     public boolean evaluate() throws MediatorException {
178         if (xppReader != null) return evaluateXPP();
179         else return evaluateSAX();
180     }
181
182     public boolean evaluateSAX() throws MediatorException {
183         try {
184             saxReader.parse(inputSource);
185             return true;
186         }
187         catch (IOException JavaDoc e) {
188             throw new MediatorException(e.getMessage());
189         }
190         catch (SAXException e) {
191             throw new MediatorException(e.getMessage());
192         }
193     }
194         
195     public boolean evaluateXPP() throws MediatorException {
196         try {
197             boolean isDone = false;
198             if (xppReader.isAtStart()) xppReader.getContentHandler().startDocument();
199             while (buftuples.isEmpty() && !isDone) {
200                 isDone = xppReader.parseNextElement();
201             }
202             if (isDone) xppReader.getContentHandler().endDocument();
203             return isDone;
204         } catch (IOException JavaDoc e) {
205             throw new MediatorException(e.getMessage());
206         } catch (SAXException e) {
207             throw new MediatorException(e.getMessage());
208         }
209     }
210         
211 }
212
Popular Tags