| David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame^] | 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. |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "SelectionCriterionRule.h" |
| 31 | #include "SelectionCriterion.h" |
| 32 | #include "XmlDomainSerializingContext.h" |
| 33 | #include "SelectionCriteriaDefinition.h" |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 34 | #include "SelectionCriterionTypeInterface.h" |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 35 | #include "RuleParser.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 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 57 | // Content dumping |
| 58 | void CSelectionCriterionRule::logValue(string& strValue, CErrorContext& errorContext) const |
| 59 | { |
| 60 | (void)errorContext; |
| 61 | |
| 62 | // Dump rule |
| 63 | dump(strValue); |
| 64 | } |
| 65 | |
| 66 | // Parse |
| 67 | bool CSelectionCriterionRule::parse(CRuleParser& ruleParser, string& strError) |
| 68 | { |
| 69 | // Criterion |
| 70 | _pSelectionCriterion = ruleParser.getSelectionCriteriaDefinition()->getSelectionCriterion(ruleParser.getType()); |
| 71 | |
| 72 | // Check existence |
| 73 | if (!_pSelectionCriterion) { |
| 74 | |
| 75 | strError = "Couldn't find selection criterion " + ruleParser.getType(); |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Verb |
| 81 | string strMatchesWhen; |
| 82 | |
| 83 | if (!ruleParser.next(strMatchesWhen, strError)) { |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | // Value |
| 88 | string strValue; |
| 89 | |
| 90 | if (!ruleParser.next(strValue, strError)) { |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // Matches when |
| 96 | if (!setMatchesWhen(strMatchesWhen, strError)) { |
| 97 | |
| 98 | strError = "Verb error: " + strError; |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Value |
| 104 | if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) { |
| 105 | |
| 106 | strError = "Value error: " + strError; |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | // Dump |
| 115 | void CSelectionCriterionRule::dump(string& strResult) const |
| 116 | { |
| 117 | // Criterion |
| 118 | strResult += _pSelectionCriterion->getName(); |
| 119 | strResult += " "; |
| 120 | // Verb |
| 121 | strResult += _astMatchesWhen[_eMatchesWhen].pcMatchesWhen; |
| 122 | strResult += " "; |
| 123 | // Value |
| 124 | string strValue; |
| 125 | _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue); |
| 126 | strResult += strValue; |
| 127 | } |
| 128 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 129 | // Rule check |
| 130 | bool CSelectionCriterionRule::matches() const |
| 131 | { |
| 132 | assert(_pSelectionCriterion); |
| 133 | |
| 134 | switch(_eMatchesWhen) { |
| 135 | case EIs: |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 136 | return _pSelectionCriterion->is(_iMatchValue); |
| 137 | case EIsNot: |
| 138 | return _pSelectionCriterion->isNot(_iMatchValue); |
| 139 | case EIncludes: |
| 140 | return _pSelectionCriterion->includes(_iMatchValue); |
| 141 | case EExcludes: |
| 142 | return _pSelectionCriterion->excludes(_iMatchValue); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 143 | default: |
| 144 | assert(0); |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // From IXmlSink |
| 150 | bool CSelectionCriterionRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 151 | { |
| 152 | // Retrieve actual context |
| 153 | CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext); |
| 154 | |
| 155 | // Get selection criterion |
| 156 | string strSelectionCriterion = xmlElement.getAttributeString("SelectionCriterion"); |
| 157 | |
| 158 | _pSelectionCriterion = xmlDomainSerializingContext.getSelectionCriteriaDefinition()->getSelectionCriterion(strSelectionCriterion); |
| 159 | |
| 160 | // Check existence |
| 161 | if (!_pSelectionCriterion) { |
| 162 | |
| 163 | xmlDomainSerializingContext.setError("Couldn't find selection criterion " + strSelectionCriterion + " in " + getKind() + " " + xmlElement.getPath()); |
| 164 | |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | // Get MatchesWhen |
| 169 | string strMatchesWhen = xmlElement.getAttributeString("MatchesWhen"); |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 170 | string strError; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 171 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 172 | if (!setMatchesWhen(strMatchesWhen, strError)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 173 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 174 | xmlDomainSerializingContext.setError("Wrong MatchesWhen attribute " + strMatchesWhen + " in " + getKind() + " " + xmlElement.getPath() + ": " + strError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | // Get Value |
| 180 | string strValue = xmlElement.getAttributeString("Value"); |
| 181 | |
| 182 | if (!_pSelectionCriterion->getCriterionType()->getNumericalValue(strValue, _iMatchValue)) { |
| 183 | |
| 184 | xmlDomainSerializingContext.setError("Wrong Value attribute value " + strValue + " in " + getKind() + " " + xmlElement.getPath()); |
| 185 | |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Done |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | // From IXmlSource |
| 194 | void CSelectionCriterionRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 195 | { |
| 196 | (void)serializingContext; |
| 197 | |
| 198 | assert(_pSelectionCriterion); |
| 199 | |
| 200 | // Set selection criterion |
| 201 | xmlElement.setAttributeString("SelectionCriterion", _pSelectionCriterion->getName()); |
| 202 | |
| 203 | // Set MatchesWhen |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 204 | xmlElement.setAttributeString("MatchesWhen", _astMatchesWhen[_eMatchesWhen].pcMatchesWhen); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 205 | |
| 206 | // Set Value |
| 207 | string strValue; |
| 208 | |
| 209 | _pSelectionCriterion->getCriterionType()->getLiteralValue(_iMatchValue, strValue); |
| 210 | |
| 211 | xmlElement.setAttributeString("Value", strValue); |
| 212 | } |
| 213 | |
| 214 | // XML MatchesWhen attribute parsing |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 215 | bool CSelectionCriterionRule::setMatchesWhen(const string& strMatchesWhen, string& strError) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 216 | { |
| 217 | uint32_t uiMatchesWhen; |
| 218 | |
| 219 | for (uiMatchesWhen = 0; uiMatchesWhen < ENbMatchesWhen; uiMatchesWhen++) { |
| 220 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 221 | const SMatchingRuleDescription* pstMatchingRuleDescription = &_astMatchesWhen[uiMatchesWhen]; |
| 222 | |
| 223 | if (strMatchesWhen == pstMatchingRuleDescription->pcMatchesWhen) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 224 | |
| 225 | // Found it! |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 226 | |
| 227 | // Get Type |
| 228 | const ISelectionCriterionTypeInterface* pSelectionCriterionType = _pSelectionCriterion->getCriterionType(); |
| 229 | |
| 230 | // Check compatibility if relevant |
| Patrick Benavoli | 082dd47 | 2011-11-07 19:46:00 +0100 | [diff] [blame] | 231 | if (!pSelectionCriterionType->isTypeInclusive() && !pstMatchingRuleDescription->bExclusiveTypeCompatible) { |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 232 | |
| Patrick Benavoli | 082dd47 | 2011-11-07 19:46:00 +0100 | [diff] [blame] | 233 | strError = "Value incompatible with exclusive kind of type"; |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // Store |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 239 | _eMatchesWhen = (MatchesWhen)uiMatchesWhen; |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | } |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 244 | |
| 245 | strError = "Value not found"; |
| 246 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 247 | return false; |
| 248 | } |