Ignore:
Timestamp:
01/21/14 09:40:14 (10 years ago)
Author:
ymipsl
Message:

Enhancement :

  • Improving error message occuring along the xml parsing : position (line number, column) is now indicated
  • New executable : parse_xml.exe : parse the xml files and reports potential error

YM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/xml_parser.cpp

    r352 r462  
    1616      { 
    1717         StdIFStream ifs ( filename.c_str() , StdIFStream::in ); 
    18          CXMLParser::ParseStream(ifs); 
     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); 
    1923      } 
    2024 
     
    2226      { 
    2327         StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ ); 
    24          CXMLParser::ParseStream(iss); 
     28         CXMLParser::ParseStream(iss,"string"); 
    2529      } 
    2630 
    27       void CXMLParser::ParseStream(StdIStream & stream) 
     31      void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId) 
    2832      { 
    2933         if (!stream.good()) 
     
    3135                  << "Bad xml stream !"); 
    3236         StdOStringStream oss; 
    33          while(!stream.eof() && !stream.fail ()) 
    34             oss.put(stream.get()); 
     37         while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); 
     38         const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); 
    3539         try 
    3640         { 
    37             //const StdString xmlcontent( oss.str(), 0, oss.str().size()-2); //<POURQUOI ? 
    38             const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); 
    3941            rapidxml::xml_document<char> doc; 
    4042            doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); 
     
    5860                  if (attributes.end() == attributes.find("id")) 
    5961                  {   
    60                      DEBUG("Le context ne sera pas traité car il n'est pas identifié !"); 
     62                     DEBUG("The context will not be processed because it is not identified (missing id)"); 
    6163                     continue;  
    6264                  } 
     
    6870                  if(hasctxt) 
    6971                  {   
    70                      DEBUG("Le context ne sera pas traité car " 
    71                            << "il existe déjà un autre context possédant le même nom !"); 
     72                     DEBUG("The context will not be processed because it exist an other context with the same id" ); 
    7273                     continue;  
    7374                  } 
     
    8485         catch (rapidxml::parse_error & exc) 
    8586         { 
    86             ERROR("CXMLParser::ParseStream(StdIStream & stream)", 
    87                   << "RapidXML error : " << exc.what() << " !"); 
     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() ) 
    88105         } 
    89106      } 
Note: See TracChangeset for help on using the changeset viewer.