source: XIOS/dev/branch_openmp/src/xml_parser.cpp @ 1620

Last change on this file since 1620 was 1460, checked in by yushan, 6 years ago

branch_openmp merged with XIOS_DEV_CMIP6@1459

  • 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.3 KB
RevLine 
[219]1#include "xml_parser.hpp"
2
[278]3#include "context.hpp"
4
[352]5#include "attribute_template.hpp"
6#include "object_template.hpp"
7#include "group_template.hpp"
[219]8
[335]9namespace xios
[219]10{
11   namespace xml
12   {
[1460]13      /// ////////////////////// Définitions ////////////////////// ///
[219]14
[509]15      void CXMLParser::ParseFile(const StdString & filename, const std::set<StdString>& parseContextList)
[278]16      {
17         StdIFStream ifs ( filename.c_str() , StdIFStream::in );
[462]18         if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
19           ERROR("void CXMLParser::ParseFile(const StdString & filename)",
20                  <<endl<< "Can not open <"<<filename<<"> file" );
21
[509]22         CXMLParser::ParseStream(ifs, filename, parseContextList);
[278]23      }
[219]24
25      void CXMLParser::ParseString(const StdString & xmlContent)
26      {
27         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
[509]28         std::set<StdString> contxtList;
29         CXMLParser::ParseStream(iss,"string", contxtList);
[219]30      }
31
[509]32      void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList)
[219]33      {
34         if (!stream.good())
35            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
36                  << "Bad xml stream !");
37         StdOStringStream oss;
[462]38         while(!stream.eof() && !stream.fail ()) oss.put(stream.get());
39         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
[219]40         try
41         {
42            rapidxml::xml_document<char> doc;
43            doc.parse<0>(const_cast<char*>(xmlcontent.c_str()));
44
45            CXMLNode node(doc.first_node());
46            THashAttributes attributes;
47
48            if (node.getElementName().compare(CXMLNode::GetRootName()) != 0)
49               ERROR("CXMLParser::ParseStream(StdIStream & stream)",
50                     << "Root element should be named simulation (actual = \'"
51                     << node.getElementName() << "\')!");
52
[509]53            std::set<StdString>::iterator it;
54            std::set<StdString>::const_iterator itE = parseContextList.end();
55            bool isParseAll = (parseContextList.empty());
[219]56            if (node.goToChildElement())
57            {
58               do
59               {
[347]60                  CContextGroup* group_context = CContext::getRoot() ;
[509]61
[219]62                  attributes = node.getAttributes();
63
64                  if (attributes.end() == attributes.find("id"))
[509]65                  {
[462]66                     DEBUG("The context will not be processed because it is not identified (missing id)");
[509]67                     continue;
[219]68                  }
69
[346]70                  CContext::setCurrent(attributes["id"]) ;
[219]71
72
[509]73                  if (isParseAll)
74                  {
75                    CContext* context = CContext::create(attributes["id"]);
76                    context->parse(node);
[219]77
[509]78                    attributes.clear();
79                  }
80                  else
81                  {
82                    it = parseContextList.find(attributes["id"]);
83                    if (itE != it)
84                    {
85                      CContext* context = CContext::create(*it);
86                      context->parse(node);
[219]87
[509]88                      attributes.clear();
89                    }
90                  }
[219]91               } while (node.goToNextElement());
92            }
93         }
94         catch (rapidxml::parse_error & exc)
95         {
[462]96            const char* ptr = exc.where<char>() ;
97            const char* begin = xmlcontent.c_str() ;
98            const char* content=oss.str().c_str() ;
99            size_t pos=ptr-begin ;
100            int lineNumber = 1 ;
101            int columnNumber = 0 ;
102            const char* line;
103            const char* endLine;
[509]104
[462]105            for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;}
106            for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ;
107            string strLine(line,endLine-line) ;
[509]108
[462]109            ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl
110                  << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl
111                  << strLine<<endl
112                  << string(columnNumber-1,'x')<<'^'<<endl
113                  <<" Error : " << exc.what() )
[219]114         }
115      }
[278]116
[219]117   }// namespace xml
[335]118} // namespace xios
Note: See TracBrowser for help on using the repository browser.