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 __COUT__ << "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 __COUT__ << "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 __COUT__ << "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 __COUT__ << "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
00176 try
00177 {
00178 child->appendChild(theDocument_->createTextNode(CONVERT_TO_XML(childText)));
00179 }
00180 catch(...)
00181 {
00182 __COUT_ERR__ << "Error caught attempting to create a text node for this text: " <<
00183 childText << ". Converting instead to 'Illegal text..'" << std::endl;
00184 child->appendChild(theDocument_->createTextNode(CONVERT_TO_XML("Illegal text content blocked.")));
00185 }
00186
00187 return child;
00188 }
00189
00190
00191
00192
00193
00194 xercesc::DOMElement* XmlDocument::addTextElementToParent(std::string childName, std::string childText, std::string parentName, unsigned int parentIndex)
00195 {
00196 xercesc::DOMNodeList* nodeList = theDocument_->getElementsByTagName(CONVERT_TO_XML(parentName));
00197
00198 if(parentIndex >= nodeList->getLength())
00199 {
00200 __COUT__ << "WARNING: Illegal parent index attempted in tags with name: " << parentName << ", index: " << parentIndex << std::endl;
00201 return 0;
00202 }
00203
00204 return addTextElementToParent(childName, childText,(xercesc::DOMElement*)(nodeList->item(parentIndex)));
00205 }
00206
00207
00208 void XmlDocument::copyDocument(const xercesc::DOMDocument* toCopy, xercesc::DOMDocument* copy)
00209 {
00210 recursiveElementCopy(toCopy->getDocumentElement(),copy->getDocumentElement());
00211 }
00212
00213
00214 void XmlDocument::recursiveElementCopy(const xercesc::DOMElement* toCopy, xercesc::DOMElement* copy)
00215 {
00216 xercesc::DOMNodeList* nodeListToCopy = toCopy->getChildNodes();
00217 xercesc::DOMNode* iNode;
00218 xercesc::DOMDocument* copyDocument = copy->getOwnerDocument();
00219 for(unsigned int i=0; i<nodeListToCopy->getLength(); i++)
00220 {
00221 iNode = nodeListToCopy->item(i);
00222 xercesc::DOMElement* child = copyDocument->createElement(iNode->getNodeName());
00223 copy->appendChild(child);
00224 if( iNode->getFirstChild() != 0 && iNode->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE)
00225 {
00226 child->appendChild(copyDocument->createTextNode(child->getFirstChild()->getNodeValue()));
00227 }
00228 recursiveElementCopy((xercesc::DOMElement*)(iNode),child);
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
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 void XmlDocument::outputXmlDocument (std::ostringstream *out, bool dispStdOut)
00395 {
00396 recursiveOutputXmlDocument(theDocument_->getDocumentElement(),out,dispStdOut);
00397 }
00398
00399
00400
00401
00402 void XmlDocument::recursiveOutputXmlDocument (xercesc::DOMElement *currEl, std::ostringstream *out, bool dispStdOut, std::string tabStr)
00403 {
00404
00405 if(dispStdOut) std::cout << __COUT_HDR_FL__<< tabStr << "<" << XML_TO_CHAR(currEl->getNodeName()) ;
00406 if(out) *out << tabStr << "<" << XML_TO_CHAR(currEl->getNodeName());
00407
00408
00409 if( currEl->getFirstChild() != NULL &&
00410 currEl->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE)
00411 {
00412 if(dispStdOut) std::cout << " value='" << (XML_TO_CHAR(currEl->getFirstChild()->getNodeValue())) << "'";
00413 if(out) *out << " value='" << (XML_TO_CHAR(currEl->getFirstChild()->getNodeValue())) << "'";
00414 }
00415
00416 xercesc::DOMNodeList *nodeList = currEl->getChildNodes();
00417
00418
00419 if(dispStdOut) std::cout << ((nodeList->getLength() == 0 ||
00420 (nodeList->getLength() == 1 && currEl->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE))? "/":"")
00421 << ">" << " len:" << nodeList->getLength() << std::endl;
00422 if(out) *out << ((nodeList->getLength() == 0 ||
00423 (nodeList->getLength() == 1 && currEl->getFirstChild()->getNodeType() == xercesc::DOMNode::TEXT_NODE))? "/":"")
00424 << ">" << std::endl;
00425
00426
00427 std::string newTabStr = tabStr + "\t";
00428 for(unsigned int i = 0; i<nodeList->getLength();++i)
00429 if(nodeList->item(i)->getNodeType() != xercesc::DOMNode::TEXT_NODE)
00430 recursiveOutputXmlDocument ((xercesc::DOMElement*)(nodeList->item(i)),out,dispStdOut,newTabStr);
00431
00432
00433 if(nodeList->getLength() > 1 || (nodeList->getLength() == 1 && currEl->getFirstChild()->getNodeType() != xercesc::DOMNode::TEXT_NODE))
00434 {
00435 if(dispStdOut) std::cout << __COUT_HDR_FL__<< tabStr << "</" << XML_TO_CHAR(currEl->getNodeName()) << ">" << std::endl;
00436 if(out) *out << tabStr << "</" << XML_TO_CHAR(currEl->getNodeName()) << ">" << std::endl;
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
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 std::string XmlDocument::escapeString(std::string inString, bool allowWhiteSpace)
00519 {
00520 bool doit = false;
00521
00522 unsigned int ws = -1;
00523 char htmlTmp[6];
00524
00525 for(unsigned int i=0; i<inString.length(); i++)
00526 if(inString[i] != ' ')
00527 {
00528 if(doit) std::cout << __COUT_HDR_FL__<< inString[i] << ":" <<
00529 (int)inString[i] << ":" << inString << std::endl;
00530
00531
00532 if(inString[i] == '\r' || inString[i] == '\n' ||
00533 inString[i] == '\t' ||
00534 inString[i] < 32 ||
00535 (inString[i] > char(126) && inString[i] < char(161)))
00536
00537 {
00538 if(
00539 inString[i] == '\n')
00540 {
00541 if(allowWhiteSpace)
00542 {
00543 sprintf(htmlTmp,"&#%3.3d",inString[i]);
00544 inString.insert(i,htmlTmp);
00545 inString.replace(i+5,1,1,';');
00546 i+=6;
00547 --i;
00548 }
00549 else
00550 inString[i] = ' ';
00551 }
00552 else if(
00553 inString[i] == '\t')
00554 {
00555 if(allowWhiteSpace)
00556 {
00557 if(0)
00558 {
00559
00560 sprintf(htmlTmp,"        ");
00561 inString.insert(i,htmlTmp);
00562 inString.replace(i+47,1,1,';');
00563 i+=48;
00564 --i;
00565 }
00566 else
00567 {
00568
00569 sprintf(htmlTmp,"	");
00570 inString.insert(i,htmlTmp);
00571 inString.replace(i+5,1,1,';');
00572 i+=6;
00573 --i;
00574 }
00575 }
00576 else
00577 inString[i] = ' ';
00578 }
00579 else
00580 {
00581 inString.erase(i,1);
00582 --i;
00583 }
00584 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00585 continue;
00586 }
00587
00588 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00589
00590
00591 if(inString[i] == '\"' || inString[i] == '\'')
00592 {
00593 inString.insert(i,(inString[i] == '\'')?"&apos":""");
00594 inString.replace(i+5,1,1,';');
00595 i+=5;
00596
00597 }
00598 else if(inString[i] == '&')
00599 {
00600 inString.insert(i,"&");
00601 inString.replace(i+4,1,1,';');
00602 i+=4;
00603 }
00604 else if(inString[i] == '<' || inString[i] == '>')
00605 {
00606 inString.insert(i,(inString[i] == '<')?"<":">");
00607 inString.replace(i+3,1,1,';');
00608 i+=3;
00609 }
00610 else if(inString[i] >= char(161) && inString[i] <= char(255))
00611 {
00612 sprintf(htmlTmp,"&#%3.3d",inString[i]);
00613 inString.insert(i,htmlTmp);
00614 inString.replace(i+5,1,1,';');
00615 i+=5;
00616 }
00617
00618 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00619
00620 ws = i;
00621 }
00622 else if(allowWhiteSpace)
00623 {
00624 if(i-1 == ws) continue;
00625
00626
00627 if(i-2 == ws)
00628 {
00629 inString.insert(i," ");
00630 i+=6;
00631 }
00632 inString.insert(i," ");
00633 inString.replace(i+5,1,1,';');
00634 i+=5;
00635 ws = i;
00636 }
00637
00638 if(doit) std::cout << __COUT_HDR_FL__<< inString.size() << " " << ws << std::endl;
00639
00640 inString.substr(0,ws+1);
00641
00642 if(doit) std::cout << __COUT_HDR_FL__<< inString.size() << " " << inString << std::endl;
00643
00644 if(ws == (unsigned int)-1) return "";
00645 return inString.substr(0,ws+1);
00646 }
00647
00648
00649
00650
00651
00652 void XmlDocument::recursiveRemoveChild(xercesc::DOMElement *childEl, xercesc::DOMElement *parentEl)
00653 {
00654
00655 xercesc::DOMNodeList* nodeList = childEl->getChildNodes();
00656 for(unsigned int i = 0; i<nodeList->getLength(); ++i)
00657 recursiveRemoveChild((xercesc::DOMElement*)(nodeList->item(nodeList->getLength()-1-i)),childEl);
00658
00659
00660 parentEl->removeChild(childEl);
00661 childEl->release();
00662 }
00663
00664
00665
00666
00667
00668 void XmlDocument::saveXmlDocument (std::string filePath)
00669 {
00670 std::cout << __COUT_HDR_FL__<< "Saving theDocument_ to file: " << filePath << std::endl;
00671
00672
00673 xercesc::DOMImplementation *saveImplementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("LS"));
00674
00675 std::cout << __COUT_HDR_FL__<< "XERCES Version: " << _XERCES_VERSION << std::endl;
00676
00677 #if _XERCES_VERSION >= 30000
00678
00679
00680
00681 xercesc::DOMLSSerializer *serializer = ((xercesc::DOMImplementationLS*)saveImplementation)->createLSSerializer();
00682
00683
00684 if (serializer->getDomConfig()->canSetParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
00685 serializer->getDomConfig()->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00686
00687
00688 serializer->setNewLine(CONVERT_TO_XML("\r\n"));
00689
00690
00691
00692
00693
00694 xercesc::XMLFormatTarget* formatTarget;
00695 try
00696 {
00697
00698 formatTarget = new xercesc::LocalFileFormatTarget(filePath.c_str());
00699 }
00700 catch(...)
00701 {
00702 std::cout << __COUT_HDR_FL__<< "Inaccessible file path: " << filePath << std::endl;
00703 serializer->release();
00704
00705
00706 return;
00707 }
00708
00709
00710 xercesc::DOMLSOutput *output = ((xercesc::DOMImplementationLS*)saveImplementation)->createLSOutput();
00711
00712
00713 output->setByteStream(formatTarget);
00714
00715 serializer->write(theDocument_, output);
00716 serializer->release();
00717
00718 delete formatTarget;
00719 #else
00720
00721 xercesc::DOMWriter *serializer = ((xercesc::DOMImplementationLS*)saveImplementation)->createDOMWriter();
00722 serializer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733 XMLCh *tempFilePath = xercesc::XMLString::transcode(filePath.c_str());
00734 xercesc::XMLFormatTarget* formatTarget;
00735 try
00736 {
00737 formatTarget = new xercesc::LocalFileFormatTarget(tempFilePath);
00738 }
00739 catch(...)
00740 {
00741 std::cout << __COUT_HDR_FL__<< "Inaccessible file path: " << filePath << std::endl;
00742 serializer->release();
00743 xercesc::XMLString::release(&tempFilePath);
00744 return;
00745 }
00746
00747
00748
00749 serializer->writeNode(formatTarget, *theDocument_);
00750 serializer->release();
00751 xercesc::XMLString::release(&tempFilePath);
00752 delete formatTarget;
00753 #endif
00754
00755
00756
00757
00758
00759 #if _XERCES_VERSION >= 30000
00760
00761
00762 output->release();
00763
00764
00765 #endif
00766 }
00767
00768
00769
00770 bool XmlDocument::loadXmlDocument (std::string filePath)
00771 {
00772 std::cout << __COUT_HDR_FL__<< "Loading theDocument_ from file: " << filePath << std::endl;
00773
00774 struct stat fileStatus;
00775
00776 if(stat(filePath.c_str(), &fileStatus) != 0)
00777 {
00778 std::cout << __COUT_HDR_FL__<< "File not accessible." << std::endl;
00779 return false;
00780 }
00781
00782
00783 terminatePlatform();
00784 initPlatform();
00785
00786 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
00787
00788 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00789 parser->setDoNamespaces ( true );
00790 parser->setDoSchema ( true );
00791 parser->useCachedGrammarInParse ( false );
00792
00793 try
00794 {
00795 parser->parse( filePath.c_str() );
00796
00797
00798 theDocument_ = parser->adoptDocument();
00799
00800
00801 rootElement_ = theDocument_->getDocumentElement();
00802 if( !rootElement_ )
00803 throw(std::runtime_error( "empty XML theDocument_" ));
00804
00805 }
00806 catch( xercesc::XMLException& e )
00807 {
00808 std::cout << __COUT_HDR_FL__<< "Error parsing file." << std::endl;
00809 return false;
00810 }
00811 delete parser;
00812
00813 return true;
00814 }
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834