source: XIOS3/trunk/src/xml_parser.cpp @ 2628

Last change on this file since 2628 was 2614, checked in by ymipsl, 3 months ago
  • 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

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 5.3 KB
Line 
1#include "xml_parser.hpp"
2
3#include "context.hpp"
4
5#include "attribute_template.hpp"
6#include "object_template.hpp"
7#include "group_template.hpp"
8
9namespace xios
10{
11   namespace xml
12   {
13      string CXMLParser::currentIncludePath_="." ;
14
15      string CXMLParser::updateIncludePath(const string& filePath)
16      {
17        string path = strTrim(filePath) ;
18        vector<string> match=splitRegex(path+'/',"/" ) ;
19
20        // check if empty path
21        bool isEmpty=true ;
22        for(auto& m : match) 
23          if (!m.empty())
24          {
25            isEmpty=false ;
26            break ;
27          }
28
29        if (isEmpty) ERROR("string CXMLParser::updatePath(const string& filePath)",
30                     << "File path to include an new XML is empty :'"<<filePath<<"'" );
31
32        if (match.back().empty()) ERROR("string CXMLParser::updatePath(const string& filePath)",
33                     << "File path to include an new XML must not be a directory but a file :'"<<filePath<<"'" );
34        bool isAbsolutePath = match.front().empty() ;
35       
36        if (isAbsolutePath) currentIncludePath_="" ;
37        for(int i=0; i<match.size()-1; ++i) if (! match[i].empty()) currentIncludePath_ = currentIncludePath_ + "/" + match[i] ;
38       
39        string finalPath = currentIncludePath_ + "/" + match.back() ;
40
41        return finalPath ;
42      }
43
44      /// ////////////////////// Définitions ////////////////////// ///
45
46      void CXMLParser::ParseFile(const StdString & filename, const std::set<StdString>& parseContextList)
47      TRY
48      {
49         StdIFStream ifs ( filename.c_str() , StdIFStream::in );
50         if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
51           ERROR("void CXMLParser::ParseFile(const StdString & filename)",
52                  <<endl<< "Can not open <"<<filename<<"> file" );
53
54         CXMLParser::ParseStream(ifs, filename, parseContextList);
55      }
56      CATCH
57
58      void CXMLParser::ParseString(const StdString & xmlContent)
59      {
60         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
61         std::set<StdString> contxtList;
62         CXMLParser::ParseStream(iss,"string", contxtList);
63      }
64
65      void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList)
66      {
67         if (!stream.good())
68            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
69                  << "Bad xml stream !");
70         StdOStringStream oss;
71         while(!stream.eof() && !stream.fail ()) oss.put(stream.get());
72         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
73         try
74         {
75            rapidxml::xml_document<char> doc;
76            doc.parse<0>(const_cast<char*>(xmlcontent.c_str()));
77
78            CXMLNode node(doc.first_node());
79            THashAttributes attributes;
80
81            if (node.getElementName().compare(CXMLNode::GetRootName()) != 0)
82               ERROR("CXMLParser::ParseStream(StdIStream & stream)",
83                     << "Root element should be named simulation (actual = \'"
84                     << node.getElementName() << "\')!");
85            try
86            {
87              CContextGroup* rootContext = CContext::getRoot() ;
88              rootContext->parse(node, true, parseContextList) ;
89            }
90            catch(CException& e)
91            {
92              CException::StackInfo stk;
93              stk.info.append("Exception occurred while parsing XML file \"");
94              stk.info.append(attributes["src"]);
95              stk.info.append("\".\n");
96              stk.file = FILE_NAME;
97              stk.function = FUNCTION_NAME;
98              stk.line = __LINE__;
99              e.stack.push_back(stk);
100              if (CXios::xiosStack)
101                throw;
102             else
103               throw 0;
104            }
105            catch(...)
106            {
107              CException exc;
108              CException::StackInfo stk;
109              stk.info.append("Exception occurred while parsing XML file \"");
110              stk.info.append(attributes["src"]);
111              stk.info.append("\".\n");
112              stk.file = FILE_NAME;
113              stk.function = FUNCTION_NAME;
114              stk.line = __LINE__;
115              exc.stack.push_back(stk);
116              if (CXios::xiosStack)
117                throw exc;
118             else
119               throw 0;
120            }
121
122         }
123         catch (rapidxml::parse_error & exc)
124         {
125            const char* ptr = exc.where<char>() ;
126            const char* begin = xmlcontent.c_str() ;
127            const char* content=oss.str().c_str() ;
128            size_t pos=ptr-begin ;
129            int lineNumber = 1 ;
130            int columnNumber = 0 ;
131            const char* line;
132            const char* endLine;
133
134            for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;}
135            for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ;
136            string strLine(line,endLine-line) ;
137
138            ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl
139                  << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl
140                  << strLine<<endl
141                  << string(columnNumber-1,'x')<<'^'<<endl)
142//                  <<" Error : " << exc.what() )
143         }
144      }
145
146   }// namespace xml
147} // namespace xios
Note: See TracBrowser for help on using the repository browser.