| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1 | /* <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" |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 35 | #include "SelectionCriterionTypeInterface.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 36 | #include <assert.h> |
| 37 | |
| 38 | #define base CRule |
| 39 | |
| Patrick Benavoli | 592ae56 | 2011-09-05 16:53:58 +0200 | [diff] [blame] | 40 | const CSelectionCriterionRule::SMatchingRuleDescription CSelectionCriterionRule::_astMatchesWhen[CSelectionCriterionRule::ENbMatchesWhen] = { |
| Patrick Benavoli | 082dd47 | 2011-11-07 19:46:00 +0100 | [diff] [blame^] | 41 | { "Is", true }, |
| 42 | { "IsNot", true }, |
| 43 | { "Includes", false }, |
| 44 | { "Excludes", false } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | CSelectionCriterionRule::CSelectionCriterionRule() : _pSelectionCriterion(NULL), _eMatchesWhen(CSelectionCriterionRule::EIs), _iMatchValue(0) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | // Class kind |
| 52 | string CSelectionCriterionRule::getKind() const |
| 53 | { |
| 54 | return "SelectionCriterionRule"; |
| 55 | } |
| 56 | |
| 57 | // Rule check |
| 58 | bool CSelectionCriterionRule::matches() const |
| 59 | { |
| 60 | assert(_pSelectionCriterion); |
| 61 | |
| 62 | switch(_eMatchesWhen) { |
| 63 | case EIs: |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 64 | return _pSelectionCriterion->is(_iMatchValue); |
| 65 | case EIsNot: |
| 66 | return _pSelectionCriterion->isNot(_iMatchValue); |
| 67 | case EIncludes: |
| 68 | return _pSelectionCriterion->includes(_iMatchValue); |
| 69 | case EExcludes: |
| 70 | return _pSelectionCriterion->excludes(_iMatchValue); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 71 | default: |
| 72 | assert(0); |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // From IXmlSink |
| 78 | bool CSelectionCriterionRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 79 | { |
| 80 | // Retrieve actual context |
| 81 | CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext); |
| 82 | |
| 83 | // Get selection criterion |
| 84 | string strSelectionCriterion = xmlElement.getAttributeString("SelectionCriterion"); |
| 85 | |
| 86 | _pSelectionCriterion = xmlDomainSerializingContext.getSelectionCriteriaDefinition()->getSelectionCriterion(strSelectionCriterion); |
| 87 | |
| 88 | // Check existence |
| 89 | if (!_pSelectionCriterion) { |
| 90 | |
| 91 | xmlDomainSerializingContext.setError("Couldn't find selection criterion " + strSelectionCriterion + " in " + getKind() + " " + xmlElement.getPath()); |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // Get MatchesWhen |
| 97 | string strMatchesWhen = xmlElement.getAttributeString("MatchesWhen"); |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 98 | string strError; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 99 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 100 | if (!setMatchesWhen(strMatchesWhen, strError)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 101 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 102 | xmlDomainSerializingContext.setError("Wrong MatchesWhen attribute " + strMatchesWhen + " in " + getKind() + " " + xmlElement.getPath() + ": " + strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Get Value |
| 108 | string strValue = xmlElement.getAttributeString("Value"); |
| 109 | |
| 110 | if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) { |
| 111 | |
| 112 | xmlDomainSerializingContext.setError("Wrong Value attribute value " + strValue + " in " + getKind() + " " + xmlElement.getPath()); |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Done |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | // From IXmlSource |
| 122 | void CSelectionCriterionRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 123 | { |
| 124 | (void)serializingContext; |
| 125 | |
| 126 | assert(_pSelectionCriterion); |
| 127 | |
| 128 | // Set selection criterion |
| 129 | xmlElement.setAttributeString("SelectionCriterion", _pSelectionCriterion->getName()); |
| 130 | |
| 131 | // Set MatchesWhen |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 132 | xmlElement.setAttributeString("MatchesWhen", _astMatchesWhen[_eMatchesWhen].pcMatchesWhen); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 133 | |
| 134 | // Set Value |
| 135 | string strValue; |
| 136 | |
| 137 | _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue); |
| 138 | |
| 139 | xmlElement.setAttributeString("Value", strValue); |
| 140 | } |
| 141 | |
| 142 | // XML MatchesWhen attribute parsing |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 143 | bool CSelectionCriterionRule::setMatchesWhen(const string& strMatchesWhen, string& strError) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 144 | { |
| 145 | uint32_t uiMatchesWhen; |
| 146 | |
| 147 | for (uiMatchesWhen = 0; uiMatchesWhen < ENbMatchesWhen; uiMatchesWhen++) { |
| 148 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 149 | const SMatchingRuleDescription* pstMatchingRuleDescription = &_astMatchesWhen[uiMatchesWhen]; |
| 150 | |
| 151 | if (strMatchesWhen == pstMatchingRuleDescription->pcMatchesWhen) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 152 | |
| 153 | // Found it! |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 154 | |
| 155 | // Get Type |
| 156 | const ISelectionCriterionTypeInterface* pSelectionCriterionType = _pSelectionCriterion->getCriterionType(); |
| 157 | |
| 158 | // Check compatibility if relevant |
| Patrick Benavoli | 082dd47 | 2011-11-07 19:46:00 +0100 | [diff] [blame^] | 159 | if (!pSelectionCriterionType->isTypeInclusive() && !pstMatchingRuleDescription->bExclusiveTypeCompatible) { |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 160 | |
| Patrick Benavoli | 082dd47 | 2011-11-07 19:46:00 +0100 | [diff] [blame^] | 161 | strError = "Value incompatible with exclusive kind of type"; |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 162 | |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | // Store |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 167 | _eMatchesWhen = (MatchesWhen)uiMatchesWhen; |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | } |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 172 | |
| 173 | strError = "Value not found"; |
| 174 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 175 | return false; |
| 176 | } |