source: XIOS/dev/dev_olga/src/xml_parser.cpp @ 1612

Last change on this file since 1612 was 1612, checked in by oabramkina, 5 years ago

Dev: adding exception handling.

To activate it, compilation flag -DXIOS_EXCEPTION should be added.

  • 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.5 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      /// ////////////////////// Définitions ////////////////////// ///
14
15      void CXMLParser::ParseFile(const StdString & filename, const std::set<StdString>& parseContextList)
16      TRY
17      {
18         StdIFStream ifs ( filename.c_str() , StdIFStream::in );
19         if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
20           ERROR("void CXMLParser::ParseFile(const StdString & filename)",
21                  <<endl<< "Can not open <"<<filename<<"> file" );
22
23         CXMLParser::ParseStream(ifs, filename, parseContextList);
24      }
25      CATCH
26
27      void CXMLParser::ParseString(const StdString & xmlContent)
28      {
29         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
30         std::set<StdString> contxtList;
31         CXMLParser::ParseStream(iss,"string", contxtList);
32      }
33
34      void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList)
35      {
36         if (!stream.good())
37            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
38                  << "Bad xml stream !");
39         StdOStringStream oss;
40         while(!stream.eof() && !stream.fail ()) oss.put(stream.get());
41         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
42         try
43         {
44            rapidxml::xml_document<char> doc;
45            doc.parse<0>(const_cast<char*>(xmlcontent.c_str()));
46
47            CXMLNode node(doc.first_node());
48            THashAttributes attributes;
49
50            if (node.getElementName().compare(CXMLNode::GetRootName()) != 0)
51               ERROR("CXMLParser::ParseStream(StdIStream & stream)",
52                     << "Root element should be named simulation (actual = \'"
53                     << node.getElementName() << "\')!");
54
55            std::set<StdString>::iterator it;
56            std::set<StdString>::const_iterator itE = parseContextList.end();
57            bool isParseAll = (parseContextList.empty());
58            try
59            {
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              }
96            }
97            catch(CException& e)
98            {
99              CException::StackInfo stk;
100              stk.info.append("Exception occurred while parsing XML file \"");
101              stk.info.append(attributes["src"]);
102              stk.info.append("\".\n");
103              stk.file = FILE_NAME;
104              stk.function = FUNCTION_NAME;
105              stk.line = __LINE__;
106              e.stack.push_back(stk);
107              if (CXios::xiosStack)
108                throw;
109             else
110               throw 0;
111            }
112            catch(...)
113            {
114              CException exc;
115              CException::StackInfo stk;
116              stk.info.append("Exception occurred while parsing XML file \"");
117              stk.info.append(attributes["src"]);
118              stk.info.append("\".\n");
119              stk.file = FILE_NAME;
120              stk.function = FUNCTION_NAME;
121              stk.line = __LINE__;
122              exc.stack.push_back(stk);
123              if (CXios::xiosStack)
124                throw exc;
125             else
126               throw 0;
127            }
128
129         }
130         catch (rapidxml::parse_error & exc)
131         {
132            const char* ptr = exc.where<char>() ;
133            const char* begin = xmlcontent.c_str() ;
134            const char* content=oss.str().c_str() ;
135            size_t pos=ptr-begin ;
136            int lineNumber = 1 ;
137            int columnNumber = 0 ;
138            const char* line;
139            const char* endLine;
140
141            for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;}
142            for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ;
143            string strLine(line,endLine-line) ;
144
145            ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl
146                  << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl
147                  << strLine<<endl
148                  << string(columnNumber-1,'x')<<'^'<<endl)
149//                  <<" Error : " << exc.what() )
150         }
151      }
152
153   }// namespace xml
154} // namespace xios
Note: See TracBrowser for help on using the repository browser.