00001
00002
00003
00004
00005
00006
00007
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "streamsax2parser.hpp"
00041
00042 #define STREAMINPUTSOURCE_SEGMENT 8192
00043 #define ERRCODE_LOC_STREAMINPUTSOURCE 9999
00044 #define STREAMINPUTSOURCE_TOKENSIZE 1024
00045 #define STREAMINPUTSOURCE_TOKENSIZE2 64
00046
00047 XERCES_CPP_NAMESPACE_USE;
00048
00049 BEGIN_XDFLENGINE_NS
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 ProgressiveInputStream::ProgressiveInputStream( StreamInputSource* p_pStreamInputSource)
00061 {
00062 m_uiVirtualPosition = 0;
00063 m_pStreamInputSource = p_pStreamInputSource;
00064 }
00065
00066
00067
00068
00069 ProgressiveInputStream::~ProgressiveInputStream()
00070 {
00071 }
00072
00073
00074
00075
00076 unsigned int ProgressiveInputStream::curPos() const
00077 {
00078 return m_uiVirtualPosition;
00079 }
00080
00081
00082
00083
00084 unsigned int ProgressiveInputStream::readBytes( XMLByte* const toFill, const unsigned int maxToRead)
00085 {
00086 unsigned int l_uiRead=0;
00087
00088 l_uiRead = m_pStreamInputSource->readData( (char*)toFill, maxToRead);
00089
00090
00091 m_uiVirtualPosition += l_uiRead;
00092 return l_uiRead;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 void StreamInputSource::setEncoding(const char* m_pszEncoding)
00109 {
00110 m_pxchEncoding = STRX(m_pszEncoding);
00111 }
00112
00113
00114
00115
00116
00117
00118 StreamInputSource::StreamInputSource()
00119 {
00120 m_uiBeginIndex=0;
00121 m_uiEndIndex=0;
00122 m_uiBufSize = STREAMINPUTSOURCE_TOKENSIZE;
00123 m_pszDataBuf = (char*) malloc( m_uiBufSize);
00124 m_fComplete=false;
00125 setEncoding( CHAR_ENCODING_DEFAULT);
00126 }
00127
00128
00129
00130
00131 StreamInputSource::~StreamInputSource()
00132 {
00133 XMLString::release(&m_pxchEncoding);
00134 free( m_pszDataBuf);
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144 void StreamInputSource::writeData( const char* p_pszData, unsigned int p_uiDataLen)
00145 {
00146 unsigned int l_uiDisp;
00147 unsigned int l_uiNewBufSize;
00148 bool l_fCirc;
00149
00150 if(p_pszData && (p_uiDataLen>0))
00151 {
00152
00153
00154 l_fCirc = false;
00155 l_uiDisp = m_uiBufSize - available() -1;
00156 if( m_uiBeginIndex > m_uiEndIndex) l_fCirc=true;
00157
00158 if( l_uiDisp < p_uiDataLen )
00159 {
00160
00161 l_uiNewBufSize=m_uiBufSize;
00162 while( l_uiNewBufSize-available()-1 < p_uiDataLen) l_uiNewBufSize = l_uiNewBufSize*2;
00163 m_pszDataBuf = (char*) realloc( m_pszDataBuf, l_uiNewBufSize);
00164
00165 if(l_fCirc )
00166 {
00167
00168 memcpy( m_pszDataBuf + l_uiNewBufSize - m_uiBufSize + m_uiBeginIndex, m_pszDataBuf + m_uiBeginIndex, m_uiBufSize - m_uiBeginIndex);
00169 m_uiBeginIndex = l_uiNewBufSize - m_uiBufSize + m_uiBeginIndex;
00170 }
00171
00172 m_uiBufSize = l_uiNewBufSize;
00173 }
00174
00175 if( (!l_fCirc) && ( m_uiBufSize - m_uiEndIndex < p_uiDataLen ) )
00176 {
00177
00178 memcpy(m_pszDataBuf + m_uiEndIndex, p_pszData, m_uiBufSize - m_uiEndIndex);
00179
00180 memcpy(m_pszDataBuf , p_pszData + m_uiBufSize - m_uiEndIndex, p_uiDataLen - m_uiBufSize + m_uiEndIndex);
00181 m_uiEndIndex = p_uiDataLen - m_uiBufSize + m_uiEndIndex;
00182 }
00183 else
00184 {
00185
00186 memcpy(m_pszDataBuf + m_uiEndIndex, p_pszData, p_uiDataLen);
00187 m_uiEndIndex+=p_uiDataLen;
00188 }
00189
00190
00191
00192 }
00193 }
00194
00195
00196
00197
00198 unsigned int StreamInputSource::readData( char* const p_pszToFill, const unsigned int p_uiMaxToRead)
00199 {
00200 bool l_fCirc;
00201 unsigned int l_uiDataSize;
00202 unsigned int l_uiDataRead;
00203 const char* l_pszNullPI="<?NULL?>";
00204
00205 l_fCirc = false;
00206 if( m_uiBeginIndex > m_uiEndIndex) l_fCirc=true;
00207
00208 l_uiDataSize = available();
00209
00210 l_uiDataRead = p_uiMaxToRead;
00211 if(l_uiDataRead > l_uiDataSize - STREAMINPUTSOURCE_TOKENSIZE ) l_uiDataRead = l_uiDataSize - STREAMINPUTSOURCE_TOKENSIZE ;
00212 if(l_uiDataSize <= STREAMINPUTSOURCE_TOKENSIZE) l_uiDataRead=32;
00213 if(l_uiDataSize <= STREAMINPUTSOURCE_TOKENSIZE2) l_uiDataRead=1;
00214
00215
00216 if(l_uiDataSize == 0)
00217 {
00218 if(!m_fComplete)
00219 {
00220 memcpy( p_pszToFill, l_pszNullPI, strlen(l_pszNullPI));
00221 return strlen(l_pszNullPI);
00222 }
00223 else return 0;
00224 }
00225
00226
00227 if( (! l_fCirc) || ( l_fCirc && ( m_uiBufSize - m_uiBeginIndex >= l_uiDataRead ) ) )
00228 {
00229
00230 memcpy( p_pszToFill, m_pszDataBuf + m_uiBeginIndex, l_uiDataRead);
00231 m_uiBeginIndex += l_uiDataRead;
00232 if( m_uiBeginIndex == m_uiBufSize) m_uiBeginIndex=0;
00233 }
00234 else
00235 {
00236
00237 memcpy( p_pszToFill, m_pszDataBuf + m_uiBeginIndex, m_uiBufSize - m_uiBeginIndex);
00238
00239 memcpy( p_pszToFill + m_uiBufSize - m_uiBeginIndex, m_pszDataBuf, l_uiDataRead - m_uiBufSize + m_uiBeginIndex);
00240 m_uiBeginIndex = (l_uiDataRead - m_uiBufSize + m_uiBeginIndex);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 return l_uiDataRead;
00253 }
00254
00255
00256
00257
00258 void StreamInputSource::flushData()
00259 {
00260 m_uiBeginIndex=0;
00261 m_uiEndIndex=0;
00262 }
00263
00264
00265
00266
00267 unsigned int StreamInputSource::available()
00268 {
00269 if( m_uiEndIndex - m_uiBeginIndex == m_uiBufSize) return 0;
00270 if( m_uiBeginIndex <= m_uiEndIndex) return m_uiEndIndex - m_uiBeginIndex;
00271 else return m_uiBufSize + m_uiEndIndex - m_uiBeginIndex;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 const XMLCh * StreamInputSource::getEncoding () const
00281 {
00282 return m_pxchEncoding;
00283 }
00284
00285
00286
00287
00288
00289
00290 BinInputStream* StreamInputSource::makeStream() const
00291 {
00292 return new ProgressiveInputStream( (StreamInputSource*) this);
00293 }
00294
00295
00296
00297
00298 void StreamInputSource::setComplete()
00299 {
00300 m_fComplete=true;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 StreamSAX2Parser::StreamSAX2Parser(ContentHandler* p_pContentHandler, ErrorHandler* p_pErrorHandler)
00324 {
00325 m_fParseStarted = false;
00326
00327 m_pContentHandler = p_pContentHandler;
00328 m_pErrorHandler = p_pErrorHandler;
00329
00330
00331 m_pSAX2XMLReader = XMLReaderFactory::createXMLReader();
00332 m_pSAX2XMLReader->setFeature( XMLUni::fgXercesContinueAfterFatalError, true);
00333 m_pSAX2XMLReader->setFeature( XMLUni::fgXercesDynamic, true);
00334 m_pSAX2XMLReader->setFeature( XMLUni::fgXercesSchema, false);
00335 m_pSAX2XMLReader->setFeature( XMLUni::fgXercesSchemaFullChecking, false);
00336
00337 m_pSAX2XMLReader->setFeature( XMLUni::fgSAX2CoreValidation, true);
00338 m_pSAX2XMLReader->setFeature( XMLUni::fgSAX2CoreNameSpaces, true);
00339 m_pSAX2XMLReader->setFeature( XMLUni::fgSAX2CoreNameSpacePrefixes, true);
00340
00341
00342 m_pSAX2XMLReader->setContentHandler( p_pContentHandler);
00343 m_pSAX2XMLReader->setErrorHandler ( p_pErrorHandler);
00344 }
00345
00346
00347
00348
00349 StreamSAX2Parser::~StreamSAX2Parser()
00350 {
00351
00352 delete( m_pSAX2XMLReader);
00353 }
00354
00355
00356
00357
00358
00359
00360 void StreamSAX2Parser::writeData( const char* p_pszData, unsigned int p_uiDataLen)
00361 {
00362 m_StreamInputSource.writeData( p_pszData, p_uiDataLen);
00363 }
00364
00365
00366
00367
00368 bool StreamSAX2Parser::commitStream(bool l_fFinal)
00369 {
00370 bool l_fParseResp = false;
00371 bool l_fGotMore = true;
00372
00373
00374
00375 if(l_fFinal) m_StreamInputSource.setComplete();
00376
00377 if( !m_fParseStarted)
00378 {
00379 l_fParseResp = m_pSAX2XMLReader->parseFirst( m_StreamInputSource, m_ScanToken);
00380 if( !l_fParseResp) return false;
00381 else m_fParseStarted = true;
00382 }
00383
00384 while( l_fGotMore && ( l_fFinal || ( m_StreamInputSource.available() > STREAMINPUTSOURCE_TOKENSIZE ) ))
00385 {
00386 l_fGotMore = m_pSAX2XMLReader->parseNext( m_ScanToken);
00387 if(m_pSAX2XMLReader->getErrorCount() ) return false;
00388 }
00389
00390 return true;
00391 }
00392
00393
00394
00395
00396
00397 END_XDFLENGINE_NS
00398