source: XIOS/dev/dev_olga/src/xml_parser.cpp @ 1204

Last change on this file since 1204 was 1158, checked in by oabramkina, 7 years ago

Two server levels: merging with trunk r1137.
There are bugs.

  • 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
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, const std::set<StdString>& parseContextList)
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, parseContextList);
23      }
24
25      void CXMLParser::ParseString(const StdString & xmlContent)
26      {
27         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
28         std::set<StdString> contxtList;
29         CXMLParser::ParseStream(iss,"string", contxtList);
30      }
31
32      void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList)
33      {
34         if (!stream.good())
35            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
36                  << "Bad xml stream !");
37         StdOStringStream oss;
38         while(!stream.eof() && !stream.fail ()) oss.put(stream.get());
39         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
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
53            std::set<StdString>::iterator it;
54            std::set<StdString>::const_iterator itE = parseContextList.end();
55            bool isParseAll = (parseContextList.empty());
56            if (node.goToChildElement())
57            {
58               do
59               {
60                  CContextGroup* group_context = CContext::getRoot() ;
61
62                  attributes = node.getAttributes();
63
64                  if (attributes.end() == attributes.find("id"))
65                  {
66                     DEBUG("The context will not be processed because it is not identified (missing id)");
67                     continue;
68                  }
69
70                  CContext::setCurrent(attributes["id"]) ;
71
72
73                  if (isParseAll)
74                  {
75                    CContext* context = CContext::create(attributes["id"]);
76                    context->parse(node);
77
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);
87
88                      attributes.clear();
89                    }
90                  }
91               } while (node.goToNextElement());
92            }
93         }
94         catch (rapidxml::parse_error & exc)
95         {
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;
104
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) ;
108
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() )
114         }
115      }
116
117   }// namespace xml
118} // namespace xios
Note: See TracBrowser for help on using the repository browser.