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

词法分析

开发平台:

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.  * $Log: SchemaGrammar.cpp,v $
  58.  * Revision 1.6  2003/05/21 21:35:31  knoaman
  59.  * Pass the memory manager.
  60.  *
  61.  * Revision 1.5  2003/05/18 14:02:08  knoaman
  62.  * Memory manager implementation: pass per instance manager.
  63.  *
  64.  * Revision 1.4  2003/05/15 18:57:27  knoaman
  65.  * Partial implementation of the configurable memory manager.
  66.  *
  67.  * Revision 1.3  2002/11/04 14:49:41  tng
  68.  * C++ Namespace Support.
  69.  *
  70.  * Revision 1.2  2002/07/11 18:21:20  knoaman
  71.  * Grammar caching/preparsing - initial implementation.
  72.  *
  73.  * Revision 1.1.1.1  2002/02/01 22:22:46  peiyongz
  74.  * sane_include
  75.  *
  76.  * Revision 1.11  2002/01/02 15:20:22  tng
  77.  * Schema Fix: should not store a temp value as the key in the element pool and the attribute pool.
  78.  *
  79.  * Revision 1.10  2001/11/19 18:26:31  knoaman
  80.  * no message
  81.  *
  82.  * Revision 1.9  2001/10/09 12:18:26  tng
  83.  * Leak fix: should use delete [] to delete fTargetNamespace.
  84.  *
  85.  * Revision 1.8  2001/09/14 14:50:22  tng
  86.  * Schema: Fix some wildcard bugs, and some retrieving qualified/unqualified element decl problems.
  87.  *
  88.  * Revision 1.7  2001/08/21 16:06:11  tng
  89.  * Schema: Unique Particle Attribution Constraint Checking.
  90.  *
  91.  * Revision 1.6  2001/07/31 15:27:10  knoaman
  92.  * Added support for <attributeGroup>.
  93.  *
  94.  * Revision 1.5  2001/07/24 18:33:46  knoaman
  95.  * Added support for <group> + extra constraint checking for complexType
  96.  *
  97.  * Revision 1.4  2001/06/25 12:51:56  knoaman
  98.  * Add constraint checking on elements in complex types to prevent same
  99.  * element names from having different definitions - use substitueGroups.
  100.  *
  101.  * Revision 1.3  2001/05/11 13:27:36  tng
  102.  * Copyright update.
  103.  *
  104.  * Revision 1.2  2001/04/19 17:43:18  knoaman
  105.  * More schema implementation classes.
  106.  *
  107.  * Revision 1.1  2001/03/21 21:56:33  tng
  108.  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  109.  *
  110.  */
  111. // ---------------------------------------------------------------------------
  112. //  Includes
  113. // ---------------------------------------------------------------------------
  114. #include <xercesc/validators/schema/SchemaGrammar.hpp>
  115. #include <xercesc/validators/schema/NamespaceScope.hpp>
  116. #include <xercesc/validators/schema/ComplexTypeInfo.hpp>
  117. #include <xercesc/validators/schema/SchemaSymbols.hpp>
  118. #include <xercesc/validators/schema/XercesGroupInfo.hpp>
  119. #include <xercesc/validators/schema/XercesAttGroupInfo.hpp>
  120. XERCES_CPP_NAMESPACE_BEGIN
  121. // ---------------------------------------------------------------------------
  122. //  SchemaGrammar: Constructors and Destructor
  123. // ---------------------------------------------------------------------------
  124. SchemaGrammar::SchemaGrammar(MemoryManager* const manager) :
  125.     fTargetNamespace(0)
  126.     , fElemDeclPool(0)
  127.     , fElemNonDeclPool(0)
  128.     , fGroupElemDeclPool(0)
  129.     , fNotationDeclPool(0)
  130.     , fAttributeDeclRegistry(0)
  131.     , fComplexTypeRegistry(0)
  132.     , fGroupInfoRegistry(0)
  133.     , fAttGroupInfoRegistry(0)
  134.     , fNamespaceScope(0)
  135.     , fValidSubstitutionGroups(0)
  136.     , fIDRefList(0)
  137.     , fMemoryManager(manager)
  138.     , fValidated(false)
  139.     , fDatatypeRegistry(manager)
  140. {
  141.     //
  142.     //  Init all the pool members.
  143.     //
  144.     //  <TBD> Investigate what the optimum values would be for the various
  145.     //  pools.
  146.     //
  147.     fElemDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(109, true, 128, fMemoryManager);
  148.     try {
  149.         fElemNonDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(29, true, 128, fMemoryManager);
  150.         fGroupElemDeclPool = new (fMemoryManager) RefHash3KeysIdPool<SchemaElementDecl>(109, false, 128, fMemoryManager);
  151.         fNotationDeclPool = new (fMemoryManager) NameIdPool<XMLNotationDecl>(109, 128, fMemoryManager);
  152.         fIDRefList = new (fMemoryManager) RefHashTableOf<XMLRefInfo>(29, fMemoryManager);
  153.         fDatatypeRegistry.expandRegistryToFullSchemaSet();
  154.         //
  155.         //  Call our own reset method. This lets us have the pool setup stuff
  156.         //  done in just one place (because this stame setup stuff has to be
  157.         //  done every time we are reset.)
  158.         //
  159.         reset();
  160.     }
  161.     catch(...) {
  162.         cleanUp();
  163.         throw;
  164.     }
  165. }
  166. SchemaGrammar::~SchemaGrammar()
  167. {
  168.     cleanUp();
  169. }
  170. // -----------------------------------------------------------------------
  171. //  Virtual methods
  172. // -----------------------------------------------------------------------
  173. XMLElementDecl* SchemaGrammar::findOrAddElemDecl (const   unsigned int    uriId
  174.         , const XMLCh* const    baseName
  175.         , const XMLCh* const    prefixName
  176.         , const XMLCh* const    qName
  177.         , unsigned int          scope
  178.         ,       bool&           wasAdded )
  179. {
  180.     // See it it exists
  181.     SchemaElementDecl* retVal = (SchemaElementDecl*) getElemDecl(uriId, baseName, qName, scope);
  182.     // if not, then add this in
  183.     if (!retVal)
  184.     {
  185.         retVal = new (fMemoryManager) SchemaElementDecl
  186.         (
  187.             prefixName
  188.             , baseName
  189.             , uriId
  190.             , SchemaElementDecl::Any
  191.             , Grammar::TOP_LEVEL_SCOPE
  192.             , fMemoryManager
  193.         );
  194.         const unsigned int elemId = fElemNonDeclPool->put((void*)retVal->getBaseName(), uriId, scope, retVal);
  195.         retVal->setId(elemId);
  196.         wasAdded = true;
  197.     }
  198.      else
  199.     {
  200.         wasAdded = false;
  201.     }
  202.     return retVal;
  203. }
  204. XMLElementDecl* SchemaGrammar::putElemDecl (const   unsigned int    uriId
  205.         , const XMLCh* const    baseName
  206.         , const XMLCh* const    prefixName
  207.         , const XMLCh* const    qName
  208.         , unsigned int          scope
  209.         , const bool            notDeclared)
  210. {
  211.     SchemaElementDecl* retVal = new (fMemoryManager) SchemaElementDecl
  212.     (
  213.         prefixName
  214.         , baseName
  215.         , uriId
  216.         , SchemaElementDecl::Any
  217.         , Grammar::TOP_LEVEL_SCOPE
  218.         , fMemoryManager
  219.     );
  220.     const unsigned int elemId = (notDeclared) ? fElemNonDeclPool->put((void*)retVal->getBaseName(), uriId, scope, retVal)
  221.                                               : fElemDeclPool->put((void*)retVal->getBaseName(), uriId, scope, retVal);
  222.     retVal->setId(elemId);
  223.     return retVal;
  224. }
  225. void SchemaGrammar::reset()
  226. {
  227.     //
  228.     //  We need to reset all of the pools.
  229.     //
  230.     fElemDeclPool->removeAll();
  231.     fElemNonDeclPool->removeAll();
  232.     fGroupElemDeclPool->removeAll();
  233.     fNotationDeclPool->removeAll();
  234.     fValidated = false;
  235. }
  236. void SchemaGrammar::cleanUp()
  237. {
  238.     delete fElemDeclPool;
  239.     delete fElemNonDeclPool;
  240.     delete fGroupElemDeclPool;
  241.     delete fNotationDeclPool;
  242.     fMemoryManager->deallocate(fTargetNamespace);//delete [] fTargetNamespace;
  243.     delete fAttributeDeclRegistry;
  244.     delete fComplexTypeRegistry;
  245.     delete fGroupInfoRegistry;
  246.     delete fAttGroupInfoRegistry;
  247.     delete fNamespaceScope;
  248.     delete fValidSubstitutionGroups;
  249.     delete fIDRefList;
  250. }
  251. XERCES_CPP_NAMESPACE_END