source: XMLIO_V2/dev/dev_rv/src/xml_parser.cpp @ 141

Last change on this file since 141 was 141, checked in by hozdoba, 13 years ago

Mise à jour depuis un autre dépôt

File size: 3.0 KB
Line 
1#include "xml_parser.hpp"
2
3#include "attribute_template_impl.hpp"
4#include "object_template_impl.hpp"
5#include "group_template_impl.hpp"
6
7namespace xmlioserver
8{
9   namespace xml
10   {
11      /// ////////////////////// Définitions ////////////////////// ///
12
13      void CXMLParser::ParseFile(const StdString & filename)
14      { StdIFStream ifs ( filename.c_str() , StdIFStream::in );
15        CXMLParser::ParseStream(ifs); }
16
17      void CXMLParser::ParseString(const StdString & xmlContent)
18      {
19         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
20         CXMLParser::ParseStream(iss);
21      }
22
23      void CXMLParser::ParseStream(StdIStream & stream)
24      {
25         if (!stream.good())
26            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
27                  << "Bad xml stream !");
28         StdOStringStream oss;
29         while(!stream.eof() && !stream.fail ())
30            oss.put(stream.get());
31         try
32         {
33            //const StdString xmlcontent( oss.str(), 0, oss.str().size()-2); //<POURQUOI ?
34            const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
35            rapidxml::xml_document<char> doc;
36            doc.parse<0>(const_cast<char*>(xmlcontent.c_str()));
37
38            CXMLNode node(doc.first_node());
39            THashAttributes attributes;
40
41            if (node.getElementName().compare(CXMLNode::GetRootName()) != 0)
42               ERROR("CXMLParser::ParseStream(StdIStream & stream)",
43                     << "Root element should be named simulation (actual = \'"
44                     << node.getElementName() << "\')!");
45
46            if (node.goToChildElement())
47            {
48               do
49               {
50                  boost::shared_ptr<tree::CContextGroup> group_context =
51                                    tree::CContext::GetContextGroup();
52                  attributes = node.getAttributes();
53
54                  if (attributes.end() == attributes.find("id"))
55                  {  DEBUG("Le context ne sera pas traité car il n'est pas identifié !");
56                     continue; }
57
58                  CObjectFactory::SetCurrentContextId(attributes["id"]);
59                  CGroupFactory::SetCurrentContextId(attributes["id"]);
60
61                  if(CObjectFactory::HasObject<tree::CContext>(attributes["id"]))
62                  {  DEBUG("Le context ne sera pas traité car il existe déjà un autre context possédant le même nom !");
63                     continue; }
64
65                  boost::shared_ptr<tree::CContext> context =
66                     CObjectFactory::CreateObject<tree::CContext>(attributes["id"]);
67                  CGroupFactory::AddChild(group_context, context);
68                  context->parse(node);
69
70                  attributes.clear();
71
72               } while (node.goToNextElement());
73            }
74         }
75         catch (rapidxml::parse_error & exc)
76         {
77            ERROR("CXMLParser::ParseStream(StdIStream & stream)",
78                  << "RapidXML error : " << exc.what() << " !");
79         }
80      }
81   }// namespace xml
82} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.