UnionDatatypeValidator.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:13k
源码类别:

xml/soap/webservice

开发平台:

C/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.cpp,v 1.5 2001/10/02 18:59:29 peiyongz Exp $
  58.  * $Log: UnionDatatypeValidator.cpp,v $
  59.  * Revision 1.5  2001/10/02 18:59:29  peiyongz
  60.  * Invalid_Facet_Tag to display the tag name
  61.  *
  62.  * Revision 1.4  2001/09/20 13:11:42  knoaman
  63.  * Regx  + misc. fixes
  64.  *
  65.  * Revision 1.3  2001/08/21 18:42:54  peiyongz
  66.  * Bugzilla# 2816: cleanUp() declared with external linkage and called
  67.  *                          before defined as inline
  68.  *
  69.  * Revision 1.2  2001/08/14 22:11:56  peiyongz
  70.  * new exception message added
  71.  *
  72.  * Revision 1.1  2001/07/13 14:10:40  peiyongz
  73.  * UnionDTV
  74.  *
  75.  */
  76. // ---------------------------------------------------------------------------
  77. //  Includes
  78. // ---------------------------------------------------------------------------
  79. #include <validators/datatype/UnionDatatypeValidator.hpp>
  80. #include <validators/datatype/InvalidDatatypeFacetException.hpp>
  81. #include <validators/datatype/InvalidDatatypeValueException.hpp>
  82. #include <util/NumberFormatException.hpp>
  83. static const int BUF_LEN = 64;
  84. static XMLCh value1[BUF_LEN+1];
  85. static XMLCh value2[BUF_LEN+1];
  86. // ---------------------------------------------------------------------------
  87. //  Constructors and Destructor
  88. // ---------------------------------------------------------------------------
  89. UnionDatatypeValidator::UnionDatatypeValidator()
  90. :DatatypeValidator(0, 0, 0, DatatypeValidator::Union)
  91. ,fEnumerationInherited(false)
  92. ,fEnumeration(0)
  93. ,fMemberTypeValidators(0)
  94. {}
  95. UnionDatatypeValidator::~UnionDatatypeValidator()
  96. {
  97.     cleanUp();
  98. }
  99. UnionDatatypeValidator::UnionDatatypeValidator(
  100.                         RefVectorOf<DatatypeValidator>* const memberTypeValidators
  101.                       , const int                                         finalSet)
  102. :DatatypeValidator(0, 0, finalSet, DatatypeValidator::Union)
  103. ,fEnumerationInherited(false)
  104. ,fEnumeration(0)
  105. ,fMemberTypeValidators(0)
  106. {
  107.     if ( !memberTypeValidators )
  108.     {
  109.         ThrowXML(InvalidDatatypeFacetException
  110.                , XMLExcepts::FACET_Union_Null_memberTypeValidators);
  111.     }
  112.     // no pattern, no enumeration
  113.     fMemberTypeValidators = memberTypeValidators;
  114. }
  115. UnionDatatypeValidator::UnionDatatypeValidator(
  116.                           DatatypeValidator*            const baseValidator
  117.                         , RefHashTableOf<KVStringPair>* const facets
  118.                         , RefVectorOf<XMLCh>*           const enums
  119.                         , const int                           finalSet)
  120. :DatatypeValidator(baseValidator, facets, finalSet, DatatypeValidator::Union)
  121. ,fEnumerationInherited(false)
  122. ,fEnumeration(0)
  123. ,fMemberTypeValidators(0)
  124. {
  125.     //
  126.     // baseValidator another UnionDTV from which, 
  127.     // this UnionDTV is derived by restriction.
  128.     // it shall be not null
  129.     //
  130.     if (!baseValidator)
  131.     {
  132.         ThrowXML(InvalidDatatypeFacetException
  133.                , XMLExcepts::FACET_Union_Null_baseValidator);
  134.     }
  135.     if (baseValidator->getType() != DatatypeValidator::Union)
  136.     {
  137.         XMLString::binToText(baseValidator->getType(), value1, BUF_LEN, 10);
  138.         ThrowXML1(InvalidDatatypeFacetException
  139.                 , XMLExcepts::FACET_Union_invalid_baseValidatorType
  140.                 , value1);
  141.     }
  142.     try
  143.     {
  144.         init(baseValidator, facets, enums);
  145.     }
  146.     catch (...)
  147.     {
  148.         cleanUp();
  149.         throw;
  150.     }
  151. }
  152. void UnionDatatypeValidator::init(DatatypeValidator*            const baseValidator
  153.                                 , RefHashTableOf<KVStringPair>* const facets
  154.                                 , RefVectorOf<XMLCh>*           const enums)
  155. {
  156.     if (enums)
  157.         setEnumeration(enums, false);
  158.     // Set Facets if any defined
  159.     if (facets)
  160.     {
  161.         XMLCh* key;
  162.         XMLCh* value;
  163.         RefHashTableOfEnumerator<KVStringPair> e(facets);
  164.         while (e.hasMoreElements())
  165.         {
  166.             KVStringPair pair = e.nextElement();
  167.             key = pair.getKey();
  168.             value = pair.getValue();
  169.             if (XMLString::compareString(key, SchemaSymbols::fgELT_PATTERN)==0)
  170.             {
  171.                 setPattern(value);
  172.                 if (getPattern())
  173.                     setFacetsDefined(DatatypeValidator::FACET_PATTERN);
  174.                 // do not construct regex until needed
  175.             }
  176.             else
  177.             {
  178.                  ThrowXML1(InvalidDatatypeFacetException
  179.                          , XMLExcepts::FACET_Invalid_Tag
  180.                          , key);
  181.             }
  182.         }//while
  183.         /***
  184.            Schema constraint: Part I -- self checking
  185.         ***/
  186.         // Nil
  187.         /***
  188.            Schema constraint: Part II base vs derived checking
  189.         ***/
  190.         // check 4.3.5.c0 must: enumeration values from the value space of base
  191.         if ( ((getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION) != 0) &&
  192.             (getEnumeration() !=0))
  193.         {
  194.             int i = 0;
  195.             int enumLength = getEnumeration()->size();
  196.             try
  197.             {
  198.                 for ( ; i < enumLength; i++)
  199.                 {
  200.                     // ask parent do a complete check
  201.                     //
  202.                     // enum need NOT be passed this->checkContent()
  203.                     // since there are no other facets for Union, parent
  204.                     // checking is good enough.
  205.                     //
  206.                     baseValidator->validate(getEnumeration()->elementAt(i));
  207.                 }
  208.             }
  209.             catch ( XMLException& )
  210.             {
  211.                 ThrowXML1(InvalidDatatypeFacetException
  212.                             , XMLExcepts::FACET_enum_base
  213.                             , getEnumeration()->elementAt(i));
  214.             }
  215.         }
  216.     }// End of Facet setting
  217.     /***
  218.         Inherit facets from base.facets
  219.         The reason of this inheriting (or copying values) is to ease
  220.         schema constraint checking, so that we need NOT trace back to our
  221.         very first base validator in the hierachy. Instead, we are pretty
  222.         sure checking against immediate base validator is enough.  
  223.     ***/
  224.    
  225.     UnionDatatypeValidator *pBaseValidator = (UnionDatatypeValidator*) baseValidator;
  226.     // inherit enumeration
  227.     if (((pBaseValidator->getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION) !=0) &&
  228.         ((getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION) == 0))
  229.     {
  230.         setEnumeration(pBaseValidator->getEnumeration(), true);
  231.     }
  232. }
  233. //
  234. // 1) the bottom level UnionDTV would check against 
  235. //        pattern and enumeration as well
  236. // 2) each UnionDTV(s) above the bottom level UnionDTV and
  237. //        below the native UnionDTV (the top level DTV)
  238. //        would check against pattern only.
  239. // 3) the natvie Union DTV (the top level DTV) would invoke
  240. //        memberTypeValidator to validate
  241. //
  242. void UnionDatatypeValidator::checkContent(const XMLCh* const content, bool asBase)
  243. {
  244.     DatatypeValidator* bv = getBaseValidator();
  245.     if (bv)
  246.         ((UnionDatatypeValidator*)bv)->checkContent(content, true);
  247.     else
  248.     {   // 3) native union type
  249.         // check content against each member type validator in Union
  250.         // report an error only in case content is not valid against all member datatypes.
  251.         //
  252.         bool memTypeValid = false;
  253.         for ( unsigned int i = 0; i < fMemberTypeValidators->size(); ++i )
  254.         {  
  255.             if ( memTypeValid ) 
  256.                 break;
  257.             try 
  258.             {
  259.                 fMemberTypeValidators->elementAt(i)->validate(content);
  260.                 memTypeValid = true;
  261.             }
  262.             catch (XMLException&)
  263.             {
  264.                 //absorbed
  265.             }  
  266.         } // for
  267.         if ( !memTypeValid ) 
  268.         {
  269.             ThrowXML1(InvalidDatatypeValueException
  270.                     , XMLExcepts::VALUE_no_match_memberType
  271.                     , content);
  272.             //( "Content '"+content+"' does not match any union types" );  
  273.         }
  274.     }
  275.     // 1) and 2). we check pattern first
  276.     if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 )
  277.     {
  278.         // lazy construction
  279.         if (getRegex() == 0) 
  280.         {
  281.             try {
  282.                 setRegex(new RegularExpression(getPattern(), SchemaSymbols::fgRegEx_XOption));
  283.             }
  284.             catch (XMLException &e)
  285.             {
  286.                 ThrowXML1(InvalidDatatypeValueException, XMLExcepts::RethrowError, e.getMessage());
  287.             }
  288.         }
  289.         if (getRegex()->matches(content) == false)
  290.         {
  291.             ThrowXML2(InvalidDatatypeValueException
  292.                     , XMLExcepts::VALUE_NotMatch_Pattern
  293.                     , content
  294.                     , getPattern());
  295.         }
  296.     }
  297.     // if this is a base validator, we only need to check pattern facet
  298.     // all other facet were inherited by the derived type
  299.     if (asBase)
  300.         return;
  301.     if ((getFacetsDefined() & DatatypeValidator::FACET_ENUMERATION) != 0 &&
  302.         (getEnumeration() != 0))
  303.     {
  304.         // If the content match (compare equal) any enumeration with 
  305.         // any of the member types, it is considerd valid.
  306.         //
  307.         RefVectorOf<DatatypeValidator>* memberDTV = getMemberTypeValidators();      
  308.         RefVectorOf<XMLCh>* tmpEnum = getEnumeration();
  309.         unsigned int memberTypeNumber = memberDTV->size();
  310.         unsigned int enumLength = tmpEnum->size();
  311.         for ( unsigned int memberIndex = 0; memberIndex < memberTypeNumber; ++memberIndex)
  312.         {           
  313.             for ( unsigned int enumIndex = 0; enumIndex < enumLength; ++enumIndex)
  314.             {  
  315.                 try 
  316.                 {
  317.                     if (memberDTV->elementAt(memberIndex)->compare(content, tmpEnum->elementAt(enumIndex)) == 0)
  318.                         return;
  319.                 }
  320.                 catch (XMLException&)
  321.                 {
  322.                     //absorbed
  323.                 }  
  324.             } // for enumIndex
  325.         } // for memberIndex
  326.         ThrowXML1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, content);
  327.     } // enumeration
  328. }
  329. //
  330. //
  331. //
  332. int UnionDatatypeValidator::compare(const XMLCh* const lValue
  333.                                   , const XMLCh* const rValue)
  334. {
  335.     RefVectorOf<DatatypeValidator>* memberDTV = getMemberTypeValidators();      
  336.     unsigned int memberTypeNumber = memberDTV->size();
  337.     for ( unsigned int memberIndex = 0; memberIndex < memberTypeNumber; ++memberIndex)
  338.     {
  339.         if (memberDTV->elementAt(memberIndex)->compare(lValue, rValue) ==0)
  340.             return  0;
  341.     }
  342.     //REVISIT: what does it mean for UNION1 to be <less than> or <greater than> UNION2 ?    
  343.     // As long as -1 or +1 indicates an unequality, return either of them is ok.
  344.     return -1;        
  345. }
  346. /**
  347.   * End of file UnionDatatypeValidator.cpp
  348.   */