abstractstringvalidator.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 400 行

CPP
400
字号
/* * Copyright 2001,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Log: AbstractStringValidator.cpp,v $ * Revision 1.23  2004/09/08 13:56:52  peiyongz * Apache License Version 2.0 * * Revision 1.22  2004/01/29 11:51:22  cargilld * Code cleanup changes to get rid of various compiler diagnostic messages. * * Revision 1.21  2004/01/13 21:18:18  peiyongz * revert code back to previous version * * Revision 1.20  2004/01/12 16:25:09  neilg * remove use of static buffers * * Revision 1.19  2004/01/06 18:13:59  peiyongz * using the no-exception-thrown ctor * * Revision 1.18  2003/12/31 10:38:00  amassari * Made virtual function checkAdditionalFacet 'const', so that it matches the declaration in a derived class * * Revision 1.17  2003/12/17 00:18:38  cargilld * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data. * * Revision 1.16  2003/11/12 20:32:03  peiyongz * Statless Grammar: ValidationContext * * Revision 1.15  2003/10/17 21:13:43  peiyongz * using XTemplateSerializer * * Revision 1.14  2003/10/07 19:39:37  peiyongz * Use of Template_Class Object Serialization/Deserialization API * * Revision 1.13  2003/09/29 21:47:35  peiyongz * Implementation of Serialization/Deserialization * * Revision 1.12  2003/05/16 06:01:57  knoaman * Partial implementation of the configurable memory manager. * * Revision 1.11  2003/05/15 18:53:26  knoaman * Partial implementation of the configurable memory manager. * * Revision 1.10  2003/02/22 18:28:26  peiyongz * Schema Errata E2-35 Length, minLength and maxLength in different derivation steps. * * Revision 1.9  2003/01/27 19:24:17  peiyongz * normalize Base64 data before checking against enumeration. * * Revision 1.8  2003/01/24 23:18:34  peiyongz * normalizeEnumeration() added to remove optional ws in Base64 data. * * Revision 1.7  2002/12/18 14:17:55  gareth * Fix to bug #13438. When you eant a vector that calls delete[] on its members you should use RefArrayVectorOf. * * Revision 1.6  2002/11/04 14:53:27  tng * C++ Namespace Support. * * Revision 1.5  2002/10/02 13:29:12  tng * Since the compare function return int, so use XMLString::compareString instead of XMLString::equals there. * * Revision 1.4  2002/09/24 19:44:40  tng * Performance: use XMLString::equals instead of XMLString::compareString * * Revision 1.3  2002/04/01 20:17:46  peiyongz * Bug#7551: Exceptions are caught by value, rather than by reference * * Revision 1.2  2002/02/14 15:17:31  peiyongz * getEnumString() * * Revision 1.1.1.1  2002/02/01 22:22:39  peiyongz * sane_include * * Revision 1.7  2001/12/13 16:48:29  peiyongz * Avoid dangling pointer * * Revision 1.6  2001/10/15 20:57:27  tng * Schema: we should propagate the exception thrown from checkContent. * * Revision 1.5  2001/10/09 21:00:54  peiyongz * . init() take 1 arg, * . make inspectFacetBase() virtual to allow ListDTV provide its own method, * . reorganize init() into assignFacet(), inspectFacet(), inspectFacetBase() and * inheritFacet() to improve mantainability. * . macro to simplify code * . save get***() to temp vars * * Revision 1.4  2001/09/24 15:30:16  peiyongz * DTV Reorganization: init() to be invoked from derived class' ctor to allow *        correct resolution of virtual methods like assignAdditionalFacet(), *        inheritAdditionalFacet(), etc. * * Revision 1.3  2001/09/19 18:48:27  peiyongz * DTV reorganization:getLength() added, move inline to class declaration to avoid inline * function interdependency. * * Revision 1.2  2001/09/18 21:16:42  peiyongz * DTV reorganization: temp vars to replace repeated invocation of getFacetsDefined() * * Revision 1.1  2001/09/18 14:45:04  peiyongz * DTV reorganization * * */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/validators/datatype/AbstractStringValidator.hpp>#include <xercesc/validators/datatype/InvalidDatatypeFacetException.hpp>#include <xercesc/validators/datatype/InvalidDatatypeValueException.hpp>#include <xercesc/util/NumberFormatException.hpp>#include <xercesc/internal/XTemplateSerializer.hpp>XERCES_CPP_NAMESPACE_BEGINstatic const int BUF_LEN = 64;#define  REPORT_FACET_ERROR(val1, val2, except_code, manager)    \    XMLCh value1[BUF_LEN+1]; \    XMLCh value2[BUF_LEN+1]; \   XMLString::binToText(val1, value1, BUF_LEN, 10, manager);     \   XMLString::binToText(val2, value2, BUF_LEN, 10, manager);     \   ThrowXMLwithMemMgr2(InvalidDatatypeFacetException             \           , except_code                                \           , value1                                     \           , value2                                     \           , manager);#define  REPORT_VALUE_ERROR(data, val1, val2, except_code, manager)      \    XMLCh value1[BUF_LEN+1]; \    XMLCh value2[BUF_LEN+1]; \   XMLString::binToText(val1, value1, BUF_LEN, 10, manager);             \   XMLString::binToText(val2, value2, BUF_LEN, 10, manager);             \   ThrowXMLwithMemMgr3(InvalidDatatypeValueException                     \           , except_code                                        \           , data                                               \           , value1                                             \           , value2                                             \           , manager);// ---------------------------------------------------------------------------//  Constructors and Destructor// ---------------------------------------------------------------------------AbstractStringValidator::~AbstractStringValidator(){    //~RefVectorOf will delete all adopted elements    if ( !fEnumerationInherited && fEnumeration)    {        delete fEnumeration;        fEnumeration = 0;    }}AbstractStringValidator::AbstractStringValidator(                          DatatypeValidator*            const baseValidator                        , RefHashTableOf<KVStringPair>* const facets                        , const int                           finalSet                        , const ValidatorType                 type                        , MemoryManager* const                manager):DatatypeValidator(baseValidator, facets, finalSet, type, manager),fLength(0),fMaxLength(SchemaSymbols::fgINT_MAX_VALUE),fMinLength(0),fEnumerationInherited(false),fEnumeration(0){    // init() is invoked from derived class's ctor instead of from    // here to allow correct resolution of virutal method, such as    // assigneAdditionalFacet(), inheritAdditionalFacet().}void AbstractStringValidator::init(RefArrayVectorOf<XMLCh>*           const enums                                   ,MemoryManager*                    const manager){    if (enums)    {        setEnumeration(enums, false);        normalizeEnumeration(manager);    }    assignFacet(manager);    inspectFacet(manager);    inspectFacetBase(manager);    inheritFacet();}////   Assign facets//        assign common facets//        assign additional facet//void AbstractStringValidator::assignFacet(MemoryManager* const manager){    RefHashTableOf<KVStringPair>* facets = getFacets();    if (!facets)        return;    XMLCh* key;    XMLCh* value;    RefHashTableOfEnumerator<KVStringPair> e(facets, false, manager);    while (e.hasMoreElements())    {        KVStringPair pair = e.nextElement();        key = pair.getKey();        value = pair.getValue();        if (XMLString::equals(key, SchemaSymbols::fgELT_LENGTH))        {            int val;            try            {                val = XMLString::parseInt(value, manager);            }            catch (NumberFormatException&)            {                ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::FACET_Invalid_Len, value, manager);            }            if ( val < 0 )                ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::FACET_NonNeg_Len, value, manager);            setLength(val);            setFacetsDefined(DatatypeValidator::FACET_LENGTH);        }        else if (XMLString::equals(key, SchemaSymbols::fgELT_MINLENGTH))        {            int val;            try            {                val = XMLString::parseInt(value, manager);            }            catch (NumberFormatException&)            {                ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::FACET_Invalid_minLen, value, manager);            }            if ( val < 0 )                ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::FACET_NonNeg_minLen, value, manager);            setMinLength(val);            setFacetsDefined(DatatypeValidator::FACET_MINLENGTH);        }        else if (XMLString::equals(key, SchemaSymbols::fgELT_MAXLENGTH))        {            int val;            try            {                val = XMLString::parseInt(value, manager);            }            catch (NumberFormatException&)            {                ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::FACET_Invalid_maxLen, value, manager);            }            if ( val < 0 )                ThrowXMLwithMemMgr1(InvalidDatatypeFacetException, XMLExcepts::FACET_NonNeg_maxLen, value, manager);            setMaxLength(val);            setFacetsDefined(DatatypeValidator::FACET_MAXLENGTH);        }        else if (XMLString::equals(key, SchemaSymbols::fgELT_PATTERN))        {            setPattern(value);            if (getPattern())                setFacetsDefined(DatatypeValidator::FACET_PATTERN);            // do not construct regex until needed        }        else if (XMLString::equals(key, SchemaSymbols::fgATT_FIXED))        {            unsigned int val;            bool         retStatus;            try            {                retStatus = XMLString::textToBin(value, val, fMemoryManager);            }            catch (RuntimeException&)            {                ThrowXMLwithMemMgr(InvalidDatatypeFacetException, XMLExcepts::FACET_internalError_fixed, manager);            }            if (!retStatus)            {                ThrowXMLwithMemMgr(InvalidDatatypeFacetException, XMLExcepts::FACET_internalError_fixed, manager);            }            setFixed(val);            //no setFacetsDefined here        }        //        // else if (XMLString::equals(key, SchemaSymbols::fgELT_SPECIAL_TOKEN))        // TODO        //        // Note: whitespace is taken care of by TraverseSchema.        //        else        {            assignAdditionalFacet(key, value, manager);        }    }//while}//end of assigneFacet()//// Check facet among self//         check common facets//         check Additional Facet Constraint//void AbstractStringValidator::inspectFacet(MemoryManager* const manager){    int thisFacetsDefined = getFacetsDefined();    if (!thisFacetsDefined)        return;    // check 4.3.1.c1 error: length & (maxLength | minLength)    if ((thisFacetsDefined & DatatypeValidator::FACET_LENGTH) != 0)    {        if ((thisFacetsDefined & DatatypeValidator::FACET_MAXLENGTH) != 0)            ThrowXMLwithMemMgr(InvalidDatatypeFacetException, XMLExcepts::FACET_Len_maxLen, manager);        else if (((thisFacetsDefined & DatatypeValidator::FACET_MINLENGTH) != 0))            ThrowXMLwithMemMgr(InvalidDatatypeFacetException, XMLExcepts::FACET_Len_minLen, manager);    }    // check 4.3.2.c1 must: minLength <= maxLength    if ((thisFacetsDefined & (DatatypeValidator::FACET_MINLENGTH        |DatatypeValidator::FACET_MAXLENGTH)) != 0)    {        int thisMinLength = getMinLength();        int thisMaxLength = getMaxLength();        if ( thisMinLength > thisMaxLength )        {            REPORT_FACET_ERROR(thisMaxLength                             , thisMinLength                             , XMLExcepts::FACET_maxLen_minLen                             , manager)        }    }}// end of inspectFacet()////  Check vs base//         check common facets//         check enumeration//         check Additional Facet Constraint//void AbstractStringValidator::inspectFacetBase(MemoryManager* const manager){    AbstractStringValidator *pBaseValidator = (AbstractStringValidator*) getBaseValidator();    int thisFacetsDefined = getFacetsDefined();    if ( (!thisFacetsDefined && !fEnumeration) ||         (!pBaseValidator)                      )        return;    int baseFacetsDefined = pBaseValidator->getFacetsDefined();    int thisLength    = getLength();    int thisMinLength = getMinLength();    int thisMaxLength = getMaxLength();    int baseLength    = pBaseValidator->getLength();    int baseMinLength = pBaseValidator->getMinLength();    int baseMaxLength = pBaseValidator->getMaxLength();    int baseFixed     = pBaseValidator->getFixed();    /***       check facets against base.facets       Note: later we need to check the "fix" option of the base type            and apply that to every individual facet.    ***/    /***                Non coexistence of derived' length and base'    (minLength | maxLength)                                   base'    length and derived' (minLength | maxLength)     E2-35     It is an 積rror

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?