1 22 23 package org.xquark.extractor.runtime; 24 25 26 import java.io.Writer ; 27 import java.util.List ; 28 29 import org.w3c.dom.Document ; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.ErrorHandler ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.ext.LexicalHandler ; 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 RCSRevision = "$Revision: 1.2 $"; 39 private static final String RCSName = "$Name: $"; 40 41 protected ContentHandler _contentHandler = null; 42 protected LexicalHandler _lexicalhandler = null; 43 protected ErrorHandler _errorhandler = null; 44 45 private List _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 compiledExprList, XMLStatementInternal statement) throws XMLDBCException{ 54 setExpressionList(compiledExprList); 55 setStatement(statement); 56 } 57 58 public void setExpressionList(List 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 78 public void setContentHandler(ContentHandler contentHandler) { 79 if (contentHandler == null) 80 throw new NullPointerException ("ContentHandler cannot be null."); 81 _contentHandler = contentHandler; 82 } 83 84 87 public void setLexicalHandler(LexicalHandler lexicalhandler) { 88 if (lexicalhandler == null) 89 throw new NullPointerException ("LexicalHandler cannot be null."); 90 _lexicalhandler = lexicalhandler; 91 } 92 93 96 public void setErrorHandler(ErrorHandler errorhandler) { 97 if (errorhandler == null) 99 throw new NullPointerException ("ErrorHandler cannot be null."); 100 _errorhandler = errorhandler; 101 } 102 103 106 public ContentHandler getContentHandler() { 107 return _contentHandler; 108 } 109 110 113 public LexicalHandler getLexicalHandler() { 114 return _lexicalhandler; 115 } 116 117 120 public ErrorHandler 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 134 public void close() throws XMLDBCException { 135 if (_currenResultSet != null) { 136 _currenResultSet.close(); 137 _currenResultSet = null; 138 } 139 } 140 141 142 143 146 public boolean isBeforeFirst() throws XMLDBCException { 147 return 0 == _positionAccumulater + _currenResultSet.getPosition() ? true : false; 148 } 149 150 153 public Document nextAsDocument() throws XMLDBCException { 154 return _currenResultSet.nextAsDocument(); 155 } 156 157 158 public org.w3c.dom.Node nextAsDOM() throws XMLDBCException { 159 return _currenResultSet.nextAsDOM(); 160 } 161 162 public void nextAsDOM(org.w3c.dom.Element parent) throws XMLDBCException { 163 _currenResultSet.nextAsDOM(parent); 164 } 165 166 169 public String nextAsString() throws XMLDBCException { 170 return _currenResultSet.nextAsString(); 171 } 172 173 public void nextAsStream(Writer out) throws XMLDBCException { 174 _currenResultSet.nextAsStream(out); 175 } 176 177 180 public void nextAsSAX() throws XMLDBCException, SAXException { 181 _currenResultSet.nextAsSAX(); 182 } 183 184 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 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 232 public java.util.Map getPrefixMap() throws XMLDBCException { 233 return java.util.Collections.EMPTY_MAP; 234 } 235 236 248 public XMLDocument getFragmentsAsDocument(String namespace, String localName, String qName) throws XMLDBCException { 249 XMLDocument retVal = null; 250 return retVal; 251 } 252 253 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 |