1 /*
2 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "MappingData.h"
31 #include "Tokenizer.h"
32 #include "Utility.h"
33 #include <assert.h>
34
CMappingData()35 CMappingData::CMappingData()
36 {
37 }
38
fromXml(const CXmlElement & xmlElement,CXmlSerializingContext & serializingContext)39 bool CMappingData::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
40 {
41 assert(xmlElement.hasAttribute("Mapping"));
42
43 std::string strMapping = xmlElement.getAttributeString("Mapping");
44
45 Tokenizer mappingTok(strMapping, ",");
46
47 std::string strMappingElement;
48
49 while (!(strMappingElement = mappingTok.next()).empty()) {
50
51 std::string::size_type iFistDelimiterOccurrence = strMappingElement.find_first_of(':');
52
53 std::string strKey, strValue;
54
55 if (iFistDelimiterOccurrence == std::string::npos) {
56
57 // There is no delimiter in the mapping field,
58 // it means that no value has been provided
59 strKey = strMappingElement;
60 strValue = "";
61
62 } else {
63
64 // Get mapping key
65 strKey = strMappingElement.substr(0, iFistDelimiterOccurrence);
66
67 // Get mapping value
68 strValue = strMappingElement.substr(iFistDelimiterOccurrence + 1);
69
70 }
71
72 if (!addValue(strKey, strValue)) {
73
74 serializingContext.setError("Duplicate Mapping data: Unable to process Mapping element key = " + strKey + ", value = " + strValue + " from XML element " + xmlElement.getPath());
75
76 return false;
77 }
78 }
79 return true;
80 }
81
getValue(const std::string & strkey,const std::string * & pStrValue) const82 bool CMappingData::getValue(const std::string& strkey, const std::string*& pStrValue) const
83 {
84 KeyToValueMapConstIterator it = _keyToValueMap.find(strkey);
85
86 if (it != _keyToValueMap.end()) {
87
88 pStrValue = &it->second;
89
90 return true;
91 }
92 return false;
93 }
94
asString() const95 std::string CMappingData::asString() const
96 {
97 std::string strValue;
98
99 CUtility::asString(_keyToValueMap, strValue, ", ", ":");
100
101 return strValue;
102 }
103
addValue(const std::string & strkey,const std::string & strValue)104 bool CMappingData::addValue(const std::string& strkey, const std::string& strValue)
105 {
106 if (_keyToValueMap.find(strkey) != _keyToValueMap.end()) {
107
108 return false;
109 }
110 _keyToValueMap[strkey] = strValue;
111
112 return true;
113 }
114
115