New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
xml_parser.cpp in vendors/XIOS/current/src – NEMO

source: vendors/XIOS/current/src/xml_parser.cpp @ 3428

Last change on this file since 3428 was 3428, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

File size: 2.9 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)
16      {
17         StdIFStream ifs ( filename.c_str() , StdIFStream::in );
18         CXMLParser::ParseStream(ifs);
19      }
20
21      void CXMLParser::ParseString(const StdString & xmlContent)
22      {
23         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
24         CXMLParser::ParseStream(iss);
25      }
26
27      void CXMLParser::ParseStream(StdIStream & stream)
28      {
29         if (!stream.good())
30            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
31                  << "Bad xml stream !");
32         StdOStringStream oss;
33         while(!stream.eof() && !stream.fail ())
34            oss.put(stream.get());
35         try
36         {
37            //const StdString xmlcontent( oss.str(), 0, oss.str().size()-2); //<POURQUOI ?
38            const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
39            rapidxml::xml_document<char> doc;
40            doc.parse<0>(const_cast<char*>(xmlcontent.c_str()));
41
42            CXMLNode node(doc.first_node());
43            THashAttributes attributes;
44
45            if (node.getElementName().compare(CXMLNode::GetRootName()) != 0)
46               ERROR("CXMLParser::ParseStream(StdIStream & stream)",
47                     << "Root element should be named simulation (actual = \'"
48                     << node.getElementName() << "\')!");
49
50            if (node.goToChildElement())
51            {
52               do
53               {
54                  CContextGroup* group_context = CContext::getRoot() ;
55 
56                  attributes = node.getAttributes();
57
58                  if (attributes.end() == attributes.find("id"))
59                  { 
60                     DEBUG("Le context ne sera pas traité car il n'est pas identifié !");
61                     continue; 
62                  }
63
64                  CContext::setCurrent(attributes["id"]) ;
65
66                  bool hasctxt = CContext::has(attributes["id"]);
67
68                  if(hasctxt)
69                  { 
70                     DEBUG("Le context ne sera pas traité car "
71                           << "il existe déjà un autre context possédant le même nom !");
72                     continue; 
73                  }
74
75                  CContext* context = CContext::create(attributes["id"]);
76//                  if (!hasctxt)  group_context->addChild(context);
77                  context->parse(node);
78
79                  attributes.clear();
80
81               } while (node.goToNextElement());
82            }
83         }
84         catch (rapidxml::parse_error & exc)
85         {
86            ERROR("CXMLParser::ParseStream(StdIStream & stream)",
87                  << "RapidXML error : " << exc.what() << " !");
88         }
89      }
90
91   }// namespace xml
92} // namespace xios
Note: See TracBrowser for help on using the repository browser.