//----------------------------------------------------------------------------- bool datareader_c::read_joint( model_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "id" ); if( !attribute ) return false; std::string name = attribute->get_value(); xml_element_ptr element; std::string tag; joint_ptr joint = joint_ptr( new joint_c() ); component_ptr component = boost::dynamic_pointer_cast<component_c>( joint ); if( key != "" ) key += "::"; key += name; joint->id = name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Field" ) { read_field( component, element, key ); } } owner->insert( joint ); return true; }
//----------------------------------------------------------------------------- bool datareader_c::read_model( component_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "id" ); if( !attribute ) return false; std::string name = attribute->get_value(); xml_element_ptr element; std::string tag; model_ptr model = model_ptr( new model_c() ); if( owner->component_type() == component_c::TRIAL ) { trial_ptr trial = boost::dynamic_pointer_cast<trial_c>( owner ); trial->models.push_back( model ); } else if( owner->component_type() == component_c::SOLUTION ) { solution_ptr solution = boost::dynamic_pointer_cast<solution_c>( owner ); solution->models.push_back( model ); } else { return false; } key = name; model->id = name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Link" ) { read_link( model, element, key ); } else if( tag == "Joint" ) { read_joint( model, element, key ); } } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_model_element( xml_element_ptr top, unsigned& column ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Link" ) map_link_element( element, column, key ); else if( element->get_name() == "Joint" ) map_joint_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool xml_c::build_tixml_element( void* tixml_element, xml_element_ptr element ) { // TODO: robust error checking TiXmlElement* tixml_e = (TiXmlElement*) tixml_element; std::string value = element->get_value(); if( value != "" ) tixml_e->LinkEndChild( new TiXmlText( value ) ); for( unsigned i = 0; i < element->attributes(); i++ ) { xml_attribute_ptr attrib = element->attribute( i ); tixml_e->SetAttribute( attrib->get_name(), attrib->get_value() ); } for( unsigned i = 0; i < element->elements(); i++ ) { xml_element_ptr child = element->element( i ); TiXmlElement* tixml_child = new TiXmlElement( child->get_name() ); tixml_e->LinkEndChild( tixml_child ); build_tixml_element( tixml_child, child ); } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_link_element( xml_element_ptr top, unsigned& column, std::string parent_key ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } if( parent_key != "" ) { key = parent_key + "::" + key; } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Field" ) map_field_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool datareader_c::read( solution_ptr& solution, std::string scenario_id, xml_element_ptr top ) { if( !buffer_line() ) return false; //print_cells(); solution = solution_ptr( new solution_c( solution_c::MODEL ) ); solution->scenario_id = scenario_id; component_ptr component = boost::dynamic_pointer_cast<solution_c>( solution ); xml_element_ptr element; std::string key, tag; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Field" ) { read_field( component, element ); } else if( tag == "Model" ) { read_model( component, element ); } } return true; }
//----------------------------------------------------------------------------- bool datareader_c::read( trial_ptr& trial, std::string scenario_id, xml_element_ptr top ) { if( !buffer_line() ) return false; //print_cells(); trial = trial_ptr( new trial_c() ); trial->scenario_id = scenario_id; component_ptr component = boost::dynamic_pointer_cast<trial_c>( trial ); xml_element_ptr element; std::string key, name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); name = element->get_name(); if( name == "Field" ) { read_field( component, element ); } else if( name == "Model" ) { read_model( component, element ); } } return true; }