00001
00002 #include "otsdaq-core/XmlUtilities/XmlDocument.h"
00003 #include "otsdaq-core/XmlUtilities/ConvertToXML.h"
00004 #include "otsdaq-core/XmlUtilities/ConvertFromXML.h"
00005 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00006 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00007
00008 #include <xercesc/parsers/XercesDOMParser.hpp>
00009 #include <stdexcept>
00010 #include <xercesc/dom/DOM.hpp>
00011 #include <xercesc/dom/DOMDocument.hpp>
00012 #include <xercesc/dom/DOMDocumentType.hpp>
00013 #include <xercesc/dom/DOMElement.hpp>
00014 #include <xercesc/dom/DOMImplementation.hpp>
00015 #include <xercesc/dom/DOMImplementationRegistry.hpp>
00016 #include <xercesc/dom/DOMImplementationLS.hpp>
00017
00018
00019 #include <xercesc/dom/DOMNodeIterator.hpp>
00020 #include <xercesc/dom/DOMNodeList.hpp>
00021 #include <xercesc/dom/DOMText.hpp>
00022 #include <xercesc/validators/common/Grammar.hpp>
00023
00024 #include <xercesc/parsers/XercesDOMParser.hpp>
00025 #include <xercesc/util/XMLUni.hpp>
00026 #include <xercesc/util/XercesDefs.hpp>
00027
00028 #include <xercesc/util/OutOfMemoryException.hpp>
00029 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00030
00031 #include <iostream>
00032 #include <sstream>
00033 #include <list>
00034
00035 #include <sys/types.h>
00036 #include <sys/stat.h>
00037 #include <unistd.h>
00038 #include <errno.h>
00039
00040 using namespace ots;
00041
00042
00043 XmlDocument::XmlDocument(std::string rootName) :
00044 rootTagName_(rootName)
00045 {
00046 INIT_MF("XmlDocument");
00047
00048 initDocument();
00049 rootElement_ = theDocument_->getDocumentElement();
00050
00051 }
00052
00053
00054 XmlDocument::XmlDocument(const XmlDocument& doc) :
00055 rootTagName_(doc.rootTagName_)
00056 {
00057
00058 *this = doc;
00059
00060 }
00061
00062
00063 XmlDocument& XmlDocument::operator=(const XmlDocument& doc)
00064 {
00065
00066 initDocument();
00067 rootElement_ = theDocument_->getDocumentElement();
00068 recursiveElementCopy(doc.rootElement_, rootElement_);
00069
00070 return *this;
00071 }
00072
00073
00074 XmlDocument::~XmlDocument(void)
00075 {
00076
00077 terminatePlatform();
00078 }
00079
00080
00081 void XmlDocument::initDocument(void)
00082 {
00083 initPlatform();
00084
00085 theImplementation_ = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("Core"));
00086
00087 if(theImplementation_)
00088 {
00089 try
00090 {
00091 theDocument_ = theImplementation_->createDocument(
00092 CONVERT_TO_XML("http://www.w3.org/2001/XMLSchema-instance"),
00093 CONVERT_TO_XML(rootTagName_),
00094 0);
00095 }
00096 catch (const xercesc::OutOfMemoryException&)
00097 {
00098 XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
00099 }
00100 catch (const xercesc::DOMException& e)
00101 {
00102 XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
00103 }
00104 catch(const xercesc::XMLException& e)
00105 {
00106 __MOUT__ << "Error Message: " << XML_TO_CHAR(e.getMessage()) << std::endl;
00107 }
00108 catch (...)
00109 {
00110 XERCES_STD_QUALIFIER cerr << "An error occurred creating the theDocument_" << XERCES_STD_QUALIFIER endl;
00111 }
00112 }
00113 else
00114 XERCES_STD_QUALIFIER cerr << "Requested theImplementation_ is not supported" << XERCES_STD_QUALIFIER endl;
00115 }
00116
00117
00118 void XmlDocument::initPlatform(void)
00119 {
00120 try
00121 {
00122 xercesc::XMLPlatformUtils::Initialize();
00123
00124 }
00125 catch( xercesc::XMLException& e )
00126 {
00127 __MOUT__ << "XML toolkit initialization error: " << XML_TO_CHAR(e.getMessage()) << std::endl;
00128 }
00129
00130 }
00131
00132
00133 void XmlDocument::terminatePlatform(void)
00134 {
00135 try
00136 {
00137
00138 theDocument_->release();
00139
00140 }
00141 catch (...)
00142 {
00143 XERCES_STD_QUALIFIER cerr << "An error occurred destroying the theDocument_" << XERCES_STD_QUALIFIER endl;
00144 }
00145
00146 try
00147 {
00148 xercesc::XMLPlatformUtils::Terminate();
00149 }
00150 catch( xercesc::XMLException& e )
00151 {
00152 __MOUT__ << "XML toolkit teardown error: " << XML_TO_CHAR(e.getMessage()) << std::endl;
00153
00154 }
00155 }
00156
00157
00158
00159
00160
00161 xercesc::DOMElement* XmlDocument::addTextElementToParent(std::string childName, std::string childText, xercesc::DOMElement* parent)
00162 {
00163 if(parent == 0) return 0;
00164 xercesc::DOMElement* child;
00165 try
00166 {
00167 child = theDocument_->createElement(CONVERT_TO_XML(childName));
00168 }
00169 catch (xercesc::DOMException& e)
00170 {
00171 __MOUT__ << "Can't use the name: " << childName << " to create the child element because the exception says: "
00172 << XML_TO_CHAR(e.getMessage()) << ". Very likely you have a name that starts with a number and that's not allowed!" << std::endl;
00173 }
00174 parent->appendChild(child);
00175 child->appendChild(theDocument_->createTextNode(CONVERT_TO_XML(childText)));
00176
00177 return child;
00178 }
00179
00180
00181
00182
00183
00184 xercesc::DOMElement* XmlDocument::addTextElementToParent(std::string childName, std::string childText, std::string parentName, unsigned int parentIndex)
00185 {
00186 xercesc::DOMNodeList* nodeList = theDocument_->getElementsByTagName(CONVERT_TO_XML(parentName));
00187
00188 if(parentIndex >= nodeList->getLength())
00189 {
00190 __MOUT__ << "WARNING: Illegal parent index attempted in tags with name: " << parentName << ", index: " << parentIndex << std::endl;
00191 return 0;
00192 }
00193
00194 return addTextElementToParent(childName, childText,(xercesc::DOMElement*)(nodeList->item(parentIndex)));
00195 }
00196
00197
00198 void XmlDocument::copyDocument(const xercesc::DOMDocument* toCopy, xercesc::DOMDocument* copy)
00199 {
00200 recursiveElementCopy(toCopy->getDocumentElement(),copy->getDocumentElement());
00201 }
00202
00203
00204 void XmlDocument::recursiveElementCopy(const xercesc::DOMElement* toCopy, xercesc::DOMElement* copy)
00205 {
00206 xercesc::DOMNodeList* nodeListToCopy = toCopy->getChildNodes();
00207 xercesc::DOMNode* iNode;
00208 xercesc::DOMDocument* copyDocument = copy->getOwnerDocument();
00209 for(unsigned int i=0; i<nodeListToCopy->getLength(); i++)
00210 {
00211 iNode = nodeListToCopy->item(i);
00212 xercesc::DOMElement* child = copyDocument->createElement(iNode->getNodeName());
00213 copy->appendChild(child);
00214 if( iNode->getFirstChild() != 0 && iNode->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE)
00215 {
00216 child->appendChild(copyDocument->createTextNode(child->getFirstChild()->getNodeValue()));
00217 }
00218 recursiveElementCopy((xercesc::DOMElement*)(iNode),child);
00219 }
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 void XmlDocument::outputXmlDocument (std::ostringstream *out, bool dispStdOut)
00385 {
00386 recursiveOutputXmlDocument(theDocument_->getDocumentElement(),out,dispStdOut);
00387 }
00388
00389
00390
00391
00392 void XmlDocument::recursiveOutputXmlDocument (xercesc::DOMElement *currEl, std::ostringstream *out, bool dispStdOut, std::string tabStr)
00393 {
00394
00395 if(dispStdOut) std::cout << __COUT_HDR_FL__<< tabStr << "<" << XML_TO_CHAR(currEl->getNodeName()) ;
00396 if(out) *out << tabStr << "<" << XML_TO_CHAR(currEl->getNodeName());
00397
00398
00399 if( currEl->getFirstChild() != NULL &&
00400 currEl->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE)
00401 {
00402 if(dispStdOut) std::cout << " value='" << (XML_TO_CHAR(currEl->getFirstChild()->getNodeValue())) << "'";
00403 if(out) *out << " value='" << (XML_TO_CHAR(currEl->getFirstChild()->getNodeValue())) << "'";
00404 }
00405
00406 xercesc::DOMNodeList *nodeList = currEl->getChildNodes();
00407
00408
00409 if(dispStdOut) std::cout << ((nodeList->getLength() == 0 ||
00410 (nodeList->getLength() == 1 && currEl->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE))? "/":"")
00411 << ">" << " len:" << nodeList->getLength() << std::endl;
00412 if(out) *out << ((nodeList->getLength() == 0 ||
00413 (nodeList->getLength() == 1 && currEl->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE))? "/":"")
00414 << ">" << std::endl;
00415
00416
00417 std::string newTabStr = tabStr + "\t";
00418 for(unsigned int i = 0; i<nodeList->getLength();++i)
00419 if(nodeList->item(i)->getNodeType() != xercesc::DOMNode::TEXT_NODE)
00420 recursiveOutputXmlDocument ((xercesc::DOMElement*)(nodeList->item(i)),out,dispStdOut,newTabStr);
00421
00422
00423 if(nodeList->getLength() > 1 || (nodeList->getLength() == 1 && currEl->getFirstChild()->getNodeType() != xercesc::DOMNode::TEXT_NODE))
00424 {
00425 if(dispStdOut) std::cout << __COUT_HDR_FL__<< tabStr << "</" << XML_TO_CHAR(currEl->getNodeName()) << ">" << std::endl;
00426 if(out) *out << tabStr << "</" << XML_TO_CHAR(currEl->getNodeName()) << ">" << std::endl;
00427 }
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 std::string XmlDocument::escapeString(std::string inString, bool allowWhiteSpace)
00509 {
00510 bool doit = false;
00511
00512 unsigned int ws = -1;
00513 char htmlTmp[6];
00514
00515 for(unsigned int i=0; i<inString.length(); i++)
00516 if(inString[i] != ' ')
00517 {
00518
00519
00520 if(doit) std::cout << __COUT_HDR_FL__<< inString[i] << ":" <<
00521 (int)inString[i] << ":" << inString << std::endl;
00522
00523
00524 if(inString[i] == '\r' || inString[i] == '\n' ||
00525 inString[i] == '\t' ||
00526 inString[i] < 32 ||
00527 (inString[i] > char(126) && inString[i] < char(161)))
00528
00529 {
00530 if(allowWhiteSpace &&
00531 inString[i] == '\n')
00532 {
00533 sprintf(htmlTmp,"&#%3.3d",inString[i]);
00534 inString.insert(i,htmlTmp);
00535 inString.replace(i+5,1,1,';');
00536 i+=6;
00537 --i;
00538 }
00539 else if(allowWhiteSpace &&
00540 inString[i] == '\t')
00541 {
00542 if(0)
00543 {
00544
00545 sprintf(htmlTmp,"        ");
00546 inString.insert(i,htmlTmp);
00547 inString.replace(i+47,1,1,';');
00548 i+=48;
00549 --i;
00550 }
00551 else
00552 {
00553
00554 sprintf(htmlTmp,"	");
00555 inString.insert(i,htmlTmp);
00556 inString.replace(i+5,1,1,';');
00557 i+=6;
00558 --i;
00559 }
00560 }
00561 else
00562 {
00563 inString.erase(i,1);
00564 --i;
00565 }
00566 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00567 continue;
00568 }
00569
00570 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00571
00572
00573 if(inString[i] == '\"' || inString[i] == '\'')
00574 {
00575 inString.insert(i,(inString[i] == '\'')?"&apos":""");
00576 inString.replace(i+5,1,1,';');
00577 i+=5;
00578
00579 }
00580 else if(inString[i] == '&')
00581 {
00582 inString.insert(i,"&");
00583 inString.replace(i+4,1,1,';');
00584 i+=4;
00585 }
00586 else if(inString[i] == '<' || inString[i] == '>')
00587 {
00588 inString.insert(i,(inString[i] == '<')?"<":">");
00589 inString.replace(i+3,1,1,';');
00590 i+=3;
00591 }
00592 else if(inString[i] >= char(161) && inString[i] <= char(255))
00593 {
00594 sprintf(htmlTmp,"&#%3.3d",inString[i]);
00595 inString.insert(i,htmlTmp);
00596 inString.replace(i+5,1,1,';');
00597 i+=5;
00598 }
00599
00600 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00601
00602 ws = i;
00603 }
00604 else if(allowWhiteSpace)
00605 {
00606 if(i-1 == ws) continue;
00607
00608
00609 if(i-2 == ws)
00610 {
00611 inString.insert(i," ");
00612 i+=6;
00613 }
00614 inString.insert(i," ");
00615 inString.replace(i+5,1,1,';');
00616 i+=5;
00617 ws = i;
00618 }
00619
00620 if(doit) std::cout << __COUT_HDR_FL__<< inString.size() << " " << ws << std::endl;
00621
00622 inString.substr(0,ws+1);
00623
00624 if(doit) std::cout << __COUT_HDR_FL__<< inString.size() << " " << inString << std::endl;
00625
00626 if(ws == (unsigned int)-1) return "";
00627 return inString.substr(0,ws+1);
00628 }
00629
00630
00631
00632
00633
00634 void XmlDocument::recursiveRemoveChild(xercesc::DOMElement *childEl, xercesc::DOMElement *parentEl)
00635 {
00636
00637 xercesc::DOMNodeList* nodeList = childEl->getChildNodes();
00638 for(unsigned int i = 0; i<nodeList->getLength(); ++i)
00639 recursiveRemoveChild((xercesc::DOMElement*)(nodeList->item(nodeList->getLength()-1-i)),childEl);
00640
00641
00642 parentEl->removeChild(childEl);
00643 childEl->release();
00644 }
00645
00646
00647
00648
00649
00650 void XmlDocument::saveXmlDocument (std::string filePath)
00651 {
00652 std::cout << __COUT_HDR_FL__<< "Saving theDocument_ to file: " << filePath << std::endl;
00653
00654
00655 xercesc::DOMImplementation *saveImplementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("LS"));
00656
00657 std::cout << __COUT_HDR_FL__<< "XERCES Version: " << _XERCES_VERSION << std::endl;
00658
00659 #if _XERCES_VERSION >= 30000
00660
00661
00662
00663 xercesc::DOMLSSerializer *serializer = ((xercesc::DOMImplementationLS*)saveImplementation)->createLSSerializer();
00664
00665
00666 if (serializer->getDomConfig()->canSetParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
00667 serializer->getDomConfig()->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00668
00669
00670 serializer->setNewLine(CONVERT_TO_XML("\r\n"));
00671
00672
00673
00674
00675
00676 xercesc::XMLFormatTarget* formatTarget;
00677 try
00678 {
00679
00680 formatTarget = new xercesc::LocalFileFormatTarget(filePath.c_str());
00681 }
00682 catch(...)
00683 {
00684 std::cout << __COUT_HDR_FL__<< "Inaccessible file path: " << filePath << std::endl;
00685 serializer->release();
00686
00687
00688 return;
00689 }
00690
00691
00692 xercesc::DOMLSOutput *output = ((xercesc::DOMImplementationLS*)saveImplementation)->createLSOutput();
00693
00694
00695 output->setByteStream(formatTarget);
00696
00697 serializer->write(theDocument_, output);
00698 serializer->release();
00699
00700 delete formatTarget;
00701 #else
00702
00703 xercesc::DOMWriter *serializer = ((xercesc::DOMImplementationLS*)saveImplementation)->createDOMWriter();
00704 serializer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715 XMLCh *tempFilePath = xercesc::XMLString::transcode(filePath.c_str());
00716 xercesc::XMLFormatTarget* formatTarget;
00717 try
00718 {
00719 formatTarget = new xercesc::LocalFileFormatTarget(tempFilePath);
00720 }
00721 catch(...)
00722 {
00723 std::cout << __COUT_HDR_FL__<< "Inaccessible file path: " << filePath << std::endl;
00724 serializer->release();
00725 xercesc::XMLString::release(&tempFilePath);
00726 return;
00727 }
00728
00729
00730
00731 serializer->writeNode(formatTarget, *theDocument_);
00732 serializer->release();
00733 xercesc::XMLString::release(&tempFilePath);
00734 delete formatTarget;
00735 #endif
00736
00737
00738
00739
00740
00741 #if _XERCES_VERSION >= 30000
00742
00743
00744 output->release();
00745
00746
00747 #endif
00748 }
00749
00750
00751
00752 bool XmlDocument::loadXmlDocument (std::string filePath)
00753 {
00754 std::cout << __COUT_HDR_FL__<< "Loading theDocument_ from file: " << filePath << std::endl;
00755
00756 struct stat fileStatus;
00757
00758 if(stat(filePath.c_str(), &fileStatus) != 0)
00759 {
00760 std::cout << __COUT_HDR_FL__<< "File not accessible." << std::endl;
00761 return false;
00762 }
00763
00764
00765 terminatePlatform();
00766 initPlatform();
00767
00768 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
00769
00770 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00771 parser->setDoNamespaces ( true );
00772 parser->setDoSchema ( true );
00773 parser->useCachedGrammarInParse ( false );
00774
00775 try
00776 {
00777 parser->parse( filePath.c_str() );
00778
00779
00780 theDocument_ = parser->adoptDocument();
00781
00782
00783 rootElement_ = theDocument_->getDocumentElement();
00784 if( !rootElement_ )
00785 throw(std::runtime_error( "empty XML theDocument_" ));
00786
00787 }
00788 catch( xercesc::XMLException& e )
00789 {
00790 std::cout << __COUT_HDR_FL__<< "Error parsing file." << std::endl;
00791 return false;
00792 }
00793 delete parser;
00794
00795 return true;
00796 }
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816