[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 | |
---|
| 15 | void CXMLParser::ParseFile(const StdString & filename) |
---|
[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 | |
---|
| 22 | CXMLParser::ParseStream(ifs, filename); |
---|
[278] | 23 | } |
---|
[219] | 24 | |
---|
| 25 | void CXMLParser::ParseString(const StdString & xmlContent) |
---|
| 26 | { |
---|
| 27 | StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ ); |
---|
[462] | 28 | CXMLParser::ParseStream(iss,"string"); |
---|
[219] | 29 | } |
---|
| 30 | |
---|
[462] | 31 | void CXMLParser::ParseStream(StdIStream & stream, const string& fluxId) |
---|
[219] | 32 | { |
---|
| 33 | if (!stream.good()) |
---|
| 34 | ERROR("CXMLParser::ParseStream(const StdIStream & stream)", |
---|
| 35 | << "Bad xml stream !"); |
---|
| 36 | StdOStringStream oss; |
---|
[462] | 37 | while(!stream.eof() && !stream.fail ()) oss.put(stream.get()); |
---|
| 38 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
[219] | 39 | try |
---|
| 40 | { |
---|
| 41 | rapidxml::xml_document<char> doc; |
---|
| 42 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
| 43 | |
---|
| 44 | CXMLNode node(doc.first_node()); |
---|
| 45 | THashAttributes attributes; |
---|
| 46 | |
---|
| 47 | if (node.getElementName().compare(CXMLNode::GetRootName()) != 0) |
---|
| 48 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
| 49 | << "Root element should be named simulation (actual = \'" |
---|
| 50 | << node.getElementName() << "\')!"); |
---|
| 51 | |
---|
| 52 | if (node.goToChildElement()) |
---|
| 53 | { |
---|
| 54 | do |
---|
| 55 | { |
---|
[347] | 56 | CContextGroup* group_context = CContext::getRoot() ; |
---|
[346] | 57 | |
---|
[219] | 58 | attributes = node.getAttributes(); |
---|
| 59 | |
---|
| 60 | if (attributes.end() == attributes.find("id")) |
---|
| 61 | { |
---|
[462] | 62 | DEBUG("The context will not be processed because it is not identified (missing id)"); |
---|
[219] | 63 | continue; |
---|
| 64 | } |
---|
| 65 | |
---|
[346] | 66 | CContext::setCurrent(attributes["id"]) ; |
---|
[219] | 67 | |
---|
[346] | 68 | bool hasctxt = CContext::has(attributes["id"]); |
---|
[219] | 69 | |
---|
| 70 | if(hasctxt) |
---|
| 71 | { |
---|
[462] | 72 | DEBUG("The context will not be processed because it exist an other context with the same id" ); |
---|
[219] | 73 | continue; |
---|
| 74 | } |
---|
| 75 | |
---|
[347] | 76 | CContext* context = CContext::create(attributes["id"]); |
---|
[346] | 77 | // if (!hasctxt) group_context->addChild(context); |
---|
[219] | 78 | context->parse(node); |
---|
| 79 | |
---|
| 80 | attributes.clear(); |
---|
| 81 | |
---|
| 82 | } while (node.goToNextElement()); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | catch (rapidxml::parse_error & exc) |
---|
| 86 | { |
---|
[462] | 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() ) |
---|
[219] | 105 | } |
---|
| 106 | } |
---|
[278] | 107 | |
---|
[219] | 108 | }// namespace xml |
---|
[335] | 109 | } // namespace xios |
---|