KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > runtime > XResultSetUnion


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
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.extractor.runtime;
24
25
26 import java.io.Writer JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.w3c.dom.Document JavaDoc;
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.ErrorHandler JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.ext.LexicalHandler JavaDoc;
34 import org.xquark.schema.validation.PSVInfoSetProvider;
35 import org.xquark.xml.xdbc.*;
36
37 public class XResultSetUnion implements XMLResultSet {
38     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40
41     protected ContentHandler JavaDoc _contentHandler = null;
42     protected LexicalHandler JavaDoc _lexicalhandler = null;
43     protected ErrorHandler JavaDoc _errorhandler = null;
44
45     private List JavaDoc _cExprList = null;
46     private XMLStatementInternal _statement = null;
47
48     private int _currentQueryIndex = 0;
49     private XMLResultSet _currenResultSet = null;
50
51     private int _positionAccumulater = 0;
52
53     public XResultSetUnion(List JavaDoc compiledExprList, XMLStatementInternal statement) throws XMLDBCException{
54         setExpressionList(compiledExprList);
55         setStatement(statement);
56     }
57
58     public void setExpressionList(List JavaDoc compiledExprList) {
59         _cExprList = compiledExprList;
60     }
61
62     public void setStatement(XMLStatementInternal statement) {
63         _statement = statement;
64     }
65
66     public XMLStatement getStatement() {
67         return _statement;
68     }
69
70     protected XMLResultSet evaluate(int queryIndex) throws XMLDBCException {
71         return _statement.runQuery((Query.CompiledExpression)_cExprList.get(queryIndex));
72     }
73     /**********************************************************************************************/
74
75     /**
76      * @see XMLResultSet#setContentHandler
77      */

78     public void setContentHandler(ContentHandler JavaDoc contentHandler) {
79         if (contentHandler == null)
80             throw new NullPointerException JavaDoc("ContentHandler cannot be null.");
81         _contentHandler = contentHandler;
82     }
83
84     /**
85      * @see ResultSet#setLexicalHandler
86      */

87     public void setLexicalHandler(LexicalHandler JavaDoc lexicalhandler) {
88         if (lexicalhandler == null)
89             throw new NullPointerException JavaDoc("LexicalHandler cannot be null.");
90         _lexicalhandler = lexicalhandler;
91     }
92
93     /**
94      * @see ResultSet#setErrorHandler
95      */

96     public void setErrorHandler(ErrorHandler JavaDoc errorhandler) {
97         //org.xquark.xml.misc.Debug.print(this, "START - XResultSetImpl setErrorHandler");
98
if (errorhandler == null)
99             throw new NullPointerException JavaDoc("ErrorHandler cannot be null.");
100         _errorhandler = errorhandler;
101     }
102
103     /**
104      * @see ResultSet#setErrorHandler
105      */

106     public ContentHandler JavaDoc getContentHandler() {
107         return _contentHandler;
108     }
109
110     /**
111      * @see ResultSet#setErrorHandler
112      */

113     public LexicalHandler JavaDoc getLexicalHandler() {
114         return _lexicalhandler;
115     }
116
117     /**
118      * @see ResultSet#setErrorHandler
119      */

120     public ErrorHandler JavaDoc getErrorHandler() {
121         return _errorhandler;
122     }
123
124     private void setHandlers(XMLResultSet resultSet) {
125         if (null !=_contentHandler ) resultSet.setContentHandler(_contentHandler);
126         if (null !=_lexicalhandler ) resultSet.setLexicalHandler(_lexicalhandler);
127         if (null !=_errorhandler ) resultSet.setErrorHandler(_errorhandler);
128     }
129     /**********************************************************************************************/
130
131     /**
132      * @see XMLResultSet#close
133      */

134     public void close() throws XMLDBCException {
135         if (_currenResultSet != null) {
136             _currenResultSet.close();
137             _currenResultSet = null;
138         }
139     }
140
141     /**********************************************************************************************/
142
143     /**
144      * @see XMLResultSet#isBeforeFirst
145      */

146     public boolean isBeforeFirst() throws XMLDBCException {
147         return 0 == _positionAccumulater + _currenResultSet.getPosition() ? true : false;
148     }
149
150     /**
151      * @see XMLResultSet#nextAsDOM
152      */

153     public Document JavaDoc nextAsDocument() throws XMLDBCException {
154         return _currenResultSet.nextAsDocument();
155     }
156
157
158     public org.w3c.dom.Node JavaDoc nextAsDOM() throws XMLDBCException {
159         return _currenResultSet.nextAsDOM();
160     }
161
162     public void nextAsDOM(org.w3c.dom.Element JavaDoc parent) throws XMLDBCException {
163         _currenResultSet.nextAsDOM(parent);
164     }
165
166     /**
167      * @see XMLResultSet#nextAsString
168      */

169     public String JavaDoc nextAsString() throws XMLDBCException {
170         return _currenResultSet.nextAsString();
171     }
172
173     public void nextAsStream(Writer JavaDoc out) throws XMLDBCException {
174         _currenResultSet.nextAsStream(out);
175     }
176
177     /**
178      * @see XMLResultSet#nextAsSAX
179      */

180     public void nextAsSAX() throws XMLDBCException, SAXException JavaDoc {
181         _currenResultSet.nextAsSAX();
182     }
183
184     /**
185      * @see XMLResultSet#hasNext
186      */

187     public boolean hasNext() throws XMLDBCException {
188         boolean retVal = false;
189
190         if (null == _currenResultSet) {
191             _currenResultSet = evaluate(_currentQueryIndex);
192             setHandlers(_currenResultSet);
193         }
194         retVal = _currenResultSet.hasNext();
195         if (!retVal) {
196             int position = _currenResultSet.getPosition();
197             _currenResultSet.close();
198             if (_currentQueryIndex < _cExprList.size() - 1 ) {
199                 do {
200                     _currentQueryIndex++;
201                     _currenResultSet = evaluate(_currentQueryIndex);
202                 }
203                 while ((!_currenResultSet.hasNext()) && (_currentQueryIndex < _cExprList.size() - 1));
204                 retVal = _currenResultSet.hasNext();
205                 if (retVal)
206                     setHandlers(_currenResultSet);
207                 
208                 _positionAccumulater += position;
209             }
210         }
211         return retVal;
212     }
213
214     /**
215      * @see XMLResultSet#getPosition
216      */

217     public int getPosition() throws XMLDBCException {
218         int retVal = _currenResultSet.getPosition();
219         retVal = -1 == retVal ? 0 : retVal;
220         retVal = _positionAccumulater + retVal;
221         retVal = 0 == retVal ? 1 : retVal;
222         return retVal;
223     }
224
225     /**
226      * Returns the result set prefix map (as prefix-namespace pairs).
227      * This map is used to produce each result in the set.
228      * @return the result set prefix map.
229      * @throws XMLDBCException if a data source access error occurs.
230      * @deprecated
231      */

232     public java.util.Map JavaDoc getPrefixMap() throws XMLDBCException {
233         return java.util.Collections.EMPTY_MAP;
234     }
235
236     /**
237      * Returns the fragments contained in the result set as a single XML document, with
238      * a document root element having the specified namespace, local name and qualified name.
239      * Note: this convenience method can only be called if the cursor is before the
240      * first element. It will process the complete result set in one step.
241      * @param namespace the namespace of the document root element.
242      * @param localName the local name of the document root element.
243      * @param qName the qualified name of the document root element.
244      * @return an XML document containing the fragments of the result set, as children of
245      * the specified document root element.
246      * @throws XMLDBCException if a data source access error occurs.
247      */

248     public XMLDocument getFragmentsAsDocument(String JavaDoc namespace, String JavaDoc localName, String JavaDoc qName) throws XMLDBCException {
249         XMLDocument retVal = null;
250         return retVal;
251     }
252
253     /**
254      * Returns the result set metadata (the XML Schema that models the returned data)
255      * as an XMLDocument.
256      * This schema describes the XML type of each result in the set.
257      * @return the result set metadata (a XML Schema).
258      * @throws XMLDBCException if a data source access error occurs.
259      */

260     public XMLDocument getMetaData() throws XMLDBCException {
261         return null;
262     }
263     public boolean hasRootTag() {
264         return false;
265     }
266     
267     public boolean isDocument() {
268         return false;
269     }
270
271     public PSVInfoSetProvider getPSVInfoSetProvider() {
272         return null;
273     }
274
275 }
276
Popular Tags