[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] | 9 | namespace xios |
---|
[219] | 10 | { |
---|
| 11 | namespace xml |
---|
| 12 | { |
---|
[2614] | 13 | string CXMLParser::currentIncludePath_="." ; |
---|
| 14 | |
---|
| 15 | string CXMLParser::updateIncludePath(const string& filePath) |
---|
| 16 | { |
---|
| 17 | string path = strTrim(filePath) ; |
---|
| 18 | vector<string> match=splitRegex(path+'/',"/" ) ; |
---|
| 19 | |
---|
| 20 | // check if empty path |
---|
| 21 | bool isEmpty=true ; |
---|
| 22 | for(auto& m : match) |
---|
| 23 | if (!m.empty()) |
---|
| 24 | { |
---|
| 25 | isEmpty=false ; |
---|
| 26 | break ; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | if (isEmpty) ERROR("string CXMLParser::updatePath(const string& filePath)", |
---|
| 30 | << "File path to include an new XML is empty :'"<<filePath<<"'" ); |
---|
| 31 | |
---|
| 32 | if (match.back().empty()) ERROR("string CXMLParser::updatePath(const string& filePath)", |
---|
| 33 | << "File path to include an new XML must not be a directory but a file :'"<<filePath<<"'" ); |
---|
| 34 | bool isAbsolutePath = match.front().empty() ; |
---|
| 35 | |
---|
| 36 | if (isAbsolutePath) currentIncludePath_="" ; |
---|
| 37 | for(int i=0; i<match.size()-1; ++i) if (! match[i].empty()) currentIncludePath_ = currentIncludePath_ + "/" + match[i] ; |
---|
| 38 | |
---|
| 39 | string finalPath = currentIncludePath_ + "/" + match.back() ; |
---|
| 40 | |
---|
| 41 | return finalPath ; |
---|
| 42 | } |
---|
| 43 | |
---|
[219] | 44 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 45 | |
---|
[509] | 46 | void CXMLParser::ParseFile(const StdString & filename, const std::set<StdString>& parseContextList) |
---|
[1622] | 47 | TRY |
---|
[278] | 48 | { |
---|
| 49 | StdIFStream ifs ( filename.c_str() , StdIFStream::in ); |
---|
[462] | 50 | if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) |
---|
| 51 | ERROR("void CXMLParser::ParseFile(const StdString & filename)", |
---|
| 52 | <<endl<< "Can not open <"<<filename<<"> file" ); |
---|
| 53 | |
---|
[509] | 54 | CXMLParser::ParseStream(ifs, filename, parseContextList); |
---|
[278] | 55 | } |
---|
[1622] | 56 | CATCH |
---|
[219] | 57 | |
---|
| 58 | void CXMLParser::ParseString(const StdString & xmlContent) |
---|
| 59 | { |
---|
| 60 | StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ ); |
---|
[509] | 61 | std::set<StdString> contxtList; |
---|
| 62 | CXMLParser::ParseStream(iss,"string", contxtList); |
---|
[219] | 63 | } |
---|
| 64 | |
---|
[509] | 65 | void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId, const std::set<StdString>& parseContextList) |
---|
[219] | 66 | { |
---|
| 67 | if (!stream.good()) |
---|
| 68 | ERROR("CXMLParser::ParseStream(const StdIStream & stream)", |
---|
| 69 | << "Bad xml stream !"); |
---|
| 70 | StdOStringStream oss; |
---|
[462] | 71 | while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); |
---|
| 72 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
[219] | 73 | try |
---|
| 74 | { |
---|
| 75 | rapidxml::xml_document<char> doc; |
---|
| 76 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
| 77 | |
---|
| 78 | CXMLNode node(doc.first_node()); |
---|
| 79 | THashAttributes attributes; |
---|
| 80 | |
---|
| 81 | if (node.getElementName().compare(CXMLNode::GetRootName()) != 0) |
---|
| 82 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
| 83 | << "Root element should be named simulation (actual = \'" |
---|
| 84 | << node.getElementName() << "\')!"); |
---|
[1622] | 85 | try |
---|
[219] | 86 | { |
---|
[2614] | 87 | CContextGroup* rootContext = CContext::getRoot() ; |
---|
| 88 | rootContext->parse(node, true, parseContextList) ; |
---|
[219] | 89 | } |
---|
[1622] | 90 | catch(CException& e) |
---|
| 91 | { |
---|
| 92 | CException::StackInfo stk; |
---|
| 93 | stk.info.append("Exception occurred while parsing XML file \""); |
---|
| 94 | stk.info.append(attributes["src"]); |
---|
| 95 | stk.info.append("\".\n"); |
---|
| 96 | stk.file = FILE_NAME; |
---|
| 97 | stk.function = FUNCTION_NAME; |
---|
| 98 | stk.line = __LINE__; |
---|
| 99 | e.stack.push_back(stk); |
---|
| 100 | if (CXios::xiosStack) |
---|
| 101 | throw; |
---|
| 102 | else |
---|
| 103 | throw 0; |
---|
| 104 | } |
---|
| 105 | catch(...) |
---|
| 106 | { |
---|
| 107 | CException exc; |
---|
| 108 | CException::StackInfo stk; |
---|
| 109 | stk.info.append("Exception occurred while parsing XML file \""); |
---|
| 110 | stk.info.append(attributes["src"]); |
---|
| 111 | stk.info.append("\".\n"); |
---|
| 112 | stk.file = FILE_NAME; |
---|
| 113 | stk.function = FUNCTION_NAME; |
---|
| 114 | stk.line = __LINE__; |
---|
| 115 | exc.stack.push_back(stk); |
---|
| 116 | if (CXios::xiosStack) |
---|
| 117 | throw exc; |
---|
| 118 | else |
---|
| 119 | throw 0; |
---|
| 120 | } |
---|
| 121 | |
---|
[219] | 122 | } |
---|
| 123 | catch (rapidxml::parse_error & exc) |
---|
| 124 | { |
---|
[462] | 125 | const char* ptr = exc.where<char>() ; |
---|
| 126 | const char* begin = xmlcontent.c_str() ; |
---|
| 127 | const char* content=oss.str().c_str() ; |
---|
| 128 | size_t pos=ptr-begin ; |
---|
| 129 | int lineNumber = 1 ; |
---|
| 130 | int columnNumber = 0 ; |
---|
| 131 | const char* line; |
---|
| 132 | const char* endLine; |
---|
[509] | 133 | |
---|
[462] | 134 | for(const char* i=content;i<content+pos; ++i, ++columnNumber) if (*i=='\n') { lineNumber++ ; line=i ; columnNumber=0 ;} |
---|
| 135 | for(endLine=content+pos; *endLine!='\n' && *endLine!='\0' ; ++endLine) ; |
---|
| 136 | string strLine(line,endLine-line) ; |
---|
[509] | 137 | |
---|
[462] | 138 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", << endl |
---|
| 139 | << "Error is occuring when parsing XML flux from <"<<fluxId<<"> at character "<< pos<<" line "<<lineNumber<<" column "<< columnNumber<< endl |
---|
| 140 | << strLine<<endl |
---|
[1622] | 141 | << string(columnNumber-1,'x')<<'^'<<endl) |
---|
| 142 | // <<" Error : " << exc.what() ) |
---|
[219] | 143 | } |
---|
| 144 | } |
---|
[278] | 145 | |
---|
[219] | 146 | }// namespace xml |
---|
[335] | 147 | } // namespace xios |
---|