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

词法分析

开发平台:

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) 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: XPathMatcher.cpp,v $
  58.  * Revision 1.8  2003/05/18 14:02:09  knoaman
  59.  * Memory manager implementation: pass per instance manager.
  60.  *
  61.  * Revision 1.7  2003/05/15 18:59:34  knoaman
  62.  * Partial implementation of the configurable memory manager.
  63.  *
  64.  * Revision 1.6  2003/01/13 20:16:52  knoaman
  65.  * [Bug 16024] SchemaSymbols.hpp conflicts C++ Builder 6 dir.h
  66.  *
  67.  * Revision 1.5  2003/01/13 16:30:19  knoaman
  68.  * [Bug 14469] Validator doesn't enforce xsd:key.
  69.  *
  70.  * Revision 1.4  2002/11/04 14:47:41  tng
  71.  * C++ Namespace Support.
  72.  *
  73.  * Revision 1.3  2002/08/27 05:56:19  knoaman
  74.  * Identity Constraint: handle case of recursive elements.
  75.  *
  76.  * Revision 1.2  2002/02/18 06:26:50  jberry
  77.  * Quiet codewarrior compiler warnings
  78.  *
  79.  * Revision 1.1.1.1  2002/02/01 22:22:51  peiyongz
  80.  * sane_include
  81.  *
  82.  * Revision 1.2  2001/11/15 17:10:19  knoaman
  83.  * Particle derivation checking support.
  84.  *
  85.  * Revision 1.1  2001/11/02 14:08:40  knoaman
  86.  * Add support for identity constraints.
  87.  *
  88.  */
  89. // ---------------------------------------------------------------------------
  90. //  Includes
  91. // ---------------------------------------------------------------------------
  92. #include <xercesc/validators/schema/identity/XPathMatcher.hpp>
  93. #include <xercesc/validators/schema/identity/XercesXPath.hpp>
  94. #include <xercesc/validators/schema/SchemaElementDecl.hpp>
  95. #include <xercesc/validators/schema/SchemaAttDef.hpp>
  96. #include <xercesc/validators/schema/SchemaSymbols.hpp>
  97. #include <xercesc/util/RuntimeException.hpp>
  98. XERCES_CPP_NAMESPACE_BEGIN
  99. // ---------------------------------------------------------------------------
  100. //  XPathMatcher: Constructors and Destructor
  101. // ---------------------------------------------------------------------------
  102. XPathMatcher::XPathMatcher( XercesXPath* const xpath
  103.                           , MemoryManager* const manager)
  104.     : fLocationPathSize(0)
  105.     , fMatched(0)
  106.     , fNoMatchDepth(0)
  107.     , fCurrentStep(0)
  108.     , fStepIndexes(0)
  109.     , fLocationPaths(0)
  110.     , fIdentityConstraint(0)
  111.     , fMemoryManager(manager)
  112. {
  113.     try {
  114.         init(xpath);
  115.     }
  116.     catch(...) {
  117.         cleanUp();
  118.         throw;
  119.     }
  120. }
  121. XPathMatcher::XPathMatcher(XercesXPath* const xpath,
  122.                            IdentityConstraint* const ic,
  123.    MemoryManager* const manager)
  124.     : fLocationPathSize(0)
  125.     , fMatched(0)
  126.     , fNoMatchDepth(0)
  127.     , fCurrentStep(0)
  128.     , fStepIndexes(0)
  129.     , fLocationPaths(0)
  130.     , fIdentityConstraint(ic)
  131.     , fMemoryManager(manager)
  132. {
  133.     try {
  134.         init(xpath);
  135.     }
  136.     catch(...) {
  137.         cleanUp();
  138.         throw;
  139.     }
  140. }
  141. XPathMatcher::~XPathMatcher()
  142. {
  143.     cleanUp();
  144. }
  145. // ---------------------------------------------------------------------------
  146. //  XPathMatcher: Helper methods
  147. // ---------------------------------------------------------------------------
  148. void XPathMatcher::init(XercesXPath* const xpath) {
  149.     if (xpath) {
  150.         fLocationPaths = xpath->getLocationPaths();
  151.         fLocationPathSize = (fLocationPaths ? fLocationPaths->size() : 0);
  152.         if (fLocationPathSize) {
  153.             fStepIndexes = new (fMemoryManager) RefVectorOf<ValueStackOf<int> >(fLocationPathSize, true, fMemoryManager);
  154.             fCurrentStep = (int*) fMemoryManager->allocate
  155.             (
  156.                 fLocationPathSize * sizeof(int)
  157.             );//new int[fLocationPathSize];
  158.             fNoMatchDepth = (int*) fMemoryManager->allocate
  159.             (
  160.                 fLocationPathSize * sizeof(int)
  161.             );//new int[fLocationPathSize];
  162.             fMatched = (int*) fMemoryManager->allocate
  163.             (
  164.                 fLocationPathSize * sizeof(int)
  165.             );//new int[fLocationPathSize];
  166.             for(unsigned int i=0; i < fLocationPathSize; i++) {
  167.                 fStepIndexes->addElement(new (fMemoryManager) ValueStackOf<int>(8, fMemoryManager));
  168.             }
  169.         }
  170.     }
  171. }
  172. // ---------------------------------------------------------------------------
  173. //  XPathMatcher: XMLDocumentHandler methods
  174. // ---------------------------------------------------------------------------
  175. void XPathMatcher::startDocumentFragment() {
  176.     for(unsigned int i = 0; i < fLocationPathSize; i++) {
  177.         fStepIndexes->elementAt(i)->removeAllElements();
  178.         fCurrentStep[i] = 0;
  179.         fNoMatchDepth[i] = 0;
  180.         fMatched[i] = 0;
  181.     }
  182. }
  183. void XPathMatcher::startElement(const XMLElementDecl& elemDecl,
  184.                                 const unsigned int urlId,
  185.                                 const XMLCh* const elemPrefix,
  186. const RefVectorOf<XMLAttr>& attrList,
  187.                                 const unsigned int attrCount) {
  188.     for (int i = 0; i < (int) fLocationPathSize; i++) {
  189.         // push context
  190.         int startStep = fCurrentStep[i];
  191.         fStepIndexes->elementAt(i)->push(startStep);
  192.         // try next xpath, if not matching
  193.         if ((fMatched[i] & XP_MATCHED_D) == XP_MATCHED || fNoMatchDepth[i] > 0) {
  194.             fNoMatchDepth[i]++;
  195.             continue;
  196.         }
  197.         if((fMatched[i] & XP_MATCHED_D) == XP_MATCHED_D) {
  198.             fMatched[i] = XP_MATCHED_DP;
  199.         }
  200.         // consume self::node() steps
  201.         XercesLocationPath* locPath = fLocationPaths->elementAt(i);
  202.         int stepSize = locPath->getStepSize();
  203.         while (fCurrentStep[i] < stepSize &&
  204.                locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::SELF) {
  205.             fCurrentStep[i]++;
  206.         }
  207.         if (fCurrentStep[i] == stepSize) {
  208.             fMatched[i] = XP_MATCHED;
  209.             continue;
  210.         }
  211.         // now if the current step is a descendant step, we let the next
  212.         // step do its thing; if it fails, we reset ourselves
  213.         // to look at this step for next time we're called.
  214.         // so first consume all descendants:
  215.         int descendantStep = fCurrentStep[i];
  216.         while (fCurrentStep[i] < stepSize &&
  217.                locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::DESCENDANT) {
  218.             fCurrentStep[i]++;
  219.         }
  220.         bool sawDescendant = fCurrentStep[i] > descendantStep;
  221.         if (fCurrentStep[i] == stepSize) {
  222.             fNoMatchDepth[i]++;
  223.             continue;
  224.         }
  225.         // match child::... step, if haven't consumed any self::node()
  226.         if ((fCurrentStep[i] == startStep || fCurrentStep[i] > descendantStep) &&
  227.             locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::CHILD) {
  228.             XercesStep* step = locPath->getStep(fCurrentStep[i]);
  229.             XercesNodeTest* nodeTest = step->getNodeTest();
  230.             if (nodeTest->getType() == XercesNodeTest::QNAME) {
  231.                 QName elemQName(elemPrefix, elemDecl.getElementName()->getLocalPart(), urlId, fMemoryManager);
  232. //                if (!(*(nodeTest->getName()) == *(elemDecl.getElementName()))) {
  233.                 if (!(*(nodeTest->getName()) == elemQName)) {
  234.                     if(fCurrentStep[i] > descendantStep) {
  235.                         fCurrentStep[i] = descendantStep;
  236.                         continue;
  237.                     }
  238.                     fNoMatchDepth[i]++;
  239.                     continue;
  240.                 }
  241.             }
  242.             fCurrentStep[i]++;
  243.         }
  244.         if (fCurrentStep[i] == stepSize) {
  245.             if (sawDescendant) {
  246.                 fCurrentStep[i] = descendantStep;
  247.                 fMatched[i] = XP_MATCHED_D;
  248.             }
  249.             else {
  250.                 fMatched[i] = XP_MATCHED;
  251.             }
  252.             continue;
  253.         }
  254.         // match attribute::... step
  255.         if (fCurrentStep[i] < stepSize &&
  256.             locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::ATTRIBUTE) {
  257.             if (attrCount) {
  258.                 XercesNodeTest* nodeTest = locPath->getStep(fCurrentStep[i])->getNodeTest();
  259.                 for (unsigned int attrIndex = 0; attrIndex < attrCount; attrIndex++) {
  260.                     const XMLAttr* curDef = attrList.elementAt(attrIndex);
  261.                     if (nodeTest->getType() != XercesNodeTest::QNAME ||
  262.                         (*(nodeTest->getName()) == *(curDef->getAttName()))) {
  263.                         fCurrentStep[i]++;
  264.                         if (fCurrentStep[i] == stepSize) {
  265.                             fMatched[i] = XP_MATCHED_A;
  266.                             int j=0;
  267.                             for(; j<i && ((fMatched[j] & XP_MATCHED) != XP_MATCHED); j++) ;
  268.                             if(j == i) {
  269.                                 SchemaAttDef* attDef = ((SchemaElementDecl&) elemDecl).getAttDef(curDef->getName(), curDef->getURIId());
  270.                                 DatatypeValidator* dv = (attDef) ? attDef->getDatatypeValidator() : 0;
  271.                                 matched(curDef->getValue(), dv, false);
  272.                             }
  273.                         }
  274.                         break;
  275.                     }
  276.                 }
  277.             }
  278.             if ((fMatched[i] & XP_MATCHED) != XP_MATCHED) {
  279.                 if(fCurrentStep[i] > descendantStep) {
  280.                     fCurrentStep[i] = descendantStep;
  281.                     continue;
  282.                 }
  283.                 fNoMatchDepth[i]++;
  284.             }
  285.         }
  286.     }
  287. }
  288. void XPathMatcher::endElement(const XMLElementDecl& elemDecl,
  289.                               const XMLCh* const elemContent) {
  290.     for(int i = 0; i < (int) fLocationPathSize; i++) {
  291.         // go back a step
  292.         fCurrentStep[i] = fStepIndexes->elementAt(i)->pop();
  293.         // don't do anything, if not matching
  294.         if (fNoMatchDepth[i] > 0) {
  295.             fNoMatchDepth[i]--;
  296.         }
  297.         // signal match, if appropriate
  298.         else {
  299.             int j=0;
  300.             for(; j<i && ((fMatched[j] & XP_MATCHED) != XP_MATCHED); j++) ;
  301.             if (j < i || (fMatched[j] == 0)
  302.                 || ((fMatched[j] & XP_MATCHED_A) == XP_MATCHED_A))
  303. continue;
  304.             DatatypeValidator* dv = ((SchemaElementDecl*) &elemDecl)->getDatatypeValidator();
  305.             bool isNillable = (((SchemaElementDecl *) &elemDecl)->getMiscFlags() & SchemaSymbols::XSD_NILLABLE) != 0;
  306.             matched(elemContent, dv, isNillable);
  307.             fMatched[i] = 0;
  308.         }
  309.     }
  310. }
  311. // ---------------------------------------------------------------------------
  312. //  XPathMatcher: Match methods
  313. // ---------------------------------------------------------------------------
  314. int XPathMatcher::isMatched() {
  315.     // xpath has been matched if any one of the members of the union have matched.
  316.     for (int i=0; i < (int) fLocationPathSize; i++) {
  317.         if (((fMatched[i] & XP_MATCHED) == XP_MATCHED)
  318.             && ((fMatched[i] & XP_MATCHED_DP) != XP_MATCHED_DP))
  319.             return fMatched[i];
  320.     }
  321.     return 0;
  322. }
  323. void XPathMatcher::matched(const XMLCh* const content,
  324.                            DatatypeValidator* const dv,
  325.                            const bool isNil) {
  326.     return;
  327. }
  328. // ---------------------------------------------------------------------------
  329. //  XPathMatcher: Match methods
  330. // ---------------------------------------------------------------------------
  331. int XPathMatcher::getInitialDepth() const
  332. {
  333.     ThrowXML(RuntimeException, XMLExcepts::Regex_NotSupported);
  334.     return 0; // to make some compilers happy
  335. }
  336. XERCES_CPP_NAMESPACE_END
  337. /**
  338.   * End of file XPathMatcher.cpp
  339.   */