1//
2// ConfigurationMapper.cpp
3//
4// Library: Util
5// Package: Configuration
6// Module: ConfigurationMapper
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Util/ConfigurationMapper.h"
16
17
18namespace Poco {
19namespace Util {
20
21
22ConfigurationMapper::ConfigurationMapper(const std::string& fromPrefix, const std::string& toPrefix, AbstractConfiguration::Ptr pConfig):
23 _fromPrefix(fromPrefix),
24 _toPrefix(toPrefix),
25 _pConfig(pConfig)
26{
27 poco_check_ptr (pConfig);
28
29 if (!_fromPrefix.empty()) _fromPrefix += '.';
30 if (!_toPrefix.empty()) _toPrefix += '.';
31}
32
33
34ConfigurationMapper::~ConfigurationMapper()
35{
36}
37
38
39bool ConfigurationMapper::getRaw(const std::string& key, std::string& value) const
40{
41 std::string translatedKey = translateKey(key);
42 return _pConfig->getRaw(translatedKey, value);
43}
44
45
46void ConfigurationMapper::setRaw(const std::string& key, const std::string& value)
47{
48 std::string translatedKey = translateKey(key);
49 _pConfig->setRaw(translatedKey, value);
50}
51
52
53void ConfigurationMapper::enumerate(const std::string& key, Keys& range) const
54{
55 std::string cKey(key);
56 if (!cKey.empty()) cKey += '.';
57 std::string::size_type keyLen = cKey.length();
58 if (keyLen < _toPrefix.length())
59 {
60 if (_toPrefix.compare(0, keyLen, cKey) == 0)
61 {
62 std::string::size_type pos = _toPrefix.find_first_of('.', keyLen);
63 poco_assert_dbg(pos != std::string::npos);
64 range.push_back(_toPrefix.substr(keyLen, pos - keyLen));
65 }
66 }
67 else
68 {
69 std::string translatedKey;
70 if (cKey == _toPrefix)
71 {
72 translatedKey = _fromPrefix;
73 if (!translatedKey.empty())
74 translatedKey.resize(translatedKey.length() - 1);
75 }
76 else translatedKey = translateKey(key);
77 _pConfig->enumerate(translatedKey, range);
78 }
79}
80
81
82void ConfigurationMapper::removeRaw(const std::string& key)
83{
84 std::string translatedKey = translateKey(key);
85 _pConfig->remove(translatedKey);
86}
87
88
89std::string ConfigurationMapper::translateKey(const std::string& key) const
90{
91 std::string result(key);
92 if (result.compare(0, _toPrefix.size(), _toPrefix) == 0)
93 result.replace(0, _toPrefix.size(), _fromPrefix);
94 return result;
95}
96
97
98} } // namespace Poco::Util
99