blob: 63c65af0e97478843d3513c4f6f79cda97f337ad [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * INTEL CONFIDENTIAL
3 * Copyright © 2011 Intel
4 * Corporation All Rights Reserved.
5 *
6 * The source code contained or described herein and all documents related to
7 * the source code ("Material") are owned by Intel Corporation or its suppliers
8 * or licensors. Title to the Material remains with Intel Corporation or its
9 * suppliers and licensors. The Material contains trade secrets and proprietary
10 * and confidential information of Intel or its suppliers and licensors. The
11 * Material is protected by worldwide copyright and trade secret laws and
12 * treaty provisions. No part of the Material may be used, copied, reproduced,
13 * modified, published, uploaded, posted, transmitted, distributed, or
14 * disclosed in any way without Intel’s prior express written permission.
15 *
16 * No license under any patent, copyright, trade secret or other intellectual
17 * property right is granted to or conferred upon you by disclosure or delivery
18 * of the Materials, either expressly, by implication, inducement, estoppel or
19 * otherwise. Any license under such intellectual property rights must be
20 * express and approved by Intel in writing.
21 *
Patrick Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include "MappingData.h"
26#include "Tokenizer.h"
Frederic Boisnard390b36d2013-05-23 15:28:31 +020027#include "Utility.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020028#include <assert.h>
29
30CMappingData::CMappingData()
31{
32}
33
34bool CMappingData::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
35{
36 assert(xmlElement.hasAttribute("Mapping"));
37
38 string strMapping = xmlElement.getAttributeString("Mapping");
39
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020040 Tokenizer mappingTok(strMapping, ",");
Patrick Benavoli68a91282011-08-31 11:23:23 +020041
42 string strMappingElement;
43
44 while (!(strMappingElement = mappingTok.next()).empty()) {
45
Kevin Rocard7c7dc292012-07-30 15:46:09 +020046 string::size_type iFistDelimiterOccurrence = strMappingElement.find_first_of(':');
Patrick Benavoli68a91282011-08-31 11:23:23 +020047
Kevin Rocard7c7dc292012-07-30 15:46:09 +020048 string strKey, strValue;
49
50 if (iFistDelimiterOccurrence == string::npos) {
51
52 // There is no delimiter in the mapping field,
53 // it means that no value has been provided
54 strKey = strMappingElement;
55 strValue = "";
56
57 } else {
58
59 // Get mapping key
60 strKey = strMappingElement.substr(0, iFistDelimiterOccurrence);
61
62 // Get mapping value
63 strValue = strMappingElement.substr(iFistDelimiterOccurrence + 1);
64
65 }
Patrick Benavoli68a91282011-08-31 11:23:23 +020066
67 if (!addValue(strKey, strValue)) {
68
69 serializingContext.setError("Duplicate Mapping data: Unable to process Mapping element key = " + strKey + ", value = " + strValue + " from XML element " + xmlElement.getPath());
70
71 return false;
72 }
73 }
74 return true;
75}
76
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020077bool CMappingData::getValue(const string& strkey, const string*& pStrValue) const
Patrick Benavoli68a91282011-08-31 11:23:23 +020078{
79 KeyToValueMapConstIterator it = _keyToValueMap.find(strkey);
80
81 if (it != _keyToValueMap.end()) {
82
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020083 pStrValue = &it->second;
Patrick Benavoli68a91282011-08-31 11:23:23 +020084
85 return true;
86 }
87 return false;
88}
89
Frederic Boisnard390b36d2013-05-23 15:28:31 +020090string CMappingData::asString() const
91{
92 string strValue;
93
94 CUtility::asString(_keyToValueMap, strValue, ", ", ":");
95
96 return strValue;
97}
98
Patrick Benavoli68a91282011-08-31 11:23:23 +020099bool CMappingData::addValue(const string& strkey, const string& strValue)
100{
101 if (_keyToValueMap.find(strkey) != _keyToValueMap.end()) {
102
103 return false;
104 }
105 _keyToValueMap[strkey] = strValue;
106
107 return true;
108}
109