CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/DetectorDescription/Parser/src/DDLSAX2FileHandler.cc

Go to the documentation of this file.
00001 /***************************************************************************
00002                           DDLSAX2FileHandler.cc  -  description
00003                              -------------------
00004     begin                : Tue Oct 23 2001
00005     email                : case@ucdhep.ucdavis.edu
00006 ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *           DDDParser sub-component of DDD                                *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include "DetectorDescription/Parser/interface/DDLSAX2FileHandler.h"
00015 #include "DetectorDescription/Parser/interface/DDLParser.h"
00016 #include "DetectorDescription/Parser/src/StrX.h"
00017 #include "DetectorDescription/Parser/src/DDXMLElement.h"
00018 
00019 #include "DetectorDescription/Base/interface/DDdebug.h"
00020 #include "DetectorDescription/Core/interface/DDConstant.h"
00021 #include "DetectorDescription/Core/interface/DDCurrentNamespace.h"
00022 
00023 #include <iostream>
00024 
00025 // XERCES_CPP_NAMESPACE_USE 
00026 
00027 DDLSAX2FileHandler::DDLSAX2FileHandler( DDCompactView & cpv )
00028   : cpv_(cpv),
00029     xmlelems_()
00030 {
00031   init();
00032 }
00033 
00034 void
00035 DDLSAX2FileHandler::init( void )
00036 {
00037   createDDConstants();
00038   namesMap_.push_back("*** root ***");
00039   names_.push_back(namesMap_.size() - 1);
00040 }
00041 
00042 DDLSAX2FileHandler::~DDLSAX2FileHandler( void )
00043 {}
00044 
00045 // ---------------------------------------------------------------------------
00046 //  DDLSAX2Handler: Implementation of the SAX DocumentHandler interface
00047 // ---------------------------------------------------------------------------
00048 void
00049 DDLSAX2FileHandler::startElement(const XMLCh* const uri
00050                                  , const XMLCh* const localname
00051                                  , const XMLCh* const qname
00052                                  , const Attributes& attrs)
00053 {
00054   DCOUT_V('P', "DDLSAX2FileHandler::startElement started");
00055 
00056   std::string myElementName(StrX(qname).localForm());
00057   size_t i = 0;
00058   for (; i < namesMap_.size(); ++i) {
00059     if ( myElementName == namesMap_.at(i) ) {
00060       names_.push_back(i);
00061       break;
00062     }
00063   }
00064   if (i >= namesMap_.size()) {
00065     namesMap_.push_back(myElementName);
00066     names_.push_back(namesMap_.size() - 1);
00067   }
00068 
00069   ++elementTypeCounter_[myElementName];
00070   //final way
00071   //  DDXMLElement* myElement = xmlelems_.getElement(myElementName); //myRegistry_->getElement(myElementName);
00072   //temporary way:
00073   DDXMLElement* myElement = DDLGlobalRegistry::instance().getElement(myElementName);
00074 
00075   unsigned int numAtts = attrs.getLength();
00076   std::vector<std::string> attrNames, attrValues;
00077 
00078   for (unsigned int i = 0; i < numAtts; ++i)
00079   {
00080     //       char* temp2 = XMLString::transcode(attrs.getLocalName(i));
00081     //       char* temp3 = XMLString::transcode(attrs.getValue(i));
00082     attrNames.push_back(std::string(StrX(attrs.getLocalName(i)).localForm()));
00083     attrValues.push_back(std::string(StrX(attrs.getValue(i)).localForm()));
00084     //       XMLString::release(&temp2);
00085     //       XMLString::release(&temp3);
00086   }
00087   
00088   myElement->loadAttributes(myElementName, attrNames, attrValues, nmspace_, cpv_);
00089   //  initialize text
00090   myElement->loadText(std::string()); 
00091   DCOUT_V('P', "DDLSAX2FileHandler::startElement completed");
00092 }
00093 
00094 void
00095 DDLSAX2FileHandler::endElement( const XMLCh* const uri,
00096                                 const XMLCh* const localname,
00097                                 const XMLCh* const qname )
00098 {
00099   std::string ts(StrX(qname).localForm());
00100   const std::string&  myElementName = self();
00101   DCOUT_V('P', "DDLSAX2FileHandler::endElement started");
00102   DCOUT_V('P', "    " + myElementName);
00103   //final way
00104   //  DDXMLElement* myElement = xmlelems_.getElement(myElementName); //myRegistry_->getElement(myElementName);
00105   //temporary way:
00106   DDXMLElement* myElement = DDLGlobalRegistry::instance().getElement(myElementName);
00107 
00108   //   DDLParser* beingParsed = DDLParser::instance();
00109   //   std::string nmspace = getnmspace(extractFileName( beingParsed->getCurrFileName()));
00110   std::string nmspace = nmspace_;
00111   // The need for processElement to have the nmspace so that it can 
00112   // do the necessary gymnastics made things more complicated in the
00113   // effort to allow fully user-controlled namespaces.  So the "magic"
00114   // trick of setting nmspace to "!" is used :(... I don't like this magic trick
00115   // -- Michael Case 2008-11-06
00116   // OPTIMISE in the near future, like the current nmspace_ impl. 
00117   // just set nmspace_ to "!" from Parser based on userNS_ so 
00118   // userNS_ is set by parser ONCE and no if nec. here. MEC: 2009-06-22
00119   if ( userNS_ ) {
00120     nmspace = "!";
00121   } 
00122   //  std::cout << "namespace " << nmspace_ << std::endl;
00123   DDCurrentNamespace::ns() = nmspace;
00124   // tell the element it's parent name for recording/reporting purposes
00125   myElement->setParent(parent());
00126   myElement->setSelf(self());
00127   myElement->processElement(myElementName, nmspace, cpv_);
00128   DCOUT_V('P', "DDLSAX2FileHandler::endElement completed");
00129   names_.pop_back();
00130 }
00131 
00132 void
00133 DDLSAX2FileHandler::characters( const XMLCh* const chars,
00134                                 const unsigned int length )
00135 {
00136   DCOUT_V('P', "DDLSAX2FileHandler::characters started");
00137   //  std::cout << "character handler started" << std::endl;
00138   //DDXMLElement* myElement = NULL;
00139   // final way
00140   //  myElement = xmlelems_.getElement(namesMap_[names_.back()]);
00141   //temporary way:
00142   //  const std::string&  myElementName = namesMap_[names_.back()];
00143   DDXMLElement* myElement = DDLGlobalRegistry::instance().getElement(self());//myElementName); //namesMap_[names_.back()]);
00144   std::string inString = "";
00145   for (unsigned int i = 0; i < length; ++i)
00146   {
00147     char s = chars[i];
00148     inString = inString + s;
00149   }
00150   if (myElement->gotText())
00151     myElement->appendText(inString);
00152   else
00153     myElement->loadText(inString);
00154 
00155   DCOUT_V('P', "DDLSAX2FileHandler::characters completed"); 
00156 }
00157 
00158 void
00159 DDLSAX2FileHandler::comment( const   XMLCh* const    chars
00160                              , const unsigned int    length)
00161 {
00162   // ignore, discard, overkill since base class also has this...
00163 }
00164 
00165 void
00166 DDLSAX2FileHandler::dumpElementTypeCounter( void )
00167 {}
00168 
00169 void
00170 DDLSAX2FileHandler::createDDConstants( void ) const
00171 {
00172   DDConstant::createConstantsFromEvaluator();
00173 }
00174 
00175 const std::string&
00176 DDLSAX2FileHandler::parent( void ) const
00177 {
00178   if (names_.size() > 2)
00179   {
00180     return namesMap_.at(names_.at(names_.size() - 2));
00181   }
00182   return namesMap_[0];//.at(names_.at(0));
00183 }
00184 
00185 const std::string&
00186 DDLSAX2FileHandler::self( void ) const
00187 {
00188   if (names_.size() > 1) {
00189     return namesMap_.at(names_.at(names_.size() - 1));
00190   }
00191   return namesMap_[0];//.at(names_.at(0));
00192 }