CMS 3D CMS Logo

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