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