00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "DetectorDescription/Parser/interface/DDLSAX2FileHandler.h"
00018 #include "DetectorDescription/Parser/interface/DDLParser.h"
00019 #include "StrX.h"
00020 #include "DDLElementRegistry.h"
00021 #include "DDXMLElement.h"
00022
00023
00024 #include "DetectorDescription/Base/interface/DDdebug.h"
00025 #include "DetectorDescription/Base/interface/DDException.h"
00026 #include "DetectorDescription/Core/interface/DDConstant.h"
00027 #include "DetectorDescription/Core/interface/DDCurrentNamespace.h"
00028
00029
00030 #include <xercesc/sax2/Attributes.hpp>
00031 #include <xercesc/sax/SAXParseException.hpp>
00032 #include <xercesc/sax/SAXException.hpp>
00033
00034 #include <iostream>
00035 #include <vector>
00036 #include <string>
00037
00038
00039
00040
00041 DDLSAX2FileHandler::DDLSAX2FileHandler()
00042 {
00043 createDDConstants();
00044 std::string* sp = new std::string("*** root ***");
00045 namesMap_[*sp] = sp;
00046 names_.push_back(sp);
00047
00048 }
00049
00050 DDLSAX2FileHandler::~DDLSAX2FileHandler()
00051 { }
00052
00053
00054
00055
00056 void DDLSAX2FileHandler::startElement(const XMLCh* const uri
00057 , const XMLCh* const localname
00058 , const XMLCh* const qname
00059 , const Attributes& attrs)
00060 {
00061
00062 DCOUT_V('P', "DDLSAX2FileHandler::startElement started");
00063
00064 std::map<std::string, std::string*>::const_iterator namePtr = namesMap_.find(std::string(StrX(qname).localForm()));
00065 std::string* nameInt;
00066 if (namePtr != namesMap_.end())
00067 {
00068 nameInt = namePtr->second;
00069 }
00070 else
00071 {
00072 std::string * sp = new std::string(StrX(qname).localForm());
00073 nameInt = sp;
00074 namesMap_[*sp] = nameInt;
00075 }
00076 names_.push_back(nameInt);
00077 std::string myElementName(StrX(qname).localForm());
00078
00079 ++elementTypeCounter_[myElementName];
00080 DDXMLElement* myElement = DDLElementRegistry::getElement(myElementName);
00081
00082 unsigned int numAtts = attrs.getLength();
00083 std::vector<std::string> attrNames, attrValues;
00084
00085 for (unsigned int i = 0; i < numAtts; ++i)
00086 {
00087 attrNames.push_back(std::string(StrX(attrs.getLocalName(i)).localForm()));
00088 attrValues.push_back(std::string(StrX(attrs.getValue(i)).localForm()));
00089 }
00090
00091 DDLParser* beingParsed = DDLParser::instance();
00092 std::string nmspace = getnmspace(extractFileName( beingParsed->getCurrFileName()));
00093 myElement->loadAttributes(myElementName, attrNames, attrValues, nmspace);
00094
00095 myElement->loadText(std::string());
00096 DCOUT_V('P', "DDLSAX2FileHandler::startElement completed");
00097 }
00098
00099 void DDLSAX2FileHandler::endElement(const XMLCh* const uri
00100 , const XMLCh* const localname
00101 , const XMLCh* const qname)
00102 {
00103 std::string myElementName = *(names_.back());
00104
00105 DCOUT_V('P', "DDLSAX2FileHandler::endElement started");
00106 DCOUT_V('P', " " + myElementName);
00107
00108 DDXMLElement* myElement = DDLElementRegistry::getElement(myElementName);
00109 DDLParser* beingParsed = DDLParser::instance();
00110 std::string nmspace = getnmspace(extractFileName( beingParsed->getCurrFileName()));
00111 DDCurrentNamespace::ns() = nmspace;
00112 myElement->processElement(myElementName, nmspace);
00113 DCOUT_V('P', "DDLSAX2FileHandler::endElement completed");
00114 names_.pop_back();
00115 }
00116
00117 void DDLSAX2FileHandler::characters( const XMLCh* const chars
00118 , const unsigned int length)
00119 {
00120 DCOUT_V('P', "DDLSAX2FileHandler::characters started");
00121
00122 DDXMLElement* myElement = NULL;
00123 myElement = DDLElementRegistry::getElement(*(names_.back()));
00124
00125 std::string inString = "";
00126 for (unsigned int i = 0; i < length; ++i)
00127 {
00128 char s = chars[i];
00129 inString = inString + s;
00130 }
00131 if (myElement->gotText())
00132 myElement->appendText(inString);
00133 else
00134 myElement->loadText(inString);
00135
00136 DCOUT_V('P', "DDLSAX2FileHandler::characters completed");
00137 }
00138
00139 void DDLSAX2FileHandler::comment( const XMLCh* const chars
00140 , const unsigned int length)
00141 {
00142
00143 }
00144
00145 std::string DDLSAX2FileHandler::extractFileName(std::string fullname)
00146 {
00147 std::string ret = "";
00148 size_t bit = fullname.rfind('/');
00149 if ( bit < fullname.size() - 2 ) {
00150 ret=fullname.substr(bit+1);
00151 }
00152 return ret;
00153 }
00154
00155 void DDLSAX2FileHandler::dumpElementTypeCounter()
00156 {
00157
00158
00159
00160 }
00161
00162 void DDLSAX2FileHandler::createDDConstants() const
00163 {
00164 DDConstant::createConstantsFromEvaluator();
00165 }
00166
00167 std::string& DDLSAX2FileHandler::parent()
00168 {
00169 if (names_.size() > 1)
00170 {
00171 return *(names_[names_.size() - 2]);
00172 }
00173 return *(names_[0]);
00174 }
00175
00176 std::string& DDLSAX2FileHandler::self()
00177 {
00178 if (names_.size() > 2)
00179 return *(names_[names_.size() - 1]);
00180 return *(names_[0]);
00181 }