00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <pion/platform/ConfigManager.hpp>
00021 #include <boost/filesystem/operations.hpp>
00022
00023
00024 namespace pion {
00025 namespace platform {
00026
00027
00028
00029
00030 const std::string ConfigManager::XML_FILE_EXTENSION = ".xml";
00031 const std::string ConfigManager::BACKUP_FILE_EXTENSION = ".bak";
00032 const std::string ConfigManager::CONFIG_NAMESPACE_URL = "http://purl.org/pion/config";
00033 const std::string ConfigManager::ROOT_ELEMENT_NAME = "PionConfig";
00034 const std::string ConfigManager::STATS_ELEMENT_NAME = "PionStats";
00035 const std::string ConfigManager::PLUGIN_ELEMENT_NAME = "Plugin";
00036 const std::string ConfigManager::NAME_ELEMENT_NAME = "Name";
00037 const std::string ConfigManager::COMMENT_ELEMENT_NAME = "Comment";
00038 const std::string ConfigManager::PION_VERSION_ATTRIBUTE_NAME = "pion_version";
00039 const std::string ConfigManager::ID_ATTRIBUTE_NAME = "id";
00040
00041
00042
00043
00044 void ConfigManager::createConfigFile(void)
00045 {
00046
00047 if (m_config_doc_ptr != NULL)
00048 throw ConfigAlreadyOpenException(m_config_file);
00049
00050
00051 if (boost::filesystem::exists(m_config_file))
00052 throw ConfigFileExistsException(m_config_file);
00053
00054
00055 if ((m_config_doc_ptr = xmlNewDoc(reinterpret_cast<const xmlChar*>("1.0"))) == NULL)
00056 throw InitializeRootConfigException(m_config_file);
00057
00058
00059 if ((m_config_node_ptr = xmlNewNode(NULL, reinterpret_cast<const xmlChar*>(ROOT_ELEMENT_NAME.c_str()))) == NULL)
00060 throw InitializeRootConfigException(m_config_file);
00061 xmlDocSetRootElement(m_config_doc_ptr, m_config_node_ptr);
00062
00063
00064 if (xmlNewProp(m_config_node_ptr,
00065 reinterpret_cast<const xmlChar*>("xmlns"),
00066 reinterpret_cast<const xmlChar*>(CONFIG_NAMESPACE_URL.c_str())) == NULL)
00067 throw InitializeRootConfigException(m_config_file);
00068
00069
00070 if (xmlNewProp(m_config_node_ptr,
00071 reinterpret_cast<const xmlChar*>(PION_VERSION_ATTRIBUTE_NAME.c_str()),
00072 reinterpret_cast<const xmlChar*>(PION_VERSION)) == NULL)
00073 throw InitializeRootConfigException(m_config_file);
00074
00075
00076 saveConfigFile();
00077 }
00078
00079 void ConfigManager::openConfigFile(void)
00080 {
00081
00082 if (m_config_doc_ptr != NULL)
00083 throw ConfigAlreadyOpenException(m_config_file);
00084
00085 m_config_doc_ptr = getConfigFromFile(m_config_file, ROOT_ELEMENT_NAME, m_config_node_ptr, m_logger);
00086 if (m_config_doc_ptr == NULL)
00087 throw ConfigManager::ReadConfigException(m_config_file);
00088 }
00089
00090 xmlDocPtr ConfigManager::getConfigFromFile(const std::string& config_file, const std::string& root_element_name, xmlNodePtr& config_ptr, PionLogger& logger)
00091 {
00092
00093 if (! boost::filesystem::exists(config_file))
00094 throw MissingConfigFileException(config_file);
00095
00096
00097 xmlDocPtr xml_doc_ptr = xmlReadFile(config_file.c_str(), NULL, XML_PARSE_NOBLANKS);
00098 if (xml_doc_ptr == NULL)
00099 throw ReadConfigException(config_file);
00100
00101
00102 if ( (config_ptr = xmlDocGetRootElement(xml_doc_ptr)) == NULL
00103 || xmlStrcmp(config_ptr->name,
00104 reinterpret_cast<const xmlChar*>(root_element_name.c_str())) )
00105 {
00106
00107 throw MissingRootElementException(config_file);
00108 }
00109
00110
00111
00112
00113 xmlChar* xml_char_ptr = xmlGetProp(config_ptr,
00114 reinterpret_cast<const xmlChar*>(PION_VERSION_ATTRIBUTE_NAME.c_str()));
00115 if (xml_char_ptr == NULL)
00116 throw ConfigFileVersionException(config_file);
00117 std::string cfg_file_version = reinterpret_cast<char*>(xml_char_ptr);
00118 xmlFree(xml_char_ptr);
00119 if (cfg_file_version != PION_VERSION && cfg_file_version != "tests") {
00120
00121 unsigned int num_dots = 0;
00122 for (size_t pos = 0; pos < cfg_file_version.size() && num_dots < 2; ++pos) {
00123 if (cfg_file_version[pos] == '.') ++num_dots;
00124 if (cfg_file_version[pos] != PION_VERSION[pos])
00125 throw ConfigFileVersionException(config_file);
00126 }
00127 if (num_dots != 2)
00128 throw ConfigFileVersionException(config_file);
00129
00130 if (xmlSetProp(config_ptr,
00131 reinterpret_cast<const xmlChar*>(PION_VERSION_ATTRIBUTE_NAME.c_str()),
00132 reinterpret_cast<const xmlChar*>(PION_VERSION)) == NULL)
00133 throw UpdateConfigException(root_element_name);
00134 if (xmlSaveFormatFileEnc(config_file.c_str(), xml_doc_ptr, "UTF-8", 1) == -1)
00135 throw WriteConfigException(config_file);
00136 }
00137
00138 return xml_doc_ptr;
00139 }
00140
00141 void ConfigManager::closeConfigFile(void)
00142 {
00143 xmlFreeDoc(m_config_doc_ptr);
00144 m_config_doc_ptr = NULL;
00145 m_config_node_ptr = NULL;
00146 }
00147
00148 void ConfigManager::saveConfigFile(void)
00149 {
00150
00151 backupConfigFile();
00152
00153
00154 if (xmlSaveFormatFileEnc(m_config_file.c_str(), m_config_doc_ptr, "UTF-8", 1) == -1)
00155 throw WriteConfigException(m_config_file);
00156 }
00157
00158 void ConfigManager::removeConfigFile(void)
00159 {
00160 backupConfigFile();
00161 boost::filesystem::remove(m_config_file);
00162 }
00163
00164 void ConfigManager::backupConfigFile(void)
00165 {
00166 try {
00167
00168
00169 if (boost::filesystem::exists(m_config_file)) {
00170 const std::string backup_filename(m_config_file + BACKUP_FILE_EXTENSION);
00171 if (boost::filesystem::exists(backup_filename))
00172 boost::filesystem::remove(backup_filename);
00173 boost::filesystem::copy_file(m_config_file, backup_filename);
00174 }
00175 } catch (...) {
00176 PION_LOG_WARN(m_logger, "Failed to backup configuration file: " << m_config_file);
00177 }
00178 }
00179
00180 void ConfigManager::writeConfigXML(std::ostream& out,
00181 xmlNodePtr config_node,
00182 bool include_siblings)
00183 {
00184
00185
00186
00187 while (config_node != NULL) {
00188
00189
00190 if (config_node->type == XML_ELEMENT_NODE) {
00191
00192
00193 out << '<' << xml_encode(reinterpret_cast<const char*>(config_node->name));
00194
00195
00196 for (_xmlAttr *attr_ptr = config_node->properties;
00197 attr_ptr != NULL; attr_ptr = attr_ptr->next)
00198 {
00199 xmlChar *xml_char_ptr = xmlGetProp(config_node, attr_ptr->name);
00200 if (xml_char_ptr != NULL) {
00201 out << ' ' << xml_encode(reinterpret_cast<const char*>(attr_ptr->name))
00202 << "=\"" << xml_encode(reinterpret_cast<const char*>(xml_char_ptr)) << '"';
00203 xmlFree(xml_char_ptr);
00204 }
00205 }
00206
00207
00208 if (config_node->children != NULL) {
00209
00210 out << '>';
00211
00212
00213 writeConfigXML(out, config_node->children, true);
00214
00215
00216 out << "</" << xml_encode(reinterpret_cast<const char*>(config_node->name)) << '>';
00217 } else {
00218
00219 out << "/>";
00220 }
00221
00222
00223 if (! include_siblings)
00224 break;
00225
00226 } else if (config_node->type == XML_TEXT_NODE) {
00227
00228
00229 out << xml_encode(reinterpret_cast<const char*>(config_node->content));
00230 }
00231
00232
00233 config_node = config_node->next;
00234 }
00235 }
00236
00237 void ConfigManager::writeConfigXMLHeader(std::ostream& out)
00238 {
00239 out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
00240 }
00241
00242 void ConfigManager::writeBeginPionConfigXML(std::ostream& out)
00243 {
00244 writeConfigXMLHeader(out);
00245 out << '<' << ROOT_ELEMENT_NAME << " xmlns=\"" << CONFIG_NAMESPACE_URL << "\" "
00246 << PION_VERSION_ATTRIBUTE_NAME << "=\"" << PION_VERSION << "\">" << std::endl;
00247 }
00248
00249 void ConfigManager::writeEndPionConfigXML(std::ostream& out)
00250 {
00251 out << "</" << ROOT_ELEMENT_NAME << '>' << std::endl;
00252 }
00253
00254 void ConfigManager::writeBeginPionStatsXML(std::ostream& out)
00255 {
00256 writeConfigXMLHeader(out);
00257 out << '<' << STATS_ELEMENT_NAME << " xmlns=\""
00258 << CONFIG_NAMESPACE_URL << "\">" << std::endl;
00259 }
00260
00261 void ConfigManager::writeEndPionStatsXML(std::ostream& out)
00262 {
00263 out << "</" << STATS_ELEMENT_NAME << '>' << std::endl;
00264 }
00265
00266 std::string ConfigManager::xml_encode(const std::string& str)
00267 {
00268 std::string result;
00269 result.reserve(str.size() + 20);
00270 const unsigned char *ptr = reinterpret_cast<const unsigned char*>(str.c_str());
00271 const unsigned char *end_ptr = ptr + str.size();
00272 while (ptr < end_ptr) {
00273
00274
00275
00276
00277 if ((*ptr >= 0x20 && *ptr <= 0x7F) || *ptr == 0x9 || *ptr == 0xa || *ptr == 0xd) {
00278
00279 switch(*ptr) {
00280
00281 case '&':
00282 result += "&";
00283 break;
00284 case '<':
00285 result += "<";
00286 break;
00287 case '>':
00288 result += ">";
00289 break;
00290 case '\"':
00291 result += """;
00292 break;
00293 case '\'':
00294 result += "'";
00295 break;
00296 default:
00297 result += *ptr;
00298 }
00299 } else if (*ptr >= 0xC2 && *ptr <= 0xDF) {
00300
00301 if (*(ptr+1) >= 0x80 && *(ptr+1) <= 0xBF) {
00302 result += *ptr;
00303 result += *(++ptr);
00304 } else {
00305
00306 result += 0xef;
00307 result += 0xbf;
00308 result += 0xbd;
00309 }
00310 } else if (*ptr >= 0xE0 && *ptr <= 0xEF) {
00311
00312 if (*(ptr+1) >= 0x80 && *(ptr+1) <= 0xBF
00313 && *(ptr+2) >= 0x80 && *(ptr+2) <= 0xBF) {
00314 result += *ptr;
00315 result += *(++ptr);
00316 result += *(++ptr);
00317 } else {
00318
00319 result += 0xef;
00320 result += 0xbf;
00321 result += 0xbd;
00322 }
00323 } else if (*ptr >= 0xF0 && *ptr <= 0xF4) {
00324
00325 if (*(ptr+1) >= 0x80 && *(ptr+1) <= 0xBF
00326 && *(ptr+2) >= 0x80 && *(ptr+2) <= 0xBF
00327 && *(ptr+3) >= 0x80 && *(ptr+3) <= 0xBF) {
00328 result += *ptr;
00329 result += *(++ptr);
00330 result += *(++ptr);
00331 result += *(++ptr);
00332 } else {
00333
00334 result += 0xef;
00335 result += 0xbf;
00336 result += 0xbd;
00337 }
00338 } else {
00339
00340 result += 0xef;
00341 result += 0xbf;
00342 result += 0xbd;
00343 }
00344 ++ptr;
00345 }
00346
00347 return result;
00348 }
00349
00350 std::string ConfigManager::createFilename(void)
00351 {
00352 std::string file_name(createUUID());
00353 file_name += XML_FILE_EXTENSION;
00354 return file_name;
00355 }
00356
00357 xmlNodePtr ConfigManager::createPluginConfig(const std::string& plugin_type)
00358 {
00359 xmlNodePtr config_ptr = xmlNewNode(NULL, reinterpret_cast<const xmlChar*>(PLUGIN_ELEMENT_NAME.c_str()));
00360 xmlNodeSetContent(config_ptr, reinterpret_cast<const xmlChar*>(plugin_type.c_str()));
00361 return config_ptr;
00362 }
00363
00364 xmlNodePtr ConfigManager::createResourceConfig(const std::string& resource_name,
00365 const char *buf, std::size_t len)
00366 {
00367
00368 if (buf == NULL || len == 0)
00369 throw BadXMLBufferException();
00370
00371
00372 xmlNodePtr node_ptr = NULL;
00373 xmlDocPtr doc_ptr = xmlParseMemory(buf, len);
00374 if (doc_ptr == NULL)
00375 throw XMLBufferParsingException(buf);
00376
00377
00378 if ( (node_ptr = xmlDocGetRootElement(doc_ptr)) == NULL
00379 || xmlStrcmp(node_ptr->name,
00380 reinterpret_cast<const xmlChar*>(ROOT_ELEMENT_NAME.c_str())) )
00381 {
00382 xmlFreeDoc(doc_ptr);
00383
00384 throw MissingRootElementException(buf);
00385 }
00386
00387 node_ptr = findConfigNodeByName(resource_name, node_ptr->children);
00388 if (node_ptr == NULL) {
00389 xmlFreeDoc(doc_ptr);
00390 throw MissingResourceElementException(resource_name);
00391 }
00392
00393
00394 node_ptr = xmlCopyNodeList(node_ptr->children);
00395
00396
00397 xmlFreeDoc(doc_ptr);
00398
00399
00400 return node_ptr;
00401 }
00402
00403 std::string ConfigManager::createFilename(const std::string& file_path)
00404 {
00405 boost::filesystem::path new_path(file_path);
00406 new_path /= createFilename();
00407 return boost::filesystem::system_complete(new_path).file_string();
00408 }
00409
00410 std::string ConfigManager::resolveRelativePath(const std::string& base_path_to_file,
00411 const std::string& orig_path)
00412 {
00413
00414 if (boost::filesystem::path(orig_path).is_complete())
00415 return orig_path;
00416
00417
00418 boost::filesystem::path new_path(boost::filesystem::system_complete(base_path_to_file));
00419 new_path.remove_leaf();
00420 new_path /= orig_path;
00421 new_path.normalize();
00422 return new_path.file_string();
00423 }
00424
00425 std::string ConfigManager::resolveRelativeDataPath(const std::string& orig_path)
00426 {
00427
00428 if (boost::filesystem::path(orig_path).is_complete())
00429 return orig_path;
00430
00431 boost::filesystem::path new_path(boost::filesystem::system_complete(getDataDirectory()));
00432 new_path /= orig_path;
00433 new_path.normalize();
00434 return new_path.file_string();
00435 }
00436
00437 bool ConfigManager::getNodeId(xmlNodePtr config_node, std::string& node_id)
00438 {
00439 node_id = "";
00440 xmlChar *xml_char_ptr = xmlGetProp(config_node,
00441 reinterpret_cast<const xmlChar*>(ID_ATTRIBUTE_NAME.c_str()));
00442 if (xml_char_ptr != NULL && xml_char_ptr[0]!='\0')
00443 node_id = reinterpret_cast<char*>(xml_char_ptr);
00444 xmlFree(xml_char_ptr);
00445 return(! node_id.empty());
00446 }
00447
00448 xmlNodePtr ConfigManager::findConfigNodeByName(const std::string& element_name,
00449 xmlNodePtr starting_node)
00450 {
00451 xmlNodePtr matching_node = NULL;
00452
00453
00454 for (xmlNodePtr cur_node = starting_node;
00455 cur_node != NULL; cur_node = cur_node->next)
00456 {
00457 if (cur_node->type == XML_ELEMENT_NODE
00458 && xmlStrcmp(cur_node->name, reinterpret_cast<const xmlChar*>(element_name.c_str()))==0)
00459 {
00460
00461 matching_node = cur_node;
00462 break;
00463 }
00464 }
00465
00466 return matching_node;
00467 }
00468
00469 xmlNodePtr ConfigManager::findConfigNodeByContent(const std::string& element_name,
00470 const std::string& content_value,
00471 xmlNodePtr starting_node)
00472 {
00473 xmlNodePtr matching_node = NULL;
00474
00475
00476 for (xmlNodePtr cur_node = starting_node;
00477 cur_node != NULL; cur_node = cur_node->next)
00478 {
00479 if (cur_node->type == XML_ELEMENT_NODE
00480 && xmlStrcmp(cur_node->name, reinterpret_cast<const xmlChar*>(element_name.c_str()))==0)
00481 {
00482
00483 xmlChar *xml_char_ptr = xmlNodeGetContent(cur_node);
00484 if (xml_char_ptr != NULL) {
00485 if (content_value == reinterpret_cast<char*>(xml_char_ptr)) {
00486
00487 matching_node = cur_node;
00488 break;
00489 }
00490 xmlFree(xml_char_ptr);
00491 }
00492 }
00493 }
00494
00495 return matching_node;
00496 }
00497
00498 xmlNodePtr ConfigManager::findConfigNodeByAttr(const std::string& element_name,
00499 const std::string& attr_name,
00500 const std::string& attr_value,
00501 xmlNodePtr starting_node)
00502 {
00503 xmlNodePtr matching_node = NULL;
00504
00505
00506 for (xmlNodePtr cur_node = starting_node;
00507 cur_node != NULL; cur_node = cur_node->next)
00508 {
00509 if (cur_node->type == XML_ELEMENT_NODE
00510 && xmlStrcmp(cur_node->name, reinterpret_cast<const xmlChar*>(element_name.c_str()))==0)
00511 {
00512
00513 xmlChar *xml_char_ptr = xmlGetProp(cur_node, reinterpret_cast<const xmlChar*>(attr_name.c_str()));
00514 if (xml_char_ptr != NULL) {
00515 if (attr_value == reinterpret_cast<char*>(xml_char_ptr)) {
00516
00517 matching_node = cur_node;
00518 break;
00519 }
00520 xmlFree(xml_char_ptr);
00521 }
00522 }
00523 }
00524
00525 return matching_node;
00526 }
00527
00528 bool ConfigManager::getConfigOption(const std::string& option_name,
00529 std::string& option_value,
00530 const xmlNodePtr starting_node)
00531 {
00532
00533 xmlNodePtr option_node = findConfigNodeByName(option_name, starting_node);
00534 if (option_node != NULL) {
00535 xmlChar *xml_char_ptr = xmlNodeGetContent(option_node);
00536 if (xml_char_ptr != NULL) {
00537
00538 if (xml_char_ptr[0] != '\0') {
00539
00540 option_value = reinterpret_cast<char*>(xml_char_ptr);
00541 xmlFree(xml_char_ptr);
00542 return true;
00543 }
00544 xmlFree(xml_char_ptr);
00545 }
00546 }
00547
00548
00549 option_value.clear();
00550 return false;
00551 }
00552
00553 bool ConfigManager::getConfigOptionEmptyOk(const std::string& option_name,
00554 std::string& option_value,
00555 const xmlNodePtr starting_node)
00556 {
00557 if (findConfigNodeByName(option_name, starting_node)) {
00558 option_value.clear();
00559 getConfigOption(option_name, option_value, starting_node);
00560 return true;
00561 } else
00562 return false;
00563 }
00564
00565 std::string ConfigManager::getAttribute(const char *name, const xmlNodePtr ptr)
00566 {
00567 std::string id_str;
00568 xmlChar *xml_char_ptr = xmlGetProp(ptr, reinterpret_cast<const xmlChar*>(name));
00569 if (xml_char_ptr != NULL) {
00570 if (xml_char_ptr[0] != '\0')
00571 id_str = reinterpret_cast<char*>(xml_char_ptr);
00572 xmlFree(xml_char_ptr);
00573 }
00574 return id_str;
00575 }
00576
00577 bool ConfigManager::updateConfigOption(const std::string& option_name,
00578 const std::string& option_value,
00579 xmlNodePtr parent_node)
00580 {
00581
00582 xmlNodePtr option_node = findConfigNodeByName(option_name, parent_node->children);
00583 if (option_node != NULL) {
00584
00585 if (option_value.empty()) {
00586
00587 xmlUnlinkNode(option_node);
00588 xmlFreeNodeList(option_node);
00589 } else {
00590
00591 xmlNodeSetContent(option_node, reinterpret_cast<const xmlChar*>(xml_encode(option_value).c_str()));
00592 }
00593 } else {
00594
00595
00596 if (! option_value.empty()) {
00597 if (xmlNewTextChild(parent_node, NULL,
00598 reinterpret_cast<const xmlChar*>(option_name.c_str()),
00599 reinterpret_cast<const xmlChar*>(option_value.c_str())) == NULL)
00600 return false;
00601 }
00602 }
00603
00604 return true;
00605 }
00606
00607 void ConfigManager::openPluginConfig(const std::string& plugin_name)
00608 {
00609
00610 ConfigManager::openConfigFile();
00611
00612 xmlNodePtr plugin_node = m_config_node_ptr->children;
00613 while ( (plugin_node = findConfigNodeByName(plugin_name, plugin_node)) != NULL)
00614 {
00615
00616 std::string new_plugin_id;
00617 if (! getNodeId(plugin_node, new_plugin_id))
00618 throw EmptyPluginIdException(getConfigFile());
00619
00620
00621 std::string new_plugin_type;
00622 if (! getConfigOption(PLUGIN_ELEMENT_NAME, new_plugin_type, plugin_node->children))
00623 throw EmptyPluginElementException(new_plugin_id);
00624
00625
00626 addPluginNoLock(new_plugin_id, new_plugin_type, plugin_node->children);
00627
00628
00629 plugin_node = plugin_node->next;
00630 }
00631 }
00632
00633 bool ConfigManager::setPluginConfig(xmlNodePtr plugin_node_ptr, xmlNodePtr config_ptr)
00634 {
00635 xmlNodePtr plugin_config_copy = xmlCopyNodeList(config_ptr);
00636 if (plugin_config_copy == NULL)
00637 return false;
00638
00639
00640 xmlNodePtr plugin_type_node = findConfigNodeByName(PLUGIN_ELEMENT_NAME,
00641 plugin_config_copy);
00642 if (plugin_type_node != NULL) {
00643
00644 if (plugin_config_copy == plugin_type_node)
00645 plugin_config_copy = plugin_type_node->next;
00646
00647
00648 xmlUnlinkNode(plugin_type_node);
00649 xmlFreeNode(plugin_type_node);
00650
00651
00652 if (plugin_config_copy == NULL)
00653 return true;
00654 }
00655
00656
00657 for (xmlNodePtr tmp_node = plugin_config_copy; tmp_node != NULL;
00658 tmp_node = tmp_node->next)
00659 {
00660 tmp_node->ns = tmp_node->nsDef = NULL;
00661 }
00662
00663
00664 if (xmlAddChildList(plugin_node_ptr, plugin_config_copy) == NULL) {
00665 xmlFreeNodeList(plugin_config_copy);
00666 return false;
00667 }
00668
00669 return true;
00670 }
00671
00672 void ConfigManager::setPluginConfig(const std::string& plugin_name,
00673 const std::string& plugin_id,
00674 const xmlNodePtr config_ptr)
00675 {
00676
00677 xmlNodePtr plugin_node = findConfigNodeByAttr(plugin_name,
00678 ID_ATTRIBUTE_NAME,
00679 plugin_id,
00680 m_config_node_ptr->children);
00681 if (plugin_node == NULL)
00682 throw UpdatePluginConfigException(plugin_id);
00683
00684
00685 xmlNodePtr cur_node;
00686 xmlNodePtr next_node = plugin_node->children;
00687 while (next_node != NULL) {
00688 cur_node = next_node;
00689 next_node = next_node->next;
00690
00691 if (cur_node->type != XML_ELEMENT_NODE
00692 || xmlStrcmp(cur_node->name, reinterpret_cast<const xmlChar*>(PLUGIN_ELEMENT_NAME.c_str()))!=0)
00693 {
00694 xmlUnlinkNode(cur_node);
00695 xmlFreeNode(cur_node);
00696 }
00697 }
00698
00699
00700 if (config_ptr != NULL) {
00701 if (! setPluginConfig(plugin_node, config_ptr))
00702 throw UpdatePluginConfigException(plugin_id);
00703 }
00704
00705
00706 saveConfigFile();
00707 }
00708
00709 void ConfigManager::addPluginConfig(const std::string& plugin_name,
00710 const std::string& plugin_id,
00711 const std::string& plugin_type,
00712 const xmlNodePtr config_ptr)
00713 {
00714
00715 xmlNodePtr new_plugin_node = xmlNewNode(NULL, reinterpret_cast<const xmlChar*>(plugin_name.c_str()));
00716 if (new_plugin_node == NULL)
00717 throw AddPluginConfigException(plugin_type);
00718 if ((new_plugin_node=xmlAddChild(m_config_node_ptr, new_plugin_node)) == NULL) {
00719 xmlFreeNode(new_plugin_node);
00720 throw AddPluginConfigException(plugin_type);
00721 }
00722
00723
00724 if (xmlNewProp(new_plugin_node, reinterpret_cast<const xmlChar*>(ID_ATTRIBUTE_NAME.c_str()),
00725 reinterpret_cast<const xmlChar*>(plugin_id.c_str())) == NULL)
00726 throw AddPluginConfigException(plugin_type);
00727
00728
00729 if (xmlNewTextChild(new_plugin_node, NULL,
00730 reinterpret_cast<const xmlChar*>(PLUGIN_ELEMENT_NAME.c_str()),
00731 reinterpret_cast<const xmlChar*>(plugin_type.c_str())) == NULL)
00732 throw AddPluginConfigException(plugin_type);
00733
00734
00735 if (config_ptr != NULL) {
00736 if (! setPluginConfig(new_plugin_node, config_ptr))
00737 throw AddPluginConfigException(plugin_type);
00738 }
00739
00740
00741 saveConfigFile();
00742 }
00743
00744 void ConfigManager::removePluginConfig(const std::string& plugin_name,
00745 const std::string& plugin_id)
00746 {
00747
00748 xmlNodePtr plugin_node = findConfigNodeByAttr(plugin_name,
00749 ID_ATTRIBUTE_NAME,
00750 plugin_id,
00751 m_config_node_ptr->children);
00752 if (plugin_node == NULL)
00753 throw RemovePluginConfigException(plugin_id);
00754
00755
00756 xmlUnlinkNode(plugin_node);
00757 xmlFreeNode(plugin_node);
00758
00759
00760 saveConfigFile();
00761 }
00762
00763
00764 }
00765 }