UnionDatatypeValidator.hpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:14k
源码类别:

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 2001, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Id: UnionDatatypeValidator.hpp,v 1.9 2003/05/15 18:53:27 knoaman Exp $
  58.  * $Log: UnionDatatypeValidator.hpp,v $
  59.  * Revision 1.9  2003/05/15 18:53:27  knoaman
  60.  * Partial implementation of the configurable memory manager.
  61.  *
  62.  * Revision 1.8  2003/02/06 13:51:55  gareth
  63.  * fixed bug with multiple attributes being validated by the same union type.
  64.  *
  65.  * Revision 1.7  2003/01/29 19:55:19  gareth
  66.  * updated to deal with null pointer issue with fValidatedDatatype.
  67.  *
  68.  * Revision 1.6  2003/01/29 19:53:35  gareth
  69.  * we now store information about which validator was used to validate.
  70.  *
  71.  * Revision 1.5  2003/01/10 16:48:47  tng
  72.  * [Bug 14912] crashes inside UnionDatatypeValidator::isSubstitutableBy.   Patch from Alberto Massari.
  73.  *
  74.  * Revision 1.4  2002/12/18 14:17:55  gareth
  75.  * Fix to bug #13438. When you eant a vector that calls delete[] on its members you should use RefArrayVectorOf.
  76.  *
  77.  * Revision 1.3  2002/11/04 14:53:28  tng
  78.  * C++ Namespace Support.
  79.  *
  80.  * Revision 1.2  2002/02/14 15:17:31  peiyongz
  81.  * getEnumString()
  82.  *
  83.  * Revision 1.1.1.1  2002/02/01 22:22:43  peiyongz
  84.  * sane_include
  85.  *
  86.  * Revision 1.9  2001/12/13 16:48:29  peiyongz
  87.  * Avoid dangling pointer
  88.  *
  89.  * Revision 1.8  2001/09/05 20:49:10  knoaman
  90.  * Fix for complexTypes with mixed content model.
  91.  *
  92.  * Revision 1.7  2001/08/31 16:53:41  knoaman
  93.  * Misc. fixes.
  94.  *
  95.  * Revision 1.6  2001/08/24 17:12:01  knoaman
  96.  * Add support for anySimpleType.
  97.  * Remove parameter 'baseValidator' from the virtual method 'newInstance'.
  98.  *
  99.  * Revision 1.5  2001/08/21 20:05:41  peiyongz
  100.  * put back changes introduced in 1.3
  101.  *
  102.  * Revision 1.3  2001/08/16 14:41:38  knoaman
  103.  * implementation of virtual methods.
  104.  *
  105.  * Revision 1.2  2001/07/24 21:23:40  tng
  106.  * Schema: Use DatatypeValidator for ID/IDREF/ENTITY/ENTITIES/NOTATION.
  107.  *
  108.  * Revision 1.1  2001/07/13 14:10:40  peiyongz
  109.  * UnionDTV
  110.  *
  111.  */
  112. #if !defined(UNION_DATATYPEVALIDATOR_HPP)
  113. #define UNION_DATATYPEVALIDATOR_HPP
  114. #include <xercesc/validators/datatype/DatatypeValidator.hpp>
  115. XERCES_CPP_NAMESPACE_BEGIN
  116. class VALIDATORS_EXPORT UnionDatatypeValidator : public DatatypeValidator
  117. {
  118. public:
  119.     // -----------------------------------------------------------------------
  120.     //  Public ctor/dtor
  121.     // -----------------------------------------------------------------------
  122. /** @name Constructors and Destructor. */
  123.     //@{
  124.     UnionDatatypeValidator
  125.     (
  126.         MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
  127.     );
  128.     //
  129.     // constructor for native Union datatype validator
  130.     // <simpleType name="nativeUnion">
  131.     //      <union   memberTypes="member1 member2 ...">
  132.     // </simpleType>
  133.     //
  134.     UnionDatatypeValidator
  135.     (
  136.         RefVectorOf<DatatypeValidator>* const memberTypeValidators
  137.         , const int finalSet
  138.         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
  139.     );
  140.     //
  141.     // constructor for derived Union datatype validator
  142.     // <simpleType name="derivedUnion">
  143.     //      <restriction base="nativeUnion">
  144.     //          <pattern     value="patter_value"/>
  145.     //          <enumeartion value="enum_value"/>
  146.     //      </restriction>
  147.     // </simpleType>
  148.     //
  149.     UnionDatatypeValidator
  150.     (
  151.         DatatypeValidator* const baseValidator
  152.         , RefHashTableOf<KVStringPair>* const facets
  153.         , RefArrayVectorOf<XMLCh>* const enums
  154.         , const int finalSet
  155.         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
  156.     );
  157.     virtual ~UnionDatatypeValidator();
  158. //@}
  159. virtual const RefArrayVectorOf<XMLCh>* getEnumString() const;
  160.     // -----------------------------------------------------------------------
  161.     // Getter methods
  162.     // -----------------------------------------------------------------------
  163.     /** @name Getter Functions */
  164.     //@{
  165.     /**
  166.       * Returns whether the type is atomic or not
  167.       */
  168.     virtual bool isAtomic() const;
  169.     //@}
  170.     // -----------------------------------------------------------------------
  171.     // Validation methods
  172.     // -----------------------------------------------------------------------
  173.     /** @name Validation Function */
  174.     //@{
  175.     /**
  176.      * validate that a string matches the boolean datatype
  177.      * @param content A string containing the content to be validated
  178.      *
  179.      * @exception throws InvalidDatatypeException if the content is
  180.      * is not valid.
  181.      */
  182. void validate(const XMLCh* const content);
  183.     /**
  184.       * Checks whether a given type can be used as a substitute
  185.       *
  186.       * @param  toCheck    A datatype validator of the type to be used as a
  187.       *                    substitute
  188.       *
  189.       * To be redefined in UnionDatatypeValidator
  190.       */
  191.     virtual bool isSubstitutableBy(const DatatypeValidator* const toCheck);
  192.     //@}
  193.     // -----------------------------------------------------------------------
  194.     // Compare methods
  195.     // -----------------------------------------------------------------------
  196.     /** @name Compare Function */
  197.     //@{
  198.     /**
  199.      * Compare two boolean data types
  200.      *
  201.      * @param content1
  202.      * @param content2
  203.      * @return
  204.      */
  205.     int compare(const XMLCh* const, const XMLCh* const);
  206.     //@}
  207.     /**
  208.       * Returns an instance of the base datatype validator class
  209.   * Used by the DatatypeValidatorFactory.
  210.       */
  211.     virtual DatatypeValidator* newInstance
  212.     (
  213.         RefHashTableOf<KVStringPair>* const facets
  214.         , RefArrayVectorOf<XMLCh>* const enums
  215.         , const int finalSet
  216.         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
  217.     );
  218.     RefVectorOf<DatatypeValidator>* getMemberTypeValidators() const;
  219.     /**
  220.      * Returns the type name that was actually used to validate the last time validate was called
  221.      * note - this does not mean that it fully validated sucessfully
  222.      **/
  223.     const XMLCh* getMemberTypeName() const;
  224.     /**
  225.      * Returns the type uri that was actually used to validate the last time validate was called
  226.      * note - this does not mean that it fully validated sucessfully
  227.      **/
  228.     const XMLCh* getMemberTypeUri() const;
  229.     /**
  230.      * Returns true if the type that was actually used to validate the last time validate was called 
  231.      * is anonymous
  232.      */
  233.     bool getMemberTypeAnonymous() const;
  234.     /**
  235.      * Returns the member DatatypeValidator used to validate the content the last time validate 
  236.      * was called
  237.      */
  238.     const DatatypeValidator* getMemberTypeValidator() const;
  239.     /**
  240.      * Called inbetween uses of this validator to reset PSVI information
  241.      */
  242.     void reset();
  243. private:
  244.     void checkContent(const XMLCh* const content, bool asBase);
  245.     void init(DatatypeValidator*            const baseValidator
  246.             , RefHashTableOf<KVStringPair>* const facets
  247.             , RefArrayVectorOf<XMLCh>*           const enums);
  248.     void cleanUp();
  249.     
  250.     RefArrayVectorOf<XMLCh>*  getEnumeration() const;
  251.     void                 setEnumeration(RefArrayVectorOf<XMLCh>*, bool);
  252.     // -----------------------------------------------------------------------
  253.     //  Private data members
  254.     //
  255.     //  fEnumeration
  256.     //      we own it.
  257.     //
  258.     //  fMemberTypeValidators
  259.     //      we own it.
  260.     //
  261.     //  fValidatedDatatype
  262.     //      the dataTypeValidator  that was actually used to validate the last time validate was called
  263.     //
  264.     // -----------------------------------------------------------------------
  265.      bool                 fEnumerationInherited;
  266.      RefArrayVectorOf<XMLCh>*  fEnumeration;
  267.      RefVectorOf<DatatypeValidator>*  fMemberTypeValidators;
  268.      DatatypeValidator*               fValidatedDatatype;
  269. };
  270. inline DatatypeValidator* UnionDatatypeValidator::newInstance
  271. (
  272.       RefHashTableOf<KVStringPair>* const facets
  273.     , RefArrayVectorOf<XMLCh>* const      enums
  274.     , const int                           finalSet
  275.     , MemoryManager* const                manager
  276. )
  277. {
  278.     return (DatatypeValidator*) new (manager) UnionDatatypeValidator(this, facets, enums, finalSet, manager);
  279. }
  280. inline void UnionDatatypeValidator::validate( const XMLCh* const content)
  281. {
  282.     checkContent(content, false);
  283. }
  284. inline void UnionDatatypeValidator::cleanUp()
  285. {
  286.     //~RefVectorOf will delete all adopted elements
  287.     if ( !fEnumerationInherited && fEnumeration)
  288.         delete fEnumeration;
  289.     if (fMemberTypeValidators)
  290.         delete fMemberTypeValidators;
  291.     
  292. }
  293. inline RefArrayVectorOf<XMLCh>* UnionDatatypeValidator:: getEnumeration() const
  294. {
  295.     return fEnumeration;
  296. }
  297. inline void UnionDatatypeValidator::setEnumeration(RefArrayVectorOf<XMLCh>* enums
  298.                                                  , bool                inherited)
  299. {
  300.     if (enums)
  301.     {
  302.         if (  !fEnumerationInherited && fEnumeration)
  303.             delete fEnumeration;
  304.         fEnumeration = enums;
  305.         fEnumerationInherited = inherited;
  306.         setFacetsDefined(DatatypeValidator::FACET_ENUMERATION);
  307.     }
  308. }
  309. //
  310. // get the native UnionDTV's fMemberTypeValidators
  311. //
  312. inline
  313. RefVectorOf<DatatypeValidator>* UnionDatatypeValidator::getMemberTypeValidators() const
  314. {
  315.     UnionDatatypeValidator* thisdv = (UnionDatatypeValidator*)this; // cast away constness
  316.     while (thisdv->getBaseValidator())
  317.         thisdv = (UnionDatatypeValidator*) thisdv->getBaseValidator();
  318.     return thisdv->fMemberTypeValidators;
  319. }
  320. inline bool UnionDatatypeValidator::isAtomic() const {
  321.     RefVectorOf<DatatypeValidator>* memberDVs = getMemberTypeValidators();
  322.     if (!memberDVs) {
  323.         return false;
  324.     }
  325.     unsigned int memberSize = memberDVs->size();
  326.     for (unsigned int i=0; i < memberSize; i++) {
  327.         if (!memberDVs->elementAt(i)->isAtomic()) {
  328.             return false;
  329.         }
  330.     }
  331.     return true;
  332. }
  333. inline bool UnionDatatypeValidator::isSubstitutableBy(const DatatypeValidator* const toCheck) {
  334.     if (toCheck == this) {
  335.         return true;
  336.     }
  337.     if (fMemberTypeValidators) {
  338.         unsigned int memberSize = fMemberTypeValidators->size();
  339.         for (unsigned int i=0; i < memberSize; i++) {
  340.             if (fMemberTypeValidators->elementAt(i)->isSubstitutableBy(toCheck)) {
  341.                 return true;
  342.             }
  343.         }
  344.     }
  345.     return false;
  346. }
  347. inline const XMLCh* UnionDatatypeValidator::getMemberTypeName() const {
  348.     if(fValidatedDatatype) {
  349.         return fValidatedDatatype->getTypeLocalName();
  350.     }
  351.     return 0;
  352. }
  353. inline const XMLCh* UnionDatatypeValidator::getMemberTypeUri() const 
  354. {
  355.     if(fValidatedDatatype) {
  356.         return fValidatedDatatype->getTypeUri();
  357.     }
  358.     return 0;
  359. }
  360. inline bool UnionDatatypeValidator::getMemberTypeAnonymous() const {
  361.     if(fValidatedDatatype) {
  362.         return fValidatedDatatype->getAnonymous();
  363.     }
  364.     return 0;
  365. }
  366. inline const DatatypeValidator* UnionDatatypeValidator::getMemberTypeValidator() const {
  367.     return fValidatedDatatype;
  368. }
  369. inline void UnionDatatypeValidator::reset() {
  370.     fValidatedDatatype = 0;
  371. }
  372. XERCES_CPP_NAMESPACE_END
  373. #endif
  374. /**
  375.   * End of file UnionDatatypeValidator.hpp
  376.   */