Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:55:08

0001 #include "DetectorDescription/Parser/interface/DDLSAX2FileHandler.h"
0002 #include "DetectorDescription/Core/interface/DDConstant.h"
0003 #include "DetectorDescription/Core/interface/DDCurrentNamespace.h"
0004 #include "DetectorDescription/Parser/src/DDXMLElement.h"
0005 #include "Utilities/Xerces/interface/XercesStrUtils.h"
0006 
0007 using namespace cms::xerces;
0008 
0009 class DDCompactView;
0010 
0011 // XERCES_CPP_NAMESPACE_USE
0012 
0013 DDLSAX2FileHandler::DDLSAX2FileHandler(DDCompactView& cpv, DDLElementRegistry& reg) : cpv_(cpv), registry_{reg} {
0014   init();
0015 }
0016 
0017 void DDLSAX2FileHandler::init() {
0018   namesMap_.emplace_back("*** root ***");
0019   names_.emplace_back(namesMap_.size() - 1);
0020 }
0021 
0022 DDLSAX2FileHandler::~DDLSAX2FileHandler() {}
0023 
0024 void DDLSAX2FileHandler::startElement(const XMLCh* const uri,
0025                                       const XMLCh* const localname,
0026                                       const XMLCh* const qname,
0027                                       const Attributes& attrs) {
0028   std::string myElementName(cStr(qname).ptr());
0029   size_t i = 0;
0030   for (; i < namesMap_.size(); ++i) {
0031     if (myElementName == namesMap_.at(i)) {
0032       names_.emplace_back(i);
0033       break;
0034     }
0035   }
0036   if (i >= namesMap_.size()) {
0037     namesMap_.emplace_back(myElementName);
0038     names_.emplace_back(namesMap_.size() - 1);
0039   }
0040 
0041   auto myElement = registry_.getElement(myElementName);
0042 
0043   unsigned int numAtts = attrs.getLength();
0044   std::vector<std::string> attrNames, attrValues;
0045 
0046   for (unsigned int i = 0; i < numAtts; ++i) {
0047     attrNames.emplace_back(std::string(cStr(attrs.getLocalName(i)).ptr()));
0048     attrValues.emplace_back(std::string(cStr(attrs.getValue(i)).ptr()));
0049   }
0050 
0051   myElement->loadAttributes(myElementName, attrNames, attrValues, nmspace_, cpv_);
0052   //  initialize text
0053   myElement->loadText(std::string());
0054 }
0055 
0056 void DDLSAX2FileHandler::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname) {
0057   std::string ts(cStr(qname).ptr());
0058   const std::string& myElementName = self();
0059 
0060   auto myElement = registry_.getElement(myElementName);
0061 
0062   std::string nmspace = nmspace_;
0063   // The need for processElement to have the nmspace so that it can
0064   // do the necessary gymnastics made things more complicated in the
0065   // effort to allow fully user-controlled namespaces.  So the "magic"
0066   // trick of setting nmspace to "!" is used :(... I don't like this magic trick
0067   // -- Michael Case 2008-11-06
0068   // OPTIMISE in the near future, like the current nmspace_ impl.
0069   // just set nmspace_ to "!" from Parser based on userNS_ so
0070   // userNS_ is set by parser ONCE and no if nec. here. MEC: 2009-06-22
0071   if (userNS_) {
0072     nmspace = "!";
0073   }
0074 
0075   DDCurrentNamespace::ns() = nmspace;
0076   // tell the element it's parent name for recording/reporting purposes
0077   myElement->setParent(parent());
0078   myElement->setSelf(self());
0079   myElement->processElement(myElementName, nmspace, cpv_);
0080 
0081   names_.pop_back();
0082 }
0083 
0084 void DDLSAX2FileHandler::characters(const XMLCh* const chars, const XMLSize_t length) {
0085   auto myElement = registry_.getElement(self());
0086   std::string inString = "";
0087   for (XMLSize_t i = 0; i < length; ++i) {
0088     char s = chars[i];
0089     inString = inString + s;
0090   }
0091   if (myElement->gotText())
0092     myElement->appendText(inString);
0093   else
0094     myElement->loadText(inString);
0095 }
0096 
0097 void DDLSAX2FileHandler::comment(const XMLCh* const chars, const XMLSize_t length) {}
0098 
0099 void DDLSAX2FileHandler::createDDConstants(void) const {
0100   DDConstant::createConstantsFromEvaluator(registry_.evaluator());
0101 }
0102 
0103 const std::string& DDLSAX2FileHandler::parent(void) const {
0104   if (names_.size() > 2) {
0105     return namesMap_.at(names_.at(names_.size() - 2));
0106   }
0107   return namesMap_[0];  //.at(names_.at(0));
0108 }
0109 
0110 const std::string& DDLSAX2FileHandler::self(void) const {
0111   if (names_.size() > 1) {
0112     return namesMap_.at(names_.at(names_.size() - 1));
0113   }
0114   return namesMap_[0];  //.at(names_.at(0));
0115 }