CMS 3D CMS Logo

Public Types | Public Member Functions | Protected Member Functions | Private Attributes

DDLParser Class Reference

DDLParser is the main class of Detector Description Language Parser. More...

#include <DDLParser.h>

List of all members.

Public Types

typedef std::map< int,
std::pair< std::string,
std::string > > 
FileNameHolder
typedef
XERCES_CPP_NAMESPACE::SAX2XMLReader 
SAX2XMLReader

Public Member Functions

void clearFiles ()
 Clear the file list - see Warning!
 DDLParser (DDCompactView &cpv)
 Constructor MUST associate a DDCompactView storage.
void dumpFileList (std::ostream &co)
void dumpFileList ()
 Print out the list of files.
std::string extractFileName (std::string fullname)
std::string getCurrFileName ()
 Report which file currently being processed (or last processed).
DDLSAX2FileHandlergetDDLSAX2FileHandler ()
 To get the parent this class allows access to the handler.
std::vector< std::string > getFileList ()
 Return list of files.
std::string getNameSpace (const std::string &fname)
SAX2XMLReadergetXMLParser ()
 Get the SAX2Parser from the DDLParser. USE WITH CAUTION. Set your own handler, etc.
size_t isFound (const std::string &filename)
 Is the file already known by the DDLParser? Returns 0 if not found, and index if found.
bool isParsed (const std::string &filename)
 Is the file already parsed?
void parse (const std::vector< unsigned char > &ablob, unsigned int bsize)
int parse (const DDLDocumentProvider &dp)
 Parse all files. Return is meaningless.
bool parseOneFile (const std::string &filename)
 Process a single files.
 ~DDLParser ()
 Destructor terminates the XMLPlatformUtils (as required by Xerces)

Protected Member Functions

 DDLParser ()
void parseFile (const int &numtoproc)
 Parse File. Just to hold some common looking code.

Private Attributes

std::string configFileName_
 Configuration file name. Only necessary until deprecated methods removed.
DDCompactViewcpv_
 reference to storage
std::string currFileName_
 Which file is currently being processed.
DDLSAX2HandlererrHandler_
DDLSAX2ExpressionHandlerexpHandler_
DDLSAX2FileHandlerfileHandler_
FileNameHolder fileNames_
 List of files to be processed, obtained from the DDLDocumentProvider.
int nFiles_
 Number of files + 1.
std::map< int, bool > parsed_
 Parse status of a given file.
SAX2XMLReaderSAX2Parser_
 SAX2XMLReader is one way of parsing.

Detailed Description

DDLParser is the main class of Detector Description Language Parser.

Author:
Michael Case

DDLParser.h - description ------------------- begin: Mon Oct 22 2001 email: case@ucdhep.ucdavis.edu

Singleton which controls the parsing of XML files (DDL). It guarantees that a given filename will only be parsed once regardless of its path. It now relies on a DDLDocumentProvider class which provides a list of file names and URLs to be parsed.

It uses the Xerces C++ Parser from the Apache Group straight-forwardly. One main thing to note is that only one DDLParser can ever be made. This allows for sub-components of the parser to easily find out information from the parser during run-time.

There is an interface to parse just one file. If one uses this method and does not use the default DDLDocumentProvider user is responsible for also setting the DDRootDef.

Modification: 2003-02-13: Michael Case, Stepan Wynhoff and Martin Liendl 2003-02-24: same. DDLParser will use DDLDocumentProvider (abstract). One of these and will be defaulted to DDLConfiguration. This will read the "configuration.xml" file provided and will be used by the Parser to "get" the files. 2005-11-13: Michael Case removed some of the un-necessary methods that were deprecated. 2010-01 to 2010-04 sometime: Michael Case removed singleton-ness. MUST have a DDCompactView to refer to and no more default constructor at the moment. 2010-07-29: removed DDLConfiguration; use FIPConfiguration, it is easier. for CMSSW Framework example see XMLIdealGeometryESSource (different DDLDocumentProvider completely

Definition at line 64 of file DDLParser.h.


Member Typedef Documentation

typedef std::map< int, std::pair<std::string, std::string> > DDLParser::FileNameHolder

Definition at line 70 of file DDLParser.h.

typedef XERCES_CPP_NAMESPACE::SAX2XMLReader DDLParser::SAX2XMLReader

Definition at line 68 of file DDLParser.h.


Constructor & Destructor Documentation

DDLParser::DDLParser ( DDCompactView cpv)

Constructor MUST associate a DDCompactView storage.

Definition at line 32 of file DDLParser.cc.

References AlgoInit(), DCOUT_V, errHandler_, expHandler_, fileHandler_, and SAX2Parser_.

  : cpv_( cpv ),
    nFiles_( 0 )
{
  XMLPlatformUtils::Initialize();
  AlgoInit();
  SAX2Parser_  = XMLReaderFactory::createXMLReader();
  
  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false);   // optional
  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);   // optional
  // Specify other parser features, e.g.
  //  SAX2Parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
  
  expHandler_  = new DDLSAX2ExpressionHandler(cpv);
  fileHandler_ = new DDLSAX2FileHandler(cpv);
  errHandler_  = new DDLSAX2Handler();
  SAX2Parser_->setErrorHandler(errHandler_); 
  SAX2Parser_->setContentHandler(fileHandler_); 
  
  DCOUT_V('P', "DDLParser::DDLParser(): new (and only) DDLParser"); 
}
DDLParser::DDLParser ( ) [protected]
DDLParser::~DDLParser ( void  )

Destructor terminates the XMLPlatformUtils (as required by Xerces)

Definition at line 55 of file DDLParser.cc.

References DCOUT_V, errHandler_, expHandler_, and fileHandler_.

{ 
  // clean up and leave
  delete expHandler_;
  delete fileHandler_;
  delete errHandler_;
  XMLPlatformUtils::Terminate();
  DCOUT_V('P', "DDLParser::~DDLParser(): destruct DDLParser"); 
}

Member Function Documentation

void DDLParser::clearFiles ( void  )

Clear the file list - see Warning!

This could result in mangled geometry if the Core has not been cleared.

Definition at line 401 of file DDLParser.cc.

References fileNames_, and parsed_.

Referenced by XMLIdealGeometryESProducer::produce().

{
  fileNames_.clear();
  parsed_.clear();
}
void DDLParser::dumpFileList ( void  )

Print out the list of files.

Definition at line 208 of file DDLParser.cc.

References fileNames_.

{
  edm::LogInfo ("DDLParser") << "File List:" << std::endl;
  for (FileNameHolder::const_iterator it = fileNames_.begin(); it != fileNames_.end(); ++it)
    edm::LogInfo ("DDLParser") << it->second.second << std::endl;
}
void DDLParser::dumpFileList ( std::ostream &  co)
std::string DDLParser::extractFileName ( std::string  fullname)

Definition at line 408 of file DDLParser.cc.

References runTheMatrix::ret.

Referenced by parse(), and parseOneFile().

{
  std::string ret = "";
  size_t bit = fullname.rfind('/');
  if ( bit < fullname.size() - 2 ) {
    ret=fullname.substr(bit+1);
  }
  return ret;
}
std::string DDLParser::getCurrFileName ( void  )

Report which file currently being processed (or last processed).

Definition at line 395 of file DDLParser.cc.

References currFileName_.

{
  return currFileName_;
}
DDLSAX2FileHandler * DDLParser::getDDLSAX2FileHandler ( void  )

To get the parent this class allows access to the handler.

In order to retrieve the name of the parent element from DDLSAX2Handlers.

Definition at line 76 of file DDLParser.cc.

References fileHandler_.

Referenced by XMLIdealGeometryESSource::produce(), and XMLIdealGeometryESProducer::produce().

{ 
  return fileHandler_; 
}
std::vector< std::string > DDLParser::getFileList ( void  )

Return list of files.

Definition at line 197 of file DDLParser.cc.

References fileNames_.

{
  std::vector<std::string> flist;
  for (FileNameHolder::const_iterator fit = fileNames_.begin(); fit != fileNames_.end(); ++fit)
  {
    flist.push_back(fit->second.first); // was .second (mec: 2003:02:19
  }
  return flist;
}
std::string DDLParser::getNameSpace ( const std::string &  fname)

Definition at line 419 of file DDLParser.cc.

References j, and runTheMatrix::ret.

Referenced by parse(), and parseOneFile().

{
  size_t j = 0;
  std::string ret="";
  while (j < fname.size() && fname[j] != '.')
    ++j;
  if (j < fname.size() && fname[j] == '.')
    ret = fname.substr(0, j);
  return ret;
}
SAX2XMLReader * DDLParser::getXMLParser ( void  )

Get the SAX2Parser from the DDLParser. USE WITH CAUTION. Set your own handler, etc.

This method allows external "users" to use the current DDLParser on their own. by giving them access to the SAX2XMLReader. This may not be a good idea! The reason that I

Definition at line 70 of file DDLParser.cc.

References SAX2Parser_.

Referenced by FIPConfiguration::readConfig().

{
  return SAX2Parser_;
}
size_t DDLParser::isFound ( const std::string &  filename)

Is the file already known by the DDLParser? Returns 0 if not found, and index if found.

Definition at line 82 of file DDLParser.cc.

References fileNames_, and i.

Referenced by isParsed(), parse(), and parseOneFile().

{
  FileNameHolder::const_iterator it = fileNames_.begin();
  size_t i = 1;
  bool foundFile = false;
  while( it != fileNames_.end() && !foundFile )
  {
    if( it->second.first == filename )
    {
      foundFile = true;
    }
    else ++i;
    ++it;
  }
  if( foundFile )
    return i;
  return 0;
}
bool DDLParser::isParsed ( const std::string &  filename)

Is the file already parsed?

Definition at line 102 of file DDLParser.cc.

References newFWLiteAna::found, isFound(), and parsed_.

{
  size_t found = isFound(filename);
  if (found)
    return parsed_[found];
  return false;
}
void DDLParser::parse ( const std::vector< unsigned char > &  ablob,
unsigned int  bsize 
)

Definition at line 189 of file DDLParser.cc.

References SAX2Parser_.

{
  char* dummy(0);
  MemBufInputSource  mbis( &*ablob.begin(), bsize, dummy );
  SAX2Parser_->parse(mbis);
}
int DDLParser::parse ( const DDLDocumentProvider dp)

Parse all files. Return is meaningless.

Definition at line 224 of file DDLParser.cc.

References currFileName_, DCOUT, DDLSAX2FileHandler::dumpElementTypeCounter(), expHandler_, extractFileName(), fileHandler_, fileNames_, DDLDocumentProvider::getFileList(), getNameSpace(), DDLDocumentProvider::getURLList(), i, isFound(), LogDebug, nFiles_, parsed_, parseFile(), SAX2Parser_, DDLSAX2Handler::setNameSpace(), and findQualityFiles::size.

Referenced by output(), XMLIdealGeometryESSource::produce(), XMLIdealGeometryESProducer::produce(), and testParser().

{
  //  edm::LogInfo ("DDLParser") << "Start Parsing.  Validation is set to " << dp.doValidation() << "." << std::endl;
  edm::LogInfo ("DDLParser") << "Start Parsing.  Validation is set off for the time being." << std::endl;
  // prep for pass 1 through DDD XML
  //   // Since this block does nothing for CMSSW right now, I have taken it all out
  //   This clean-up involves interface changes such as the removal of doValidation() everywhere (OR NOT 
  //   if I decide to keep it for other testing reasons.)
  //    if (dp.doValidation())
  //      { 
  // //       DCOUT_V('P', "WARNING:  PARSER VALIDATION IS TURNED OFF REGARDLESS OF <Schema... ELEMENT");
  //   SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, true);
  //   SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
  // //       //  SAX2Parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
  //      }
  //    else
  //     {
  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false);
  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
  //       //  SAX2Parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, false);

  //     }

  //  This need be only done once, so might as well to it here.
  size_t fileIndex = 0;
  std::vector<std::string> fullFileName;

  for (; fileIndex < (dp.getFileList()).size(); ++fileIndex)
  { 
    std::string ts = dp.getURLList()[fileIndex];
    std::string tf = dp.getFileList()[fileIndex];
    if ( ts.size() > 0 ) {
      if ( ts[ts.size() - 1] == '/') {
        fullFileName.push_back( ts + tf );
      } else {
        fullFileName.push_back( ts + "/" + tf );
      }
    } else {
      fullFileName.push_back( tf );
    }
  }

  for (std::vector<std::string>::const_iterator fnit = fullFileName.begin(); 
       fnit != fullFileName.end();
       ++fnit)
  {
    size_t foundFile = isFound(extractFileName( *fnit )); 
        
    if (!foundFile)
    {
      pair <std::string, std::string> pss;
      pss.first = extractFileName( *fnit );
      pss.second = *fnit;
      fileNames_[nFiles_++] = pss;
      parsed_[nFiles_ - 1]=false;
    }
  }
    
  // Start processing the files found in the config file.
  
  // PASS 1:  This was added later (historically) to implement the DDD
  // requirement for Expressions.
  DCOUT('P', "DDLParser::parse(): PASS1: Just before setting Xerces content and error handlers... ");
  
  
  // in cleaning up try-catch blocks 2007-06-26 I decided to remove
  // this because of CMSSW rules. but keep the commented way I used to
  // do it...
  //   DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
  //     {
  SAX2Parser_->setContentHandler(expHandler_);
  for (size_t i = 0; i < fileNames_.size(); ++i)
  {
    //    seal::SealTimer t("DDLParser: parsing expressions of file " +fileNames_[i].first);
    if (!parsed_[i])
    {
      currFileName_ = fileNames_[i].second;
      //              std::cout << "currFileName = " << currFileName_ << std::endl;
      expHandler_->setNameSpace( getNameSpace(extractFileName(currFileName_)) );
      //              std::cout << "2)  namespace = " << getNameSpace(extractFileName(currFileName_)) << std::endl;
      parseFile(i);
    }
  }
  expHandler_->dumpElementTypeCounter();
  //     }
  //   DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
  //     edm::LogInfo ("DDLParser") << "\nPASS1: XMLException while processing files... \n"
  //     << "Exception message is: \n"
  //     << StrX(toCatch.getMessage()) << "\n" ;
  //     XMLPlatformUtils::Terminate();
  //     // FIX use this after DEPRECATED stuff removed:    throw(DDException("See XML Exception above"));
  //     return -1;
  //   }
  // PASS 2:

  DCOUT('P', "DDLParser::parse(): PASS2: Just before setting Xerces content and error handlers... ");

  // in cleaning up try-catch blocks 2007-06-26 I decided to remove
  // this because of CMSSW rules. but keep the commented way I used to
  // do it...
  //   DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
  //     {
  SAX2Parser_->setContentHandler(fileHandler_);

  // No need to validate (regardless of user's doValidation
  // because the files have already been validated on the first pass.
  // This optimization suggested by Martin Liendl.
  //       SAX2Parser_->setFeature(StrX("https://xml.org/sax/features/validation"), false);   // optional
  //       SAX2Parser_->setFeature(StrX("https://xml.org/sax/features/namespaces"), false);   // optional
  //       SAX2Parser_->setFeature(StrX("https://apache.org/xml/features/validation/dynamic"), false);


  // Process files again.
  for (size_t i = 0; i < fileNames_.size(); ++i)
  {
    //    seal::SealTimer t("DDLParser: parsing all elements of file " +fileNames_[i].first);
    if (!parsed_[i]) {
      currFileName_ = fileNames_[i].second;
      //            std::cout << "currFileName = " << currFileName_ << std::endl;
      fileHandler_->setNameSpace( getNameSpace(extractFileName(currFileName_)) );
      //            std::cout << "3)  namespace = " << getNameSpace(extractFileName(currFileName_)) << std::endl;
      parseFile(i);
      parsed_[i] = true;
      pair<std::string, std::string> namePair = fileNames_[i];
      LogDebug ("DDLParser") << "Completed parsing file " << namePair.second << std::endl;
    }
  }
  //     }
  //   DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
  //     edm::LogError ("DDLParser") << "\nPASS2: XMLException while processing files... \n"
  //     << "Exception message is: \n"
  //     << StrX(toCatch.getMessage()) << "\n" ;
  //     XMLPlatformUtils::Terminate();
  //     return -1;
  //   }
  return 0;
}
void DDLParser::parseFile ( const int &  numtoproc) [protected]

Parse File. Just to hold some common looking code.

Definition at line 363 of file DDLParser.cc.

References currFileName_, DCOUT, fileNames_, first, alignmentValidation::fname, parsed_, SAX2Parser_, and edm::second().

Referenced by parse(), and parseOneFile().

{
  if (!parsed_[numtoproc])
  {
    const std::string & fname = fileNames_[numtoproc].second;

    // in cleaning up try-catch blocks 2007-06-26 I decided to remove
    // this because of CMSSW rules. but keep the commented way I used to
    // do it...
    //       DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
    //  {
    currFileName_ = fname;
    SAX2Parser_->parse(currFileName_.c_str());
    //  }
    //       DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch)
    //  {
    //    std::string e("\nWARNING: DDLParser::parseFile, File: '");
    //    e += currFileName_ + "'\n"
    //      + "Exception message is: \n"
    //      + std::string(StrX(toCatch.getMessage()).localForm()) + "\n";
    //    throw(DDException(e));
    //  }
  }
  else
  {
    DCOUT('P', "\nWARNING: File " + fileNames_[numtoproc].first 
          + " has already been processed as " + fileNames_[numtoproc].second);
  }
}
bool DDLParser::parseOneFile ( const std::string &  filename)

Process a single files.

This method allows a user to add to an existing DDD by parsing a new XML file. Ideally, these would be in addition to an existing DDD configuration which was processed using Parse(...).

The idea is based on whether users decide that the configuration will only hold "standard geometry files" and that any ancillary parameter files, filters and so forth will be unknown to the main configuration file. For me, this seems to go against the principle of knowing what files are relevant because now, there is no central way to find out (externally) what XML files generate the DDD in memory.

On the other hand, if on any run, a dumpFileList is run, then the user will at least know what files were used from where in a given run.

2007-04-06: filename is now relative to src directory of checkout (or share). edm:FileInPath used internally.

Definition at line 113 of file DDLParser.cc.

References currFileName_, DCOUT, DCOUT_V, expHandler_, extractFileName(), fileHandler_, lut2db_cfg::filename, fileNames_, edm::FileInPath::fullPath(), getNameSpace(), isFound(), LogDebug, nFiles_, parsed_, parseFile(), SAX2Parser_, edm::second(), and DDLSAX2Handler::setNameSpace().

{
  //  std::string filename = expHandler_->extractFileName(fullname);
  std::string filename = extractFileName(fullname);
  //  std::cout << "parseOneFile - fullname = " << fullname << std::endl;
  //  std::cout << "parseOneFile - filename = " << filename << std::endl;
  edm::FileInPath fp(fullname);
  std::string absoluteFileName = fp.fullPath();
  size_t foundFile = isFound(filename);
  if (!foundFile)
  {
    int fIndex = foundFile;
    pair <std::string, std::string> pss;
    pss.first = filename;
    pss.second = absoluteFileName; //url+filename;
    fIndex = nFiles_;
    fileNames_[nFiles_] = pss;
    ++nFiles_;
    parsed_[fIndex]=false;

    currFileName_ = fileNames_[fIndex].second;

    // in cleaning up try-catch blocks 2007-06-26 I decided to remove
    // this because of CMSSW rules. but keep the commented way I used to
    // do it...
    //       DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
    //         {
    SAX2Parser_->setContentHandler(expHandler_);
    expHandler_->setNameSpace( getNameSpace(filename) );
    //    std::cout << "0) namespace = " << getNameSpace(filename) << std::endl;
    LogDebug ("DDLParser") << "ParseOneFile() Parsing: " << fileNames_[fIndex].second << std::endl;
    parseFile ( fIndex );

    //         }
    //       DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
    //         edm::LogError ("DDLParser") << "\nDDLParser::ParseOneFile, PASS1: XMLException while processing files... \n"
    //              << "Exception message is: \n"
    //              << StrX(toCatch.getMessage()) << "\n" ;
    //         XMLPlatformUtils::Terminate();
    //         throw (DDException("  See XMLException above. "));
    //       }

    // PASS 2:

    DCOUT_V('P', "DDLParser::ParseOneFile(): PASS2: Just before setting Xerces content and error handlers... ");

    //       DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
    //         {

    SAX2Parser_->setContentHandler(fileHandler_);
    //      std::cout << "currFileName = " << currFileName_ << std::endl;
    fileHandler_->setNameSpace( getNameSpace(extractFileName(currFileName_)) );
    //      std::cout << "1)  namespace = " << getNameSpace(currFileName_) << std::endl;
    parseFile ( fIndex );
    parsed_[fIndex] = true;

    //         }
    //       DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
    //         edm::LogError ("DDLParser") << "\nDDLParser::ParseOneFile, PASS2: XMLException while processing files... \n"
    //              << "Exception message is: \n"
    //              << StrX(toCatch.getMessage()) << "\n" ;
    //         XMLPlatformUtils::Terminate();
    //         throw (DDException("  See XMLException above."));
    //       }
  }
  else // was found and is parsed...
  {
    DCOUT('P', " WARNING: DDLParser::ParseOneFile() file " + filename
          + " was already parsed as " + fileNames_[foundFile].second);
    return true;
  }
  return false;
}

Member Data Documentation

std::string DDLParser::configFileName_ [private]

Configuration file name. Only necessary until deprecated methods removed.

Definition at line 167 of file DDLParser.h.

reference to storage

Definition at line 155 of file DDLParser.h.

std::string DDLParser::currFileName_ [private]

Which file is currently being processed.

Definition at line 170 of file DDLParser.h.

Referenced by getCurrFileName(), parse(), parseFile(), and parseOneFile().

Definition at line 177 of file DDLParser.h.

Referenced by DDLParser(), and ~DDLParser().

Definition at line 176 of file DDLParser.h.

Referenced by DDLParser(), parse(), parseOneFile(), and ~DDLParser().

Definition at line 175 of file DDLParser.h.

Referenced by DDLParser(), getDDLSAX2FileHandler(), parse(), parseOneFile(), and ~DDLParser().

List of files to be processed, obtained from the DDLDocumentProvider.

Definition at line 158 of file DDLParser.h.

Referenced by clearFiles(), dumpFileList(), getFileList(), isFound(), parse(), parseFile(), and parseOneFile().

int DDLParser::nFiles_ [private]

Number of files + 1.

Definition at line 164 of file DDLParser.h.

Referenced by parse(), and parseOneFile().

std::map<int, bool> DDLParser::parsed_ [private]

Parse status of a given file.

Definition at line 161 of file DDLParser.h.

Referenced by clearFiles(), isParsed(), parse(), parseFile(), and parseOneFile().

SAX2XMLReader is one way of parsing.

Definition at line 173 of file DDLParser.h.

Referenced by DDLParser(), getXMLParser(), parse(), parseFile(), and parseOneFile().