Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

configBuilder.cxx

Go to the documentation of this file.
00001 
00002 // $Id: configBuilder.cxx,v 1.1 2007/02/09 05:38:28 fmwk Exp $
00003 //
00006 //
00007 // messier@indiana.edu
00009 #include "Config/xml/configBuilder.h"
00010 #include <iostream>
00011 #include "Config/Config.h"
00012 #include "Config/Exception.h"
00013 #include "Config/Param.h"
00014 #include "Config/Table.h"
00015 #include "XMLInterface/ChString.h"
00016 #include "XMLInterface/Stack.h"
00017 #include "XMLInterface/Parser.h"
00018 
00019 // Allocate a handler for the <config> tag
00020 static cfg::configBuilder gsConfigBuilder("config");
00021 
00022 //......................................................................
00023 
00024 cfg::configBuilder::configBuilder(const char* tag) :
00025   cfg::ConfigBuilder(),
00026   xmli::Builder(tag)
00027 { }
00028 
00029 //......................................................................
00030 
00031 void cfg::configBuilder::Build(const xmli::AttributeList& attr, 
00032                                const std::string& /*buffer*/) 
00033 {
00034 //======================================================================
00045 //======================================================================
00046   // Parse out the attributes
00047   std::string name    = "<null>"; // name of config
00048   std::string version = "0";      // version of config
00049   std::string source  = "??";     // Where config was loaded from
00050   std::string base    = "0";      // Version of config this one is based on
00051   bool        ronly   = false;    // Flagged as read only?
00052 
00053   // Ask parser name of file/database we're looking at
00054   source = xmli::Parser::GetCurrentSource();
00055   
00056   // Pull out attributes
00057   xmli::AttributeList::const_iterator itr   (attr.begin());
00058   xmli::AttributeList::const_iterator itrEnd(attr.end());
00059   for (; itr!=itrEnd; ++itr) {
00060     if (itr->Name()=="name")    name   = itr->Value();
00061     if (itr->Name()=="version") version= itr->Value();
00062     if (itr->Name()=="base")    base   = itr->Value();
00063     if (itr->Name()=="ro")      ronly  = xmli::ChString::IsTrue(itr->Value());
00064   }
00065   // Create a new configuration using the attributes
00066   cfg::Config* cfg = new cfg::Config(name.c_str(),
00067                                      version.c_str(),
00068                                      source.c_str());
00069   if (base=="0") {
00070     // Scoop up any parameters left on the stack by the XML parser and
00071     // add them to the configuration
00072     xmli::Stack<cfg::Param>& s = xmli::Stack<cfg::Param>::Instance();
00073     for (; !s.empty(); s.pop()) cfg->AdoptParam( new cfg::Param(s.top()) );
00074   }
00075   else {   
00076     // Find the existing configuration and make the new one a copy of
00077     // it
00078     const cfg::Config* basecfg =
00079       cfg::Table::Instance().GetConfig(name.c_str(), base.c_str());
00080     if (basecfg==0) {
00081       std::ostringstream os;
00082       os << "Failed to find base configuration " 
00083          << name << ":" << base
00084          << " while constructing " 
00085          << name << ":" << version << std::endl;
00086       throw cfg::Exception(Exception::kConfigNotFound,
00087                            os.str().c_str(),__FILE__,__LINE__);
00088     }
00089 
00090     // Copy the base into the derived configuration
00091     cfg::Config::Copy(*cfg,
00092                       *basecfg,
00093                       version.c_str(),
00094                       source.c_str());
00095     
00096     // Scoop up all the parameters which are associated with the
00097     // derived configuration
00098     xmli::Stack<cfg::Param>& s = xmli::Stack<cfg::Param>::Instance();
00099     for (; !s.empty(); s.pop()) cfg->Par(s.top().GetName()) = s.top();
00100   }
00101   
00102   // Finally, ship it!
00103   if (ronly) cfg->SetReadOnly();
00104   this->Publish(cfg);
00105 }
00106 

Generated on Sat Nov 7 04:46:56 2009 for NOvA Offline by  doxygen 1.3.9.1