source: XIOS3/trunk/src/xml_parser_impl.hpp

Last change on this file was 2614, checked in by ymipsl, 4 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: 4.8 KB
Line 
1#ifndef __XIOS_CXML_PARSER_IMPL__
2#define __XIOS_CXML_PARSER_IMPL__
3
4/// XIOS headers ///
5#include "xml_parser.hpp"
6
7namespace xios
8{
9   namespace xml
10   {
11     template <class T> void CXMLParser::ParseInclude(const string& fluxId,  T& object)
12      {
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
25         StdOStringStream oss;
26         while(!stream.eof() && !stream.fail ()) oss.put(stream.get());
27         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
28         try
29         {
30            rapidxml::xml_document<char> doc;
31            doc.parse<0>(const_cast<char*>(xmlcontent.c_str()));
32            CXMLNode node(doc.first_node());
33            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
85         }
86         catch (rapidxml::parse_error & exc)
87         {
88             const char* ptr = exc.where<char>() ;
89            const char* begin = xmlcontent.c_str() ;
90            const char* content=oss.str().c_str() ;
91            size_t pos=ptr-begin ;
92            int lineNumber = 1 ;
93            int columnNumber = 0 ;
94            const char* line;
95            const char* endLine;
96           
97            for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;}
98            for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ;
99            string strLine(line,endLine-line) ;
100                 
101            ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl
102                  << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl
103                  << strLine<<endl
104                  << string(columnNumber-1,'x')<<'^'<<endl)
105//                  <<" Error : " << exc.what() )
106         }
107      }
108
109
110   } // namespace xml
111} // namespace xios
112
113#endif // __XIOS_CXML_PARSER_IMPL__
114
Note: See TracBrowser for help on using the repository browser.