[88] | 1 | /* |
---|
| 2 | * $Id: Generator_fromFile.cpp 88 2008-12-10 18:31:39Z rosiere $ |
---|
| 3 | * |
---|
| 4 | * [ Description ] |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include "Behavioural/Configuration/include/Generator.h" |
---|
| 9 | #include "Common/include/FromString.h" |
---|
| 10 | #include <fstream> |
---|
| 11 | |
---|
| 12 | namespace morpheo { |
---|
| 13 | namespace behavioural { |
---|
| 14 | namespace configuration { |
---|
| 15 | |
---|
| 16 | using namespace XMLUtils; |
---|
| 17 | |
---|
| 18 | #undef FUNCTION |
---|
| 19 | #define FUNCTION "Generator::fromFile" |
---|
| 20 | void Generator::fromFile (std::string filename) |
---|
| 21 | { |
---|
| 22 | log_begin(Configuration,FUNCTION); |
---|
| 23 | |
---|
| 24 | msg_printf(INFORMATION,_("Read file \"%s\"."),filename.c_str()); |
---|
| 25 | |
---|
| 26 | std::ifstream file (filename.c_str()); |
---|
| 27 | |
---|
| 28 | // Test the file open |
---|
| 29 | if (!file) |
---|
| 30 | throw ERRORMORPHEO(FUNCTION,toString(_("Error opening \"%s\" file.\n"),filename.c_str())); |
---|
| 31 | |
---|
| 32 | XML_t * _xml = new FileXMLLight<char,ClassicReferenceCounter>(file); |
---|
| 33 | |
---|
| 34 | // Test root node : Root name, must not a singleton, have none attribute |
---|
| 35 | testNodeName (_xml,"parameters"); |
---|
| 36 | testSingleton (_xml,false); |
---|
| 37 | testNoAttribute(_xml); |
---|
| 38 | |
---|
| 39 | XMLLightVector<XML_t> vect = _xml->getNodes(); |
---|
| 40 | |
---|
| 41 | // std::cout << vect << std::endl; |
---|
| 42 | |
---|
| 43 | for (uint32_t i=0; i<vect.size(); ++i) |
---|
| 44 | { |
---|
| 45 | XML_t * xml = vect[i]; |
---|
| 46 | |
---|
| 47 | std::string node_name = xml->getName(); |
---|
| 48 | |
---|
| 49 | //-------------------------------------- |
---|
| 50 | // Node : Parameter |
---|
| 51 | //-------------------------------------- |
---|
| 52 | if (node_name == "parameter") |
---|
| 53 | { |
---|
| 54 | testSingleton (xml,true); |
---|
| 55 | |
---|
| 56 | // Take attributes of this tag |
---|
| 57 | |
---|
| 58 | attributes_t attributes = xml->getAttributes(); |
---|
| 59 | |
---|
| 60 | // std::cout << "Node [" << name << "]" << std::endl; |
---|
| 61 | // std::cout << " * nb attributes : " << xml->getAttributes().size() << std::endl; |
---|
| 62 | |
---|
| 63 | attribute_t value_name = getAttribute(xml,attributes,"name" ); |
---|
| 64 | attribute_t value_default = getAttribute(xml,attributes,"default"); |
---|
| 65 | attribute_t value_level = (xml->containsAttribute("level" ))?getAttribute(xml,attributes,"level" ):"3"; |
---|
| 66 | attribute_t value_description = (xml->containsAttribute("description"))?getAttribute(xml,attributes,"description"):""; |
---|
| 67 | |
---|
| 68 | bool is_binary = not (xml->containsAttribute("min" ) or |
---|
| 69 | xml->containsAttribute("max" ) or |
---|
| 70 | xml->containsAttribute("step")); |
---|
| 71 | |
---|
| 72 | attribute_t value_min = (not is_binary)?getAttribute(xml,attributes,"min" ):"0"; |
---|
| 73 | attribute_t value_max = (not is_binary)?getAttribute(xml,attributes,"max" ):"1"; |
---|
| 74 | attribute_t value_step = (not is_binary)?getAttribute(xml,attributes,"step"):"+ 1"; |
---|
| 75 | |
---|
| 76 | testAttributesEmpty(xml,attributes); |
---|
| 77 | |
---|
| 78 | // Insert in array |
---|
| 79 | Parameter_definition * param = new Parameter_definition(value_name , |
---|
| 80 | value_min , |
---|
| 81 | value_max , |
---|
| 82 | value_step , |
---|
| 83 | value_default , |
---|
| 84 | value_level , |
---|
| 85 | value_description); |
---|
| 86 | |
---|
| 87 | insert(param); |
---|
| 88 | } |
---|
| 89 | else |
---|
| 90 | //-------------------------------------- |
---|
| 91 | // Node : Link |
---|
| 92 | //-------------------------------------- |
---|
| 93 | if (node_name == "link") |
---|
| 94 | { |
---|
| 95 | testSingleton (xml,true); |
---|
| 96 | |
---|
| 97 | // Take attributes of this tag |
---|
| 98 | |
---|
| 99 | attributes_t attributes = xml->getAttributes(); |
---|
| 100 | |
---|
| 101 | // std::cout << "Node [" << name << "]" << std::endl; |
---|
| 102 | // std::cout << " * nb attributes : " << xml->getAttributes().size() << std::endl; |
---|
| 103 | |
---|
| 104 | attribute_t value_name = getAttribute(xml,attributes,"name"); |
---|
| 105 | attribute_t value_src = getAttribute(xml,attributes,"src" ); |
---|
| 106 | attribute_t value_dest = (xml->containsAttribute("dest" ))?getAttribute(xml,attributes,"dest" ):""; |
---|
| 107 | attribute_t value_description = (xml->containsAttribute("description"))?getAttribute(xml,attributes,"description"):""; |
---|
| 108 | |
---|
| 109 | testAttributesEmpty(xml,attributes); |
---|
| 110 | |
---|
| 111 | // Insert in array |
---|
| 112 | Link_definition * param = new Link_definition(value_name , |
---|
| 113 | value_src , |
---|
| 114 | value_dest , |
---|
| 115 | value_description); |
---|
| 116 | |
---|
| 117 | insert(param); |
---|
| 118 | } |
---|
| 119 | //-------------------------------------- |
---|
| 120 | // Node : Unknow |
---|
| 121 | //-------------------------------------- |
---|
| 122 | else |
---|
| 123 | throw ERRORMORPHEO(FUNCTION,toString(_("Syntax error, node \"%s\" is unknow.\n"),node_name.c_str())); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | delete _xml; |
---|
| 127 | |
---|
| 128 | log_end(Configuration,FUNCTION); |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | }; // end namespace configuration |
---|
| 132 | }; // end namespace behavioural |
---|
| 133 | }; // end namespace morpheo |
---|