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* 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 _pConfig->duplicate();
33}
34
35
36ConfigurationMapper::~ConfigurationMapper()
37{
38 _pConfig->release();
39}
40
41
42bool ConfigurationMapper::getRaw(const std::string& key, std::string& value) const
43{
44 std::string translatedKey = translateKey(key);
45 return _pConfig->getRaw(translatedKey, value);
46}
47
48
49void ConfigurationMapper::setRaw(const std::string& key, const std::string& value)
50{
51 std::string translatedKey = translateKey(key);
52 _pConfig->setRaw(translatedKey, value);
53}
54
55
56void ConfigurationMapper::enumerate(const std::string& key, Keys& range) const
57{
58 std::string cKey(key);
59 if (!cKey.empty()) cKey += '.';
60 std::string::size_type keyLen = cKey.length();
61 if (keyLen < _toPrefix.length())
62 {
63 if (_toPrefix.compare(0, keyLen, cKey) == 0)
64 {
65 std::string::size_type pos = _toPrefix.find_first_of('.', keyLen);
66 poco_assert_dbg(pos != std::string::npos);
67 range.push_back(_toPrefix.substr(keyLen, pos - keyLen));
68 }
69 }
70 else
71 {
72 std::string translatedKey;
73 if (cKey == _toPrefix)
74 {
75 translatedKey = _fromPrefix;
76 if (!translatedKey.empty())
77 translatedKey.resize(translatedKey.length() - 1);
78 }
79 else translatedKey = translateKey(key);
80 _pConfig->enumerate(translatedKey, range);
81 }
82}
83
84
85void ConfigurationMapper::removeRaw(const std::string& key)
86{
87 std::string translatedKey = translateKey(key);
88 _pConfig->remove(translatedKey);
89}
90
91
92std::string ConfigurationMapper::translateKey(const std::string& key) const
93{
94 std::string result(key);
95 if (result.compare(0, _toPrefix.size(), _toPrefix) == 0)
96 result.replace(0, _toPrefix.size(), _fromPrefix);
97 return result;
98}
99
100
101} } // namespace Poco::Util
102