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