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 "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
00052
00053
00054
00055
00056
00057
00058
00059 void XMLProcessor::init()
00060 {
00061 DEBUG_INIT
00062 DEBUG_IN(XDFLengine)
00063
00064
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
00071 XMLPlatformUtils::Initialize();
00072
00073
00074 setThreadCount( 1);
00075 }
00076
00077
00078
00079
00080 void XMLProcessor::terminate()
00081 {
00082
00083 XMLPlatformUtils::Terminate();
00084
00085 logout << "_________________________________________________________________________\n";
00086
00087 DEBUG_OUT(XDFLengine)
00088 DEBUG_END
00089 }
00090
00091
00092
00093
00094
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
00126
00127
00128
00129
00130 void XMLProcessor::setThreadCount(unsigned int p_uiThreadCount)
00131 {
00132 DEBUG_FUNC(XMLProcessor::setThreadCount)
00133 g_uiMaxThreads = p_uiThreadCount;
00134 }
00135
00136
00137
00138
00139 unsigned int XMLProcessor::getThreadCount()
00140 {
00141 DEBUG_FUNC( XMLProcessor::getThreadCount)
00142 return g_uiMaxThreads;
00143 }
00144
00145
00146
00147
00148
00149
00150 XMLProcessor::XMLProcessor()
00151 {
00152
00153 m_pszActiveURI = 0;
00154
00155 m_fOwnContext = true;
00156 m_pContext = new XMLFlowContext(this,0, 0, CHAR_ENCODING_DEFAULT);
00157
00158
00159 setActiveURI( XMLPROCESSOR_ACTIVEURI);
00160 DEBUG_CREATE(XMLProcessor)
00161 }
00162
00163
00164
00165
00166 XMLProcessor::~XMLProcessor()
00167 {
00168 DEBUG_DEL(XMLProcessor)
00169
00170
00171 while(! m_vaStreamerFactories.empty()) delete( m_vaStreamerFactories.pop());
00172
00173
00174
00175 while(! m_pvaScriptCache.empty()) delete( m_pvaScriptCache.pop());
00176
00177
00178 if(m_fOwnContext) delete m_pContext;
00179 m_pszActiveURI = releaseCharBuffer( m_pszActiveURI);
00180
00181 }
00182
00183
00184
00185
00186
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
00196
00197 const char* XMLProcessor::getActiveURI()
00198 {
00199 return exportCharBuffer( m_pszActiveURI);
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 void XMLProcessor::loadStreamerFactories()
00209 {
00210 DEBUG_IN(XMLProcessor::loadStreamerFactories)
00211
00212
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
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
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
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
00290
00291
00292
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
00306
00307 XMLFlowContext* XMLProcessor::getContext() const
00308 {
00309 return m_pContext;
00310 }
00311
00312
00313
00314
00315
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
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
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;
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
00353 l_pStreamBuilder = new XMLFlowBuilder(this, l_pStreamRoot, XMLPROCESSOR_ACTIVEURI);
00354 l_pErrReporter = new XDFLSaxErrorReporter( "XMLProcessor::processXML");
00355
00356
00357 l_pStreamParser = new StreamSAX2Parser(l_pStreamBuilder, l_pErrReporter);
00358
00359
00360 (*p_pInput) >> (*l_pStreamParser);
00361 l_pStreamParser->commitStream(true);
00362
00363 }
00364 CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN
00365 catch( ... )
00366 {
00367
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
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
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
00416 p_pOutput = new NullConsumer();
00417 l_fDelOutput=true;
00418 }
00419
00420 loadValuers( p_pContext);
00421
00422
00423 p_pRootNode->streamXML( p_pOutput, p_pContext);
00424
00425 }
00426 CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN
00427 catch( ... )
00428 {
00429
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
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
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