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

词法分析

开发平台:

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: DoubleDatatypeValidator.cpp,v 1.6 2003/05/18 14:02:07 knoaman Exp $
  58.  * $Log: DoubleDatatypeValidator.cpp,v $
  59.  * Revision 1.6  2003/05/18 14:02:07  knoaman
  60.  * Memory manager implementation: pass per instance manager.
  61.  *
  62.  * Revision 1.5  2003/05/16 06:01:57  knoaman
  63.  * Partial implementation of the configurable memory manager.
  64.  *
  65.  * Revision 1.4  2003/05/15 18:53:26  knoaman
  66.  * Partial implementation of the configurable memory manager.
  67.  *
  68.  * Revision 1.3  2002/12/18 14:17:55  gareth
  69.  * Fix to bug #13438. When you eant a vector that calls delete[] on its members you should use RefArrayVectorOf.
  70.  *
  71.  * Revision 1.2  2002/11/04 14:53:28  tng
  72.  * C++ Namespace Support.
  73.  *
  74.  * Revision 1.1.1.1  2002/02/01 22:22:41  peiyongz
  75.  * sane_include
  76.  *
  77.  * Revision 1.6  2001/10/02 18:59:29  peiyongz
  78.  * Invalid_Facet_Tag to display the tag name
  79.  *
  80.  * Revision 1.5  2001/10/01 21:03:55  peiyongz
  81.  * DTV Reorganization:derived from AbstractNumericValidator
  82.  *
  83.  * Revision 1.4  2001/09/20 13:11:42  knoaman
  84.  * Regx  + misc. fixes
  85.  *
  86.  * Revision 1.3  2001/08/21 18:42:53  peiyongz
  87.  * Bugzilla# 2816: cleanUp() declared with external linkage and called
  88.  *                          before defined as inline
  89.  *
  90.  * Revision 1.2  2001/08/15 18:08:44  peiyongz
  91.  * Fix to potential leakage in strEnumeration
  92.  *
  93.  * Revision 1.1  2001/07/24 13:59:03  peiyongz
  94.  * DoubleDTV
  95.  *
  96.  */
  97. // ---------------------------------------------------------------------------
  98. //  Includes
  99. // ---------------------------------------------------------------------------
  100. #include <xercesc/validators/datatype/DoubleDatatypeValidator.hpp>
  101. #include <xercesc/validators/datatype/InvalidDatatypeFacetException.hpp>
  102. #include <xercesc/validators/datatype/InvalidDatatypeValueException.hpp>
  103. XERCES_CPP_NAMESPACE_BEGIN
  104. // ---------------------------------------------------------------------------
  105. //  Constructors and Destructor
  106. // ---------------------------------------------------------------------------
  107. DoubleDatatypeValidator::DoubleDatatypeValidator(MemoryManager* const manager)
  108. :AbstractNumericValidator(0, 0, 0, DatatypeValidator::Double, manager)
  109. {}
  110. DoubleDatatypeValidator::DoubleDatatypeValidator(
  111.                           DatatypeValidator*            const baseValidator
  112.                         , RefHashTableOf<KVStringPair>* const facets
  113.                         , RefArrayVectorOf<XMLCh>*      const enums
  114.                         , const int                           finalSet
  115.                         , MemoryManager* const                manager)
  116. :AbstractNumericValidator(baseValidator, facets, finalSet, DatatypeValidator::Double, manager)
  117. {
  118.     init(enums);
  119. }
  120. DoubleDatatypeValidator::~DoubleDatatypeValidator()
  121. {}
  122. // -----------------------------------------------------------------------
  123. // Compare methods
  124. // -----------------------------------------------------------------------
  125. int DoubleDatatypeValidator::compare(const XMLCh* const lValue
  126.                                    , const XMLCh* const rValue)
  127. {
  128.     XMLDouble lObj(lValue, fMemoryManager);
  129.     XMLDouble rObj(rValue, fMemoryManager);
  130.     return compareValues(&lObj, &rObj);
  131. }
  132. DatatypeValidator* DoubleDatatypeValidator::newInstance
  133. (
  134.       RefHashTableOf<KVStringPair>* const facets
  135.     , RefArrayVectorOf<XMLCh>* const      enums
  136.     , const int                           finalSet
  137.     , MemoryManager* const                manager
  138. )
  139. {
  140.     return (DatatypeValidator*) new (manager) DoubleDatatypeValidator(this, facets, enums, finalSet, manager);
  141. }
  142. // -----------------------------------------------------------------------
  143. // ctor provided to be used by derived classes
  144. // -----------------------------------------------------------------------
  145. DoubleDatatypeValidator::DoubleDatatypeValidator(DatatypeValidator*            const baseValidator
  146.                                                , RefHashTableOf<KVStringPair>* const facets
  147.                                                , const int                           finalSet
  148.                                                , const ValidatorType                 type
  149.                                                , MemoryManager* const                manager)
  150. :AbstractNumericValidator(baseValidator, facets, finalSet, type, manager)
  151. {
  152.     //do not invoke init here !!!
  153. }
  154. void DoubleDatatypeValidator::assignAdditionalFacet(const XMLCh* const key
  155.                                                   , const XMLCh* const)
  156. {
  157.     ThrowXML1(InvalidDatatypeFacetException
  158.             , XMLExcepts::FACET_Invalid_Tag
  159.             , key);
  160. }
  161. void DoubleDatatypeValidator::inheritAdditionalFacet()
  162. {}
  163. void DoubleDatatypeValidator::checkAdditionalFacetConstraints() const
  164. {}
  165. void DoubleDatatypeValidator::checkAdditionalFacetConstraintsBase() const
  166. {}
  167. int  DoubleDatatypeValidator::compareValues(const XMLNumber* const lValue
  168.                                           , const XMLNumber* const rValue)
  169. {
  170.     return XMLDouble::compareValues((XMLDouble*) lValue, (XMLDouble*) rValue);
  171. }
  172. void  DoubleDatatypeValidator::setMaxInclusive(const XMLCh* const value)
  173. {
  174.     fMaxInclusive = new (fMemoryManager) XMLDouble(value, fMemoryManager);
  175. }
  176. void  DoubleDatatypeValidator::setMaxExclusive(const XMLCh* const value)
  177. {
  178.     fMaxExclusive = new (fMemoryManager) XMLDouble(value, fMemoryManager);
  179. }
  180. void  DoubleDatatypeValidator::setMinInclusive(const XMLCh* const value)
  181. {
  182.     fMinInclusive = new (fMemoryManager) XMLDouble(value, fMemoryManager);
  183. }
  184. void  DoubleDatatypeValidator::setMinExclusive(const XMLCh* const value)
  185. {
  186.     fMinExclusive = new (fMemoryManager) XMLDouble(value, fMemoryManager);
  187. }
  188. void  DoubleDatatypeValidator::setEnumeration()
  189. {
  190.     // check 4.3.5.c0 must: enumeration values from the value space of base
  191.     //
  192.     // 1. shall be from base value space
  193.     // 2. shall be from current value space as well ( shall go through boundsCheck() )
  194.     //
  195.     if (!fStrEnumeration)
  196.         return;
  197.     int i = 0;
  198.     int enumLength = fStrEnumeration->size();
  199.     DoubleDatatypeValidator *numBase = (DoubleDatatypeValidator*) getBaseValidator();
  200.     if (numBase)
  201.     {
  202.         try
  203.         {
  204.             for ( i = 0; i < enumLength; i++)
  205.             {
  206.                 numBase->checkContent(fStrEnumeration->elementAt(i), false);
  207.             }
  208.         }
  209.         catch (XMLException&)
  210.         {
  211.             ThrowXML1(InvalidDatatypeFacetException
  212.                     , XMLExcepts::FACET_enum_base
  213.                     , fStrEnumeration->elementAt(i));
  214.         }
  215.     }
  216.     // We put the this->checkContent in a separate loop
  217.     // to not block original message with in that method.
  218.     //
  219.     for ( i = 0; i < enumLength; i++)
  220.     {
  221.         checkContent(fStrEnumeration->elementAt(i), false);
  222.     }
  223.     fEnumeration = new (fMemoryManager) RefVectorOf<XMLNumber>(enumLength, true, fMemoryManager);
  224.     fEnumerationInherited = false;
  225.     for ( i = 0; i < enumLength; i++)
  226.     {
  227.         fEnumeration->insertElementAt(new (fMemoryManager) XMLDouble(fStrEnumeration->elementAt(i), fMemoryManager), i);
  228.     }
  229. }
  230. // -----------------------------------------------------------------------
  231. // Abstract interface from AbstractNumericValidator
  232. // -----------------------------------------------------------------------
  233. void DoubleDatatypeValidator::checkContent( const XMLCh* const content, bool asBase)
  234. {
  235.     //validate against base validator if any
  236.     DoubleDatatypeValidator *pBase = (DoubleDatatypeValidator*) this->getBaseValidator();
  237.     if (pBase)
  238.         pBase->checkContent(content, true);
  239.     // we check pattern first
  240.     if ( (getFacetsDefined() & DatatypeValidator::FACET_PATTERN ) != 0 )
  241.     {
  242.         // lazy construction
  243.         if (getRegex() ==0) {
  244.             try {
  245.                 setRegex(new (fMemoryManager) RegularExpression(getPattern(), SchemaSymbols::fgRegEx_XOption, fMemoryManager));
  246.             }
  247.             catch (XMLException &e)
  248.             {
  249.                 ThrowXML1(InvalidDatatypeValueException, XMLExcepts::RethrowError, e.getMessage());
  250.             }
  251.         }
  252.         if (getRegex()->matches(content) ==false)
  253.         {
  254.             ThrowXML2(InvalidDatatypeValueException
  255.                     , XMLExcepts::VALUE_NotMatch_Pattern
  256.                     , content
  257.                     , getPattern());
  258.         }
  259.     }
  260.     // if this is a base validator, we only need to check pattern facet
  261.     // all other facet were inherited by the derived type
  262.     if (asBase)
  263.         return;
  264.     try {
  265.         XMLDouble theValue(content, fMemoryManager);
  266.         XMLDouble *theData = &theValue;
  267.         if (getEnumeration())
  268.         {
  269.             int i=0;
  270.             int enumLength = getEnumeration()->size();
  271.             for ( ; i < enumLength; i++)
  272.             {
  273.                 if (compareValues(theData, (XMLDouble*) getEnumeration()->elementAt(i)) ==0 )
  274.                     break;
  275.             }
  276.             if (i == enumLength)
  277.                 ThrowXML1(InvalidDatatypeValueException, XMLExcepts::VALUE_NotIn_Enumeration, content);
  278.         }
  279.         boundsCheck(theData);
  280.     }
  281.     catch (XMLException &e)
  282.     {
  283.        ThrowXML1(InvalidDatatypeFacetException, XMLExcepts::RethrowError, e.getMessage());
  284.     }
  285. }
  286. XERCES_CPP_NAMESPACE_END
  287. /**
  288.   * End of file DoubleDatatypeValidator::cpp
  289.   */