00001
00002
00003
00006
00007
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
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& )
00033 {
00034
00045
00046
00047 std::string name = "<null>";
00048 std::string version = "0";
00049 std::string source = "??";
00050 std::string base = "0";
00051 bool ronly = false;
00052
00053
00054 source = xmli::Parser::GetCurrentSource();
00055
00056
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
00066 cfg::Config* cfg = new cfg::Config(name.c_str(),
00067 version.c_str(),
00068 source.c_str());
00069 if (base=="0") {
00070
00071
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
00077
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
00091 cfg::Config::Copy(*cfg,
00092 *basecfg,
00093 version.c_str(),
00094 source.c_str());
00095
00096
00097
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
00103 if (ronly) cfg->SetReadOnly();
00104 this->Publish(cfg);
00105 }
00106