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
00176 try
00177 {
00178 child->appendChild(theDocument_->createTextNode(CONVERT_TO_XML(childText)));
00179 }
00180 catch(...)
00181 {
00182 __MOUT_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 __MOUT__ << "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
00529
00530 if(doit) std::cout << __COUT_HDR_FL__<< inString[i] << ":" <<
00531 (int)inString[i] << ":" << inString << std::endl;
00532
00533
00534 if(inString[i] == '\r' || inString[i] == '\n' ||
00535 inString[i] == '\t' ||
00536 inString[i] < 32 ||
00537 (inString[i] > char(126) && inString[i] < char(161)))
00538
00539 {
00540 if(allowWhiteSpace &&
00541 inString[i] == '\n')
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 if(allowWhiteSpace &&
00550 inString[i] == '\t')
00551 {
00552 if(0)
00553 {
00554
00555 sprintf(htmlTmp,"        ");
00556 inString.insert(i,htmlTmp);
00557 inString.replace(i+47,1,1,';');
00558 i+=48;
00559 --i;
00560 }
00561 else
00562 {
00563
00564 sprintf(htmlTmp,"	");
00565 inString.insert(i,htmlTmp);
00566 inString.replace(i+5,1,1,';');
00567 i+=6;
00568 --i;
00569 }
00570 }
00571 else
00572 {
00573 inString.erase(i,1);
00574 --i;
00575 }
00576 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00577 continue;
00578 }
00579
00580 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00581
00582
00583 if(inString[i] == '\"' || inString[i] == '\'')
00584 {
00585 inString.insert(i,(inString[i] == '\'')?"&apos":""");
00586 inString.replace(i+5,1,1,';');
00587 i+=5;
00588
00589 }
00590 else if(inString[i] == '&')
00591 {
00592 inString.insert(i,"&");
00593 inString.replace(i+4,1,1,';');
00594 i+=4;
00595 }
00596 else if(inString[i] == '<' || inString[i] == '>')
00597 {
00598 inString.insert(i,(inString[i] == '<')?"<":">");
00599 inString.replace(i+3,1,1,';');
00600 i+=3;
00601 }
00602 else if(inString[i] >= char(161) && inString[i] <= char(255))
00603 {
00604 sprintf(htmlTmp,"&#%3.3d",inString[i]);
00605 inString.insert(i,htmlTmp);
00606 inString.replace(i+5,1,1,';');
00607 i+=5;
00608 }
00609
00610 if(doit) std::cout << __COUT_HDR_FL__<< inString << std::endl;
00611
00612 ws = i;
00613 }
00614 else if(allowWhiteSpace)
00615 {
00616 if(i-1 == ws) continue;
00617
00618
00619 if(i-2 == ws)
00620 {
00621 inString.insert(i," ");
00622 i+=6;
00623 }
00624 inString.insert(i," ");
00625 inString.replace(i+5,1,1,';');
00626 i+=5;
00627 ws = i;
00628 }
00629
00630 if(doit) std::cout << __COUT_HDR_FL__<< inString.size() << " " << ws << std::endl;
00631
00632 inString.substr(0,ws+1);
00633
00634 if(doit) std::cout << __COUT_HDR_FL__<< inString.size() << " " << inString << std::endl;
00635
00636 if(ws == (unsigned int)-1) return "";
00637 return inString.substr(0,ws+1);
00638 }
00639
00640
00641
00642
00643
00644 void XmlDocument::recursiveRemoveChild(xercesc::DOMElement *childEl, xercesc::DOMElement *parentEl)
00645 {
00646
00647 xercesc::DOMNodeList* nodeList = childEl->getChildNodes();
00648 for(unsigned int i = 0; i<nodeList->getLength(); ++i)
00649 recursiveRemoveChild((xercesc::DOMElement*)(nodeList->item(nodeList->getLength()-1-i)),childEl);
00650
00651
00652 parentEl->removeChild(childEl);
00653 childEl->release();
00654 }
00655
00656
00657
00658
00659
00660 void XmlDocument::saveXmlDocument (std::string filePath)
00661 {
00662 std::cout << __COUT_HDR_FL__<< "Saving theDocument_ to file: " << filePath << std::endl;
00663
00664
00665 xercesc::DOMImplementation *saveImplementation = xercesc::DOMImplementationRegistry::getDOMImplementation(CONVERT_TO_XML("LS"));
00666
00667 std::cout << __COUT_HDR_FL__<< "XERCES Version: " << _XERCES_VERSION << std::endl;
00668
00669 #if _XERCES_VERSION >= 30000
00670
00671
00672
00673 xercesc::DOMLSSerializer *serializer = ((xercesc::DOMImplementationLS*)saveImplementation)->createLSSerializer();
00674
00675
00676 if (serializer->getDomConfig()->canSetParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
00677 serializer->getDomConfig()->setParameter(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00678
00679
00680 serializer->setNewLine(CONVERT_TO_XML("\r\n"));
00681
00682
00683
00684
00685
00686 xercesc::XMLFormatTarget* formatTarget;
00687 try
00688 {
00689
00690 formatTarget = new xercesc::LocalFileFormatTarget(filePath.c_str());
00691 }
00692 catch(...)
00693 {
00694 std::cout << __COUT_HDR_FL__<< "Inaccessible file path: " << filePath << std::endl;
00695 serializer->release();
00696
00697
00698 return;
00699 }
00700
00701
00702 xercesc::DOMLSOutput *output = ((xercesc::DOMImplementationLS*)saveImplementation)->createLSOutput();
00703
00704
00705 output->setByteStream(formatTarget);
00706
00707 serializer->write(theDocument_, output);
00708 serializer->release();
00709
00710 delete formatTarget;
00711 #else
00712
00713 xercesc::DOMWriter *serializer = ((xercesc::DOMImplementationLS*)saveImplementation)->createDOMWriter();
00714 serializer->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725 XMLCh *tempFilePath = xercesc::XMLString::transcode(filePath.c_str());
00726 xercesc::XMLFormatTarget* formatTarget;
00727 try
00728 {
00729 formatTarget = new xercesc::LocalFileFormatTarget(tempFilePath);
00730 }
00731 catch(...)
00732 {
00733 std::cout << __COUT_HDR_FL__<< "Inaccessible file path: " << filePath << std::endl;
00734 serializer->release();
00735 xercesc::XMLString::release(&tempFilePath);
00736 return;
00737 }
00738
00739
00740
00741 serializer->writeNode(formatTarget, *theDocument_);
00742 serializer->release();
00743 xercesc::XMLString::release(&tempFilePath);
00744 delete formatTarget;
00745 #endif
00746
00747
00748
00749
00750
00751 #if _XERCES_VERSION >= 30000
00752
00753
00754 output->release();
00755
00756
00757 #endif
00758 }
00759
00760
00761
00762 bool XmlDocument::loadXmlDocument (std::string filePath)
00763 {
00764 std::cout << __COUT_HDR_FL__<< "Loading theDocument_ from file: " << filePath << std::endl;
00765
00766 struct stat fileStatus;
00767
00768 if(stat(filePath.c_str(), &fileStatus) != 0)
00769 {
00770 std::cout << __COUT_HDR_FL__<< "File not accessible." << std::endl;
00771 return false;
00772 }
00773
00774
00775 terminatePlatform();
00776 initPlatform();
00777
00778 xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser;
00779
00780 parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00781 parser->setDoNamespaces ( true );
00782 parser->setDoSchema ( true );
00783 parser->useCachedGrammarInParse ( false );
00784
00785 try
00786 {
00787 parser->parse( filePath.c_str() );
00788
00789
00790 theDocument_ = parser->adoptDocument();
00791
00792
00793 rootElement_ = theDocument_->getDocumentElement();
00794 if( !rootElement_ )
00795 throw(std::runtime_error( "empty XML theDocument_" ));
00796
00797 }
00798 catch( xercesc::XMLException& e )
00799 {
00800 std::cout << __COUT_HDR_FL__<< "Error parsing file." << std::endl;
00801 return false;
00802 }
00803 delete parser;
00804
00805 return true;
00806 }
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826