[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 ); |
---|
| 18 | CXMLParser::ParseStream(ifs); |
---|
| 19 | } |
---|
[219] | 20 | |
---|
| 21 | void CXMLParser::ParseString(const StdString & xmlContent) |
---|
| 22 | { |
---|
| 23 | StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ ); |
---|
| 24 | CXMLParser::ParseStream(iss); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void CXMLParser::ParseStream(StdIStream & stream) |
---|
| 28 | { |
---|
| 29 | if (!stream.good()) |
---|
| 30 | ERROR("CXMLParser::ParseStream(const StdIStream & stream)", |
---|
| 31 | << "Bad xml stream !"); |
---|
| 32 | StdOStringStream oss; |
---|
| 33 | while(!stream.eof() && !stream.fail ()) |
---|
| 34 | oss.put(stream.get()); |
---|
| 35 | try |
---|
| 36 | { |
---|
| 37 | //const StdString xmlcontent( oss.str(), 0, oss.str().size()-2); //<POURQUOI ? |
---|
| 38 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
| 39 | rapidxml::xml_document<char> doc; |
---|
| 40 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
| 41 | |
---|
| 42 | CXMLNode node(doc.first_node()); |
---|
| 43 | THashAttributes attributes; |
---|
| 44 | |
---|
| 45 | if (node.getElementName().compare(CXMLNode::GetRootName()) != 0) |
---|
| 46 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
| 47 | << "Root element should be named simulation (actual = \'" |
---|
| 48 | << node.getElementName() << "\')!"); |
---|
| 49 | |
---|
| 50 | if (node.goToChildElement()) |
---|
| 51 | { |
---|
| 52 | do |
---|
| 53 | { |
---|
[347] | 54 | CContextGroup* group_context = CContext::getRoot() ; |
---|
[346] | 55 | |
---|
[219] | 56 | attributes = node.getAttributes(); |
---|
| 57 | |
---|
| 58 | if (attributes.end() == attributes.find("id")) |
---|
| 59 | { |
---|
| 60 | DEBUG("Le context ne sera pas traité car il n'est pas identifié !"); |
---|
| 61 | continue; |
---|
| 62 | } |
---|
| 63 | |
---|
[346] | 64 | CContext::setCurrent(attributes["id"]) ; |
---|
[219] | 65 | |
---|
[346] | 66 | bool hasctxt = CContext::has(attributes["id"]); |
---|
[219] | 67 | |
---|
| 68 | if(hasctxt) |
---|
| 69 | { |
---|
| 70 | DEBUG("Le context ne sera pas traité car " |
---|
| 71 | << "il existe déjà un autre context possédant le même nom !"); |
---|
| 72 | continue; |
---|
| 73 | } |
---|
| 74 | |
---|
[347] | 75 | CContext* context = CContext::create(attributes["id"]); |
---|
[346] | 76 | // if (!hasctxt) group_context->addChild(context); |
---|
[219] | 77 | context->parse(node); |
---|
| 78 | |
---|
| 79 | attributes.clear(); |
---|
| 80 | |
---|
| 81 | } while (node.goToNextElement()); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | catch (rapidxml::parse_error & exc) |
---|
| 85 | { |
---|
| 86 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
| 87 | << "RapidXML error : " << exc.what() << " !"); |
---|
| 88 | } |
---|
| 89 | } |
---|
[278] | 90 | |
---|
[219] | 91 | }// namespace xml |
---|
[335] | 92 | } // namespace xios |
---|