blob: 4b5956c0688f189b01c0276f3e5dc5ecca58e7ba [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "SelectionCriterionRule.h"
32#include "SelectionCriterion.h"
33#include "XmlDomainSerializingContext.h"
34#include "SelectionCriteriaDefinition.h"
35#include <assert.h>
36
37#define base CRule
38
39const char* CSelectionCriterionRule::_apcMatchesWhen[CSelectionCriterionRule::ENbMatchesWhen] = {
40 "Is",
41 "Contains"
42};
43
44CSelectionCriterionRule::CSelectionCriterionRule() : _pSelectionCriterion(NULL), _eMatchesWhen(CSelectionCriterionRule::EIs), _iMatchValue(0)
45{
46}
47
48// Class kind
49string CSelectionCriterionRule::getKind() const
50{
51 return "SelectionCriterionRule";
52}
53
54// Rule check
55bool CSelectionCriterionRule::matches() const
56{
57 assert(_pSelectionCriterion);
58
59 switch(_eMatchesWhen) {
60 case EIs:
61 return _pSelectionCriterion->equals(_iMatchValue);
62 case EContains:
63 return _pSelectionCriterion->contains(_iMatchValue);
64 default:
65 assert(0);
66 return false;
67 }
68}
69
70// From IXmlSink
71bool CSelectionCriterionRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
72{
73 // Retrieve actual context
74 CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext);
75
76 // Get selection criterion
77 string strSelectionCriterion = xmlElement.getAttributeString("SelectionCriterion");
78
79 _pSelectionCriterion = xmlDomainSerializingContext.getSelectionCriteriaDefinition()->getSelectionCriterion(strSelectionCriterion);
80
81 // Check existence
82 if (!_pSelectionCriterion) {
83
84 xmlDomainSerializingContext.setError("Couldn't find selection criterion " + strSelectionCriterion + " in " + getKind() + " " + xmlElement.getPath());
85
86 return false;
87 }
88
89 // Get MatchesWhen
90 string strMatchesWhen = xmlElement.getAttributeString("MatchesWhen");
91
92 if (!setMatchesWhen(strMatchesWhen)) {
93
94 xmlDomainSerializingContext.setError("Wrong MatchesWhen attribute " + strMatchesWhen + " in " + getKind() + " " + xmlElement.getPath());
95
96 return false;
97 }
98
99 // Get Value
100 string strValue = xmlElement.getAttributeString("Value");
101
102 if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) {
103
104 xmlDomainSerializingContext.setError("Wrong Value attribute value " + strValue + " in " + getKind() + " " + xmlElement.getPath());
105
106 return false;
107 }
108
109 // Done
110 return true;
111}
112
113// From IXmlSource
114void CSelectionCriterionRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
115{
116 (void)serializingContext;
117
118 assert(_pSelectionCriterion);
119
120 // Set selection criterion
121 xmlElement.setAttributeString("SelectionCriterion", _pSelectionCriterion->getName());
122
123 // Set MatchesWhen
124 xmlElement.setAttributeString("MatchesWhen", _apcMatchesWhen[_eMatchesWhen]);
125
126 // Set Value
127 string strValue;
128
129 _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue);
130
131 xmlElement.setAttributeString("Value", strValue);
132}
133
134// XML MatchesWhen attribute parsing
135bool CSelectionCriterionRule::setMatchesWhen(const string& strMatchesWhen)
136{
137 uint32_t uiMatchesWhen;
138
139 for (uiMatchesWhen = 0; uiMatchesWhen < ENbMatchesWhen; uiMatchesWhen++) {
140
141 if (strMatchesWhen == _apcMatchesWhen[uiMatchesWhen]) {
142
143 // Found it!
144 _eMatchesWhen = (MatchesWhen)uiMatchesWhen;
145
146 return true;
147 }
148 }
149 return false;
150}