File indexing completed on 2024-04-06 12:05:33
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
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
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
0064
0065
0066
0067
0068
0069
0070
0071 if (userNS_) {
0072 nmspace = "!";
0073 }
0074
0075 DDCurrentNamespace::ns() = nmspace;
0076
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];
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];
0115 }