source: XIOS/trunk/src/xml_parser.cpp @ 345

Last change on this file since 345 was 345, checked in by ymipsl, 12 years ago

removed "tree" namespace

YM

File size: 3.2 KB
Line 
1#include "xml_parser.hpp"
2
3#include "context.hpp"
4
5#include "attribute_template_impl.hpp"
6#include "object_template_impl.hpp"
7#include "group_template_impl.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                  boost::shared_ptr<CContextGroup> group_context =
55                                    CContext::GetContextGroup();
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                  CObjectFactory::SetCurrentContextId(attributes["id"]);
65                  CGroupFactory::SetCurrentContextId(attributes["id"]);
66
67                  bool hasctxt = CObjectFactory::HasObject<CContext>(attributes["id"]);
68
69                  if(hasctxt)
70                  { 
71                     DEBUG("Le context ne sera pas traité car "
72                           << "il existe déjà un autre context possédant le même nom !");
73                     continue; 
74                  }
75
76                  boost::shared_ptr<CContext> context =
77                     CObjectFactory::CreateObject<CContext>(attributes["id"]);
78                  if (!hasctxt) CGroupFactory::AddChild(group_context, context);
79                  context->parse(node);
80
81                  attributes.clear();
82
83               } while (node.goToNextElement());
84            }
85         }
86         catch (rapidxml::parse_error & exc)
87         {
88            ERROR("CXMLParser::ParseStream(StdIStream & stream)",
89                  << "RapidXML error : " << exc.what() << " !");
90         }
91      }
92
93   }// namespace xml
94} // namespace xios
Note: See TracBrowser for help on using the repository browser.