Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

streamsax2parser.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //-----------------------------------------------------------------------------
00006 //  STREAMSAX2PARSER.CPP
00007 //----------------------------------------------------------------------------- 
00013 //_____________________________________________________________________________
00014 //
00015 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00016 //
00017 //  This file is part of the XDFLengine project.
00018 //
00019 //  The XDFLengine is free software; you can redistribute it and/or modify
00020 //  it under the terms of the GNU General Public License as published by
00021 //  the Free Software Foundation; either version 2 of the License, or
00022 //  (at your option) any later version.
00023 //
00024 //  This program is distributed in the hope that it will be useful,
00025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 //  GNU General Public License for more details.
00028 //
00029 //  You should have received a copy of the GNU General Public License
00030 //  along with this program; if not, write to the Free Software
00031 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00032 //  USA.
00033 //
00034 //  For more information, 
00035 //      contact  : guillaume@baurand.net 
00036 //      or visit : http://xdflengine.sourceforge.net
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 //  CLASS PROGRESSIVEINPUTSTREAM
00054 //----------------------------------------------------------------------------- 
00055 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00056 
00057     //_____________________________________________________________________
00058     //  PROGRESSIVEINPUTSTREAM
00059     //--------------------------------------------------------------------- 
00060     ProgressiveInputStream::ProgressiveInputStream( StreamInputSource* p_pStreamInputSource)
00061     {
00062         m_uiVirtualPosition = 0;
00063         m_pStreamInputSource = p_pStreamInputSource;
00064     }
00065 
00066     //_____________________________________________________________________
00067     //  ~PROGRESSIVEINPUTSTREAM
00068     //--------------------------------------------------------------------- 
00069     ProgressiveInputStream::~ProgressiveInputStream()
00070     {
00071     }
00072 
00073     //_____________________________________________________________________
00074     //  CURPOS
00075     //--------------------------------------------------------------------- 
00076     unsigned int ProgressiveInputStream::curPos() const
00077     {
00078         return m_uiVirtualPosition;
00079     }
00080 
00081     //_____________________________________________________________________
00082     //  READBYTES
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 //  CLASS STREAMINPUTSOURCE
00103 //----------------------------------------------------------------------------- 
00104 
00105     //_____________________________________________________________________
00106     //  SETENCODING
00107     //--------------------------------------------------------------------- 
00108     void StreamInputSource::setEncoding(const char* m_pszEncoding)
00109     {
00110         m_pxchEncoding = STRX(m_pszEncoding);
00111     }
00112 
00113 // CONSTRUCTOR AND DESCTRUCTOR
00114 
00115     //_____________________________________________________________________
00116     //  STREAMINPUTSOURCE
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     //  ~STREAMINPUTSOURCE
00130     //--------------------------------------------------------------------- 
00131     StreamInputSource::~StreamInputSource()
00132     {
00133         XMLString::release(&m_pxchEncoding);    
00134         free( m_pszDataBuf);
00135     }
00136 
00137 // DATA BUFFER MANAGEMENT
00138 
00139 
00140 
00141     //_____________________________________________________________________
00142     //  WRITEDATA
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             // check buffer size
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                 // realloc buffer
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                     // if circular, copy data at end of old block to the end of the new block.
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                 // copy first part at end of block ...
00178                 memcpy(m_pszDataBuf + m_uiEndIndex, p_pszData, m_uiBufSize - m_uiEndIndex);
00179                 // ... and the rest at the beginning
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                 // enough space : copy at end of current data
00186                 memcpy(m_pszDataBuf + m_uiEndIndex, p_pszData, p_uiDataLen);
00187                 m_uiEndIndex+=p_uiDataLen;
00188             }
00189 
00190 
00191             //logout << "\n+" << p_uiDataLen << "=" << available() << " /" << m_uiBufSize << "\n";
00192         }
00193     }
00194 
00195     //_____________________________________________________________________
00196     //  READDATA
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             // copy data from block to output buffer
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             // circular : copy end of the block to the begin ouput buffer ..
00237             memcpy( p_pszToFill, m_pszDataBuf + m_uiBeginIndex, m_uiBufSize - m_uiBeginIndex);
00238             //.. and the begin of the block to the end of the ouput buffer
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         char* pick=(char*)malloc(l_uiDataRead+1);
00245         memcpy(pick, p_pszToFill, l_uiDataRead);
00246         pick[l_uiDataRead]='\0';
00247         logout << pick << ".";
00248         free(pick);
00249         */
00250         //logout << "\n-" << l_uiDataRead << "=" << available() << " /" << m_uiBufSize << "\n";
00251 
00252         return l_uiDataRead;
00253     }
00254 
00255     //_____________________________________________________________________
00256     //  FLUSHDATA
00257     //--------------------------------------------------------------------- 
00258     void StreamInputSource::flushData()
00259     {
00260         m_uiBeginIndex=0;
00261         m_uiEndIndex=0;
00262     }
00263 
00264     //_____________________________________________________________________
00265     //  AVAILABLE
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 // INPUTSOURCE
00276 
00277     //_____________________________________________________________________
00278     //  GETENCODING
00279     //--------------------------------------------------------------------- 
00280     const XMLCh * StreamInputSource::getEncoding () const
00281     {
00282         return m_pxchEncoding;
00283     }
00284     
00285     
00286     
00287     //_____________________________________________________________________
00288     //  MAKESTREAM
00289     //--------------------------------------------------------------------- 
00290     BinInputStream* StreamInputSource::makeStream() const
00291     {
00292         return new ProgressiveInputStream( (StreamInputSource*) this);
00293     }
00294 
00295     //_____________________________________________________________________
00296     //  SETCOMPLETE
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 //  CLASS STREAMSAX2PARSER
00316 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00317 
00318 // CONSTRUCTOR AND DESCTRUCTOR
00319 
00320     //_____________________________________________________________________
00321     //  STREAMSAX2PARSER
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         // inits the SAX parser object.  
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         // Give handler to the parser
00342         m_pSAX2XMLReader->setContentHandler( p_pContentHandler);
00343         m_pSAX2XMLReader->setErrorHandler  ( p_pErrorHandler);
00344     }
00345 
00346     //_____________________________________________________________________
00347     //  STREAMSAX2PARSER
00348     //---------------------------------------------------------------------
00349     StreamSAX2Parser::~StreamSAX2Parser()
00350     {
00351         //m_pSAX2XMLReader->parseReset( m_ScanToken);
00352         delete( m_pSAX2XMLReader);
00353     }
00354 
00355 // STREAM OPERATORS
00356 
00357     //_____________________________________________________________________
00358     //  WRITEDATA
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     //  COMMITSTREAM
00367     //---------------------------------------------------------------------
00368     bool StreamSAX2Parser::commitStream(bool l_fFinal)
00369     {
00370         bool l_fParseResp = false;
00371         bool l_fGotMore = true;
00372 
00373         //logout << "commit for "<< m_StreamInputSource.available() ;
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 

Generated on Sat Oct 4 13:20:01 2003 for XDFLengine by doxygen1.3-rc2