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