[219] | 1 | #include "xml_parser.hpp" |
---|
| 2 | |
---|
| 3 | #include "attribute_template_impl.hpp" |
---|
| 4 | #include "object_template_impl.hpp" |
---|
| 5 | #include "group_template_impl.hpp" |
---|
| 6 | |
---|
| 7 | namespace xmlioserver |
---|
| 8 | { |
---|
| 9 | namespace xml |
---|
| 10 | { |
---|
| 11 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 12 | |
---|
| 13 | void CXMLParser::ParseFile(const StdString & filename) |
---|
| 14 | { StdIFStream ifs ( filename.c_str() , StdIFStream::in ); |
---|
| 15 | CXMLParser::ParseStream(ifs); } |
---|
| 16 | |
---|
| 17 | void CXMLParser::ParseString(const StdString & xmlContent) |
---|
| 18 | { |
---|
| 19 | StdIStringStream iss ( xmlContent /*, StdIStringStream::in*/ ); |
---|
| 20 | CXMLParser::ParseStream(iss); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | void CXMLParser::ParseStream(StdIStream & stream) |
---|
| 24 | { |
---|
| 25 | if (!stream.good()) |
---|
| 26 | ERROR("CXMLParser::ParseStream(const StdIStream & stream)", |
---|
| 27 | << "Bad xml stream !"); |
---|
| 28 | StdOStringStream oss; |
---|
| 29 | while(!stream.eof() && !stream.fail ()) |
---|
| 30 | oss.put(stream.get()); |
---|
| 31 | try |
---|
| 32 | { |
---|
| 33 | //const StdString xmlcontent( oss.str(), 0, oss.str().size()-2); //<POURQUOI ? |
---|
| 34 | const StdString xmlcontent( oss.str(), 0, oss.str().size()-1 ); |
---|
| 35 | rapidxml::xml_document<char> doc; |
---|
| 36 | doc.parse<0>(const_cast<char*>(xmlcontent.c_str())); |
---|
| 37 | |
---|
| 38 | CXMLNode node(doc.first_node()); |
---|
| 39 | THashAttributes attributes; |
---|
| 40 | |
---|
| 41 | if (node.getElementName().compare(CXMLNode::GetRootName()) != 0) |
---|
| 42 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
| 43 | << "Root element should be named simulation (actual = \'" |
---|
| 44 | << node.getElementName() << "\')!"); |
---|
| 45 | |
---|
| 46 | if (node.goToChildElement()) |
---|
| 47 | { |
---|
| 48 | do |
---|
| 49 | { |
---|
| 50 | boost::shared_ptr<tree::CContextGroup> group_context = |
---|
| 51 | tree::CContext::GetContextGroup(); |
---|
| 52 | attributes = node.getAttributes(); |
---|
| 53 | |
---|
| 54 | if (attributes.end() == attributes.find("id")) |
---|
| 55 | { |
---|
| 56 | DEBUG("Le context ne sera pas traité car il n'est pas identifié !"); |
---|
| 57 | continue; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | CObjectFactory::SetCurrentContextId(attributes["id"]); |
---|
| 61 | CGroupFactory::SetCurrentContextId(attributes["id"]); |
---|
| 62 | |
---|
| 63 | bool hasctxt = CObjectFactory::HasObject<tree::CContext>(attributes["id"]); |
---|
| 64 | |
---|
| 65 | if(hasctxt) |
---|
| 66 | { |
---|
| 67 | DEBUG("Le context ne sera pas traité car " |
---|
| 68 | << "il existe déjà un autre context possédant le même nom !"); |
---|
| 69 | continue; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | boost::shared_ptr<tree::CContext> context = |
---|
| 73 | CObjectFactory::CreateObject<tree::CContext>(attributes["id"]); |
---|
| 74 | if (!hasctxt) CGroupFactory::AddChild(group_context, context); |
---|
| 75 | context->parse(node); |
---|
| 76 | |
---|
| 77 | attributes.clear(); |
---|
| 78 | |
---|
| 79 | } while (node.goToNextElement()); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | catch (rapidxml::parse_error & exc) |
---|
| 83 | { |
---|
| 84 | ERROR("CXMLParser::ParseStream(StdIStream & stream)", |
---|
| 85 | << "RapidXML error : " << exc.what() << " !"); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | }// namespace xml |
---|
| 89 | } // namespace xmlioserver |
---|