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

Last change on this file since 792 was 509, checked in by mhnguyen, 9 years ago

Implementing buffer size auto-detection for mode client -server

+) Process xml tree in client side then send all the information to server
+) Only information enabled fields in enabled files are sent to server
+) Some important change in structure of code which must be refactored

Test
+) On Curie
+) Only mode client-server
+) Passed for all tests

  • 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.7 KB
RevLine 
[219]1#include "xml_parser.hpp"
2
[278]3#include "context.hpp"
4
[352]5#include "attribute_template.hpp"
6#include "object_template.hpp"
7#include "group_template.hpp"
[219]8
[335]9namespace xios
[219]10{
11   namespace xml
12   {
13      /// ////////////////////// Définitions ////////////////////// ///
14
[509]15      void CXMLParser::ParseFile(const StdString & filename, const std::set<StdString>& parseContextList)
[278]16      {
17         StdIFStream ifs ( filename.c_str() , StdIFStream::in );
[462]18         if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
19           ERROR("void CXMLParser::ParseFile(const StdString & filename)",
20                  <<endl<< "Can not open <"<<filename<<"> file" );
21
[509]22         CXMLParser::ParseStream(ifs, filename, parseContextList);
[278]23      }
[219]24
25      void CXMLParser::ParseString(const StdString & xmlContent)
26      {
27         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ );
[509]28         std::set<StdString> contxtList;
29         CXMLParser::ParseStream(iss,"string", contxtList);
[219]30      }
31
[509]32      void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList)
[219]33      {
34         if (!stream.good())
35            ERROR("CXMLParser::ParseStream(const StdIStream & stream)",
36                  << "Bad xml stream !");
37         StdOStringStream oss;
[462]38         while(!stream.eof() && !stream.fail ()) oss.put(stream.get());
39         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 );
[219]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
[509]53            std::set<StdString>::iterator it;
54            std::set<StdString>::const_iterator itE = parseContextList.end();
55            bool isParseAll = (parseContextList.empty());
[219]56            if (node.goToChildElement())
57            {
58               do
59               {
[347]60                  CContextGroup* group_context = CContext::getRoot() ;
[509]61
[219]62                  attributes = node.getAttributes();
63
64                  if (attributes.end() == attributes.find("id"))
[509]65                  {
[462]66                     DEBUG("The context will not be processed because it is not identified (missing id)");
[509]67                     continue;
[219]68                  }
69
[346]70                  CContext::setCurrent(attributes["id"]) ;
[219]71
[346]72                  bool hasctxt = CContext::has(attributes["id"]);
[219]73
74                  if(hasctxt)
[509]75                  {
[462]76                     DEBUG("The context will not be processed because it exist an other context with the same id" );
[509]77                     continue;
[219]78                  }
79
[509]80                  if (isParseAll)
81                  {
82                    CContext* context = CContext::create(attributes["id"]);
[346]83//                  if (!hasctxt)  group_context->addChild(context);
[509]84                    context->parse(node);
[219]85
[509]86                    attributes.clear();
87                  }
88                  else
89                  {
90                    it = parseContextList.find(attributes["id"]);
91                    if (itE != it)
92                    {
93                      CContext* context = CContext::create(*it);
94  //                  if (!hasctxt)  group_context->addChild(context);
95                      context->parse(node);
[219]96
[509]97                      attributes.clear();
98                    }
99                  }
[219]100               } while (node.goToNextElement());
101            }
102         }
103         catch (rapidxml::parse_error & exc)
104         {
[462]105            const char* ptr = exc.where<char>() ;
106            const char* begin = xmlcontent.c_str() ;
107            const char* content=oss.str().c_str() ;
108            size_t pos=ptr-begin ;
109            int lineNumber = 1 ;
110            int columnNumber = 0 ;
111            const char* line;
112            const char* endLine;
[509]113
[462]114            for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;}
115            for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ;
116            string strLine(line,endLine-line) ;
[509]117
[462]118            ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl
119                  << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl
120                  << strLine<<endl
121                  << string(columnNumber-1,'x')<<'^'<<endl
122                  <<" Error : " << exc.what() )
[219]123         }
124      }
[278]125
[219]126   }// namespace xml
[335]127} // namespace xios
Note: See TracBrowser for help on using the repository browser.