| 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 "CompoundRule.h" |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 31 | #include "RuleParser.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 32 | |
| 33 | #define base CRule |
| 34 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 35 | // Types |
| 36 | const char* CCompoundRule::_apcTypes[2] = { |
| 37 | "Any", |
| 38 | "All" |
| 39 | }; |
| 40 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 41 | CCompoundRule::CCompoundRule() : _bTypeAll(false) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | // Class kind |
| 46 | string CCompoundRule::getKind() const |
| 47 | { |
| 48 | return "CompoundRule"; |
| 49 | } |
| 50 | |
| 51 | // Returns true if children dynamic creation is to be dealt with |
| 52 | bool CCompoundRule::childrenAreDynamic() const |
| 53 | { |
| 54 | return true; |
| 55 | } |
| 56 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 57 | // Content dumping |
| 58 | void CCompoundRule::logValue(string& strValue, CErrorContext& errorContext) const |
| 59 | { |
| 60 | (void)errorContext; |
| 61 | |
| 62 | // Type |
| 63 | strValue = _apcTypes[_bTypeAll]; |
| 64 | } |
| 65 | |
| 66 | // Parse |
| 67 | bool CCompoundRule::parse(CRuleParser& ruleParser, string& strError) |
| 68 | { |
| 69 | // Get rule type |
| 70 | uint32_t uiType; |
| 71 | |
| 72 | for (uiType = 0; uiType < 2; uiType++) { |
| 73 | |
| 74 | if (ruleParser.getType() == _apcTypes[uiType]) { |
| 75 | |
| 76 | // Set type |
| 77 | _bTypeAll = uiType != 0; |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Failed |
| 84 | strError = "Unknown compound rule type: "; |
| 85 | strError += ruleParser.getType(); |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // Dump |
| 91 | void CCompoundRule::dump(string& strResult) const |
| 92 | { |
| 93 | strResult += _apcTypes[_bTypeAll]; |
| 94 | strResult += "{"; |
| 95 | |
| 96 | // Children |
| 97 | uint32_t uiChild; |
| 98 | uint32_t uiNbChildren = getNbChildren(); |
| 99 | bool bFirst = true; |
| 100 | |
| 101 | for (uiChild = 0; uiChild < uiNbChildren; uiChild++) { |
| 102 | |
| 103 | if (!bFirst) { |
| 104 | |
| 105 | strResult += ", "; |
| 106 | } |
| 107 | |
| 108 | // Dump inner rule |
| 109 | const CRule* pRule = static_cast<const CRule*>(getChild(uiChild)); |
| 110 | |
| 111 | pRule->dump(strResult); |
| 112 | |
| 113 | bFirst = false; |
| 114 | } |
| 115 | |
| 116 | strResult += "}"; |
| 117 | } |
| 118 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 119 | // Rule check |
| 120 | bool CCompoundRule::matches() const |
| 121 | { |
| 122 | uint32_t uiChild; |
| 123 | uint32_t uiNbChildren = getNbChildren(); |
| 124 | |
| 125 | for (uiChild = 0; uiChild < uiNbChildren; uiChild++) { |
| 126 | |
| 127 | const CRule* pRule = static_cast<const CRule*>(getChild(uiChild)); |
| 128 | |
| 129 | if (pRule->matches() ^ _bTypeAll) { |
| 130 | |
| 131 | return !_bTypeAll; |
| 132 | } |
| 133 | } |
| 134 | return _bTypeAll; |
| 135 | } |
| 136 | |
| 137 | // From IXmlSink |
| 138 | bool CCompoundRule::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) |
| 139 | { |
| 140 | // Get type |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 141 | _bTypeAll = xmlElement.getAttributeBoolean("Type", _apcTypes[true]); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 142 | |
| 143 | // Base |
| 144 | return base::fromXml(xmlElement, serializingContext); |
| 145 | } |
| 146 | |
| 147 | // From IXmlSource |
| 148 | void CCompoundRule::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const |
| 149 | { |
| 150 | // Set type |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 151 | xmlElement.setAttributeString("Type", _apcTypes[_bTypeAll]); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 152 | |
| 153 | // Base |
| 154 | base::toXml(xmlElement, serializingContext); |
| 155 | } |