KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.List JavaDoc;
26
27 import org.xquark.schema.validation.PSVInfoSetProvider;
28 import org.xquark.xml.xdbc.XMLDBCException;
29 import org.xquark.xquery.xdbc.XDBCResultSetInterface;
30
31 /**
32  * This implementation of the interface is used to concatenate results when
33  * multiple FLWOR are side-by-side in a return clause.
34  */

35 public class UnionResultSet implements XDBCResultSetInterface {
36
37     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
38     private static final String JavaDoc RCSName = "$Name: $";
39
40     private List JavaDoc _sRSList = null;
41     private int _sRSNum = 0;
42     private SynchronizedResultSet _cRS = null;
43     private int _cRSIndex = -1;
44
45     public UnionResultSet() {
46     }
47
48     public UnionResultSet(List JavaDoc sRSList) {
49         setSubResultSetList(sRSList);
50     }
51
52     public void setSubResultSetList(List JavaDoc sRSList) {
53         _sRSList = sRSList;
54         _sRSNum = _sRSList.size();
55         _cRSIndex = 0;
56         _cRS = (SynchronizedResultSet)_sRSList.get(_cRSIndex);
57     }
58
59     public String JavaDoc fetch(String JavaDoc path,int nodeAccessor,String JavaDoc loopID) throws XMLDBCException {
60         return fetch(path,nodeAccessor);
61     }
62     public String JavaDoc fetch(String JavaDoc path,int nodeAccessor) throws XMLDBCException {
63         return fetch(path,nodeAccessor, (PSVInfoSetProvider)null);
64     }
65     public String JavaDoc fetch(String JavaDoc path,int nodeAccessor,String JavaDoc loopID, PSVInfoSetProvider psvisp) throws XMLDBCException {
66         return fetch(path,nodeAccessor,psvisp);
67     }
68     public String JavaDoc fetch(String JavaDoc path,int nodeAccessor, PSVInfoSetProvider psvisp) throws XMLDBCException {
69         return _cRS.fetch(path, nodeAccessor, psvisp);
70     }
71
72     public void generate(String JavaDoc path,int nodeAccessor,String JavaDoc loopID) throws XMLDBCException{
73         generate(path, nodeAccessor);
74     }
75     public void generate(String JavaDoc path,int nodeAccessor) throws XMLDBCException{
76         generate(path, nodeAccessor, (PSVInfoSetProvider)null);
77     }
78     public void generate(String JavaDoc path,int nodeAccessor,String JavaDoc loopID, PSVInfoSetProvider psvisp) throws XMLDBCException{
79         generate(path, nodeAccessor, psvisp);
80     }
81     public void generate(String JavaDoc path,int nodeAccessor, PSVInfoSetProvider psvisp) throws XMLDBCException{
82         _cRS.generate(path, nodeAccessor, psvisp);
83     }
84
85     public void getIdentifiers(Object JavaDoc[] identifiers) throws XMLDBCException{
86         _cRS.getIdentifiers(identifiers);
87         shiftIdentifiers(_cRS, identifiers);
88     }
89
90     public void getNextIdentifiers(Object JavaDoc[] identifiers) throws XMLDBCException {
91         if (_cRS.hasNext()) {
92             _cRS.getNextIdentifiers(identifiers);
93             shiftIdentifiers(_cRS, identifiers);
94         }
95         else {
96             SynchronizedResultSet rs = null;
97             for (int i = _cRSIndex +1 ; i < _sRSNum; i++) {
98                 rs = (SynchronizedResultSet)_sRSList.get(i);
99                 if (rs.hasNext()) {
100                     rs.getNextIdentifiers(identifiers);
101                     shiftIdentifiers(rs, identifiers);
102                     break;
103                 }
104             }
105         }
106     }
107
108     public boolean next() throws XMLDBCException{
109         boolean retVal = false;
110         retVal = _cRS.next();
111         if (!retVal) {
112             SynchronizedResultSet rs = null;
113             for (int i = _cRSIndex +1 ; i < _sRSNum; i++) {
114                 rs = (SynchronizedResultSet)_sRSList.get(i);
115                 retVal = rs.next();
116                 if (retVal) {
117                     _cRSIndex = i;
118                     _cRS = rs;
119                     break;
120                 }
121             }
122         }
123         return retVal;
124     }
125
126     public boolean hasNext() throws XMLDBCException{
127         boolean retVal = false;
128         retVal = _cRS.hasNext();
129         if (!retVal) {
130             SynchronizedResultSet rs = null;
131             for (int i = _cRSIndex +1 ; i < _sRSNum; i++) {
132                 rs = (SynchronizedResultSet)_sRSList.get(i);
133                 retVal = rs.hasNext();
134                 if (retVal) {
135                     break;
136                 }
137             }
138         }
139         return retVal;
140     }
141     /**
142      * To set a content handler (SAX 2) to intercept events produce when retrieves results by SAX 2 way.
143      * @param handler the content handler implementation (see org.xml.sax.ContentHandler)
144      * @throws XDBCException if a database access error occurs.
145      */

146     public void setContentHandler(org.xml.sax.ContentHandler JavaDoc handler){
147         SynchronizedResultSet rs = null;
148         for (int i = 0; i < _sRSNum; i++) {
149             rs = (SynchronizedResultSet)_sRSList.get(i);
150             rs.setContentHandler(handler);
151         }
152     }
153
154     public void setLexicalHandler(org.xml.sax.ext.LexicalHandler JavaDoc handler) {
155         SynchronizedResultSet rs = null;
156         for (int i = 0; i < _sRSNum; i++) {
157             rs = (SynchronizedResultSet)_sRSList.get(i);
158             rs.setLexicalHandler(handler);
159         }
160     }
161
162     public void setErrorHandler(org.xml.sax.ErrorHandler JavaDoc handler){
163         SynchronizedResultSet rs = null;
164         for (int i = 0; i < _sRSNum; i++) {
165             rs = (SynchronizedResultSet)_sRSList.get(i);
166             rs.setErrorHandler(handler);
167         }
168     }
169
170     public void close() throws XMLDBCException {
171         if (_sRSList != null) {
172             SynchronizedResultSet rs = null;
173             for (int i = 0; i < _sRSNum; i++) {
174                 rs = (SynchronizedResultSet)_sRSList.get(i);
175                 rs.close();
176             }
177             _sRSList = null;
178         }
179     }
180
181     private void shiftIdentifiers(SynchronizedResultSet rs, Object JavaDoc[] ids) throws XMLDBCException {
182         int position = 0;
183         for (int i = 0; i < _sRSNum; i++) {
184             rs = (SynchronizedResultSet)_sRSList.get(i);
185             if (rs != _cRS) {
186                 position += rs.getReconstructionIdCount();
187             }
188             else {
189                 break;
190             }
191         }
192
193         if (0 != position) {
194             for (int i = 0; i < _cRS.getReconstructionIdCount(); i++) {
195                 ids[i + position] = ids[i];
196                 ids[i] = null;
197             }
198         }
199     }
200 }
201
Popular Tags