TOBI SignalServer
0.1
|
00001 /* 00002 This file is part of the TOBI SignalServer. 00003 00004 Commercial Usage 00005 Licensees holding valid Graz University of Technology Commercial 00006 licenses may use this file in accordance with the Graz University 00007 of Technology Commercial License Agreement provided with the 00008 Software or, alternatively, in accordance with the terms contained in 00009 a written agreement between you and Graz University of Technology. 00010 00011 -------------------------------------------------- 00012 00013 GNU General Public License Usage 00014 Alternatively, this file may be used under the terms of the GNU 00015 General Public License version 3.0 as published by the Free Software 00016 Foundation and appearing in the file gpl.txt included in the 00017 packaging of this file. Please review the following information to 00018 ensure the GNU General Public License version 3.0 requirements will be 00019 met: http://www.gnu.org/copyleft/gpl.html. 00020 00021 In case of GNU General Public License Usage ,the TOBI SignalServer 00022 is distributed in the hope that it will be useful, 00023 but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 GNU General Public License for more details. 00026 00027 You should have received a copy of the GNU General Public License 00028 along with the TOBI SignalServer. If not, see <http://www.gnu.org/licenses/>. 00029 00030 Copyright 2010 Graz University of Technology 00031 Contact: SignalServer@tobi-project.org 00032 */ 00033 00038 #include "config/xml_parser.h" 00039 #include <boost/algorithm/string.hpp> 00040 00041 namespace tobiss 00042 { 00043 00044 using std::vector; 00045 using std::string; 00046 using std::map; 00047 using std::pair; 00048 using std::cout; 00049 using std::endl; 00050 00051 using boost::lexical_cast; 00052 using boost::bad_lexical_cast; 00053 00054 using boost::uint16_t; 00055 using boost::algorithm::to_lower_copy; 00056 00057 //--------------------------------------------------------------------------------------- 00058 00059 XMLParser::XMLParser(string xml_file) 00060 : doc_(xml_file), external_data_file_(0) 00061 { 00062 #ifdef DEBUG 00063 cout << "XMLParser: Constructor" << endl; 00064 #endif 00065 00066 doc_.LoadFile(); 00067 ticpp::Iterator<ticpp::Element> config(doc_.FirstChildElement(cst_.tobi, true)); 00068 00069 // Filereader part --> TODO and move to other file 00070 // file_reader_ = config->FirstChildElement(cst_.file_reader, false); 00071 00072 // if( file_reader_ != file_reader_.end() ) 00073 // { 00074 // for(ticpp::Iterator<ticpp::Element> it(file_reader_) ; ++it != it.end(); ) 00075 // if(it->Value() == cst_.file_reader) 00076 // throw(ticpp::Exception("Error -- Only one subject definition allowed!")); 00077 // 00078 // external_data_file_ = 1; 00079 // 00080 // parseFileReader(); 00081 // } 00082 00083 00084 server_settings_ = config->FirstChildElement(cst_.ss, true); 00085 for(ticpp::Iterator<ticpp::Element> it(server_settings_); ++it != it.end(); ) 00086 if(it->Value() == cst_.ss) 00087 throw(ticpp::Exception("Error -- Multiple server settings found!")); 00088 00089 if(external_data_file_) 00090 return; 00091 00092 subject_ = config->FirstChildElement(cst_.subject, true); 00093 for(ticpp::Iterator<ticpp::Element> it(subject_); ++it != it.end(); ) 00094 if(it->Value() == cst_.subject) 00095 throw(ticpp::Exception("Error -- Only one subject definition allowed!")); 00096 00097 00098 // FIXME -- hardcoded strings --> shifted to HWThread 00099 00100 ticpp::Iterator< ticpp::Attribute > attribute; 00101 for(ticpp::Iterator<ticpp::Element> it(config->FirstChildElement("hardware", true)); 00102 it != it.end(); it++) 00103 if(it->Value() == "hardware") 00104 { 00105 map<string, string> m; 00106 for(attribute = attribute.begin(it.Get()); attribute != attribute.end(); 00107 attribute++) 00108 m.insert(pair<string, string>(attribute->Name(), attribute->Value())); 00109 checkHardwareAttributes(m); 00110 00111 hardware_.push_back(make_pair(m.find("name")->second,it)); 00112 } 00113 } 00114 00115 //--------------------------------------------------------------------------------------- 00116 00117 bool XMLParser::equalsYesOrNo(const std::string& s) 00118 { 00119 if(to_lower_copy(s) == "yes" || s == "1") 00120 return(true); 00121 if(to_lower_copy(s) == "no" || s == "0") 00122 return(false); 00123 else 00124 { 00125 string e = s + " -- Value equals neiter \"yes, no, 0 or 1\"!"; 00126 throw std::invalid_argument(e); 00127 } 00128 } 00129 00130 //--------------------------------------------------------------------------------------- 00131 00132 map<string,string> XMLParser::parseSubject() 00133 { 00134 #ifdef DEBUG 00135 cout << "XMLParser: parseSubject" << endl; 00136 #endif 00137 00138 if(external_data_file_) 00139 return(subject_map_); 00140 00141 map<string, string>& m = subject_map_; 00142 ticpp::Iterator<ticpp::Element> elem(subject_->FirstChildElement(cst_.s_id,true)); 00143 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00144 00145 elem = subject_->FirstChildElement(cst_.s_first_name, true); 00146 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00147 00148 elem = subject_->FirstChildElement(cst_.s_surname, true); 00149 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00150 00151 elem = subject_->FirstChildElement(cst_.s_birthday, true); 00152 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00153 00154 elem = subject_->FirstChildElement(cst_.s_sex, true); 00155 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00156 00157 for(elem = subject_->FirstChildElement(true) ; elem != elem.end(); elem++) 00158 { 00159 string tmp(elem->Value()); 00160 if(tmp == cst_.s_id || tmp == cst_.s_first_name || tmp == cst_.s_surname || tmp == cst_.s_birthday || tmp == cst_.s_sex) 00161 continue; 00162 if(elem->GetText(false) != "") 00163 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00164 00165 ticpp::Iterator< ticpp::Attribute > attribute; 00166 for(attribute = attribute.begin(elem.Get()); attribute != attribute.end(); 00167 attribute++) 00168 m.insert(pair<string, string>(attribute->Name(), attribute->Value())); 00169 } 00170 return(subject_map_); 00171 } 00172 00173 //--------------------------------------------------------------------------------------- 00174 00175 map<string,string> XMLParser::parseServerSettings() 00176 { 00177 #ifdef DEBUG 00178 cout << "XMLParser: parseServerSettings" << endl; 00179 #endif 00180 00181 map<string, string>& m = server_settings_map_; 00182 00183 ticpp::Iterator<ticpp::Element> elem(server_settings_->FirstChildElement(cst_.ss_ctl_port, true)); 00184 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00185 00186 elem = server_settings_->FirstChildElement(cst_.ss_udp_bc_addr, true); 00187 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00188 00189 elem = server_settings_->FirstChildElement(cst_.ss_udp_port, true); 00190 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00191 00192 #ifdef USE_TID_SERVER 00193 elem = server_settings_->FirstChildElement(cst_.ss_tid_port, true); 00194 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00195 #endif 00196 00197 for(elem = server_settings_->FirstChildElement(true) ; elem != elem.end(); elem++) 00198 { 00199 string tmp(elem->Value()); 00200 if(tmp == cst_.ss_ctl_port || tmp == cst_.ss_udp_bc_addr || tmp == cst_.ss_udp_port 00201 || tmp == cst_.ss_tid_port) 00202 continue; 00203 00204 // Save as gdf part 00205 if(tmp == cst_.ss_store_data) 00206 { 00207 parseFileLocation(elem, m); 00208 00209 string overwrite(cst_.ss_file_overwrite_default); 00210 ticpp::Iterator<ticpp::Element> child = elem->FirstChildElement(cst_.ss_file_overwrite, false); 00211 00212 if(child != child.end()) 00213 overwrite = lexical_cast<string>( equalsYesOrNo(child->GetText(false)) ); 00214 00215 m.insert(pair<string, string>(cst_.ss_file_overwrite, overwrite)); 00216 00217 continue; 00218 } 00219 00220 if(elem->GetText(false) != "") 00221 m.insert(pair<string, string>(elem->Value(), elem->GetText(false))); 00222 00223 ticpp::Iterator< ticpp::Attribute > attribute; 00224 for(attribute = attribute.begin(elem.Get()); attribute != attribute.end(); 00225 attribute++) 00226 m.insert(pair<string, string>(attribute->Name(), attribute->Value())); 00227 } 00228 return(server_settings_map_); 00229 } 00230 00231 //----------------------------------------------------------------------------- 00232 00233 void XMLParser::checkHardwareAttributes(std::map<std::string,std::string>& m) 00234 { 00235 #ifdef DEBUG 00236 cout << "XMLParser: checkHardwareAttributes" << endl; 00237 #endif 00238 00239 // FIXME -- hardcoded strings --> shifted to HWThread 00240 00241 map<string,string>::iterator it; 00242 string error; 00243 if( (it = m.find("name")) == m.end()) 00244 { 00245 throw(ticpp::Exception("Error in hardware -- Device name not specified!")); 00246 } 00247 if(it->second.empty() || it->second == " ") 00248 { 00249 throw(ticpp::Exception("Error in hardware -- Device has to be named!")); 00250 } 00251 } 00252 00253 //--------------------------------------------------------------------------------------- 00254 00255 //void XMLParser::parseFileReader() 00256 //{ 00257 // 00258 // // ticpp::Iterator<ticpp::Element> elem(server_settings_->FirstChildElement(cst_.ss_ctl_port, true)); 00259 // 00260 // parseFileLocation(file_reader_, file_reader_map_); 00261 // 00262 // string speedup = "0"; 00263 // ticpp::Iterator<ticpp::Element> child = file_reader_->FirstChildElement(cst_.fr_speedup, false); 00264 // 00265 // if(child != child.end()) 00266 // speedup = lexical_cast<string>( child->GetText(false) ); 00267 // 00268 // file_reader_map_.insert(pair<string, string>(cst_.fr_speedup, speedup)); 00269 // 00270 // 00271 // string stop_end = "1"; 00272 // child = file_reader_->FirstChildElement(cst_.fr_stop, false); 00273 // 00274 // if(child != child.end()) 00275 // stop_end = lexical_cast<string>( cst_.equalsYesOrNo(child->GetText(false)) ); 00276 // 00277 // file_reader_map_.insert(pair<string, string>(cst_.fr_stop, stop_end)); 00278 // 00279 // 00280 // // parse subject info from file 00281 // 00282 // 00283 // // parse signal info from file 00284 // 00285 // 00289 //} 00290 00291 //--------------------------------------------------------------------------------------- 00292 00293 void XMLParser::parseFileLocation(ticpp::Iterator<ticpp::Element> elem, map<string,string>& m) 00294 { 00295 00296 string filename; 00297 string filetype; 00298 string filepath(cst_.ss_filepath_default); 00299 string tmp_filetype; 00300 00301 ticpp::Iterator<ticpp::Element> child(elem->FirstChildElement(cst_.ss_filename, true)); 00302 filename = child->GetText(false); 00303 00304 child = elem->FirstChildElement(cst_.ss_filepath, false); 00305 if(child != child.end()) 00306 filepath = child->GetText(false); 00307 00308 size_t pos = filename.rfind("."); 00309 00310 if(pos != string::npos) 00311 tmp_filetype = to_lower_copy(filename.substr(pos + 1, filename.length()-pos )); 00312 00313 child = elem->FirstChildElement(cst_.ss_filetype, false); 00314 if(child != child.end()) 00315 filetype = to_lower_copy(child->GetText(false)); 00316 00317 if(filetype == "" && tmp_filetype != "") 00318 filetype = tmp_filetype; 00319 if(filetype == tmp_filetype) 00320 filename = filename.substr(0, pos); 00321 00322 00323 if(filename == "") 00324 throw(ticpp::Exception("Error in " + cst_.ss + " -- No filename given!")); 00325 if(filetype == "") 00326 throw(ticpp::Exception("Error in " + cst_.ss + " -- No filetype given!")); 00327 00328 m.insert(pair<string, string>(cst_.ss_filename, filename)); 00329 m.insert(pair<string, string>(cst_.ss_filetype, filetype)); 00330 m.insert(pair<string, string>(cst_.ss_filepath, filepath)); 00331 } 00332 00333 } // Namespace tobiss 00334 00335 //---------------------------------------------------------------------------------------