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

Last change on this file since 501 was 501, checked in by ymipsl, 10 years ago

Add licence copyright to all file ond directory src using the command :
svn propset -R copyright -F header_licence src

XIOS is now officialy under CeCILL licence

YM

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