3#include "rdbModel/Management/XercesBuilder.h"
4#include "facilities/Util.h"
5#include "rdbModel/Management/Manager.h"
6#include "rdbModel/Tables/Assertion.h"
7#include "rdbModel/Tables/Column.h"
8#include "rdbModel/Tables/Datatype.h"
9#include "rdbModel/Tables/Index.h"
10#include "rdbModel/Tables/InsertNew.h"
11#include "rdbModel/Tables/InterRow.h"
12#include "rdbModel/Tables/Query.h"
13#include "rdbModel/Tables/Set.h"
14#include "rdbModel/Tables/Supersede.h"
15#include "rdbModel/Tables/Table.h"
16#include "xmlBase/Dom.h"
17#include "xmlBase/XmlParser.h"
22#define SCHEMA_MAJOR_VERSION 2
23#define SCHEMA_MINOR_VERSION 0
25 using XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument;
26 using XERCES_CPP_NAMESPACE_QUALIFIER DOMElement;
36 m_doc = parser.
parse( filename.c_str() );
38 return ( m_doc == 0 ) ? 0xffffffff : 0;
46 if ( m_doc == 0 )
return 0;
48 DOMElement* docElt = m_doc->getDocumentElement();
51 m_rdb->m_dbName = Dom::getAttribute( docElt,
"dbs" );
52 m_rdb->m_majorVersion = 0;
53 m_rdb->m_minorVersion = 0;
55 std::string versionString = Dom::getAttribute( docElt,
"SchemaVersion" );
56 if ( !versionString.size() ) { versionString = Dom::getAttribute( docElt,
"DTDversion" ); }
58 unsigned dotPos = versionString.find(
'.' );
60 std::string minorStr = std::string( versionString, dotPos + 1 );
62 versionString.resize( dotPos );
69 { std::cerr <<
"rdbModel::XercesBuilder: Bad version string " << std::endl; }
70 m_rdb->m_CVSid = Dom::getAttribute( docElt,
"CVSid" );
73 std::cerr <<
"Schema major version " << m_rdb->m_majorVersion
75 std::cerr <<
"Bye for now";
81 std::vector<DOMElement*> tables;
82 Dom::getChildrenByTagName( docElt,
"table", tables );
83 unsigned int nTable = tables.size();
84 unsigned int processed = 0;
86 for (
unsigned int iTable = 0; iTable < nTable; iTable++ )
88 Table* newTable = buildTable( tables[iTable] );
92 m_rdb->addTable( newTable );
96 return nTable - processed;
99 Table* XercesBuilder::buildTable( DOMElement* tableElt ) {
103 newTable->m_name = Dom::getAttribute( tableElt,
"name" );
104 newTable->m_version = Dom::getAttribute( tableElt,
"version" );
105 newTable->m_comment = Dom::getAttribute( tableElt,
"comment" );
107 std::vector<DOMElement*> children;
108 Dom::getChildrenByTagName( tableElt,
"col", children );
109 unsigned int nChild = children.size();
112 for (
unsigned int iCol = 0; iCol < nChild; iCol++ )
114 Column* newCol = buildColumn( children[iCol], newTable );
116 if ( newCol ) { newTable->addColumn( newCol ); }
122 DOMElement* primaryKey = Dom::findFirstChildByName( tableElt,
"primary" );
123 if ( primaryKey != 0 )
125 Index* newIndex = buildIndex( primaryKey,
true, newTable );
128 newIndex->m_myTable = newTable;
129 newTable->addIndex( newIndex );
132 newTable->setPrimaryKeyCol();
135 Dom::getChildrenByTagName( tableElt,
"index", children );
136 nChild = children.size();
138 for (
unsigned int iIndex = 0; iIndex < nChild; iIndex++ )
140 Index* newIndex = buildIndex( children[iIndex],
false, newTable );
141 if ( newIndex ) { newTable->addIndex( newIndex ); }
147 Dom::getChildrenByTagName( tableElt,
"assert", children );
148 nChild = children.size();
150 for (
unsigned int iAssert = 0; iAssert < nChild; iAssert++ )
152 Assertion* newAssert = buildAssertion( children[iAssert], newTable );
153 if ( newAssert ) { newTable->addAssert( newAssert ); }
157 newTable->setValidRow(
v );
159 DOMElement* iNewElt = Dom::findFirstChildByName( tableElt,
"insertNew" );
160 if ( iNewElt ) { newTable->m_iNew = buildInsertNew( iNewElt, newTable ); }
161 DOMElement* supElt = Dom::findFirstChildByName( tableElt,
"supersede" );
162 if ( supElt ) { newTable->m_sup = buildSupersede( supElt, newTable ); }
167 Column* XercesBuilder::buildColumn( DOMElement* e,
Table* myTable ) {
170 Column* newCol =
new Column( myTable );
172 newCol->m_name = Dom::getAttribute( e,
"name" );
173 DOMElement* com = Dom::findFirstChildByName( e,
"comment" );
174 newCol->m_comment = Dom::getTextContent( com );
176 DOMElement* src = Dom::findFirstChildByName( e,
"src" );
178 newCol->m_null = ( Dom::getAttribute( src,
"null" ) ==
"true" );
179 newCol->m_stickyInsert = ( Dom::getAttribute( src,
"stickyInsert" ) ==
"true" );
181 DOMElement* child = Dom::getFirstChildElement( src );
182 if ( Dom::checkTagName( child,
"default" ) )
185 newCol->m_default = Dom::getAttribute( child,
"value" );
187 else if ( Dom::checkTagName( child,
"from" ) )
189 std::string agent = Dom::getAttribute( child,
"agent" );
193 else if ( agent ==
"service" )
196 std::string contents = Dom::getAttribute( child,
"contents" );
199 else if ( contents ==
"insert_time" )
201 else if ( contents ==
"update_time" )
208 DOMElement* dtype = Dom::findFirstChildByName( e,
"type" );
209 newCol->m_type = buildDatatype( dtype );
214 Datatype* XercesBuilder::buildDatatype( DOMElement* e ) {
217 Datatype* newType =
new Datatype;
218 newType->setType( Dom::getAttribute( e,
"typename" ) );
220 if ( Dom::hasAttribute( e,
"size" ) )
224 newType->m_outputSize = Dom::getIntAttribute( e,
"size" );
225 }
catch ( xmlBase::DomException ex )
227 std::cerr <<
"Error in rdb database description file" << std::endl;
228 std::cerr << ex.
getMsg() << std::endl;
229 std::cerr <<
"Ignoring column size specification " << std::endl;
230 newType->m_outputSize = -1;
233 else newType->m_outputSize = -1;
234 if ( ( newType->m_outputSize == -1 ) && ( newType->getType() ==
Datatype::TYPEchar ) )
235 newType->m_outputSize = 1;
238 std::cerr <<
"Error in rdb database description file: " << std::endl;
239 std::cerr <<
"Missing size spec. for varchar field " << std::endl;
245 DOMElement* restrict = Dom::getFirstChildElement( e );
249 DOMElement* rtype = Dom::getFirstChildElement( restrict );
250 std::string tagname = Dom::getTagName( rtype );
251 if ( ( newType->m_type ==
Datatype::TYPEenum ) && ( tagname != std::string(
"enum" ) ) )
253 std::cerr <<
"From rdbMode::XercesBuilder::buildDatatype" << std::endl;
254 std::cerr <<
"Bad enum type. Missing value list " << std::endl;
260 if ( tagname == std::string(
"nonnegative" ) )
263 if ( newType->m_isInt ) newType->m_minInt = 0;
265 else if ( tagname == std::string(
"positive" ) )
268 if ( newType->m_isInt ) newType->m_minInt = 1;
270 else if ( tagname == std::string(
"interval" ) )
272 newType->setInterval( Dom::getAttribute( rtype,
"min" ),
273 Dom::getAttribute( rtype,
"max" ) );
275 else if ( tagname == std::string(
"file" ) )
277 else if ( tagname == std::string(
"enum" ) )
280 Enum* newEnum =
new Enum();
281 newEnum->m_required = ( Dom::getAttribute( rtype,
"use" ) ==
"require" );
286 std::cerr <<
"From rdbMode::XercesBuilder::buildDatatype" << std::endl;
287 std::cerr <<
"Bad enum type. List must be 'required' " << std::endl;
292 std::string enums = Dom::getAttribute( rtype,
"values" );
294 unsigned int start = 0;
295 std::string::size_type blankLoc = enums.find( std::string(
" " ), start );
297 while ( blankLoc != std::string::npos )
299 newEnum->m_choices.push_back( enums.substr( start, blankLoc - start ) );
300 start = blankLoc + 1;
301 blankLoc = enums.find( std::string(
" " ), start );
303 newEnum->m_choices.push_back( enums.substr( start ) );
304 newType->m_enum = newEnum;
312 std::cerr <<
"From rdbMode::XercesBuilder::buildDatatype" << std::endl;
313 std::cerr <<
"Bad enum type. Missing value list " << std::endl;
321 Index* XercesBuilder::buildIndex( DOMElement* e,
bool primaryElt,
Table* myTable ) {
328 newIndex->m_primary =
true;
329 std::string col = newIndex->m_name = Dom::getAttribute( e,
"col" );
330 newIndex->m_indexCols.push_back( newIndex->m_name );
331 Column* myCol = myTable->getColumnByName( col );
332 myCol->m_isPrimaryKey =
true;
336 newIndex->m_name = Dom::getAttribute( e,
"name" );
338 std::string primaryVal = Dom::getAttribute( e,
"primary" );
339 newIndex->m_primary = ( primaryVal ==
"yes" );
342 std::string cols = Dom::getAttribute( e,
"cols" );
345 if ( newIndex->m_primary )
347 Column* myCol = myTable->getColumnByName( cols );
348 myCol->m_isPrimaryKey =
true;
351 unsigned int start = 0;
352 std::string::size_type blankLoc = cols.find( std::string(
" " ), start );
354 while ( blankLoc != std::string::npos )
356 newIndex->m_indexCols.push_back( cols.substr( start, blankLoc - start ) );
357 start = blankLoc + 1;
358 blankLoc = cols.find( std::string(
" " ), start );
360 newIndex->m_indexCols.push_back( cols.substr( start ) );
365 Assertion* XercesBuilder::buildAssertion( DOMElement* e,
Table* myTable ) {
373 Assertion::Operator* op = buildOperator( opElt, myTable );
375 Assertion* newAssert =
new Assertion( op, myTable );
377 newAssert->setName( name );
384 std::string opName = Dom::getTagName( e );
386 if ( ( opName ==
"isNull" ) || ( opName ==
"isEmpty" ) )
389 DOMElement* child = Dom::getFirstChildElement( e );
391 std::string which = Dom::getAttribute( child,
"which" );
394 return new Assertion::Operator( opType, Dom::getAttribute( child,
"col" ),
399 else if ( opName ==
"compare" )
401 std::string relation = Dom::getAttribute( e,
"relation" );
404 else if ( relation ==
"equal" ) opType =
OPTYPEequal;
408 DOMElement* child[2];
409 child[0] = Dom::getFirstChildElement( e );
410 child[1] = Dom::getSiblingElement( child[0] );
412 std::string compareArgs[2];
415 for (
unsigned iChild = 0; iChild < 2; iChild++ )
423 if ( Dom::checkTagName( child[iChild],
"value" ) )
426 compareArgs[iChild] =
427 Dom::getTextContent( child[iChild] );
431 compareArgs[iChild] = Dom::getAttribute( child[iChild],
"col" );
433 std::string which = Dom::getAttribute( child[iChild],
"which" );
434 if ( which == std::string(
"old" ) ) { valueType[iChild] =
FIELDTYPEold; }
435 else if ( which == std::string(
"toBe" ) ) { valueType[iChild] =
FIELDTYPEtoBe; }
439 Assertion::Operator* newOp =
new Assertion::Operator(
440 opType, compareArgs[0], compareArgs[1], valueType[0], valueType[1] );
441 if ( !newOp->validCompareOp( myTable ) )
450 else if ( opName ==
"exists" )
452 std::string tableName;
454 if ( Dom::hasAttribute( e,
"tableName" ) )
455 { tableName = Dom::getAttribute( e,
"tableName" ); }
456 else tableName = myTable->getName();
457 DOMElement* child = Dom::getFirstChildElement( e );
458 Assertion::Operator* childOp = buildOperator( child, myTable );
459 return new Assertion::Operator( opType, tableName, childOp );
462 else if ( opName ==
"or" ) opType =
OPTYPEor;
463 else if ( opName ==
"and" ) opType =
OPTYPEand;
464 else if ( opName ==
"not" ) opType =
OPTYPEnot;
467 std::vector<DOMElement*> children;
468 std::vector<Assertion::Operator*> childOps;
469 Dom::getChildrenByTagName( e,
"*", children );
470 unsigned nChild = children.size();
471 for (
unsigned iChild = 0; iChild < nChild; iChild++ )
473 Assertion::Operator* childOp = buildOperator( children[iChild], myTable );
474 if ( childOp ) { childOps.push_back( childOp ); }
480 return new Assertion::Operator( opType, childOps );
483 Set* XercesBuilder::buildSet( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* e,
Table*
t ) {
486 std::string destCol = Dom::getAttribute( e,
"destCol" );
488 std::string destRow = Dom::getAttribute( e,
"destRow" );
493 std::string srcValue;
494 std::string interp(
"" );
495 DOMElement* srcElt = Dom::findFirstChildByName( e,
"*" );
496 std::string tag = Dom::getTagName( srcElt );
497 if ( tag == std::string(
"ask" ) )
502 else if ( tag == std::string(
"value" ) )
505 srcValue = Dom::getTextContent( srcElt );
506 interp = Dom::getAttribute( srcElt,
"interp" );
510 std::string forceStr = Dom::getAttribute( srcElt,
"force" );
511 bool force = ( forceStr == std::string(
"true" ) );
512 srcValue = Dom::getAttribute( srcElt,
"col" );
513 std::string which = Dom::getAttribute( srcElt,
"which" );
521 return new Set(
t, destCol, destType, srcValue, srcType, interp );
524 Supersede* XercesBuilder::buildSupersede( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* e,
532 std::string onlyIfStr = Dom::getAttribute( e,
"onlyIf" );
533 Assertion* onlyIf =
t->getAssertionByName( onlyIfStr );
534 Supersede* super =
new Supersede(
t, onlyIf );
538 e = Dom::findFirstChildByName( e,
"*" );
541 Set*
s = buildSet( e,
t );
543 e = Dom::getSiblingElement( e );
549 Query* XercesBuilder::buildQuery( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* e,
Table*
t ) {
551 std::string whereStr = Dom::getAttribute( e,
"assertRef" );
552 Assertion* where =
t->getAssertionByName( whereStr );
554 Query*
q =
new Query(
t, 0, where );
555 e = Dom::findFirstChildByName( e,
"*" );
558 q->addSelect( Dom::getTextContent( e ) );
559 e = Dom::getSiblingElement( e );
564 InsertNew* XercesBuilder::buildInsertNew( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* e,
568 std::string internalStr = Dom::getAttribute( e,
"internalCond" );
569 Assertion* internal =
t->getAssertionByName( internalStr );
571 std::string officialStr = Dom::getAttribute( e,
"official" );
572 Assertion* official =
t->getAssertionByName( officialStr );
574 InsertNew* in =
new InsertNew(
t, internal, official );
575 e = Dom::findFirstChildByName( e,
"*" );
578 InterRow* ir = buildInterRow( e,
t );
580 in->addInterRow( ir );
581 e = Dom::getSiblingElement( e );
586 InterRow* XercesBuilder::buildInterRow( XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* e,
590 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* queryElt = Dom::findFirstChildByName( e,
"*" );
591 Query*
q = buildQuery( queryElt,
t );
593 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* sib = Dom::getSiblingElement( queryElt );
594 bool quit = Dom::checkTagName( sib,
"quit" );
596 InterRow* inter =
new InterRow(
t,
q, quit );
597 if ( quit )
return inter;
602 Set*
s = buildSet( sib,
t );
604 sib = Dom::getSiblingElement( sib );
std::map< int, double >::value_type valType
****INTEGER imax DOUBLE PRECISION m_pi *DOUBLE PRECISION m_amfin DOUBLE PRECISION m_Chfin DOUBLE PRECISION m_Xenph DOUBLE PRECISION m_sinw2 DOUBLE PRECISION m_GFermi DOUBLE PRECISION m_MfinMin DOUBLE PRECISION m_ta2 INTEGER m_out INTEGER m_KeyFSR INTEGER m_KeyQCD *COMMON c_Semalib $ !copy of input $ !CMS energy $ !beam mass $ !final mass $ !beam charge $ !final charge $ !smallest final mass $ !Z mass $ !Z width $ !EW mixing angle $ !Gmu Fermi $ alphaQED at q
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
#define SCHEMA_MAJOR_VERSION
static int stringToInt(const std::string &InStr)
Exception class used when converting from string to numeric type.
static Manager * getManager()
Assertion * getAssertionByName(const std::string &name) const
virtual unsigned int parseInput(const std::string &inputPath)
virtual std::string getMsg()
static std::string getAttribute(const DOMElement *elt, const char *attName)
static DOMElement * getFirstChildElement(const DOMNode *parent)
Get first child which is an element node, if any.
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
void doSchema(bool doit)
Call this method to turn on schema processing (else it's off).