KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.DataOutputStream JavaDoc;
26 import java.sql.*;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
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.xml.sax.helpers.AttributesImpl JavaDoc;
35 import org.xquark.extractor.algebra.MapItem;
36 import org.xquark.extractor.algebra.Mapper;
37 import org.xquark.extractor.common.MessageLibrary;
38 import org.xquark.schema.validation.PSVInfoSetProvider;
39 import org.xquark.xml.xdbc.XMLDBCException;
40 import org.xquark.xquery.parser.Step;
41 import org.xquark.xquery.xdbc.XDBCResultSetInterface;
42
43 /**
44  * NOTE: This result set is not currently used. Could be reactivated if the
45  * SOU was enabled again ? This is the vesion with one big result set...
46  */

47 public class XDBCResultSet implements XDBCResultSetInterface {
48     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
49     private static final String JavaDoc RCSName = "$Name: $";
50
51     private ContentHandler JavaDoc _contenthandler = null;
52     private LexicalHandler JavaDoc _lexicalhandler = null;
53     private ErrorHandler JavaDoc _errorhandler = null;
54     private Mapper _mapper = null;
55     private LookAheadResultSet _sqlResultSet = null;
56
57     /* if the sql resultset contains multiple part, partItemName is the
58      * name of the attribute that indicates the part */

59     private String JavaDoc _partAttributeName = null;
60
61     /* for represent identifiers whoes value is null */
62     private List JavaDoc _nullIdentifier = new ArrayList JavaDoc(1);
63
64     /* used by generate to avoid create new object on echo call*/
65     private AttributesImpl JavaDoc _nullAttribute = new AttributesImpl JavaDoc();
66
67     private String JavaDoc _namespace = null;
68
69     private boolean _isClosed = false;
70
71     private Statement _jdbcStatement = null;
72
73     public void setNamespace(String JavaDoc namespace) {
74         this._namespace = namespace;
75     }
76
77     // public XDBCResultSet(List requestList, Mapper mapper, Statement jdbcStatement) throws XMLDBCException
78
// {
79
// /* execute "queries" and obtain a jdbc resulset*/
80
// _jdbcStatement = jdbcStatement;
81
// ResultSet sqlResultSet = null;
82
// try {
83
// sqlResultSet = executeRequests(requestList, jdbcStatement);
84
// }
85
// catch (SQLException ex) {
86
// throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
87
// }
88
//
89
// _nullIdentifier.add(null);
90
//
91
// _mapper = mapper;
92
//
93
// if (_mapper.isMultiPart()) {
94
// _partAttributeName = _mapper.getPartAttributeName();
95
// try {
96
// _sqlResultSet = new EliminatedSqlResultSet(sqlResultSet,_mapper.getEliminationGuide());
97
// }
98
// catch (Exception ex) {
99
// throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
100
// }
101
// }
102
// else {
103
// try {
104
// _sqlResultSet = new LookAheadSqlResultSet(sqlResultSet);
105
// }
106
// catch (Exception ex) {
107
// throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
108
// }
109
// }
110
// }
111
//
112
// protected ResultSet executeRequests(List requestList, Statement jdbcStatement) throws SQLException {
113
// Trace.enter(this, "executeRequests(List requestList, Statement jdbcStatement)");
114
// ResultSet retVal = null;
115
//
116
// String request = null;
117
// int number = requestList.size();
118
// for (int i = 0; i < number-1; i++) {
119
// request = (String)requestList.get(i);
120
// jdbcStatement.executeUpdate(request);
121
// }
122
// request = (String)requestList.get(number-1);
123
// retVal = jdbcStatement.executeQuery(request);
124
//
125
// Trace.exit(this, "executeRequests(List requestList, Statement jdbcStatement)");
126
// return retVal;
127
// }
128

129     public XDBCResultSet(ResultSet sqlResultSet, Mapper mapper) throws XMLDBCException {
130         //Trace.enter(this, "XDBCResultSet(ResultSet sqlResultSet, Mapper mapper)");
131
if (sqlResultSet == null || mapper == null)
132             throw new XMLDBCException("The arguments of the XDBCResultSet constructor cannot be null.", null);
133
134         /*
135         try {
136                 org.xquark.extractor.common.Debug.showResultSet(sqlResultSet);
137                 sqlResultSet.beforeFirst();
138         }
139         catch (Exception e) {}
140          */

141
142         _nullIdentifier.add(null);
143
144         _mapper = mapper;
145
146         if (_mapper.isMultiPart()) {
147             _partAttributeName = _mapper.getPartAttributeName();
148             try {
149                 _sqlResultSet = new EliminatedSqlResultSet(sqlResultSet, _mapper.getEliminationGuide());
150             } catch (SQLException ex) {
151                 throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
152             }
153         } else {
154             try {
155                 _sqlResultSet = new LookAheadSqlResultSet(sqlResultSet);
156             } catch (SQLException ex) {
157                 throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
158             }
159         }
160
161         //Trace.exit(this, "XDBCResultSet(ResultSet sqlResultSet, Mapper mapper)");
162
}
163
164     public XDBCResultSet(ResultSet sqlResultSet, Mapper mapper, boolean heavyElimination) throws XMLDBCException {
165         //Trace.enter(this, "XDBCResultSet(ResultSet sqlResultSet, Mapper mapper, boolean heavyElimination)");
166
if (sqlResultSet == null || mapper == null)
167             throw new XMLDBCException("The arguments of the XDBCResultSet constructor cannot be null.", null);
168
169         /*
170         try {
171                 org.xquark.extractor.common.Debug.showResultSet(sqlResultSet);
172                 sqlResultSet.beforeFirst();
173         }
174         catch (Exception e) {}
175          */

176
177         _nullIdentifier.add(null);
178
179         _mapper = mapper;
180
181         if (heavyElimination && _mapper.isMultiPart()) {
182             _partAttributeName = _mapper.getPartAttributeName();
183             try {
184                 _sqlResultSet = new HeavyElimination(sqlResultSet, _mapper.getEliminationGuide());
185                 // ((EliminatedSqlResultSet)_sqlResultSet).print();
186
} catch (SQLException ex) {
187                 throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
188             }
189
190         } else if (_mapper.isMultiPart()) {
191             _partAttributeName = _mapper.getPartAttributeName();
192             try {
193                 _sqlResultSet = new EliminatedSqlResultSet(sqlResultSet, _mapper.getEliminationGuide());
194                 // ((EliminatedSqlResultSet)_sqlResultSet).print();
195
} catch (SQLException ex) {
196                 throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
197             }
198         } else {
199             try {
200                 _sqlResultSet = new LookAheadSqlResultSet(sqlResultSet);
201             } catch (SQLException ex) {
202                 throw new XMLDBCException("XDBCResultSet constructor failed .", ex);
203             }
204         }
205
206         //Trace.exit(this, "XDBCResultSet(ResultSet sqlResultSet, Mapper mapper, boolean heavyElimination)");
207
}
208
209     /**
210      * @see XDBCResultSetInterface#fetch
211      */

212     // TODO if used again used psvisp when necessary
213
public String JavaDoc fetch(String JavaDoc path, int nodeAccessor, String JavaDoc loopID, PSVInfoSetProvider psvisp) throws XMLDBCException {
214         return fetch(path, nodeAccessor);
215     }
216
217     public String JavaDoc fetch(String JavaDoc path, int nodeAccessor, String JavaDoc loopID) throws XMLDBCException {
218         return fetch(path, nodeAccessor);
219     }
220     public String JavaDoc fetch(String JavaDoc path, int nodeAccessor) throws XMLDBCException {
221         //org.xquark.xml.misc.Debug.print(this,"START XDBCResultSet::FETCH - path = " + path + " - nodeAccessor = " + nodeAccessor);
222
//Trace.enter(this, "fetch(String path)", path);
223

224         if (_isClosed)
225             throw new XMLDBCException("XMLResultSet is already closed");
226         String JavaDoc retVal;
227         try {
228             MapItem mapItem = _mapper.map(path);
229             if (mapItem != null)
230                 retVal = _sqlResultSet.getString(mapItem.getItemName());
231             else
232                 throw new XMLDBCException("(fetch) This path \"" + path + "\" can not be found in the result set.", null);
233         } catch (SQLException sqle) {
234             throw new XMLDBCException(sqle.getMessage(), sqle);
235         }
236
237         //Trace.exit(this, "fetch(String path)", path);
238
//org.xquark.xml.misc.Debug.print(this,"STOP XDBCResultSet::FETCH - path = " + path + " -> " + retVal);
239
return retVal;
240     }
241
242     /**
243      * @see XDBCResultSetInterface#generate
244      */

245     // TODO if used again used psvisp when necessary
246
public void generate(String JavaDoc path, int nodeAccessor, String JavaDoc loopID, PSVInfoSetProvider psvisp) throws XMLDBCException {
247         generate(path, nodeAccessor);
248     }
249
250     public void generate(String JavaDoc path, int nodeAccessor, String JavaDoc loopID) throws XMLDBCException {
251         generate(path, nodeAccessor);
252     }
253     public void generate(String JavaDoc path, int nodeAccessor) throws XMLDBCException {
254         //org.xquark.xml.misc.Debug.print(this,"START XDBCResultSet::GENERATE - path = " + path + " - nodeAccessor = " + nodeAccessor);
255
//org.xquark.xml.misc.Debug.print(this,"START XDBCResultSet::GENERATE - _mapper = " + _mapper.pprint());
256
//Trace.enter(this, "generate(String path, ContentHandler handler)", path);
257
if (_isClosed)
258             throw new XMLDBCException("XMLResultSet is already closed");
259         MapItem mapItem = _mapper.map(path);
260         //org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - mapItem = " + mapItem);
261

262         if (null == mapItem) {
263             throw new XMLDBCException(MessageLibrary.getMessage("IE_INV_ARG", "generate(String path, int nodeAccessor, String loopID, org.xml.sax.ContentHandler handler)", "path = " + path), null);
264         }
265         try {
266             String JavaDoc tag = mapItem.getElementName();
267             String JavaDoc namespace = mapItem.getNamespace();
268             if (namespace == null)
269                 namespace = "";
270             //org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - tag = " + tag);
271
//org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - mapItem.isLeafPath() = " + mapItem.isLeafPath());
272
if (mapItem.isLeafPath()) {
273                 //org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - mapItem.getItemName() = " + mapItem.getItemName());
274
//org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - _sqlResultSet.getString(mapItem.getItemName() = " + _sqlResultSet.getString(mapItem.getItemName()));
275
//org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - mapItem.getItemName() = " + mapItem.getItemName());
276
String JavaDoc value = _sqlResultSet.getString(mapItem.getItemName());
277                 //org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::GENERATE - value = " + value);
278
if (value != null) {
279                     if (null != tag) {
280                         if (nodeAccessor == 0 || nodeAccessor == Step.CHILD_NODE) {
281                         //if (nodeAccessor == 0 || nodeAccessor == XDBCResultSetInterface.NODE_ACCESSOR_NODE) {
282
if (_contenthandler != null) {
283                                 _contenthandler.startElement(namespace, tag, "", _nullAttribute);
284                                 _contenthandler.characters(value.toCharArray(), 0, value.length());
285                                 _contenthandler.endElement(namespace, tag, "");
286                             }
287                         } else if (nodeAccessor == Step.CHILD_TEXT) {
288                         //} else if (nodeAccessor == XDBCResultSetInterface.NODE_ACCESSOR_TEXT) {
289
//org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::generate - STOP startElement = " + tag);
290
if (_contenthandler != null)
291                                 _contenthandler.characters(value.toCharArray(), 0, value.length());
292                         }
293                     } else if (_contenthandler != null) {
294                         _contenthandler.characters(value.toCharArray(), 0, value.length());
295                     }
296                 }
297             } else {
298                 if (nodeAccessor == 0) {
299                     List JavaDoc children = mapItem.getChildren();
300                     if (null != tag) {
301                         if (_contenthandler != null) {
302                             _contenthandler.startElement(namespace, tag, "", _nullAttribute);
303                             for (int i = 0; i < children.size(); i++)
304                                 generate((String JavaDoc) children.get(i), nodeAccessor);
305                             _contenthandler.endElement(namespace, tag, "");
306                         }
307                     } else {
308                         for (int i = 0; i < children.size(); i++) {
309                             generate((String JavaDoc) children.get(i), nodeAccessor);
310                         }
311                     }
312                 } else if (nodeAccessor == Step.CHILD_NODE) {
313                 //} else if (nodeAccessor == XDBCResultSetInterface.NODE_ACCESSOR_NODE) {
314
List JavaDoc children = mapItem.getChildren();
315                     for (int i = 0; i < children.size(); i++) {
316                         generate((String JavaDoc) children.get(i), (tag == null) ? nodeAccessor : 0);
317                     }
318                 }
319             }
320         } catch (SAXException JavaDoc e) {
321             throw new XMLDBCException(e.getMessage());
322         } catch (SQLException e) {
323             throw new XMLDBCException(e.getMessage());
324         }
325         //Trace.exit(this, "generate(String path)", path);
326
}
327
328     /**
329      * @see XDBCResultSetInterface#getIdentifiers
330      */

331     public void getIdentifiers(Object JavaDoc[] identifiers) throws XMLDBCException {
332         //Trace.enter(this, "getIdentifiers(Object[] identifiers)");
333

334         if (_isClosed)
335             throw new XMLDBCException("XMLResultSet is already closed");
336         List JavaDoc keyList = _mapper.getIdentifierList();
337         //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - keyList: "+keyList);
338
MapItem keyItem;
339         try {
340             if (null == keyList) {
341                 throw new XMLDBCException("There is no identifier in the resultset.");
342             }
343             if (null != _partAttributeName) {
344                 for (int i = 0; i < keyList.size(); i++) {
345
346                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - _partItemName: "+_partItemName);
347

348                     long currentPart = _sqlResultSet.getLong(_partAttributeName);
349
350                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - currentPart: "+currentPart);
351
keyItem = (MapItem) keyList.get(i);
352
353                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - keyItem: "+keyItem);
354
if (keyItem.isLeafPath()) {
355                         identifiers[i] = _sqlResultSet.getString(keyItem.getItemName());
356                         if (null == identifiers[i] && keyItem.isPartOf(currentPart)) {
357                             identifiers[i] = _nullIdentifier;
358                         }
359                     } else {
360                         if (keyItem.isPartOf(currentPart)) {
361                             List JavaDoc idAttrs = new ArrayList JavaDoc();
362                             List JavaDoc children = _mapper.getChildren(keyItem);
363                             for (int j = 0; j < children.size(); j++) {
364                                 MapItem child = (MapItem) children.get(j);
365                                 String JavaDoc attrValue = _sqlResultSet.getString(child.getItemName());
366                                 idAttrs.add(attrValue);
367                             }
368                             identifiers[i] = idAttrs;
369                         } else {
370                             identifiers[i] = null;
371                         }
372
373                     }
374
375                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - identifiers["+i+"]: "+identifiers[i]);
376

377                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - identifiers[i]: "+identifiers[i]);
378

379                 }
380             } else {
381                 for (int i = 0; i < keyList.size(); i++) {
382                     keyItem = (MapItem) keyList.get(i);
383                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - keyItem: "+keyItem);
384
identifiers[i] = _sqlResultSet.getString(keyItem.getItemName());
385                     if (null == identifiers[i]) {
386                         identifiers[i] = _nullIdentifier;
387                     }
388                     //org.xquark.xml.misc.Debug.print(this,"IN GETIDENTIFIER - identifiers["+i+"]: "+identifiers[i]);
389
}
390             }
391         } catch (SQLException sqle) {
392             throw new XMLDBCException(sqle.getMessage());
393         }
394
395         //Trace.exit(this, "getIdentifiers(Object[] identifiers)");
396
}
397
398     /**
399      * @see XDBCResultSetInterface#getNextIdentifiers
400      */

401     public void getNextIdentifiers(Object JavaDoc[] identifiers) throws XMLDBCException {
402         //Trace.enter(this, "getNextIdentifiers(Object[] identifiers)");
403

404         if (_isClosed)
405             throw new XMLDBCException("XMLResultSet is already closed");
406         boolean hasNext;
407         // try
408
// {
409
hasNext = _sqlResultSet.lookAhead();
410         if (hasNext) {
411             getIdentifiers(identifiers);
412             _sqlResultSet.goBack();
413         } else {
414             identifiers = null;
415         }
416         // }
417
// catch (SQLException sqle)
418
// {
419
// throw new XDBCException(sqle.getMessage());
420
// }
421

422         //Trace.exit(this, "getNextIdentifiers(Object[] identifiers)");
423
}
424
425     /**
426      * @see XDBCResultSetInterface#next
427      */

428     public boolean next() throws XMLDBCException {
429         //Trace.enter(this, "next()");
430

431         if (_isClosed)
432             throw new XMLDBCException("XMLResultSet is already closed");
433         boolean retVal = false;
434         try {
435             retVal = _sqlResultSet.next();
436
437             ///* For debugging */
438
// java.sql.ResultSetMetaData metaData = _sqlResultSet.getSqlResultSet().getMetaData();
439
// StringBuffer sb = new StringBuffer();
440
// for (int i = 1; i <= metaData.getColumnCount(); i++)
441
// {
442
// if (i > 1) sb.append(", " );
443
// sb.append(_sqlResultSet.getSqlResultSet().getString(i));
444
// }
445
//org.xquark.xml.misc.Debug.print(this,"******************************************************************************");
446
//org.xquark.xml.misc.Debug.print(this,"current ResultSet: ["+sb+"]");
447
//org.xquark.xml.misc.Debug.print(this,"******************************************************************************");
448

449         } catch (SQLException sqle) {
450             //org.xquark.xml.misc.Debug.print(this,"IN XDBCResultSet::next() -> error!!!!!!!!!!");
451
throw new XMLDBCException(sqle.getMessage(), sqle);
452         }
453
454         //Trace.exit(this, "next()");
455
return retVal;
456     }
457
458     /**
459      * @see XDBCResultSetInterface#hasNext
460      */

461
462     public boolean hasNext() throws XMLDBCException {
463         //Trace.enter(this, "hasNext()");
464
if (_isClosed)
465             throw new XMLDBCException("XMLResultSet is already closed");
466         boolean retVal = _sqlResultSet.lookAhead();
467         //org.xquark.xml.misc.Debug.print(this,"IN hasNext::retVal -> "+retVal);
468
if (retVal) {
469             _sqlResultSet.goBack();
470         }
471         //Trace.exit(this, "hasNext()");
472
return retVal;
473     }
474
475     /**
476      * @see XDBCResultSetInterface#setContentHandler
477      */

478     public void setContentHandler(ContentHandler JavaDoc handler) {
479         if (handler == null)
480             throw new NullPointerException JavaDoc("ContentHandler can not be null.");
481         this._contenthandler = handler;
482     }
483
484     /**
485      * @see XDBCResultSetInterface#setLexicalHandler
486      */

487     public void setLexicalHandler(LexicalHandler JavaDoc handler) {
488         if (handler == null)
489             throw new NullPointerException JavaDoc("LexicalHandler can not be null.");
490         this._lexicalhandler = handler;
491     }
492
493     /**
494      * @see XDBCResultSetInterface#setErrorHandler
495      */

496     public void setErrorHandler(ErrorHandler JavaDoc handler) {
497         if (handler == null)
498             throw new NullPointerException JavaDoc("ErrorHandler can not be null.");
499         this._errorhandler = handler;
500     }
501
502     /** @todo this function is to be removed after debug */
503     public ResultSet getSqlResultSet() {
504         return _sqlResultSet.getSqlResultSet();
505     }
506
507     public Mapper getMapper() {
508         return _mapper;
509     }
510
511     public void close() throws XMLDBCException {
512         if (!_isClosed) {
513             try {
514                 _isClosed = true;
515                 _sqlResultSet.close();
516             } catch (SQLException ex) {
517                 throw new XMLDBCException(ex.getMessage(), ex);
518             }
519         }
520     }
521
522     class LookAheadSqlResultSet implements LookAheadResultSet {
523         private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
524         private static final String JavaDoc RCSName = "$Name: $";
525
526         private int BOR = 0; /* begin of resultset*/
527         private int BUFFER = 1;
528         private int RESULTSET = 2;
529
530         private ResultSet _resultset;
531         private List JavaDoc _buffer = new ArrayList JavaDoc();
532         private List JavaDoc _attributeNameList = new ArrayList JavaDoc();
533         private int _attributeNumber;
534
535         private boolean _bufferNotEmpty;
536         private boolean _resultSetNotEmpty;
537         private int _cursor;
538
539         public LookAheadSqlResultSet(ResultSet resultset) throws SQLException {
540             //Trace.enter(this, "LookAheadSqlResultSet()");
541
_resultset = resultset;
542
543             ResultSetMetaData _rsm = _resultset.getMetaData();
544
545             _attributeNumber = _rsm.getColumnCount();
546             for (int i = 1; i <= _attributeNumber; i++) {
547                 _attributeNameList.add(_rsm.getColumnName(i));
548             }
549
550             _bufferNotEmpty = _resultset.next();
551             if (_bufferNotEmpty) {
552                 refreshBuffer();
553                 _resultSetNotEmpty = _resultset.next();
554             }
555             _cursor = BOR;
556             //Trace.exit(this, "LookAheadSqlResultSet()");
557
}
558
559         public boolean next() throws SQLException {
560             //Trace.enter(this, "next()");
561
boolean retVal;
562             if (BOR == _cursor) {
563                 _cursor = BUFFER;
564             } else {
565                 if (_resultSetNotEmpty) {
566                     refreshBuffer();
567                     _resultSetNotEmpty = _resultset.next();
568                 } else {
569                     _resultset.close();
570                     _bufferNotEmpty = false;
571                 }
572             }
573             //Trace.exit(this, "next()");
574
return _bufferNotEmpty;
575         }
576
577         public boolean hasNext() {
578             return _resultSetNotEmpty;
579         }
580
581         public boolean lookAhead() {
582             //Trace.enter(this, "lookAhead()");
583
boolean retVal = false;
584             if (BOR == _cursor) {
585                 retVal = _bufferNotEmpty;
586                 //org.xquark.xml.misc.Debug.print(this,"IN lookAhead (BOF == _cursor)::retVal -> "+retVal);
587
if (retVal) {
588                     _cursor = BUFFER;
589                 }
590             } else if (BUFFER == _cursor) {
591                 retVal = _resultSetNotEmpty;
592                 //org.xquark.xml.misc.Debug.print(this,"IN lookAhead (BUFFER == _cursor)::retVal -> "+retVal);
593
if (retVal) {
594                     _cursor = RESULTSET;
595                 }
596             } else {
597                 org.xquark.extractor.common.Debug.assertTrue(false, "Logic error");
598             }
599
600             //Trace.exit(this, "lookAhead()");
601
return retVal;
602         }
603
604         public boolean goBack() {
605             //Trace.enter(this, "goBack()");
606
boolean retVal = false;
607             if (BUFFER == _cursor) {
608                 _cursor = BOR;
609                 retVal = true;
610             } else if (RESULTSET == _cursor) {
611                 _cursor = BUFFER;
612                 retVal = true;
613             } else if (BOR == _cursor) {
614                 retVal = false;
615             }
616
617             //Trace.exit(this, "goBack()");
618
return retVal;
619         }
620
621         public long getLong(String JavaDoc name) throws SQLException {
622             //Trace.enter(this, "getLong(String name)");
623
long retVal = 0;
624             if (BUFFER == _cursor) {
625                 retVal = getLongFromBuffer(name);
626             } else {
627                 try {
628                     retVal = _resultset.getLong(name);
629                 } catch (SQLException e) {
630                 }
631             }
632             //Trace.exit(this, "getLong(String name)");
633
return retVal;
634         }
635
636         public String JavaDoc getString(String JavaDoc name) throws SQLException {
637             //Trace.enter(this, "getSting(String name)");
638
String JavaDoc retVal = null;
639             if (BUFFER == _cursor) {
640                 retVal = getStringFromBuffer(name);
641             } else {
642                 try {
643                     retVal = _resultset.getString(name);
644                 } catch (SQLException e) {
645                 }
646             }
647             //Trace.exit(this, "getSting(String name)");
648
return retVal;
649         }
650
651         private void refreshBuffer() throws SQLException {
652             //Trace.enter(this, "refreshBuffer()");
653
_buffer.clear();
654             for (int i = 1; i <= _attributeNumber; i++) {
655                 _buffer.add(_resultset.getString(i));
656             }
657             //Trace.exit(this, "refreshBuffer()");
658
}
659
660         public String JavaDoc getStringFromBuffer(String JavaDoc name) {
661             //Trace.enter(this, "getStingFromBuffer(String name)");
662
String JavaDoc retVal = null;
663             int index = -1;
664             for (int i = 0; i < _attributeNameList.size(); i++) {
665                 if (name.equalsIgnoreCase((String JavaDoc) _attributeNameList.get(i))) {
666                     index = i;
667                     break;
668                 }
669             }
670             if (-1 != index) {
671                 if (!_buffer.isEmpty()) // added 22/03/2002
672
retVal = (String JavaDoc) _buffer.get(index);
673             }
674             //Trace.exit(this, "getStingFromBuffer(String name)");
675
return retVal;
676         }
677
678         public long getLongFromBuffer(String JavaDoc name) {
679             //Trace.enter(this, "getStingFromBuffer(String name)");
680
long retVal = 0;
681             int index = -1;
682             for (int i = 0; i < _attributeNameList.size(); i++) {
683                 if (name.equalsIgnoreCase((String JavaDoc) _attributeNameList.get(i))) {
684                     index = i;
685                     break;
686                 }
687             }
688             if (-1 != index) {
689                 if (!_buffer.isEmpty()) // added 22/03/2002
690
retVal = Long.parseLong((String JavaDoc) _buffer.get(index));
691             }
692             //Trace.exit(this, "getStingFromBuffer(String name)");
693
return retVal;
694         }
695
696         /** @todo this function is to be removed after debug */
697         public ResultSet getSqlResultSet() {
698             return _resultset;
699         }
700
701         public void write(DataOutputStream JavaDoc dos) throws java.io.IOException JavaDoc {
702             //Trace.enter(this, "write(DataOutputStream dos)");
703

704             String JavaDoc newLine = System.getProperty("line.separator");
705
706             dos.writeBytes("<LookAheadResultSet>");
707             dos.writeBytes(newLine);
708
709             dos.writeBytes("<header>");
710             for (int i = 0; i < _attributeNameList.size(); i++) {
711                 Object JavaDoc obj = _attributeNameList.get(i);
712                 if (null != obj) {
713                     dos.writeBytes((String JavaDoc) obj);
714                     dos.writeBytes(",\t");
715                 }
716             }
717             dos.writeBytes("</header>");
718             dos.writeBytes(newLine);
719
720             try {
721                 while (next()) {
722                     dos.writeBytes("<row>");
723                     for (int i = 0; i < _buffer.size(); i++) {
724                         Object JavaDoc obj = _buffer.get(i);
725                         if (null != obj) {
726                             dos.writeBytes((String JavaDoc) obj);
727                             dos.writeBytes(",\t");
728                         }
729                     }
730                     dos.writeBytes("</row>");
731                     dos.writeBytes(newLine);
732                 }
733
734                 dos.writeBytes("</LookAheadResultSet>");
735                 dos.writeBytes(newLine);
736                 ;
737             } catch (SQLException ex) {
738                 ex.printStackTrace();
739             }
740
741             //Trace.exit(this, "write(DataOutputStream dos)");
742
}
743         public void close() throws SQLException {
744             try {
745                 _resultset.close();
746             } catch (SQLException ex) {
747             }
748         }
749     }
750
751     class HeavyElimination extends EliminatedSqlResultSet {
752         List JavaDoc _keyColumnList = new ArrayList JavaDoc();
753         String JavaDoc partAttributeName = _partAttributeName;
754         HeavyElimination(ResultSet resultSet, int[] eliminationGuide) throws SQLException {
755             super(resultSet, eliminationGuide);
756             _keyColumnList = _mapper.getAllIdentifierAttributeNames();
757         }
758
759         protected boolean feedBuffer2() throws SQLException {
760             //Trace.enter(this, "feedBuffer2()");
761
boolean retVal = false;
762
763             int oldCursor = _cursor;
764             _cursor = BUFFER2;
765             boolean isRedundant = false;
766             if (_resultSetNotEmpty) {
767                 fillBuffer2();
768                 _resultSetNotEmpty = _resultSet.next();
769                 if (_buffer2NotEmpty) {
770                     if (_resultSetNotEmpty) {
771                         isRedundant = isRedundant();
772                         if (isRedundant) {
773                             retVal = feedBuffer2();
774                         } else {
775                             retVal = true;
776                         }
777                     } else {
778                         _resultSetNotEmpty = false;
779                         retVal = true;
780                     }
781                 } else {
782                     retVal = false;
783                 }
784             } else {
785                 retVal = false;
786                 _buffer2NotEmpty = false;
787                 _resultSet.close();
788             }
789             _cursor = oldCursor;
790
791             //Trace.exit(this, "feedBuffer2()");
792
return retVal;
793         }
794
795         private boolean isRedundant() throws SQLException {
796             //Trace.enter(this, "isIdenticalLines()");
797
boolean retVal = true;
798
799             int oldCursor = _cursor;
800             _cursor = BUFFER2;
801             String JavaDoc idInBuffer = null;
802             String JavaDoc idInResultset = null;
803             String JavaDoc keyColumnName = null;
804             for (int i = 0; i < __keyColumnList.size(); i++) {
805                 keyColumnName = (String JavaDoc) __keyColumnList.get(i);
806                 idInBuffer = getStringFromBuffer(keyColumnName);
807                 idInResultset = _resultSet.getString(keyColumnName);
808                 if (null != idInBuffer && null != idInResultset) {
809                     retVal = idInBuffer.equals(idInResultset);
810                 }
811                 // else if (null == idInBuffer && null != idInResultset) {
812
// retVal = false;
813
// }
814
else if (null != idInBuffer && null == idInResultset) {
815                     retVal = false;
816                 }
817                 if (!retVal) {
818                     break;
819                 }
820             }
821             _cursor = oldCursor;
822             //Trace.exit(this, "isIdenticalLines()", retVal ? "true" : "false");
823
return retVal;
824         }
825     }
826
827     class EliminatedSqlResultSet implements LookAheadResultSet {
828         private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
829         private static final String JavaDoc RCSName = "$Name: $";
830
831         protected int BOR = 0; /* begin of resultset*/
832         protected int BUFFER1 = 1;
833         protected int BUFFER2 = 2;
834         protected int RESULTSET = 3;
835
836         protected ResultSet _resultSet;
837         protected List JavaDoc _buffer1;
838         protected List JavaDoc _buffer2;
839
840         protected List JavaDoc _attributeNameList = new ArrayList JavaDoc();
841         protected int _attributeNumber;
842
843         protected boolean _buffer1NotEmpty = false;
844         protected boolean _buffer2NotEmpty = false;
845         ;
846         protected boolean _resultSetNotEmpty = false;
847         ;
848         protected int _cursor = BOR;
849
850         int[] _eliminationGuide;
851
852         /* added for heavyElimination*/
853         protected String JavaDoc __partAttributeName = null;
854         protected List JavaDoc __keyColumnList = new ArrayList JavaDoc();
855         /* end of added for heavyElimination*/
856
857         public EliminatedSqlResultSet(ResultSet resultSet, int[] eliminationGuide) throws SQLException {
858             //Trace.enter(this, "EliminatedSqlResultSet( ResultSet resultset, int[] eliminationGuide)");
859

860             _resultSet = resultSet;
861             _eliminationGuide = eliminationGuide;
862
863             /* added for heavyElimination*/
864             __partAttributeName = _partAttributeName;
865             __keyColumnList = _mapper.getAllIdentifierAttributeNames();
866             /* end of added for heavyElimination*/
867             ResultSetMetaData _rsm = _resultSet.getMetaData();
868
869             _resultSetNotEmpty = _resultSet.next();
870
871             if (_resultSetNotEmpty) {
872                 _attributeNumber = _rsm.getColumnCount();
873                 for (int i = 1; i <= _attributeNumber; i++) {
874                     _attributeNameList.add(_rsm.getColumnName(i));
875                 }
876
877                 _buffer1 = new ArrayList JavaDoc(_attributeNumber);
878                 _buffer2 = new ArrayList JavaDoc(_attributeNumber);
879                 for (int i = 0; i < _attributeNumber; i++) {
880                     _buffer1.add(null);
881                     _buffer2.add(null);
882                 }
883
884                 feedBuffer2();
885                 feedBuffer1();
886                 feedBuffer2();
887
888             }
889             _cursor = BOR;
890
891             //Trace.exit(this, "EliminatedSqlResultSet( ResultSet resultset, int[] eliminationGuide)");
892
}
893
894         protected boolean feedBuffer1() {
895             //Trace.enter(this, "feedBuffer1()");
896
boolean retVal = false;
897
898             List JavaDoc tmp = _buffer1;
899
900             _buffer1 = _buffer2;
901             _buffer1NotEmpty = _buffer2NotEmpty;
902             retVal = _buffer1NotEmpty;
903
904             _buffer2 = tmp;
905             _buffer2NotEmpty = false;
906
907             //Trace.exit(this, "feedBuffer1()");
908
return retVal;
909         }
910
911         protected boolean feedBuffer2() throws SQLException {
912             //Trace.enter(this, "feedBuffer2()");
913
boolean retVal = false;
914
915             int oldCursor = _cursor;
916             _cursor = BUFFER2;
917             if (_resultSetNotEmpty) {
918                 fillBuffer2();
919                 _resultSetNotEmpty = _resultSet.next();
920                 if (_buffer2NotEmpty) {
921                     if (_resultSetNotEmpty) {
922                         long currentPartBitmap = getLongFromBuffer(_partAttributeName);
923                         int curQueryNo = 1;
924                         while (1 != currentPartBitmap) {
925                             currentPartBitmap = currentPartBitmap >> 1;
926                             curQueryNo++;
927                         }
928
929                         long nextParttBitmap = _resultSet.getLong(_partAttributeName);
930                         int nextQueryNo = 1;
931                         while (1 != nextParttBitmap) {
932                             nextParttBitmap = nextParttBitmap >> 1;
933                             nextQueryNo++;
934                         }
935
936                         if ((nextQueryNo <= _eliminationGuide[curQueryNo - 1]) && (nextQueryNo > curQueryNo)) {
937                             /* current data in buffer2 is not neccessary */
938                             //Trace.message(this, "feedBuffer2()", "tuple of query : " + Integer.toString(curQueryNo) + " is eliminated");
939
retVal = feedBuffer2();
940                             org.xquark.extractor.common.Debug.assertTrue(retVal, "retVal");
941                         } else {
942                             retVal = true;
943                         }
944                     } else {
945                         _resultSetNotEmpty = false;
946                         retVal = true;
947                     }
948                 } else {
949                     retVal = false;
950                 }
951             } else {
952                 retVal = false;
953                 _buffer2NotEmpty = false;
954                 _resultSet.close();
955             }
956             _cursor = oldCursor;
957
958             //Trace.exit(this, "feedBuffer2()");
959
return retVal;
960         }
961
962         protected void fillBuffer2() throws SQLException {
963             //Trace.enter(this, "fillBuffer2()");
964
for (int i = 1; i <= _attributeNumber; i++) {
965                 _buffer2.set(i - 1, _resultSet.getString(i));
966             }
967             _buffer2NotEmpty = true;
968
969             //Trace.exit(this, "fillBuffer2()");
970
}
971
972         public boolean next() throws SQLException {
973             //Trace.enter(this, "next()");
974
boolean retVal = false;
975
976             if (BOR == _cursor) {
977                 _cursor = BUFFER1;
978                 retVal = _buffer1NotEmpty;
979             } else if (BUFFER1 == _cursor) {
980                 retVal = feedBuffer1();
981                 feedBuffer2();
982             } else {
983                 org.xquark.extractor.common.Debug.assertTrue(false, "Logic error");
984             }
985
986             //Trace.exit(this, "next()");
987
return retVal;
988         }
989
990         public boolean lookAhead() {
991             //Trace.enter(this, "lookAhead()");
992
boolean retVal = false;
993             if (BOR == _cursor) {
994                 retVal = _buffer1NotEmpty;
995                 if (retVal) {
996                     _cursor = BUFFER1;
997                 }
998             } else if (BUFFER1 == _cursor) {
999                 retVal = _buffer2NotEmpty;
1000                if (retVal) {
1001                    _cursor = BUFFER2;
1002                }
1003            } else {
1004                org.xquark.extractor.common.Debug.assertTrue(false, "Logic error");
1005            }
1006
1007            //Trace.exit(this, "lookAhead()");
1008
return retVal;
1009        }
1010
1011        public boolean goBack() {
1012            //Trace.enter(this, "goBack()");
1013
boolean retVal = false;
1014            if (BUFFER1 == _cursor) {
1015                _cursor = BOR;
1016                retVal = true;
1017            } else if (BUFFER2 == _cursor) {
1018                _cursor = BUFFER1;
1019                retVal = true;
1020            } else if (BOR == _cursor) {
1021                retVal = false;
1022            }
1023
1024            //Trace.exit(this, "goBack()");
1025
return retVal;
1026        }
1027
1028        public long getLong(String JavaDoc name) throws SQLException {
1029            long retVal = getLongFromBuffer(name);
1030            return retVal;
1031        }
1032
1033        public String JavaDoc getString(String JavaDoc name) throws SQLException {
1034            //Trace.enter(this, "getSting(String name)");
1035

1036            String JavaDoc retVal = null;
1037            if (BUFFER1 == _cursor || BUFFER2 == _cursor) {
1038                retVal = getStringFromBuffer(name);
1039            } else {
1040                // try {
1041
retVal = _resultSet.getString(name);
1042                // }
1043
// catch (Exception e) {}
1044
}
1045
1046            //Trace.exit(this, "getSting(String name)");
1047
return retVal;
1048        }
1049
1050        public String JavaDoc getStringFromBuffer(String JavaDoc name) {
1051            //Trace.enter(this, "getStingFromBuffer(String name)");
1052
String JavaDoc retVal = null;
1053
1054            int index = -1;
1055            if (BUFFER1 == _cursor) {
1056                for (int i = 0; i < _attributeNameList.size(); i++) {
1057                    if (name.equalsIgnoreCase((String JavaDoc) _attributeNameList.get(i))) {
1058                        index = i;
1059                        break;
1060                    }
1061                }
1062                if (-1 != index) {
1063                    retVal = (String JavaDoc) _buffer1.get(index);
1064                }
1065            } else if (BUFFER2 == _cursor) {
1066                for (int i = 0; i < _attributeNameList.size(); i++) {
1067                    if (name.equalsIgnoreCase((String JavaDoc) _attributeNameList.get(i))) {
1068                        index = i;
1069                        break;
1070                    }
1071                }
1072                if (-1 != index) {
1073                    retVal = (String JavaDoc) _buffer2.get(index);
1074                }
1075
1076            } else {
1077                org.xquark.extractor.common.Debug.assertTrue(false, "Logic error : cursor is not on buffers when calling getStringFromBuffer(String name)");
1078            }
1079
1080            //Trace.exit(this, "getStingFromBuffer(String name)");
1081
return retVal;
1082        }
1083
1084        public long getLongFromBuffer(String JavaDoc name) {
1085            //Trace.enter(this, "getLongFromBuffer(String name)");
1086
long retVal = 0;
1087
1088            int index = -1;
1089            if (BUFFER1 == _cursor) {
1090                for (int i = 0; i < _attributeNameList.size(); i++) {
1091                    if (name.equalsIgnoreCase((String JavaDoc) _attributeNameList.get(i))) {
1092                        index = i;
1093                        break;
1094                    }
1095                }
1096                if (-1 != index) {
1097                    retVal = Long.parseLong((String JavaDoc) _buffer1.get(index));
1098                }
1099            } else if (BUFFER2 == _cursor) {
1100                for (int i = 0; i < _attributeNameList.size(); i++) {
1101                    if (name.equalsIgnoreCase((String JavaDoc) _attributeNameList.get(i))) {
1102                        index = i;
1103                        break;
1104                    }
1105                }
1106                if (-1 != index) {
1107                    retVal = Long.parseLong((String JavaDoc) _buffer2.get(index));
1108                }
1109            } else {
1110                org.xquark.extractor.common.Debug.assertTrue(false, "Logic error : cursor is not on buffers when calling getLongFromBuffer(String name)");
1111            }
1112
1113            //Trace.exit(this, "getLongFromBuffer(String name)");
1114
return retVal;
1115        }
1116
1117        /** @todo this function is to be removed after debug */
1118        public ResultSet getSqlResultSet() {
1119            return _resultSet;
1120        }
1121
1122        public void close() throws SQLException {
1123            try {
1124                _resultSet.close();
1125            } catch (SQLException ex) {
1126            }
1127        }
1128
1129        public void print() throws SQLException {
1130            System.err.println(this.toString());
1131            System.err.println("===============================================");
1132            for (int i = 0; i < _attributeNameList.size(); i++) {
1133                System.err.print(_attributeNameList.get(i));
1134                System.err.print('\t');
1135            }
1136            System.err.println();
1137
1138            while (next()) {
1139                for (int i = 0; i < _attributeNameList.size(); i++) {
1140                    System.err.print(getString((String JavaDoc) _attributeNameList.get(i)));
1141                    System.err.print('\t');
1142                }
1143                System.err.println();
1144            }
1145        }
1146    }
1147    interface LookAheadResultSet {
1148        public boolean next() throws SQLException;
1149        public boolean lookAhead();
1150        public boolean goBack();
1151        public long getLong(String JavaDoc name)throws SQLException;
1152        public long getLongFromBuffer(String JavaDoc name);
1153        public String JavaDoc getString(String JavaDoc name)throws SQLException;
1154        public String JavaDoc getStringFromBuffer(String JavaDoc name);
1155        public ResultSet getSqlResultSet();
1156        public void close() throws SQLException;
1157}
1158}
1159
Popular Tags