Back to index

nordugrid-arc-nox  1.1.0~rc6
Profile.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <arc/Profile.h>
00008 #include <arc/StringConv.h>
00009 
00010 namespace Arc {
00011 
00012   Profile::Profile(const std::string& filename)
00013     : XMLNode(NS(), "ArcConfig") {
00014     ReadFromFile(filename);
00015   }
00016 
00017   Profile::~Profile() {}
00018 
00019   static void MapTagToNode(XMLNode node, const std::string& sections, const std::string& tag, XMLNode iniNode)
00020   {
00021     if (!sections.empty() && !tag.empty()) {
00022       std::list<std::string> section;
00023       tokenize(sections, section);
00024       for (std::list<std::string>::const_iterator it = section.begin();
00025            it != section.end(); it++) {
00026         if (iniNode[*it][tag]) {
00027           node = (std::string)iniNode[*it][tag];
00028           break;
00029         }
00030       }
00031     }
00032   }
00033 
00034   static void EvaluateNode (XMLNode n, const IniConfig& ini) {
00035     XMLNode iniNode = const_cast<IniConfig&>(ini);
00036 
00037     // First map attributes.
00038     while (XMLNode attrMap = n["AttributeRepresentation"]) {
00039       const std::string id = (std::string)attrMap.Attribute("id");
00040       if (!id.empty()) {
00041         if (!n.Attribute(id))
00042           n.NewAttribute(id);
00043         MapTagToNode(n.Attribute(id), attrMap.Attribute("inisections"), attrMap.Attribute("initag"), iniNode);
00044         if (((std::string)n.Attribute(id)).empty())
00045           n.Attribute(id).Destroy();
00046       }
00047       attrMap.Destroy();
00048     }
00049 
00050     // Then map elements.
00051     XMLNode sections = n.Attribute("inisections");
00052     XMLNode tag = n.Attribute("initag");
00053     MapTagToNode(n, sections, tag, iniNode);
00054     sections.Destroy();
00055     tag.Destroy();
00056     for (int i = 0; n.Child(i); i++)
00057       EvaluateNode(n.Child(i), ini);
00058 
00059     // Trimming leaf-node.
00060     if (!n.Child()) {
00061       const std::string content = n;
00062       if (n != trim(n))
00063         n = trim(content);
00064     }
00065   }
00066 
00067   void Profile::Evaluate(Config &cfg, const IniConfig& ini) {
00068     cfg.Replace(*this);
00069     for (int i = 0; cfg.Child(i); i++)
00070       EvaluateNode(cfg.Child(i), ini);
00071   }
00072 
00073 } // namespace Arc