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

xmlprocessor.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //----------------------------------------------------------------------------- 
00006 //  XMLPROCESSOR.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 "xercesc/util/PlatformUtils.hpp"
00041 #   include "xercesc/util/XMLString.hpp"
00042 
00043 #   include "xmlprocessor.hpp"
00044 #   include "flow/consumers/streamsax2parser.hpp"
00045 #   include "flow/consumers/nullconsumer.hpp"
00046 #   include "streamers/xmlstreamerfactories.hpp"
00047 
00048 BEGIN_XDFLENGINE_NS
00049 
00050 //============================================================================= 
00051 //  CLASS XMLPROCESSOR
00052 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00053 
00054 // STATICS
00055 
00056     //_________________________________________________________________________
00057     //  INIT
00058     //-------------------------------------------------------------------------
00059     void XMLProcessor::init()
00060     {
00061         DEBUG_INIT
00062         DEBUG_IN(XDFLengine)
00063 
00064         // Display version info 
00065         logout << "\n_________________________________________________________________________\n";
00066         logout.display_version( "XDFLengine", XDFLENGINE_NAME, XDFLENGINE_VERSION, "");
00067         logout << "----------------------------------------------------------------------\n";
00068         logout.display_version( "XERCES", "Xerces-C++", XERCES_FULLVERSIONSTR, "The Apache Software Foundation");
00069 
00070         // Start Xerces
00071         XMLPlatformUtils::Initialize();
00072 
00073         // Set default settings 
00074         setThreadCount( 1);
00075     }
00076 
00077     //_________________________________________________________________________
00078     //  TERMINATE
00079     //-------------------------------------------------------------------------
00080     void XMLProcessor::terminate()
00081     {
00082         // Terminate Xerces
00083         XMLPlatformUtils::Terminate();
00084 
00085         logout << "_________________________________________________________________________\n";
00086 
00087         DEBUG_OUT(XDFLengine)
00088         DEBUG_END
00089     }
00090 
00091 // BUFFER 
00092 
00093     //_________________________________________________________________________
00094     //  GETNEWBUFFER
00095     //-------------------------------------------------------------------------
00096     XMLStreamBuffer* XMLProcessor::getNewBuffer(const char* p_pszBufferType ,const char* p_pszFileName, bool p_fReadOnly, unsigned int p_uiBufferSize) 
00097     {
00098         //---------------------------------------------------------------------
00099         XMLStreamBuffer*    l_pXMLStreamBuffer=0;
00100         //---------------------------------------------------------------------
00101 
00102         DEBUG_FUNC(XMLProcessor::getNewBuffer)
00103 
00104         if( ! p_pszBufferType) p_pszBufferType = XMLPROCESSOR_STRING;
00105         if( ! p_pszFileName)
00106         {
00107             
00108             if( ! strcmp( p_pszBufferType, XMLPROCESSOR_FILE) )     l_pXMLStreamBuffer= (XMLStreamBuffer*) new FileBuffer();
00109             if( ! strcmp( p_pszBufferType, XMLPROCESSOR_ZIP) )      l_pXMLStreamBuffer= (XMLStreamBuffer*) new ZipBuffer();
00110             if( ! strcmp( p_pszBufferType, XMLPROCESSOR_STRING) )   l_pXMLStreamBuffer= (XMLStreamBuffer*) new MemBuffer();
00111         }
00112         else
00113         {
00114             if( ! strcmp( p_pszBufferType, XMLPROCESSOR_FILE) )     l_pXMLStreamBuffer= (XMLStreamBuffer*) new FileBuffer(  p_pszFileName, p_fReadOnly, p_uiBufferSize);
00115             if( ! strcmp( p_pszBufferType, XMLPROCESSOR_ZIP) )      l_pXMLStreamBuffer= (XMLStreamBuffer*) new ZipBuffer(   p_pszFileName, p_fReadOnly, p_uiBufferSize);
00116         }
00117 
00118         if( ! l_pXMLStreamBuffer )  THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_XMLPROCESSOR + ERRCODE_CAUSE_MISSINGREF , "Invalid buffer type." , p_pszBufferType, "ActiveStreamNode::streamXML", "", false );
00119 
00120 
00121         return l_pXMLStreamBuffer;
00122     }
00123 
00124 
00125 // THREAD COUNT
00126 
00127     //_____________________________________________________________________
00128     //  SETTHREADCOUNT
00129     //---------------------------------------------------------------------
00130     void XMLProcessor::setThreadCount(unsigned int p_uiThreadCount)
00131     {
00132         DEBUG_FUNC(XMLProcessor::setThreadCount)
00133         g_uiMaxThreads = p_uiThreadCount;
00134     }
00135 
00136     //_____________________________________________________________________
00137     //  GETTHREADCOUNT
00138     //---------------------------------------------------------------------
00139     unsigned int XMLProcessor::getThreadCount()
00140     {
00141         DEBUG_FUNC( XMLProcessor::getThreadCount)
00142         return g_uiMaxThreads;
00143     }
00144 
00145 //  CONSTRUCTOR & DESTRUCTOR
00146 
00147     //_________________________________________________________________________
00148     //  XMLPROCESSOR
00149     //-------------------------------------------------------------------------
00150     XMLProcessor::XMLProcessor()
00151     {
00152         // Init pointers
00153         m_pszActiveURI = 0;
00154 
00155         m_fOwnContext = true;
00156         m_pContext = new XMLFlowContext(this,0, 0, CHAR_ENCODING_DEFAULT);
00157 
00158         // Set default URI
00159         setActiveURI( XMLPROCESSOR_ACTIVEURI);
00160         DEBUG_CREATE(XMLProcessor)
00161     }
00162 
00163     //_________________________________________________________________________
00164     //  ~XMLPROCESSOR
00165     //-------------------------------------------------------------------------
00166     XMLProcessor::~XMLProcessor()
00167     {
00168         DEBUG_DEL(XMLProcessor) 
00169 
00170         // delete streamer factories
00171         while(! m_vaStreamerFactories.empty()) delete( m_vaStreamerFactories.pop());
00172 
00173 
00174         // delete cached scripts
00175         while(! m_pvaScriptCache.empty()) delete( m_pvaScriptCache.pop());
00176 
00177         // release owned objects
00178         if(m_fOwnContext) delete m_pContext;
00179         m_pszActiveURI = releaseCharBuffer( m_pszActiveURI);
00180             
00181     }
00182 
00183 // ACTIVE URI
00184 
00185     //_________________________________________________________________________
00186     //  SETACTIVEURI
00187     //-------------------------------------------------------------------------
00188     void XMLProcessor::setActiveURI( const char* p_pszActiveURI)
00189     {
00190         DEBUG_FUNC(XMLProcessor::setActiveURI)      
00191         m_pszActiveURI = importCharBuffer( m_pszActiveURI, p_pszActiveURI);
00192     }
00193 
00194     //_________________________________________________________________________
00195     //  GETACTIVEURI
00196     //-------------------------------------------------------------------------
00197     const char* XMLProcessor::getActiveURI()
00198     {
00199         return exportCharBuffer( m_pszActiveURI);
00200     }
00201 
00202 // STREAMERS
00203 
00204 
00205     //_________________________________________________________________________
00206     //  LOADHANDLERS
00207     //-------------------------------------------------------------------------     
00208     void XMLProcessor::loadStreamerFactories()
00209     {
00210                 DEBUG_IN(XMLProcessor::loadStreamerFactories)
00211 
00212         // Add XML Handlers
00213         addStreamerFactory( new ValuesStreamerFactory,              VALUESSTREAMER_TAGNAME );       
00214         addStreamerFactory( new NamedBufferSetStreamerFactory,      NAMEDBUFSETSTREAMER_TAGNAME );
00215         addStreamerFactory( new NamedBufferGetStreamerFactory,      NAMEDBUFGETSTREAMER_TAGNAME );
00216         addStreamerFactory( new NullStreamerFactory,                NULLSTREAMER_TAGNAME );   
00217         addStreamerFactory( new RawStreamerFactory,                 RAWSTREAMER_TAGNAME );
00218         addStreamerFactory( new SystemStreamerFactory,              SYSTEMSTREAMER_TAGNAME );
00219         addStreamerFactory( new RaiseErrorStreamerFactory,          RAISEERRORSTREAMER_TAGNAME );
00220         addStreamerFactory( new IFStreamerFactory,                  IFSTREAMER_TAGNAME);
00221         addStreamerFactory( new FSSetStreamerFactory,               FSSETSTREAMER_TAGNAME );      
00222         addStreamerFactory( new FSGetStreamerFactory,               FSGETSTREAMER_TAGNAME );        
00223         addStreamerFactory( new DBOBJGetStreamerFactory,            DBOBJGETSTREAMER_TAGNAME    );  
00224         addStreamerFactory( new DBOBJSetStreamerFactory,            DBOBJSETSTREAMER_TAGNAME    );
00225         addStreamerFactory( new AutodescStreamerFactory,            AUTODESCSTREAMER_TAGNAME    );
00226         addStreamerFactory( new DDLStreamerFactory,                 DDLSTREAMER_TAGNAME );
00227         addStreamerFactory( new SQLStreamerFactory,                 SQLSTREAMER_TAGNAME );
00228         addStreamerFactory( new ZipStreamerFactory,                 ZIPSTREAMER_TAGNAME );    
00229         addStreamerFactory( new UnzipStreamerFactory,               UNZIPSTREAMER_TAGNAME );
00230         addStreamerFactory( new FuncCallStreamerFactory,            FUNCCALLSTREAMER_TAGNAME ); 
00231         addStreamerFactory( new ExecStreamerFactory,                EXECSTREAMER_TAGNAME );
00232         addStreamerFactory( new LangSetStreamerFactory,             LANGSETSTREAMER_TAGNAME );
00233 
00234         addStreamerFactory( new CacheDefinitionStreamerFactory,     CACHEDEFINITIONSTREAMER_TAGNAME );
00235         addStreamerFactory( new FuncDefineStreamerFactory,          FUNCDEFINESTREAMER_TAGNAME ); 
00236         addStreamerFactory( new LogStreamerFactory,                 LOGSTREAMER_TAGNAME ); 
00237         addStreamerFactory( new _ValuesStreamerFactory,             S_VALUESSTREAMER_TAGNAME );
00238         addStreamerFactory( new _NamedBufferSetStreamerFactory,     S_NAMEDBUFSETSTREAMER_TAGNAME );
00239         addStreamerFactory( new LangLoadStreamerFactory,            LANGLOADSTREAMER_TAGNAME );
00240         addStreamerFactory( new CacheScriptStreamerFactory,         CACHESCRIPTSTREAMER_TAGNAME );
00241         
00242         logout << "\n";
00243 
00244                 DEBUG_OUT(XMLProcessor::loadStreamerFactories)
00245 
00246     }
00247 
00248 
00249     //_________________________________________________________________________
00250     //  LOADVALUERS
00251     //-------------------------------------------------------------------------
00252     void XMLProcessor::loadValuers( XMLFlowContext* p_pContext) const
00253     {
00254         p_pContext->addValuer( new DateValuer,                      DATEVALUER_TAG );
00255         p_pContext->addValuer( new EnvValuer,                       ENVVALUER_TAG );
00256         p_pContext->addValuer( new ParamValuer( p_pContext),        PARAMVALUER_TAG );
00257         p_pContext->addValuer( new XpathDBValuer,                   XPATHVALUER_TAG );
00258         p_pContext->addValuer( new SQLDBValuer,                     SQLVALUER_TAG );
00259         p_pContext->addValuer( new LangValuer( p_pContext),         LANGVALUER_TAG );
00260 
00261         logout << "\n";
00262     }
00263 
00264     //_________________________________________________________________________
00265     //  ADDSTREAMERFACTORY
00266     //-------------------------------------------------------------------------
00267     void XMLProcessor::addStreamerFactory ( XMLStreamerFactory* p_pFactory, const char* p_pszName)
00268     {
00269         DEBUG_IN(XMLProcessor::addStreamerFactory)
00270 
00271         p_pFactory->setParent(this);
00272 
00273                 DEBUG_ECHO << "Load " << p_pszName << "...\n";
00274         m_vaStreamerFactories.add( p_pFactory, p_pszName);
00275 
00276                 DEBUG_OUT(XMLProcessor::addStreamerFactory)
00277     }
00278 
00279     //_________________________________________________________________________
00280     //  GETSTREAMERFACTORY
00281     //-------------------------------------------------------------------------
00282     XMLStreamerFactory* XMLProcessor::getStreamerFactory ( const char* p_pszName) const
00283     {
00284         DEBUG_FUNC(XMLProcessor::getStreamerFactory)
00285 
00286         return m_vaStreamerFactories.get( p_pszName);
00287     }
00288 
00289 // CONTEXT
00290 
00291     //_____________________________________________________________________
00292     //  SETCONTEXT
00293     //---------------------------------------------------------------------
00294     void XMLProcessor::setContext(XMLFlowContext* p_pContext)
00295     {
00296         DEBUG_FUNC(XMLProcessor::setContext)
00297         if(m_pContext)
00298         { 
00299             if(m_fOwnContext) delete p_pContext;
00300         }
00301         m_pContext = p_pContext;
00302     }
00303 
00304     //_____________________________________________________________________
00305     //  GETCONTEXT
00306     //---------------------------------------------------------------------
00307     XMLFlowContext* XMLProcessor::getContext() const
00308     {
00309         return m_pContext;
00310     }
00311 
00312 // SCRIPT CACHE FEATURES
00313 
00314     //_____________________________________________________________________
00315     //  CACHESCRIPT
00316     //---------------------------------------------------------------------
00317     void XMLProcessor::cacheScript( RootStreamNode* p_pRootNode, const char* p_pszCacheName, unsigned int p_uiThreadId)
00318     {
00319         m_pvaScriptCache.add( p_pRootNode, p_pszCacheName);
00320     }
00321 
00322     //_____________________________________________________________________
00323     //  GETCACHEDSCRIPT
00324     //---------------------------------------------------------------------
00325     const RootStreamNode*   XMLProcessor::getCachedScript( const char* p_pszCacheName, unsigned int p_uiThreadId)
00326     {
00327         return m_pvaScriptCache.get( p_pszCacheName);
00328     }
00329 
00330 
00331 
00332     //_____________________________________________________________________
00333     //  COMPILE
00334     //---------------------------------------------------------------------
00335     RootStreamNode* XMLProcessor::compile( XMLStreamProvider* p_pInput, unsigned int p_uiThreadId ) const
00336     {
00337         //---------------------------------------------------------------------
00338         StreamSAX2Parser*           l_pStreamParser=0;
00339         XMLFlowBuilder*             l_pStreamBuilder=0;
00340         RootStreamNode*             l_pStreamRoot=0;
00341         XDFLSaxErrorReporter*   l_pErrReporter=0;   // Error reporter for the sax parsing
00342         //---------------------------------------------------------------------
00343 
00344         PREP_CATCH_XML_FLOW_ERROR;      
00345 
00346         logout << "\n--[ compiling ]-------------------------------------------------------\n";
00347 
00348         WATCH_XML_FLOW_ERROR { 
00349 
00350             l_pStreamRoot = new RootStreamNode;
00351             
00352             // build handlers
00353             l_pStreamBuilder = new XMLFlowBuilder(this, l_pStreamRoot, XMLPROCESSOR_ACTIVEURI);
00354             l_pErrReporter = new XDFLSaxErrorReporter( "XMLProcessor::processXML");
00355 
00356             // build parser
00357             l_pStreamParser = new StreamSAX2Parser(l_pStreamBuilder, l_pErrReporter);
00358 
00359             // parse !
00360             (*p_pInput) >> (*l_pStreamParser);
00361             l_pStreamParser->commitStream(true);
00362 
00363         } 
00364         CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN
00365         catch( ... )
00366         {
00367             // Unknown error handling
00368 
00369             MAKE_XMLFLOW_EXCEPTION( ERRCODE_LOC_XMLPROCESSOR + ERRCODE_CAUSE_UNKNOWN , "Unknown fatal error while compiling." , "", "XMLProcessor::compile", "", false );           
00370         }
00371 
00372     RELEASE_AND_RETURN: 
00373 
00374         logout << "----------------------------------------------------------------------\n";
00375 
00376         delete l_pStreamParser;
00377         delete l_pErrReporter;
00378         delete l_pStreamBuilder;
00379 
00380         ON_XML_FLOW_ERROR_DO delete l_pStreamRoot;
00381         ON_XML_FLOW_ERROR_THROW;
00382     
00383         return l_pStreamRoot;
00384     }
00385     
00386     //_____________________________________________________________________
00387     //  EXECUTE
00388     //---------------------------------------------------------------------
00389     int XMLProcessor::execute( const RootStreamNode* p_pRootNode, XMLStreamConsumer* p_pOutput, XMLFlowContext* p_pContext) const
00390     {
00391         //---------------------------------------------------------------------
00392         bool                l_fDelContext;
00393         bool                l_fDelOutput;
00394         unsigned int        l_uiError;
00395         //---------------------------------------------------------------------
00396 
00397         PREP_CATCH_XML_FLOW_ERROR;  
00398         
00399         logout << "\n--[ executing ]-------------------------------------------------------\n";
00400 
00401         WATCH_XML_FLOW_ERROR {      
00402 
00403             l_uiError = 0;
00404             l_fDelContext = false;
00405             if(! p_pContext)
00406             {
00407                 // if no context provided; create one for the request
00408                 p_pContext = new XMLFlowContext( this, getContext(),  0, getContext()->getEncoding());
00409                 l_fDelContext = true;
00410             }
00411 
00412             l_fDelOutput=false;
00413             if(! p_pOutput)
00414             {
00415                 // if no output provided, create a null consumer to receive data.
00416                 p_pOutput = new NullConsumer();
00417                 l_fDelOutput=true;
00418             }   
00419 
00420             loadValuers( p_pContext);
00421 
00422             // execute script : stream through root node
00423             p_pRootNode->streamXML( p_pOutput, p_pContext);
00424 
00425         } 
00426         CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN
00427         catch( ... )
00428         {
00429             // Unknown error handling
00430 
00431             MAKE_XMLFLOW_EXCEPTION( ERRCODE_LOC_XMLPROCESSOR + ERRCODE_CAUSE_UNKNOWN , "Unknown fatal error while executing." , "", "XMLProcessor::execute", "", false );           
00432         }
00433 
00434     RELEASE_AND_RETURN:
00435 
00436         logout << "----------------------------------------------------------------------\n";
00437 
00438 
00439         if(l_fDelContext) delete p_pContext;
00440         if(l_fDelOutput) delete p_pOutput;
00441 
00442         ON_XML_FLOW_ERROR_THROW;
00443 
00444         return l_uiError;
00445 
00446     }
00447 
00448     //_____________________________________________________________________
00449     //  PROCESS
00450     //---------------------------------------------------------------------
00451     int XMLProcessor::process( XMLStreamProvider* p_pInput , XMLStreamConsumer* p_pOutput, XMLFlowContext* p_pContext) const
00452     {
00453         //-----------------------------------------------------------------
00454         RootStreamNode*             l_pStreamRoot=0;
00455         unsigned int                l_uiError=0;
00456         unsigned int                l_uiThreadId=0;
00457         //-----------------------------------------------------------------
00458         PREP_CATCH_XML_FLOW_ERROR;
00459 
00460         if( p_pContext) l_uiThreadId = p_pContext->getThreadId();
00461 
00462         WATCH_XML_FLOW_ERROR
00463         {
00464             l_pStreamRoot = compile( p_pInput, l_uiThreadId);
00465             l_uiError = execute( l_pStreamRoot, p_pOutput, p_pContext);
00466         }
00467         CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN
00468 
00469     RELEASE_AND_RETURN:
00470 
00471         delete( l_pStreamRoot);
00472         ON_XML_FLOW_ERROR_THROW;
00473         return l_uiError;
00474     }
00475 
00476 
00477     //_____________________________________________________________________
00478     //  TICK
00479     //---------------------------------------------------------------------
00480     void XMLProcessor::tick(unsigned long  p_ulClock)
00481     {
00482         //-----------------------------------------------------------------
00483         unsigned int i;
00484         //-----------------------------------------------------------------
00485 
00486         logout << "\n! TICK #" << p_ulClock << "!";
00487         for( i=0; i< m_vaStreamerFactories.size(); i++)
00488         {
00489             m_vaStreamerFactories.get(i)->tick( p_ulClock);
00490         }
00491     }
00492 
00493 
00494 
00495 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00496 //=============================================================================
00497 
00498 END_XDFLENGINE_NS

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