[591] | 1 | #ifndef __XIOS_CXML_PARSER_IMPL__ |
---|
| 2 | #define __XIOS_CXML_PARSER_IMPL__ |
---|
[352] | 3 | |
---|
[591] | 4 | /// XIOS headers /// |
---|
[352] | 5 | #include "xml_parser.hpp" |
---|
| 6 | |
---|
| 7 | namespace xios |
---|
| 8 | { |
---|
| 9 | namespace xml |
---|
| 10 | { |
---|
[2614] | 11 | template <class T> void CXMLParser::ParseInclude(const string& fluxId, T& object) |
---|
[352] | 12 | { |
---|
[2614] | 13 | string parentPath = currentIncludePath_ ; |
---|
| 14 | string filePath = updateIncludePath(fluxId) ; |
---|
| 15 | |
---|
| 16 | StdIFStream stream(filePath, StdIFStream::in ) ; |
---|
| 17 | if ( (stream.rdstate() & std::ifstream::failbit ) != 0 ) |
---|
| 18 | ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId, T& object)", |
---|
| 19 | <<endl<< "Can not open <"<<fluxId<<"> file" ); |
---|
| 20 | |
---|
| 21 | if (!stream.good()) |
---|
| 22 | ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId, T& object)", |
---|
| 23 | << "[ filename = " << fluxId << " ] Bad xml stream !"); |
---|
| 24 | |
---|
[352] | 25 | StdOStringStream oss; |
---|
[462] | 26 | while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); |
---|
| 27 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
[352] | 28 | try |
---|
| 29 | { |
---|
| 30 | rapidxml::xml_document<char> doc; |
---|
| 31 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
| 32 | CXMLNode node(doc.first_node()); |
---|
| 33 | object.parse(node); |
---|
[2614] | 34 | currentIncludePath_ = parentPath ; |
---|
[352] | 35 | } |
---|
| 36 | catch (rapidxml::parse_error & exc) |
---|
| 37 | { |
---|
[462] | 38 | const char* ptr = exc.where<char>() ; |
---|
| 39 | const char* begin = xmlcontent.c_str() ; |
---|
| 40 | const char* content=oss.str().c_str() ; |
---|
| 41 | size_t pos=ptr-begin ; |
---|
| 42 | int lineNumber = 1 ; |
---|
| 43 | int columnNumber = 0 ; |
---|
| 44 | const char* line; |
---|
| 45 | const char* endLine; |
---|
| 46 | |
---|
| 47 | for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;} |
---|
| 48 | for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ; |
---|
| 49 | string strLine(line,endLine-line) ; |
---|
| 50 | |
---|
| 51 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl |
---|
| 52 | << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl |
---|
| 53 | << strLine<<endl |
---|
[1622] | 54 | << string(columnNumber-1,'x')<<'^'<<endl) |
---|
| 55 | // <<" Error : " << exc.what() ) |
---|
[352] | 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
[2614] | 59 | template <class T> void CXMLParser::ParseInclude(const string& fluxId, T& object, const std::set<StdString>& parseContextList) |
---|
| 60 | { |
---|
| 61 | |
---|
| 62 | string parentPath = currentIncludePath_ ; // save current path |
---|
| 63 | string filePath = updateIncludePath(fluxId) ; // path is updated |
---|
| 64 | |
---|
| 65 | StdIFStream stream(filePath, StdIFStream::in ) ; |
---|
| 66 | if ( (stream.rdstate() & std::ifstream::failbit ) != 0 ) |
---|
| 67 | ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId, T& object)", |
---|
| 68 | <<endl<< "Can not open <"<<fluxId<<"> file" ); |
---|
| 69 | |
---|
| 70 | if (!stream.good()) |
---|
| 71 | ERROR("template <class T> void CXMLParser::ParseInclude(const string& fluxId, T& object)", |
---|
| 72 | << "[ filename = " << fluxId << " ] Bad xml stream !"); |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | StdOStringStream oss; |
---|
| 76 | while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); |
---|
| 77 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
| 78 | try |
---|
| 79 | { |
---|
| 80 | rapidxml::xml_document<char> doc; |
---|
| 81 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
| 82 | CXMLNode node(doc.first_node()); |
---|
| 83 | object.parse(node, true, parseContextList); |
---|
| 84 | currentIncludePath_ = parentPath ; // restore include path |
---|
| 85 | } |
---|
| 86 | catch (rapidxml::parse_error & exc) |
---|
| 87 | { |
---|
| 88 | const char* ptr = exc.where<char>() ; |
---|
| 89 | const char* begin = xmlcontent.c_str() ; |
---|
| 90 | const char* content=oss.str().c_str() ; |
---|
| 91 | size_t pos=ptr-begin ; |
---|
| 92 | int lineNumber = 1 ; |
---|
| 93 | int columnNumber = 0 ; |
---|
| 94 | const char* line; |
---|
| 95 | const char* endLine; |
---|
| 96 | |
---|
| 97 | for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;} |
---|
| 98 | for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ; |
---|
| 99 | string strLine(line,endLine-line) ; |
---|
| 100 | |
---|
| 101 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl |
---|
| 102 | << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl |
---|
| 103 | << strLine<<endl |
---|
| 104 | << string(columnNumber-1,'x')<<'^'<<endl) |
---|
| 105 | // <<" Error : " << exc.what() ) |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
[352] | 110 | } // namespace xml |
---|
| 111 | } // namespace xios |
---|
| 112 | |
---|
[591] | 113 | #endif // __XIOS_CXML_PARSER_IMPL__ |
---|
[352] | 114 | |
---|