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