Changeset 2615


Ignore:
Timestamp:
03/12/24 20:09:55 (2 months ago)
Author:
ymipsl
Message:
  • Permit now usage of contex_group into xml file for more modularity
  • Src path is now relative to parent file, except if path is an absolute path

YM

Location:
XIOS2/trunk/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • XIOS2/trunk/src/group_template.hpp

    r1542 r2615  
    5151         virtual void parse(xml::CXMLNode & node); 
    5252         virtual void parse(xml::CXMLNode & node, bool withAttr); 
     53         virtual void parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList); 
     54          
    5355         virtual void parseChild(xml::CXMLNode & node); 
    5456         
  • XIOS2/trunk/src/group_template_impl.hpp

    r1542 r2615  
    252252   } 
    253253 
    254    //--------------------------------------------------------------- 
    255  
    256    template <class U, class V, class W> 
    257       void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node) 
    258    {  
    259       this->parse(node, true);  
    260    } 
    261254    
    262255   //--------------------------------------------------------------- 
     
    511504   } 
    512505 
     506  //--------------------------------------------------------------- 
     507 
     508   template <class U, class V, class W> 
     509      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node) 
     510   {  
     511      this->parse(node, true);  
     512   } 
     513    
     514   template <class U, class V, class W> 
     515   void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList) 
     516   { 
     517     ERROR("void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList)", 
     518                     <<"must not be called by this kind of object : "<<GetName() ) ; 
     519   } 
     520 
    513521   template <class U, class V, class W> 
    514522      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr) 
     
    520528      { 
    521529         CGroupTemplate<U, V, W>::SuperClass::parse(node); 
    522          if (attributes.end() != attributes.find("src")) 
    523          { 
    524             StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in ); 
    525             if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) 
    526                ERROR("void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)", 
    527                      <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" ); 
    528              
    529             if (!ifs.good()) 
    530                ERROR("CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)", 
    531                      << "[ filename = " << attributes["src"] << " ] Bad xml stream !"); 
    532             xml::CXMLParser::ParseInclude(ifs, attributes["src"].c_str(), *this); 
    533          } 
     530         if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"].c_str(), *this); 
    534531      } 
    535532 
  • XIOS2/trunk/src/node/context.cpp

    r2428 r2615  
    113113      xml::THashAttributes attributes = node.getAttributes(); 
    114114 
    115       if (attributes.end() != attributes.find("src")) 
    116       { 
    117          StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in ); 
    118          if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) 
    119             ERROR("void CContext::parse(xml::CXMLNode & node)", 
    120                   <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" ); 
    121          if (!ifs.good()) 
    122             ERROR("CContext::parse(xml::CXMLNode & node)", 
    123                   << "[ filename = " << attributes["src"] << " ] Bad xml stream !"); 
    124          xml::CXMLParser::ParseInclude(ifs, attributes["src"], *this); 
    125       } 
     115      if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"], *this); 
    126116 
    127117      if (node.getElementName().compare(CContext::GetName())) 
     
    162152   } 
    163153   CATCH_DUMP_ATTR 
     154 
     155   template <> 
     156   void CGroupTemplate<CContext, CContextGroup, CContextAttributes>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList) 
     157   TRY 
     158   { 
     159      StdString name = node.getElementName(); 
     160      xml::THashAttributes attributes = node.getAttributes(); 
     161      CGroupTemplate<CContext, CContextGroup, CContextAttributes>::SuperClass::parse(node); 
     162      if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"].c_str(), *this, parseContextList); 
     163 
     164      if (!(node.goToChildElement())) 
     165      { 
     166        // no children ? 
     167      } 
     168      else 
     169      { 
     170        do // Parcours pour traitement. 
     171        {  
     172 
     173          StdString name = node.getElementName(); 
     174          attributes.clear(); 
     175          attributes = node.getAttributes(); 
     176 
     177          if (name.compare(CContextGroup::GetName()) == 0) 
     178          { 
     179            CContextGroup contextGroup ; 
     180            contextGroup.parse(node, true, parseContextList) ; 
     181            continue; 
     182          } 
     183 
     184          if (name.compare(CContext::GetName()) == 0) 
     185          { 
     186            if (attributes.end() == attributes.find("id")) 
     187            { 
     188              DEBUG("The context will not be processed because it is not identified (missing id)"); 
     189              continue; 
     190            } 
     191            else 
     192            {   
     193              if (parseContextList.empty() || parseContextList.count(attributes["id"]) > 0) 
     194              { 
     195                CContext::setCurrent(attributes["id"]) ; 
     196                CContext* context = CContext::create(attributes["id"]); 
     197                context->parse(node); 
     198                attributes.clear(); 
     199              } 
     200              continue; 
     201            } 
     202          } 
     203 
     204          DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId() 
     205                << "\', un objet de type \'" << CContextGroup::GetName() 
     206                << "\' ne peut contenir qu'un objet de type \'" << CContextGroup::GetName() 
     207                << "\' ou de type \'" << CContext::GetName() 
     208                << "\' (reçu : " << name << ") !"); 
     209 
     210         } while (node.goToNextElement()); 
     211         node.goToParentElement(); // Retour au parent 
     212      } 
     213   } 
     214   CATCH_DUMP_ATTR 
     215 
     216 
     217 
    164218 
    165219   //---------------------------------------------------------------- 
  • XIOS2/trunk/src/node/context.hpp

    r1639 r2615  
    274274   // Declare/Define CContextGroup and CContextDefinition 
    275275   DECLARE_GROUP(CContext); 
     276   template <> 
     277      void CGroupTemplate<CContext, CContextGroup, CContextAttributes>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList) ; 
    276278 
    277279   ///-------------------------------------------------------------- 
  • XIOS2/trunk/src/string_tools.hpp

    r1585 r2615  
    99{ 
    1010  std::vector<std::string> splitRegex(const std::string& input, const std::string& regex) ; 
     11  string strTrim(const std::string& input) ; 
    1112 
    1213  inline std::vector<std::string> splitRegex(const std::string& input, const std::string& regex) 
     
    2021  } 
    2122 
     23  inline string strTrim(const std::string& input) 
     24  { 
     25    return std::regex_replace(input, std::regex("^ +| +$"), "$1"); 
     26  } 
     27 
    2228} 
    2329#endif 
  • XIOS2/trunk/src/xml_parser.cpp

    r1622 r2615  
    66#include "object_template.hpp" 
    77#include "group_template.hpp" 
     8#include "string_tools.hpp" 
    89 
    910namespace xios 
     
    1112   namespace xml 
    1213   { 
     14       
     15      string CXMLParser::currentIncludePath_="." ; 
     16 
     17      string CXMLParser::updateIncludePath(const string& filePath) 
     18      { 
     19        string path = strTrim(filePath) ; 
     20        vector<string> match=splitRegex(path+'/',"/" ) ; 
     21 
     22        // check if empty path 
     23        bool isEmpty=true ; 
     24        for(auto& m : match)  
     25          if (!m.empty()) 
     26          { 
     27            isEmpty=false ; 
     28            break ; 
     29          } 
     30 
     31        if (isEmpty) ERROR("string CXMLParser::updatePath(const string& filePath)", 
     32                     << "File path to include an new XML is empty :'"<<filePath<<"'" ); 
     33 
     34        if (match.back().empty()) ERROR("string CXMLParser::updatePath(const string& filePath)", 
     35                     << "File path to include an new XML must not be a directory but a file :'"<<filePath<<"'" ); 
     36        bool isAbsolutePath = match.front().empty() ; 
     37         
     38        if (isAbsolutePath) currentIncludePath_="" ; 
     39        for(int i=0; i<match.size()-1; ++i) if (! match[i].empty()) currentIncludePath_ = currentIncludePath_ + "/" + match[i] ; 
     40         
     41        string finalPath = currentIncludePath_ + "/" + match.back() ; 
     42 
     43        return finalPath ; 
     44      } 
     45 
     46 
    1347      /// ////////////////////// Définitions ////////////////////// /// 
    1448 
     
    5286                     << "Root element should be named simulation (actual = \'" 
    5387                     << node.getElementName() << "\')!"); 
    54  
    55             std::set<StdString>::iterator it; 
    56             std::set<StdString>::const_iterator itE = parseContextList.end(); 
    57             bool isParseAll = (parseContextList.empty()); 
    5888            try 
    5989            { 
    60               if (node.goToChildElement()) 
    61               { 
    62                  do 
    63                  { 
    64                     CContextGroup* group_context = CContext::getRoot() ; 
    65  
    66                     attributes = node.getAttributes(); 
    67  
    68                     if (attributes.end() == attributes.find("id")) 
    69                     { 
    70                        DEBUG("The context will not be processed because it is not identified (missing id)"); 
    71                        continue; 
    72                     } 
    73  
    74                     CContext::setCurrent(attributes["id"]) ; 
    75  
    76                     if (isParseAll) 
    77                     { 
    78                       CContext* context = CContext::create(attributes["id"]); 
    79                       context->parse(node); 
    80  
    81                       attributes.clear(); 
    82                     } 
    83                     else 
    84                     { 
    85                       it = parseContextList.find(attributes["id"]); 
    86                       if (itE != it) 
    87                       { 
    88                         CContext* context = CContext::create(*it); 
    89                         context->parse(node); 
    90  
    91                         attributes.clear(); 
    92                       } 
    93                     } 
    94                  } while (node.goToNextElement()); 
    95               } 
     90              CContextGroup* rootContext = CContext::getRoot() ; 
     91              rootContext->parse(node, true, parseContextList) ; 
    9692            } 
    9793            catch(CException& e) 
  • XIOS2/trunk/src/xml_parser.hpp

    r1622 r2615  
    2121            static void ParseString(const StdString & xmlContent); 
    2222            static void ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseList); 
     23             
    2324            template <class T> 
    24                static void ParseInclude(StdIStream & stream, const string& fluxId, T & object); 
     25            static void ParseInclude(const string& fluxId, T & object); 
     26             
     27            template <class T> 
     28            static void ParseInclude(const string& fluxId, T & object, const std::set<StdString>& parseContextList); 
     29             
     30            static string updateIncludePath(const string& filePath) ; 
    2531 
     32         private: 
     33            static string currentIncludePath_ ; // current include path for XML file 
    2634      }; //class CXMLParser 
    27 /* 
    28       template <class T> 
    29          void CXMLParser::ParseInclude(StdIStream & stream, T& object) 
    30       { 
    31          StdOStringStream oss; 
    32          while(!stream.eof() && !stream.fail ()) 
    33             oss.put(stream.get()); 
    34          try 
    35          { 
    36             const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); 
    37             rapidxml::xml_document<char> doc; 
    38             doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); 
    39             CXMLNode node(doc.first_node()); 
    40             object.parse(node); 
    41          } 
    42          catch (rapidxml::parse_error & exc) 
    43          { 
    44             ERROR("CXMLParser::ParseStream(StdIStream & stream)", 
    45                   << "RapidXML error : " << exc.what() << " !"); 
    46          } 
    47       } 
    48 */ 
    4935   }// namespace xml 
    5036} // namespace xios 
  • XIOS2/trunk/src/xml_parser_decl.cpp

    r1558 r2615  
    77  namespace xml 
    88  { 
    9     template void CXMLParser::ParseInclude<CContext>(StdIStream & stream, const string& fluxId, CContext& object) ; 
     9    template void CXMLParser::ParseInclude<CContext>(const string& fluxId, CContext& object) ; 
    1010 
    1111 #   define macro(T) \ 
    12     template void CXMLParser::ParseInclude< CGroupTemplate<C##T, C##T##Group, C##T##Attributes> >(StdIStream & stream, const string& fluxId, CGroupTemplate<C##T, C##T##Group, C##T##Attributes>& object) ; 
    13  
     12    template void CXMLParser::ParseInclude< CGroupTemplate<C##T, C##T##Group, C##T##Attributes> >(const string& fluxId, CGroupTemplate<C##T, C##T##Group, C##T##Attributes>& object) ; 
    1413    macro( Context ) 
    1514    macro( Axis ) 
     
    4039    macro( ReorderDomain ) 
    4140    macro( ExtractDomain ) 
     41 
     42#   undef macro 
     43#   define macro(T) \ 
     44    template void CXMLParser::ParseInclude< CGroupTemplate<C##T, C##T##Group, C##T##Attributes> >(const string& fluxId, CGroupTemplate<C##T, C##T##Group, C##T##Attributes>& object, const std::set<StdString>& parseContextList) ; 
     45    macro( Context ) 
     46#   undef macro 
    4247  } 
    4348} 
  • XIOS2/trunk/src/xml_parser_impl.hpp

    r1622 r2615  
    99   namespace xml 
    1010   { 
    11      template <class T> void CXMLParser::ParseInclude(StdIStream & stream, const string& fluxId,  T& object) 
     11      template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object) 
    1212      { 
     13         string parentPath = currentIncludePath_ ;         
     14         string filePath = updateIncludePath(fluxId) ; 
     15 
     16         StdIFStream stream(filePath, StdIFStream::in ) ; 
     17         if ( (stream.rdstate() & std::ifstream::failbit ) != 0 ) 
     18               ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object)", 
     19                     <<endl<< "Can not open <"<<fluxId<<"> file" ); 
     20             
     21         if (!stream.good()) 
     22               ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object)", 
     23                     << "[ filename = " << fluxId << " ] Bad xml stream !"); 
     24 
    1325         StdOStringStream oss; 
    1426         while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); 
     
    2032            CXMLNode node(doc.first_node()); 
    2133            object.parse(node); 
     34            currentIncludePath_ = parentPath ;     
     35         } 
     36         catch (rapidxml::parse_error & exc) 
     37         { 
     38             const char* ptr = exc.where<char>() ; 
     39            const char* begin = xmlcontent.c_str() ; 
     40            const char* content=oss.str().c_str() ; 
     41            size_t pos=ptr-begin ; 
     42            int lineNumber = 1 ; 
     43            int columnNumber = 0 ; 
     44            const char* line; 
     45            const char* endLine; 
     46             
     47            for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;} 
     48            for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ; 
     49            string strLine(line,endLine-line) ; 
     50                   
     51            ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl 
     52                  << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl 
     53                  << strLine<<endl 
     54                  << string(columnNumber-1,'x')<<'^'<<endl) 
     55//                  <<" Error : " << exc.what() ) 
     56         } 
     57      } 
     58 
     59      template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object, const std::set<StdString>& parseContextList) 
     60      { 
     61          
     62         string parentPath = currentIncludePath_ ;   // save current path      
     63         string filePath = updateIncludePath(fluxId) ; // path is updated 
     64 
     65         StdIFStream stream(filePath, StdIFStream::in ) ; 
     66         if ( (stream.rdstate() & std::ifstream::failbit ) != 0 ) 
     67               ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object)", 
     68                     <<endl<< "Can not open <"<<fluxId<<"> file" ); 
     69             
     70         if (!stream.good()) 
     71               ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object)", 
     72                     << "[ filename = " << fluxId << " ] Bad xml stream !"); 
     73 
     74 
     75         StdOStringStream oss; 
     76         while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); 
     77         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); 
     78         try 
     79         { 
     80            rapidxml::xml_document<char> doc; 
     81            doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); 
     82            CXMLNode node(doc.first_node()); 
     83            object.parse(node, true, parseContextList); 
     84            currentIncludePath_ = parentPath ;    // restore include path 
    2285         } 
    2386         catch (rapidxml::parse_error & exc) 
Note: See TracChangeset for help on using the changeset viewer.