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

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-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) 1999, 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: SimpleContentModel.cpp,v $
  58.  * Revision 1.11  2001/11/28 16:46:20  tng
  59.  * Schema fix: Check for invalid URI index first.
  60.  *
  61.  * Revision 1.10  2001/11/21 14:30:13  knoaman
  62.  * Fix for UPA checking.
  63.  *
  64.  * Revision 1.9  2001/08/21 16:06:11  tng
  65.  * Schema: Unique Particle Attribution Constraint Checking.
  66.  *
  67.  * Revision 1.8  2001/05/11 13:27:19  tng
  68.  * Copyright update.
  69.  *
  70.  * Revision 1.7  2001/05/03 21:02:32  tng
  71.  * Schema: Add SubstitutionGroupComparator and update exception messages.  By Pei Yong Zhang.
  72.  *
  73.  * Revision 1.6  2001/04/19 18:17:33  tng
  74.  * Schema: SchemaValidator update, and use QName in Content Model
  75.  *
  76.  * Revision 1.5  2001/03/21 21:56:29  tng
  77.  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  78.  *
  79.  * Revision 1.4  2001/03/21 19:30:00  tng
  80.  * Schema: Content Model Updates, by Pei Yong Zhang.
  81.  *
  82.  * Revision 1.3  2001/02/27 14:48:57  tng
  83.  * Schema: Add CMAny and ContentLeafNameTypeVector, by Pei Yong Zhang
  84.  *
  85.  * Revision 1.2  2001/02/16 14:58:57  tng
  86.  * Schema: Update Makefile, configure files, project files, and include path in
  87.  * certain cpp files because of the move of the common Content Model files.  By Pei Yong Zhang.
  88.  *
  89.  * Revision 1.1  2001/02/16 14:17:29  tng
  90.  * Schema: Move the common Content Model files that are shared by DTD
  91.  * and schema from 'DTD' folder to 'common' folder.  By Pei Yong Zhang.
  92.  *
  93.  * Revision 1.4  2000/03/03 22:33:00  roddey
  94.  * Fixed a bug in SimpleContentModel that allowed an <a/> to be taken
  95.  * as valid for a content model of (a,b).
  96.  *
  97.  * Revision 1.3  2000/03/02 19:55:40  roddey
  98.  * This checkin includes many changes done while waiting for the
  99.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  100.  * available elsewhere.
  101.  *
  102.  * Revision 1.2  2000/02/09 21:42:39  abagchi
  103.  * Copyright swatswat
  104.  *
  105.  * Revision 1.1.1.1  1999/11/09 01:03:46  twl
  106.  * Initial checkin
  107.  *
  108.  * Revision 1.2  1999/11/08 20:45:44  rahul
  109.  * Swat for adding in Product name and CVS comment log variable.
  110.  *
  111.  */
  112. // ---------------------------------------------------------------------------
  113. //  Includes
  114. // ---------------------------------------------------------------------------
  115. #include <util/RuntimeException.hpp>
  116. #include <framework/XMLValidator.hpp>
  117. #include <validators/common/SimpleContentModel.hpp>
  118. #include <validators/schema/SubstitutionGroupComparator.hpp>
  119. #include <validators/schema/XercesElementWildcard.hpp>
  120. // ---------------------------------------------------------------------------
  121. //  SimpleContentModel: Implementation of the ContentModel virtual interface
  122. // ---------------------------------------------------------------------------
  123. //
  124. //  This method is called to validate our content. For this one, its just a
  125. //  pretty simple 'bull your way through it' test according to what kind of
  126. //  operation it is for.
  127. //
  128. int
  129. SimpleContentModel::validateContent(QName** const       children
  130.                                   , const unsigned int  childCount
  131.                                   , const unsigned int  emptyNamespaceId) const
  132. {
  133.     //
  134.     //  According to the type of operation, we do the correct type of
  135.     //  content check.
  136.     //
  137.     unsigned int index;
  138.     switch(fOp)
  139.     {
  140.         case ContentSpecNode::Leaf :
  141.             //
  142.             //  There can only be one child and it has to be of the
  143.             //  element type we stored.
  144.             //
  145.             if (!childCount)
  146.                 return 0;
  147.             // If the 0th child is not the right kind, report an error at 0
  148.             if (fDTD) {
  149.                 if (XMLString::compareString(children[0]->getRawName(), fFirstChild->getRawName())!=0) {
  150.                     return 0;
  151.                 }
  152.             }
  153.             else {
  154.                 if ((children[0]->getURI() != fFirstChild->getURI()) ||
  155.                     (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0)) {
  156.                     return 0;
  157.                 }
  158.             }
  159.             if (childCount > 1)
  160.                 return 1;
  161.             break;
  162.         case ContentSpecNode::ZeroOrOne :
  163.             //
  164.             //  If the child count is greater than one, then obviously
  165.             //  bad. Otherwise, if its one, then the one child must be
  166.             //  of the type we stored.
  167.             //
  168.             if (childCount == 1) {
  169.                 if (fDTD) {
  170.                     if (XMLString::compareString(children[0]->getRawName(), fFirstChild->getRawName())!=0) {
  171.                             return 0;
  172.                     }
  173.                 }
  174.                 else {
  175.                     if ((children[0]->getURI() != fFirstChild->getURI()) ||
  176.                         (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0)) {
  177.                         return 0;
  178.                     }
  179.                 }
  180.             }
  181.             if (childCount > 1)
  182.                 return 1;
  183.             break;
  184.         case ContentSpecNode::ZeroOrMore :
  185.             //
  186.             //  If the child count is zero, that's fine. If its more than
  187.             //  zero, then make sure that all children are of the element
  188.             //  type that we stored.
  189.             //
  190.             if (childCount > 0)
  191.             {
  192.                 if (fDTD) {
  193.                     for (index = 0; index < childCount; index++) {
  194.                         if (XMLString::compareString(children[index]->getRawName(), fFirstChild->getRawName())!=0) {
  195.                             return index;
  196.                         }
  197.                     }
  198.                 }
  199.                 else {
  200.                     for (index = 0; index < childCount; index++) {
  201.                         if ((children[index]->getURI() != fFirstChild->getURI()) ||
  202.                             (XMLString::compareString(children[index]->getLocalPart(), fFirstChild->getLocalPart())!=0)) {
  203.                             return index;
  204.                         }
  205.                     }
  206.                 }
  207.             }
  208.             break;
  209.         case ContentSpecNode::OneOrMore :
  210.             //
  211.             //  If the child count is zero, that's an error. If its more
  212.             //  than zero, then make sure that all children are of the
  213.             //  element type that we stored.
  214.             //
  215.             if (childCount == 0)
  216.                 return 0;
  217.             if (fDTD) {
  218.                 for (index = 0; index < childCount; index++) {
  219.                     if (XMLString::compareString(children[index]->getRawName(), fFirstChild->getRawName())!=0) {
  220.                         return index;
  221.                     }
  222.                 }
  223.             }
  224.             else {
  225.                 for (index = 0; index < childCount; index++) {
  226.                     if ((children[index]->getURI() != fFirstChild->getURI()) ||
  227.                         (XMLString::compareString(children[index]->getLocalPart(), fFirstChild->getLocalPart())!=0)) {
  228.                         return index;
  229.                     }
  230.                 }
  231.             }
  232.             break;
  233.         case ContentSpecNode::Choice :
  234.             //
  235.             //  There can only be one child, and it must be one of the
  236.             //  two types we stored.
  237.             //
  238.             if (!childCount)
  239.                 return 0;
  240.             if (fDTD) {
  241.                 if ((XMLString::compareString(children[0]->getRawName(), fFirstChild->getRawName())!=0) &&
  242.                     (XMLString::compareString(children[0]->getRawName(), fSecondChild->getRawName())!=0)) {
  243.                     return 0;
  244.                 }
  245.             }
  246.             else {
  247.                 if (((children[0]->getURI() != fFirstChild->getURI()) ||
  248.                      (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0)) &&
  249.                     ((children[0]->getURI() != fSecondChild->getURI()) ||
  250.                      (XMLString::compareString(children[0]->getLocalPart(), fSecondChild->getLocalPart())!=0))) {
  251.                     return 0;
  252.                 }
  253.             }
  254.             if (childCount > 1)
  255.                 return 1;
  256.             break;
  257.         case ContentSpecNode::Sequence :
  258.             //
  259.             //  There must be two children and they must be the two values
  260.             //  we stored, in the stored order. So first check the obvious
  261.             //  problem of an empty content, which would never be valid
  262.             //  in this content mode.
  263.             //
  264.             if (!childCount)
  265.                 return 0;
  266.             if (childCount == 2) {
  267.                 if (fDTD) {
  268.                     if (XMLString::compareString(children[0]->getRawName(), fFirstChild->getRawName())!=0) {
  269.                         return 0;
  270.                     }
  271.                     if (XMLString::compareString(children[1]->getRawName(), fSecondChild->getRawName())!=0) {
  272.                         return 1;
  273.                     }
  274.                 }
  275.                 else {
  276.                     if ((children[0]->getURI() != fFirstChild->getURI()) ||
  277.                         (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0)) {
  278.                         return 0;
  279.                     }
  280.                     if ((children[1]->getURI() != fSecondChild->getURI()) ||
  281.                         (XMLString::compareString(children[1]->getLocalPart(), fSecondChild->getLocalPart())!=0)) {
  282.                         return 1;
  283.                     }
  284.                 }
  285.             }
  286.             else {
  287.                 if (childCount > 2) {
  288.                     return 2;
  289.                 }
  290.                 return childCount;
  291.             }
  292.             break;
  293.         default :
  294.             ThrowXML(RuntimeException, XMLExcepts::CM_UnknownCMSpecType);
  295.             break;
  296.     }
  297.     return -1;
  298. }
  299. int SimpleContentModel::validateContentSpecial(QName** const          children
  300.                                             , const unsigned int      childCount
  301.                                             , const unsigned int      emptyNamespaceId
  302.                                             , GrammarResolver*  const pGrammarResolver
  303.                                             , XMLStringPool*    const pStringPool) const
  304. {
  305.     SubstitutionGroupComparator comparator(pGrammarResolver, pStringPool);
  306.     //
  307.     //  According to the type of operation, we do the correct type of
  308.     //  content check.
  309.     //
  310.     unsigned int index;
  311.     switch(fOp)
  312.     {
  313.         case ContentSpecNode::Leaf :
  314.             //
  315.             //  There can only be one child and it has to be of the
  316.             //  element type we stored.
  317.             //
  318.             if (!childCount)
  319.                 return 0;
  320.             if ((children[0]->getURI() != fFirstChild->getURI()) ||
  321.                 (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0))
  322.             {
  323.                 if (!comparator.isEquivalentTo(children[0], fFirstChild))
  324.                    return 0;
  325.             }
  326.             if (childCount > 1)
  327.                 return 1;
  328.             break;
  329.         case ContentSpecNode::ZeroOrOne :
  330.             //
  331.             //  If the child count is greater than one, then obviously
  332.             //  bad. Otherwise, if its one, then the one child must be
  333.             //  of the type we stored.
  334.             //
  335.             if ((childCount == 1) &&
  336.                ((children[0]->getURI() != fFirstChild->getURI()) ||
  337.                 (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0)))
  338.             {
  339.                 if(!comparator.isEquivalentTo(children[0], fFirstChild))
  340.                     return 0;
  341.             }
  342.             if (childCount > 1)
  343.                 return 1;
  344.             break;
  345.         case ContentSpecNode::ZeroOrMore :
  346.             //
  347.             //  If the child count is zero, that's fine. If its more than
  348.             //  zero, then make sure that all children are of the element
  349.             //  type that we stored.
  350.             //
  351.             if (childCount > 0)
  352.             {
  353.                 for (index = 0; index < childCount; index++)
  354.                 {
  355.                     if ((children[index]->getURI() != fFirstChild->getURI()) ||
  356.                         (XMLString::compareString(children[index]->getLocalPart(), fFirstChild->getLocalPart())!=0))
  357.                     {
  358.          if (!comparator.isEquivalentTo(children[index], fFirstChild))
  359.                             return index;
  360.                     }
  361.                 }
  362.             }
  363.             break;
  364.         case ContentSpecNode::OneOrMore :
  365.             //
  366.             //  If the child count is zero, that's an error. If its more
  367.             //  than zero, then make sure that all children are of the
  368.             //  element type that we stored.
  369.             //
  370.             if (childCount == 0)
  371.                 return 0;
  372.             for (index = 0; index < childCount; index++)
  373.             {
  374.                 if ((children[index]->getURI() != fFirstChild->getURI()) ||
  375.                     (XMLString::compareString(children[index]->getLocalPart(), fFirstChild->getLocalPart())!=0))
  376.                 {
  377.          if (!comparator.isEquivalentTo(children[index], fFirstChild))
  378.                         return index;
  379.                 }
  380.             }
  381.             break;
  382.         case ContentSpecNode::Choice :
  383.             //
  384.             //  There can only be one child, and it must be one of the
  385.             //  two types we stored.
  386.             //
  387.             if (!childCount)
  388.                 return 0;
  389.             if (((children[0]->getURI() != fFirstChild->getURI()) ||
  390.                  (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0)) &&
  391.                 ((children[0]->getURI() != fSecondChild->getURI()) ||
  392.                  (XMLString::compareString(children[0]->getLocalPart(), fSecondChild->getLocalPart())!=0)))
  393.             {
  394.                  if (!comparator.isEquivalentTo(children[0], fFirstChild) &&
  395.                      !comparator.isEquivalentTo(children[0], fSecondChild) )
  396.                      return 0;
  397.             }
  398.             if (childCount > 1)
  399.                 return 1;
  400.             break;
  401.         case ContentSpecNode::Sequence :
  402.             //
  403.             //  There must be two children and they must be the two values
  404.             //  we stored, in the stored order. So first check the obvious
  405.             //  problem of an empty content, which would never be valid
  406.             //  in this content mode.
  407.             //
  408.             if (!childCount)
  409.                 return 0;
  410.             if (childCount == 2)
  411.             {
  412.                 if ((children[0]->getURI() != fFirstChild->getURI()) ||
  413.                     (XMLString::compareString(children[0]->getLocalPart(), fFirstChild->getLocalPart())!=0))
  414.                 {
  415.                     if(!comparator.isEquivalentTo(children[0], fFirstChild))
  416.                         return 0;
  417.                 }
  418.                 if ((children[1]->getURI() != fSecondChild->getURI()) ||
  419.                     (XMLString::compareString(children[1]->getLocalPart(), fSecondChild->getLocalPart())!=0))
  420.                 {
  421.                     if (!comparator.isEquivalentTo(children[1], fSecondChild))
  422.                         return 1;
  423.                 }
  424.             }
  425.             else
  426.             {
  427.                 if (childCount > 2)
  428.                 {
  429.                     return 2;
  430.                 }
  431.                 return childCount;
  432.             }
  433.             break;
  434.         default :
  435.             ThrowXML(RuntimeException, XMLExcepts::CM_UnknownCMSpecType);
  436.             break;
  437.     }
  438.     return -1;
  439. }
  440. ContentLeafNameTypeVector* SimpleContentModel::getContentLeafNameTypeVector() const
  441. {
  442.     return 0;
  443. }
  444. void SimpleContentModel::checkUniqueParticleAttribution
  445.     (
  446.         SchemaGrammar*    const pGrammar
  447.       , GrammarResolver*  const pGrammarResolver
  448.       , XMLStringPool*    const pStringPool
  449.       , XMLValidator*     const pValidator
  450.       , unsigned int*     const pContentSpecOrgURI
  451.     )
  452. {
  453.     // rename back
  454.     unsigned int orgURIIndex = 0;
  455.     orgURIIndex = fFirstChild->getURI();
  456.     if (orgURIIndex != XMLContentModel::gEOCFakeId)
  457.         fFirstChild->setURI(pContentSpecOrgURI[orgURIIndex]);
  458.     orgURIIndex = fSecondChild->getURI();
  459.     if (orgURIIndex != XMLContentModel::gEOCFakeId)
  460.         fSecondChild->setURI(pContentSpecOrgURI[orgURIIndex]);
  461.     // only possible violation is when it's a choice
  462.     if (fOp == ContentSpecNode::Choice) {
  463.         SubstitutionGroupComparator comparator(pGrammarResolver, pStringPool);
  464.         if (XercesElementWildcard::conflict(pGrammar,
  465.                                             ContentSpecNode::Leaf,
  466.                                             fFirstChild,
  467.                                             ContentSpecNode::Leaf,
  468.                                             fSecondChild,
  469.                                             &comparator))
  470.             pValidator->emitError(XMLValid::UniqueParticleAttributionFail,
  471.                                   fFirstChild->getRawName(),
  472.                                   fSecondChild->getRawName());
  473.     }
  474. }