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