00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <xercesc/util/XMLUniDefs.hpp>
00029 #include <xercesc/sax/AttributeList.hpp>
00030
00031 #include <xercesc/util/PlatformUtils.hpp>
00032 #include <xercesc/util/TransService.hpp>
00033 #include <xercesc/parsers/SAXParser.hpp>
00034 #include <xercesc/util/OutOfMemoryException.hpp>
00035
00036 #include <iostream>
00037 #include <cstring>
00038 #include <string>
00039
00040 #include "tools/Log.hh"
00041
00042 #include "xml/XMLTool.hh"
00043
00044
00045
00046
00047
00048
00049
00050 static const XMLCh gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
00051 static const XMLCh gEndPI[] = { chQuestion, chCloseAngle, chNull };
00052 static const XMLCh gStartPI[] = { chOpenAngle, chQuestion, chNull };
00053 static const XMLCh gXMLDecl1[] =
00054 {
00055 chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
00056 , chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
00057 , chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
00058 , chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
00059 , chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
00060 , chDoubleQuote, chNull
00061 };
00062
00063 static const XMLCh gXMLDecl2[] =
00064 {
00065 chDoubleQuote, chQuestion, chCloseAngle
00066 , chLF, chNull
00067 };
00068
00069
00070
00071
00072
00073
00074
00075 XMLHandlers::XMLHandlers( const XMLTool &aFather
00076 , const char* const encodingName
00077 , const XMLFormatter::UnRepFlags unRepFlags) :
00078
00079 fFormatter
00080 (
00081 encodingName
00082 , 0
00083 , this
00084 , XMLFormatter::NoEscapes
00085 , unRepFlags
00086 ),
00087 father(aFather)
00088 {
00089
00090
00091
00092
00093
00094 fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
00095 }
00096
00097 XMLHandlers::~XMLHandlers()
00098 {
00099 }
00100
00101
00102
00103
00104
00105 void XMLHandlers::writeChars(const XMLByte* const )
00106 {
00107 }
00108
00109 void XMLHandlers::writeChars(const XMLByte* const toWrite,
00110 const XMLSize_t count,
00111 XMLFormatter* const )
00112 {
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 }
00123
00124
00125
00126
00127
00128 void XMLHandlers::error(const SAXParseException& e)
00129 {
00130 XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
00131 << ", line " << e.getLineNumber()
00132 << ", char " << e.getColumnNumber()
00133 << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
00134 }
00135
00136 void XMLHandlers::fatalError(const SAXParseException& e)
00137 {
00138 XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
00139 << ", line " << e.getLineNumber()
00140 << ", char " << e.getColumnNumber()
00141 << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
00142 }
00143
00144 void XMLHandlers::warning(const SAXParseException& e)
00145 {
00146 XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
00147 << ", line " << e.getLineNumber()
00148 << ", char " << e.getColumnNumber()
00149 << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
00150 }
00151
00152
00153
00154
00155
00156 void XMLHandlers::unparsedEntityDecl(const XMLCh* const
00157 , const XMLCh* const
00158 , const XMLCh* const
00159 , const XMLCh* const )
00160 {
00161
00162 }
00163
00164
00165 void XMLHandlers::notationDecl(const XMLCh* const
00166 , const XMLCh* const
00167 , const XMLCh* const )
00168 {
00169
00170 }
00171
00172
00173
00174
00175
00176 void XMLHandlers::characters(const XMLCh* const chars
00177 , const XMLSize_t length)
00178 {
00179 fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
00180 }
00181
00182
00183 void XMLHandlers::endDocument()
00184 {
00185 }
00186
00187
00188 void XMLHandlers::endElement(const XMLCh* const name)
00189 {
00190
00191
00192 }
00193
00194
00195 void XMLHandlers::ignorableWhitespace( const XMLCh* const chars
00196 ,const XMLSize_t length)
00197 {
00198 fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
00199 }
00200
00201
00202 void XMLHandlers::processingInstruction(const XMLCh* const target
00203 , const XMLCh* const data)
00204 {
00205
00206
00207
00208
00209 }
00210
00211
00212 void XMLHandlers::startDocument()
00213 {
00214 }
00215
00216
00217 void XMLHandlers::startElement(const XMLCh* const name
00218 , AttributeList& attributes)
00219 {
00220
00221
00222
00223
00224
00225
00226 string balise = XMLCh_to_String(name);
00227
00228
00229 XMLSize_t len = attributes.getLength();
00230
00231
00232 map<string,string> key_value;
00233 for (XMLSize_t index = 0; index < len; index++)
00234 {
00235 string key = XMLCh_to_String( attributes.getName(index));
00236 string value = XMLCh_to_String( attributes.getValue(index));
00237 key_value[key]=value;
00238
00239 }
00240 father.receiveEvent(balise,key_value);
00241
00242 }
00243
00244 string XMLHandlers::XMLCh_to_String( const XMLCh* toTranscode )
00245 {
00246 return string(XMLString::transcode(toTranscode));
00247 }
00248
00249
00250
00251
00252
00253 using namespace std;
00254
00255
00256
00257
00258 XMLTool::XMLTool():steerDesc(NULL)
00259
00260 {
00261 }
00262
00263
00264 int XMLTool::parse(SteerDesc& aSteerDesc, string aXmlFileName)
00265 {
00266
00267 steerDesc = &aSteerDesc;
00268
00269 if ( steerDesc != NULL )
00270 {
00271 xmlFileName = aXmlFileName;
00272
00273
00274 bool doNamespaces = false;
00275 bool doSchema = false;
00276 bool schemaFullChecking = false;
00277 const char* encodingName = "LATIN1";
00278 XMLFormatter::UnRepFlags unRepFlags = XMLFormatter::UnRep_CharRef;
00279 const char* xmlFile = 0;
00280 SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
00281
00282
00283
00284 try
00285 {
00286 XMLPlatformUtils::Initialize();
00287 }
00288
00289 catch (const XMLException& toCatch)
00290 {
00291 XERCES_STD_QUALIFIER cerr << "Error during initialization! :\n"
00292 << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
00293 return 1;
00294 }
00295
00296 xmlFile = xmlFileName.data() ;
00297 int errorCount = 0;
00298
00299
00300
00301
00302
00303 SAXParser* parser = new SAXParser;
00304 parser->setValidationScheme(valScheme);
00305 parser->setDoNamespaces(doNamespaces);
00306 parser->setDoSchema(doSchema);
00307 parser->setValidationSchemaFullChecking(schemaFullChecking);
00308
00309
00310
00311
00312
00313
00314 int errorCode = 0;
00315 try
00316 {
00317 XMLHandlers handler(*this, encodingName, unRepFlags);
00318 parser->setDocumentHandler(&handler);
00319 parser->setErrorHandler(&handler);
00320 parser->parse(xmlFile);
00321 errorCount = parser->getErrorCount();
00322 }
00323 catch (const OutOfMemoryException&)
00324 {
00325 XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
00326 errorCode = 5;
00327 }
00328 catch (const XMLException& toCatch)
00329 {
00330 XERCES_STD_QUALIFIER cerr << "\nAn error occurred\n Error: "
00331 << StrX(toCatch.getMessage())
00332 << "\n" << XERCES_STD_QUALIFIER endl;
00333 errorCode = 4;
00334 }
00335 if(errorCode) {
00336 XMLPlatformUtils::Terminate();
00337 return errorCode;
00338 }
00339
00340
00341
00342
00343 delete parser;
00344
00345
00346 XMLPlatformUtils::Terminate();
00347
00348 if (errorCount > 0)
00349 return 4;
00350 else
00351 {
00352 return 0;
00353 }
00354 }
00355 else { return 4; }
00356 }
00357
00358
00359
00360 void XMLTool::receiveEvent(string balise,const map<string,string> &key_value) const
00361 {
00362 string key,value;
00363 map<string, string>::const_iterator iter;
00364
00365
00366
00367 for (iter=key_value.begin(); iter != key_value.end(); ++iter)
00368 {
00369 key = iter->first;
00370 value = iter->second;
00371
00372
00373
00374
00375
00376
00377 if ( !balise.compare(INFOR) && !key.compare(DATE) )
00378 {
00379 steerDesc->date = value;
00380 }
00381
00382
00383
00384 else if ( !balise.compare(MONDIF) && !key.compare(ID) )
00385 {
00386 int id = atoi(value.c_str());
00387 steerDesc->monitoringDif = atoi(value.c_str());
00388 }
00389
00390 else if ( !balise.compare(SLOWCONTROL) && !key.compare(PATH) )
00391 {
00392 steerDesc->slowControl = value;
00393 }
00394
00395
00396 else if ( !balise.compare(INPUT) && !key.compare(PATH) )
00397 {
00398 steerDesc->inputInfo = new input_info_t();
00399 steerDesc->inputInfo->path = value;
00400 }
00401 else if ( !balise.compare(INPUT) && !key.compare(TYPE) )
00402 {
00403 if ( steerDesc->inputInfo != NULL )
00404 {
00405 steerDesc->inputInfo->type = value;
00406 }
00407 }
00408
00409 else if ( !balise.compare(INPUT) && !key.compare(RUNID) )
00410 {
00411 if ( steerDesc->inputInfo != NULL )
00412 {
00413 steerDesc->inputInfo->runId = atoi(value.c_str());
00414 }
00415 }
00416
00417
00418 else if ( !balise.compare(OUTPUT) && !key.compare(PATH) )
00419 {
00420 steerDesc->outputFile = value;
00421 }
00422
00423 else if ( !balise.compare(DETECTOR) && !key.compare(NAME) )
00424 {
00425 steerDesc->detectorName = value;
00426 }
00427 else if ( !balise.compare(DETECTOR) && !key.compare(DESCRIPTION) )
00428 {
00429 steerDesc->detectorDescription = value;
00430 }
00431
00432
00433 else if ( !balise.compare(CHAMBER) && !key.compare(TYPE) )
00434 {
00435 if ( steerDesc->chamberInfo == NULL )
00436 {
00437 steerDesc->chamberInfo = new chamber_info_t();
00438 steerDesc->chamberInfo->xRotation = false;
00439 steerDesc->chamberInfo->yRotation = false;
00440 steerDesc->chamberInfo->zRotation = 0;
00441 steerDesc->chamberInfo->stripHorizontal = false;
00442 steerDesc->chamberInfo->unit = "cm" ;
00443 }
00444 steerDesc->chamberInfo->type = value;
00445 }
00446 else if ( !balise.compare(CHAMBER) && !key.compare(XPOS) )
00447 {
00448 if ( steerDesc->chamberInfo == NULL )
00449 {
00450 steerDesc->chamberInfo = new chamber_info_t();
00451 steerDesc->chamberInfo->xRotation = false;
00452 steerDesc->chamberInfo->yRotation = false;
00453 steerDesc->chamberInfo->zRotation = 0;
00454 steerDesc->chamberInfo->stripHorizontal = false;
00455 steerDesc->chamberInfo->unit = "cm" ;
00456 }
00457 steerDesc->chamberInfo->xPos = atof(value.c_str());
00458
00459 }
00460 else if ( !balise.compare(CHAMBER) && !key.compare(YPOS) )
00461 {
00462 if ( steerDesc->chamberInfo == NULL )
00463 {
00464 steerDesc->chamberInfo = new chamber_info_t();
00465 steerDesc->chamberInfo->xRotation = false;
00466 steerDesc->chamberInfo->yRotation = false;
00467 steerDesc->chamberInfo->zRotation = 0;
00468 steerDesc->chamberInfo->stripHorizontal = false;
00469 steerDesc->chamberInfo->unit = "cm" ;
00470 }
00471 steerDesc->chamberInfo->yPos = atof(value.c_str());
00472
00473
00474 }
00475 else if ( !balise.compare(CHAMBER) && !key.compare(ZPOS) )
00476 {
00477 if ( steerDesc->chamberInfo == NULL )
00478 {
00479 steerDesc->chamberInfo = new chamber_info_t();
00480 steerDesc->chamberInfo->xRotation = false;
00481 steerDesc->chamberInfo->yRotation = false;
00482 steerDesc->chamberInfo->zRotation = 0;
00483 steerDesc->chamberInfo->stripHorizontal = false;
00484 steerDesc->chamberInfo->unit = "cm" ;
00485 }
00486 steerDesc->chamberInfo->zPos = atof(value.c_str());
00487 }
00488 else if ( !balise.compare(CHAMBER) && !key.compare(XROTATION) )
00489 {
00490 if ( steerDesc->chamberInfo == NULL )
00491 {
00492 steerDesc->chamberInfo = new chamber_info_t();
00493 steerDesc->chamberInfo->xRotation = false;
00494 steerDesc->chamberInfo->yRotation = false;
00495 steerDesc->chamberInfo->zRotation = 0;
00496 steerDesc->chamberInfo->stripHorizontal = false;
00497 steerDesc->chamberInfo->unit = "cm" ;
00498 }
00499 if ( !value.compare("true") || !value.compare("TRUE") )
00500 {
00501 steerDesc->chamberInfo->xRotation = true;
00502 }
00503 }
00504 else if ( !balise.compare(CHAMBER) && !key.compare(YROTATION) )
00505 {
00506 if ( steerDesc->chamberInfo == NULL )
00507 {
00508 steerDesc->chamberInfo = new chamber_info_t();
00509 steerDesc->chamberInfo->xRotation = false;
00510 steerDesc->chamberInfo->yRotation = false;
00511 steerDesc->chamberInfo->zRotation = 0;
00512 steerDesc->chamberInfo->stripHorizontal = false;
00513 steerDesc->chamberInfo->unit = "cm" ;
00514 }
00515 if ( !value.compare("true") || !value.compare("TRUE") )
00516 {
00517 steerDesc->chamberInfo->yRotation = true;
00518 }
00519 }
00520
00521 else if ( !balise.compare(CHAMBER) && !key.compare(ZROTATION) )
00522 {
00523 if ( steerDesc->chamberInfo == NULL )
00524 {
00525 steerDesc->chamberInfo = new chamber_info_t();
00526 steerDesc->chamberInfo->xRotation = false;
00527 steerDesc->chamberInfo->yRotation = false;
00528 steerDesc->chamberInfo->zRotation = 0;
00529 steerDesc->chamberInfo->stripHorizontal = false;
00530 steerDesc->chamberInfo->unit = "cm" ;
00531 }
00532 i16 rotation = atoi(value.c_str());
00533 if ( rotation == 0 || rotation == -90 || rotation == 90 )
00534 {
00535 steerDesc->chamberInfo->zRotation = rotation;
00536 }
00537 else
00538 {
00539 cout << "XML error. zRotation value choise is [-90,0,90]." << endl;
00540 exit(-1);
00541 }
00542
00543 }
00544
00545
00546 else if ( !balise.compare(CHAMBER) && !key.compare(STRIPHORIZONTAL) )
00547 {
00548 if ( steerDesc->chamberInfo == NULL )
00549 {
00550 steerDesc->chamberInfo = new chamber_info_t();
00551 steerDesc->chamberInfo->xRotation = false;
00552 steerDesc->chamberInfo->yRotation = false;
00553 steerDesc->chamberInfo->zRotation = 0;
00554 steerDesc->chamberInfo->stripHorizontal = false;
00555 steerDesc->chamberInfo->unit = "cm" ;
00556 }
00557 if ( !value.compare("true") || !value.compare("TRUE") )
00558 {
00559 steerDesc->chamberInfo->stripHorizontal = true;
00560 }
00561 }
00562 else if ( !balise.compare(CHAMBER) && !key.compare(UNIT) )
00563 {
00564 if ( steerDesc->chamberInfo == NULL )
00565 {
00566 steerDesc->chamberInfo = new chamber_info_t();
00567 steerDesc->chamberInfo->xRotation = false;
00568 steerDesc->chamberInfo->yRotation = false;
00569 steerDesc->chamberInfo->zRotation = 0;
00570 steerDesc->chamberInfo->stripHorizontal = false;
00571 steerDesc->chamberInfo->unit = "cm" ;
00572 }
00573 steerDesc->chamberInfo->unit = value;
00574 }
00575
00576 else if ( !balise.compare(CHAMBER) && !key.compare(ID) )
00577 {
00578 if ( steerDesc->chamberInfo == NULL )
00579 {
00580 steerDesc->chamberInfo = new chamber_info_t();
00581 steerDesc->chamberInfo->xRotation = false;
00582 steerDesc->chamberInfo->yRotation = false;
00583 steerDesc->chamberInfo->zRotation = 0;
00584 steerDesc->chamberInfo->stripHorizontal = false;
00585 steerDesc->chamberInfo->unit = "cm" ;
00586 }
00587 steerDesc->chamberInfo->id = atoi(value.c_str());
00588 }
00589
00590
00591 else if ( !balise.compare(BOARD) && !key.compare(ID) )
00592 {
00593 if ( steerDesc->boardInfo == NULL ) steerDesc->boardInfo = new BoardInfo_t();
00594 steerDesc->boardInfo->id = atoi(value.c_str());
00595 steerDesc->boardInfo->chamberId = steerDesc->lastChamberId;
00596 steerDesc->boardInfo->difId = steerDesc->lastDifId;
00597 }
00598
00599
00600
00601 else if ( !balise.compare(DIF) && !key.compare(ID) )
00602 {
00603 if ( steerDesc->difInfo == NULL ) steerDesc->difInfo = new DifInfo_t();
00604 steerDesc->difInfo->id = atoi(value.c_str());
00605 steerDesc->difInfo->chamberId = steerDesc->lastChamberId;
00606 }
00607
00608
00609 else if ( !balise.compare(CHIP) && !key.compare(ID) )
00610 {
00611 if ( steerDesc->chipInfo == NULL )
00612 {
00613 steerDesc->chipInfo = new ChipInfo_t();
00614 steerDesc->chipInfo->boardId = steerDesc->lastBoardId;
00615 steerDesc->chipInfo->difId = steerDesc->lastDifId;
00616 steerDesc->chipInfo->chamberId = steerDesc->lastChamberId;
00617 }
00618 steerDesc->chipInfo->id = atoi(value.c_str());
00619 }
00620 else if ( !balise.compare(CHIP) && !key.compare(SERIAL) )
00621 {
00622 if ( steerDesc->chipInfo == NULL )
00623 {
00624 steerDesc->chipInfo = new ChipInfo_t();
00625 steerDesc->chipInfo->boardId = steerDesc->lastBoardId;
00626 steerDesc->chipInfo->difId = steerDesc->lastDifId;
00627 steerDesc->chipInfo->chamberId = steerDesc->lastChamberId;
00628 }
00629 steerDesc->chipInfo->serial = atoi(value.c_str());
00630 }
00631
00632
00633 else if (!balise.compare(SCPARAM) && !key.compare(SCID) )
00634 {
00635 if ( steerDesc->slowControlInfo == NULL )
00636 {
00637 steerDesc->slowControlInfo = new SlowControl_t();
00638 }
00639 steerDesc->slowControlInfo->scId = atoi(value.c_str());
00640 }
00641 else if (!balise.compare(SCPARAM) && !key.compare(TYPE) )
00642 {
00643 if ( steerDesc->slowControlInfo == NULL ) steerDesc->slowControlInfo = new SlowControl_t();
00644 steerDesc->slowControlInfo->type = value;
00645 }
00646 else if (!balise.compare(SCPARAM) && !key.compare(CHAMBERID) )
00647 {
00648 if ( steerDesc->slowControlInfo == NULL ) steerDesc->slowControlInfo = new SlowControl_t();
00649 steerDesc->slowControlInfo->chamberId = atoi(value.c_str());
00650 }
00651 else if (!balise.compare(SCPARAM) && !key.compare(DIFID) )
00652 {
00653 if ( steerDesc->slowControlInfo == NULL ) steerDesc->slowControlInfo = new SlowControl_t();
00654 steerDesc->slowControlInfo->difId = atoi(value.c_str());
00655 }
00656 else if (!balise.compare(SCPARAM) && !key.compare(BOARDID) )
00657 {
00658 if ( steerDesc->slowControlInfo == NULL ) steerDesc->slowControlInfo = new SlowControl_t();
00659 steerDesc->slowControlInfo->boardId = atoi(value.c_str());
00660 }
00661 else if (!balise.compare(SCPARAM) && !key.compare(NAME) )
00662 {
00663 if ( steerDesc->slowControlInfo == NULL ) steerDesc->slowControlInfo = new SlowControl_t();
00664 steerDesc->slowControlInfo->name = value;
00665 }
00666 else if (!balise.compare(SCPARAM) && !key.compare(PMID) )
00667 {
00668 if ( steerDesc->slowControlInfo == NULL ) steerDesc->slowControlInfo = new SlowControl_t();
00669 steerDesc->slowControlInfo->pmId = atoi(value.c_str());
00670 }
00671
00672 }
00673
00674 if ( !balise.compare(CHAMBER) && ( steerDesc->chamberInfo != NULL ) )
00675 {
00676 steerDesc->chambers.push_back(*(steerDesc->chamberInfo));
00677 steerDesc->lastChamberId = steerDesc->chamberInfo->id;
00678 steerDesc->chamberInfo = NULL;
00679 }
00680 if ( !balise.compare(BOARD) && ( steerDesc->boardInfo != NULL ) )
00681 {
00682 steerDesc->boards.push_back(*(steerDesc->boardInfo));
00683 steerDesc->lastBoardId = steerDesc->boardInfo->id;
00684 steerDesc->boardInfo = NULL;
00685 }
00686 if ( !balise.compare(INPUT) && ( steerDesc->inputInfo != NULL ) )
00687 {
00688 steerDesc->inputFiles.push_back(*(steerDesc->inputInfo));
00689 steerDesc->inputInfo = NULL;
00690 }
00691 if (!balise.compare(DIF) && ( steerDesc->difInfo != NULL ) )
00692 {
00693 steerDesc->difs.push_back(*(steerDesc->difInfo));
00694 steerDesc->lastDifId = steerDesc->difInfo->id;
00695 steerDesc->difInfo = NULL;
00696 }
00697 if (!balise.compare(CHIP) && ( steerDesc->chipInfo != NULL ) )
00698 {
00699 steerDesc->chips.push_back(*(steerDesc->chipInfo));
00700 steerDesc->chipInfo = NULL;
00701 }
00702 if (!balise.compare(SCPARAM) && ( steerDesc->slowControlInfo != NULL ) )
00703 {
00704 steerDesc->slowControls.push_back(*(steerDesc->slowControlInfo));
00705 steerDesc->slowControlInfo = NULL;
00706 }
00707
00708 }
00709