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

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.  * $Log: SchemaElementDecl.cpp,v $
  58.  * Revision 1.17  2001/11/02 14:13:45  knoaman
  59.  * Add support for identity constraints.
  60.  *
  61.  * Revision 1.16  2001/10/11 12:07:39  tng
  62.  * Schema: model type should be based on complextypeinfo if exists.
  63.  *
  64.  * Revision 1.15  2001/09/14 14:50:22  tng
  65.  * Schema: Fix some wildcard bugs, and some retrieving qualified/unqualified element decl problems.
  66.  *
  67.  * Revision 1.14  2001/09/11 13:02:40  tng
  68.  * [Bug 3523] SchemaElementDecl.cpp(242) : error C2202 : not all control paths return a value
  69.  *
  70.  * Revision 1.13  2001/08/29 20:52:35  tng
  71.  * Schema: xsi:type support
  72.  *
  73.  * Revision 1.12  2001/08/21 16:06:11  tng
  74.  * Schema: Unique Particle Attribution Constraint Checking.
  75.  *
  76.  * Revision 1.11  2001/07/24 18:33:46  knoaman
  77.  * Added support for <group> + extra constraint checking for complexType
  78.  *
  79.  * Revision 1.10  2001/07/09 15:22:43  knoaman
  80.  * complete <any> declaration.
  81.  *
  82.  * Revision 1.9  2001/06/13 20:51:18  peiyongz
  83.  * fIsMixed: to handle mixed Content Model
  84.  *
  85.  * Revision 1.8  2001/05/11 13:27:35  tng
  86.  * Copyright update.
  87.  *
  88.  * Revision 1.7  2001/05/03 20:34:42  tng
  89.  * Schema: SchemaValidator update
  90.  *
  91.  * Revision 1.6  2001/05/03 19:17:59  knoaman
  92.  * TraverseSchema Part II.
  93.  *
  94.  * Revision 1.5  2001/04/19 17:43:16  knoaman
  95.  * More schema implementation classes.
  96.  *
  97.  * Revision 1.4  2001/03/21 21:56:33  tng
  98.  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  99.  *
  100.  * Revision 1.3  2001/03/21 19:30:15  tng
  101.  * Schema: Content Model Updates, by Pei Yong Zhang.
  102.  *
  103.  * Revision 1.2  2001/03/07 17:42:12  tng
  104.  * fix typo
  105.  *
  106.  * Revision 1.1  2001/02/27 18:48:22  tng
  107.  * Schema: Add SchemaAttDef, SchemaElementDecl, SchemaAttDefList.
  108.  *
  109.  */
  110. // ---------------------------------------------------------------------------
  111. //  Includes
  112. // ---------------------------------------------------------------------------
  113. #include <util/XMLString.hpp>
  114. #include <util/XMLUniDefs.hpp>
  115. #include <util/XMLUni.hpp>
  116. #include <validators/schema/SchemaAttDefList.hpp>
  117. #include <validators/schema/SchemaElementDecl.hpp>
  118. #include <validators/schema/identity/IdentityConstraint.hpp>
  119. // ---------------------------------------------------------------------------
  120. //  SchemaElementDecl: Constructors and Destructor
  121. // ---------------------------------------------------------------------------
  122. SchemaElementDecl::SchemaElementDecl() :
  123.     fModelType(Any)
  124.     , fDatatypeValidator(0)
  125.     , fEnclosingScope(Grammar::TOP_LEVEL_SCOPE)
  126.     , fBlockSet(0)
  127.     , fFinalSet(0)
  128.     , fMiscFlags(0)
  129.     , fDefaultValue(0)
  130.     , fSubstitutionGroupName(0)
  131.     , fTypeFromAnotherSchemaURI(0)
  132.     , fComplexTypeInfo(0)
  133.     , fXsiComplexTypeInfo(0)
  134.     , fAttDefs(0)
  135.     , fIdentityConstraints(0)
  136. {
  137. }
  138. SchemaElementDecl::SchemaElementDecl(const XMLCh* const                  prefix
  139.                                    , const XMLCh* const                  localPart
  140.                                    , const int                           uriId
  141.                                    , const SchemaElementDecl::ModelTypes type
  142.                                    , const int                           enclosingScope
  143.                                    ) :
  144.     fModelType(type)
  145.     , fDatatypeValidator(0)
  146.     , fEnclosingScope(enclosingScope)
  147.     , fBlockSet(0)
  148.     , fFinalSet(0)
  149.     , fMiscFlags(0)
  150.     , fDefaultValue(0)
  151.     , fSubstitutionGroupName(0)
  152.     , fTypeFromAnotherSchemaURI(0)
  153.     , fComplexTypeInfo(0)
  154.     , fXsiComplexTypeInfo(0)
  155.     , fAttDefs(0)
  156.     , fIdentityConstraints(0)
  157. {
  158.     setElementName(prefix, localPart, uriId);
  159. }
  160. SchemaElementDecl::SchemaElementDecl(const QName* const                  elementName
  161.                                    , const SchemaElementDecl::ModelTypes type
  162.                                    , const int                           enclosingScope
  163.                                    ) :
  164.     fModelType(type)
  165.     , fDatatypeValidator(0)
  166.     , fEnclosingScope(enclosingScope)
  167.     , fBlockSet(0)
  168.     , fFinalSet(0)
  169.     , fMiscFlags(0)
  170.     , fDefaultValue(0)
  171.     , fSubstitutionGroupName(0)
  172.     , fTypeFromAnotherSchemaURI(0)
  173.     , fComplexTypeInfo(0)
  174.     , fXsiComplexTypeInfo(0)
  175.     , fAttDefs(0)
  176.     , fIdentityConstraints(0)
  177. {
  178.     setElementName(elementName);
  179. }
  180. SchemaElementDecl::~SchemaElementDecl()
  181. {
  182.     delete [] fDefaultValue;
  183.     delete [] fSubstitutionGroupName;
  184.     delete [] fTypeFromAnotherSchemaURI;
  185.     delete fAttDefs;
  186.     delete fIdentityConstraints;
  187. }
  188. // ---------------------------------------------------------------------------
  189. //  SchemaElementDecl: XMLElementDecl virtual interface implementation
  190. // ---------------------------------------------------------------------------
  191. XMLAttDef* SchemaElementDecl::findAttr(const XMLCh* const    qName
  192.                                      , const unsigned int    uriId
  193.                                      , const XMLCh* const    baseName
  194.                                      , const XMLCh* const    prefix
  195.                                      , const LookupOpts      options
  196.                                      , bool&           wasAdded) const
  197. {
  198.     if (fXsiComplexTypeInfo) {
  199.         return fXsiComplexTypeInfo->findAttr(qName, uriId, baseName, prefix, options, wasAdded);
  200.     }
  201.     else if (fComplexTypeInfo) {
  202.         return fComplexTypeInfo->findAttr(qName, uriId, baseName, prefix, options, wasAdded);
  203.     }
  204.     else {
  205.         if (options == XMLElementDecl::AddIfNotFound) {
  206.             SchemaAttDef* retVal = 0;
  207.             // If no att list exist yet, then create one
  208.             if (!fAttDefs) {
  209.                 // Use a hash modulus of 29 and tell it owns its elements
  210.                 ((SchemaElementDecl*)this)->fAttDefs = new RefHash2KeysTableOf<SchemaAttDef>(29, true);
  211.             }
  212.             retVal = fAttDefs->get(baseName, uriId);
  213.             // Fault it in if not found and ask to add it
  214.             if (!retVal)
  215.             {
  216.                 // And add a default attribute for this name
  217.                 retVal = new SchemaAttDef(prefix, baseName, uriId);
  218.                 retVal->setElemId(getId());
  219.                 fAttDefs->put((void*)baseName, uriId, retVal);
  220.                 wasAdded = true;
  221.             }
  222.              else
  223.             {
  224.                 wasAdded = false;
  225.             }
  226.             return retVal;
  227.         }
  228.         else {
  229.             wasAdded = false;
  230.             return 0;
  231.         }
  232.     }
  233. }
  234. XMLAttDefList& SchemaElementDecl::getAttDefList() const
  235. {
  236.     if (fXsiComplexTypeInfo) {
  237.         return fXsiComplexTypeInfo->getAttDefList();
  238.     }
  239.     else if (fComplexTypeInfo) {
  240.         return fComplexTypeInfo->getAttDefList();
  241.     }
  242.     else {
  243.         ThrowXML(RuntimeException, XMLExcepts::DV_InvalidOperation);
  244.     }
  245.     return *(XMLAttDefList*)0 ;
  246. }
  247. XMLElementDecl::CharDataOpts SchemaElementDecl::getCharDataOpts() const
  248. {
  249.     SchemaElementDecl::ModelTypes modelType = fModelType;
  250.     if (fXsiComplexTypeInfo) {
  251.         modelType = (SchemaElementDecl::ModelTypes) fXsiComplexTypeInfo->getContentType();
  252.     }
  253.     else if (fComplexTypeInfo) {
  254.         modelType = (SchemaElementDecl::ModelTypes) fComplexTypeInfo->getContentType();
  255.     }
  256.     XMLElementDecl::CharDataOpts retVal;
  257.     switch(modelType)
  258.     {
  259.         case Children :
  260.             retVal = XMLElementDecl::SpacesOk;
  261.             break;
  262.         case Empty :
  263.             retVal = XMLElementDecl::NoCharData;
  264.             break;
  265.         default :
  266.             retVal = XMLElementDecl::AllCharData;
  267.             break;
  268.     }
  269.     return retVal;
  270. }
  271. bool SchemaElementDecl::hasAttDefs() const
  272. {
  273.     if (fXsiComplexTypeInfo) {
  274.         return fXsiComplexTypeInfo->hasAttDefs();
  275.     }
  276.     else if (fComplexTypeInfo) {
  277.         return fComplexTypeInfo->hasAttDefs();
  278.     }
  279.     // If the collection hasn't been faulted in, then no att defs
  280.     return false;
  281. }
  282. bool SchemaElementDecl::resetDefs()
  283. {
  284.     if (fXsiComplexTypeInfo) {
  285.         return fXsiComplexTypeInfo->resetDefs();
  286.     }
  287.     else if (fComplexTypeInfo) {
  288.         return fComplexTypeInfo->resetDefs();
  289.     }
  290.     else if (fAttDefs) {
  291.         //all the attdefs here are faulted-in, so just reset the fAttDefs
  292.         //but still return false to indicate there is no real att defs
  293.         // defined in this element
  294.         fAttDefs->removeAll();
  295.     }
  296.     return false;
  297. }
  298. const XMLCh*
  299. SchemaElementDecl::getFormattedContentModel() const
  300. {
  301.     if (fXsiComplexTypeInfo) {
  302.         return fXsiComplexTypeInfo->getFormattedContentModel();
  303.     }
  304.     else if (fComplexTypeInfo) {
  305.         return fComplexTypeInfo->getFormattedContentModel();
  306.     }
  307.     return 0;
  308. }
  309. // ---------------------------------------------------------------------------
  310. //  SchemaElementDecl: Getter methods
  311. // ---------------------------------------------------------------------------
  312. const SchemaAttDef* SchemaElementDecl::getAttDef(const XMLCh* const baseName, const int uriId) const
  313. {
  314.     if (fXsiComplexTypeInfo) {
  315.         return fXsiComplexTypeInfo->getAttDef(baseName, uriId);
  316.     }
  317.     else if (fComplexTypeInfo) {
  318.         return fComplexTypeInfo->getAttDef(baseName, uriId);
  319.     }
  320.     // If no complex type, then return a null
  321.     return 0;
  322. }
  323. SchemaAttDef* SchemaElementDecl::getAttDef(const XMLCh* const baseName, const int uriId)
  324. {
  325.     if (fXsiComplexTypeInfo) {
  326.         return fXsiComplexTypeInfo->getAttDef(baseName, uriId);
  327.     }
  328.     else if (fComplexTypeInfo) {
  329.         return fComplexTypeInfo->getAttDef(baseName, uriId);
  330.     }
  331.     // If no complex type, then return a null
  332.     return 0;
  333. }