XPathMatcher.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) 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.2  2001/11/15 17:10:19  knoaman
  59.  * Particle derivation checking support.
  60.  *
  61.  * Revision 1.1  2001/11/02 14:08:40  knoaman
  62.  * Add support for identity constraints.
  63.  *
  64.  */
  65. // ---------------------------------------------------------------------------
  66. //  Includes
  67. // ---------------------------------------------------------------------------
  68. #include <validators/schema/identity/XPathMatcher.hpp>
  69. #include <validators/schema/identity/XercesXPath.hpp>
  70. #include <validators/schema/SchemaElementDecl.hpp>
  71. #include <validators/schema/SchemaAttDef.hpp>
  72. #include <validators/schema/SchemaSymbols.hpp>
  73. // ---------------------------------------------------------------------------
  74. //  XPathMatcher: Constructors and Destructor
  75. // ---------------------------------------------------------------------------
  76. XPathMatcher::XPathMatcher(XercesXPath* const xpath)
  77.     : fShouldBufferContent(false)
  78.     , fBufferContent(false)
  79.     , fLocationPathSize(0)
  80.     , fMatched(0)
  81.     , fNoMatchDepth(0)
  82.     , fCurrentStep(0)
  83.     , fStepIndexes(0)
  84.     , fLocationPaths(0)
  85.     , fIdentityConstraint(0)
  86.     , fMatchedBuffer(128)
  87. {
  88.     try {
  89.         init(xpath);
  90.     }
  91.     catch(...) {
  92.         cleanUp();
  93.         throw;
  94.     }
  95. }
  96. XPathMatcher::XPathMatcher(XercesXPath* const xpath, 
  97.                            const bool shouldBufferContent,
  98.                            IdentityConstraint* const ic)
  99.     : fShouldBufferContent(shouldBufferContent)
  100.     , fLocationPathSize(0)
  101.     , fMatched(0)
  102.     , fNoMatchDepth(0)
  103.     , fCurrentStep(0)
  104.     , fLocationPaths(0)
  105.     , fIdentityConstraint(ic)
  106.     , fMatchedBuffer(0)
  107. {
  108.     try {
  109.         init(xpath);
  110.     }
  111.     catch(...) {
  112.         cleanUp();
  113.         throw;
  114.     }
  115. }
  116. XPathMatcher::~XPathMatcher()
  117. {
  118.     cleanUp();
  119. }
  120. // ---------------------------------------------------------------------------
  121. //  XPathMatcher: Helper methods
  122. // ---------------------------------------------------------------------------
  123. void XPathMatcher::init(XercesXPath* const xpath) {
  124.     if (xpath) {
  125.         fLocationPaths = xpath->getLocationPaths();
  126.         fLocationPathSize = (fLocationPaths ? fLocationPaths->size() : 0);
  127.         if (fLocationPathSize) {
  128.             fStepIndexes = new RefVectorOf<ValueStackOf<int> >(fLocationPathSize);
  129.             fCurrentStep = new int[fLocationPathSize];
  130.             fNoMatchDepth = new int[fLocationPathSize];
  131.             fMatched = new bool[fLocationPathSize];
  132.             for(unsigned int i=0; i < fLocationPathSize; i++) {
  133.                 fStepIndexes->addElement(new ValueStackOf<int>(8));
  134.             }
  135.         }
  136.     }
  137. }
  138. void XPathMatcher::clear() {
  139.         fBufferContent = false;
  140.         fMatchedBuffer.reset();
  141.         for(int i = 0; i < (int) fLocationPathSize; i++)
  142.             fMatched[i] = false;
  143. }
  144. // ---------------------------------------------------------------------------
  145. //  XPathMatcher: XMLDocumentHandler methods
  146. // ---------------------------------------------------------------------------
  147. void XPathMatcher::startDocumentFragment() {
  148.     // reset state
  149.     clear();
  150.     for(unsigned int i = 0; i < fLocationPathSize; i++) {
  151.         fStepIndexes->elementAt(i)->removeAllElements();
  152.         fCurrentStep[i] = 0;
  153.         fNoMatchDepth[i] = 0;
  154.         fMatched[i] = false;
  155.     }
  156. }
  157. void XPathMatcher::startElement(const XMLElementDecl& elemDecl,
  158.                                 const unsigned int urlId,
  159.                                 const XMLCh* const elemPrefix,
  160. const RefVectorOf<XMLAttr>& attrList,
  161.                                 const unsigned int attrCount) {
  162.     for (int i = 0; i < (int) fLocationPathSize; i++) {
  163.         // push context 
  164.         int startStep = fCurrentStep[i];
  165.         fStepIndexes->elementAt(i)->push(startStep);
  166.         // try next xpath, if not matching
  167.         if (fMatched[i] || fNoMatchDepth[i] > 0) {
  168.             fNoMatchDepth[i]++;
  169.             continue;
  170.         }
  171.         // consume self::node() steps
  172.         XercesLocationPath* locPath = fLocationPaths->elementAt(i);
  173.         int stepSize = locPath->getStepSize();
  174.         while (fCurrentStep[i] < stepSize &&
  175.                locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::SELF) {
  176.             fCurrentStep[i]++;
  177.         }
  178.         if (fCurrentStep[i] == stepSize) {
  179.             fMatched[i] = true;
  180.             int j=0;
  181.             for(; j<i && !fMatched[j]; j++);
  182.             if(j==i)
  183.                 fBufferContent = fShouldBufferContent;
  184.             continue;
  185.         }
  186.         
  187.         // now if the current step is a descendant step, we let the next
  188.         // step do its thing; if it fails, we reset ourselves
  189.         // to look at this step for next time we're called.
  190.         // so first consume all descendants:
  191.         int descendantStep = fCurrentStep[i];
  192.         while (fCurrentStep[i] < stepSize &&
  193.                locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::DESCENDANT) {
  194.             fCurrentStep[i]++;
  195.         }
  196.         if (fCurrentStep[i] == stepSize) {
  197.             fNoMatchDepth[i]++;
  198.             continue;
  199.         }
  200.         // match child::... step, if haven't consumed any self::node()
  201.         if ((fCurrentStep[i] == startStep || fCurrentStep[i] > descendantStep) &&
  202.             locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::CHILD) {
  203.             XercesStep* step = locPath->getStep(fCurrentStep[i]);
  204.             XercesNodeTest* nodeTest = step->getNodeTest();
  205.             if (nodeTest->getType() == XercesNodeTest::QNAME) {
  206.                 QName elemQName(elemPrefix, elemDecl.getElementName()->getLocalPart(), urlId);
  207. //                if (!(*(nodeTest->getName()) == *(elemDecl.getElementName()))) {
  208.                 if (!(*(nodeTest->getName()) == elemQName)) {
  209.                     if(fCurrentStep[i] > descendantStep) {
  210.                         fCurrentStep[i] = descendantStep;
  211.                         continue;
  212.                     }
  213.                     fNoMatchDepth[i]++;
  214.                     continue;
  215.                 }
  216.             }
  217.             fCurrentStep[i]++;
  218.         }
  219.         if (fCurrentStep[i] == stepSize) {
  220.             fMatched[i] = true;
  221.             int j=0;
  222.             for(; j<i && !fMatched[j]; j++);
  223.             if(j==i)
  224.                 fBufferContent = fShouldBufferContent;
  225.             continue;
  226.         }
  227.         // match attribute::... step
  228.         if (fCurrentStep[i] < stepSize &&
  229.             locPath->getStep(fCurrentStep[i])->getAxisType() == XercesStep::ATTRIBUTE) {
  230.             if (attrCount) {
  231.                 XercesNodeTest* nodeTest = locPath->getStep(fCurrentStep[i])->getNodeTest();
  232.                 for (unsigned int attrIndex = 0; attrIndex < attrCount; attrIndex++) {
  233.                     const XMLAttr* curDef = attrList.elementAt(attrIndex);
  234.                     if (nodeTest->getType() != XercesNodeTest::QNAME ||
  235.                         (*(nodeTest->getName()) == *(curDef->getAttName()))) {
  236.                         fCurrentStep[i]++;
  237.                         if (fCurrentStep[i] == stepSize) {
  238.                             fMatched[i] = true;
  239.                             int j=0;
  240.                             for(; j<i && !fMatched[j]; j++);
  241.                             if(j == i) {
  242.                                 SchemaAttDef* attDef = ((SchemaElementDecl&) elemDecl).getAttDef(curDef->getName(), curDef->getURIId());
  243.                                 DatatypeValidator* dv = (attDef) ? attDef->getDatatypeValidator() : 0;
  244.                                 matched(curDef->getValue(), dv, false);
  245.                             }
  246.                         }
  247.                         break;
  248.                     }
  249.                 }
  250.             }
  251.             if (!fMatched[i]) {
  252.                 if(fCurrentStep[i] > descendantStep) {
  253.                     fCurrentStep[i] = descendantStep;
  254.                     continue;
  255.                 }
  256.                 fNoMatchDepth[i]++;
  257.             }
  258.         }
  259.     }
  260. }
  261. void XPathMatcher::docCharacters(const XMLCh* const chars,
  262.                                  const unsigned int length) {
  263.     // collect match content
  264.     // so long as one of our paths is matching, store the content
  265.     for(int i=0; i < (int) fLocationPathSize; i++) {
  266.         if (fBufferContent && fNoMatchDepth[i] == 0) {
  267.             fMatchedBuffer.append(chars, length);
  268.             break;
  269.         }
  270.     }
  271. }
  272. void XPathMatcher::endElement(const XMLElementDecl& elemDecl) {
  273.     for(int i = 0; i < (int) fLocationPathSize; i++) {
  274.         // don't do anything, if not matching
  275.         if (fNoMatchDepth[i] > 0) {
  276.             fNoMatchDepth[i]--;
  277.         }
  278.         // signal match, if appropriate
  279.         else {
  280.             int j=0;
  281.             for(; j<i && !fMatched[j]; j++);
  282.             if (j < i) 
  283. continue;
  284.             if (fBufferContent) {
  285.                 DatatypeValidator* dv = ((SchemaElementDecl*) &elemDecl)->getDatatypeValidator();
  286.                 bool isNillable = (((SchemaElementDecl *) &elemDecl)->getMiscFlags() & SchemaSymbols::NILLABLE) != 0;
  287.                 fBufferContent = false;
  288.                 matched(fMatchedBuffer.getRawBuffer(), dv, isNillable);
  289.             }
  290.             clear();
  291.         }
  292.         // go back a step
  293.         fCurrentStep[i] = fStepIndexes->elementAt(i)->pop();
  294.     }
  295. }
  296. void XPathMatcher::endDocumentFragment() {
  297.     clear();
  298. }
  299. // ---------------------------------------------------------------------------
  300. //  XPathMatcher: Match methods
  301. // ---------------------------------------------------------------------------
  302. bool XPathMatcher::isMatched() {
  303.     // xpath has been matched if any one of the members of the union have matched.
  304.     for (int i=0; i < (int) fLocationPathSize; i++) {
  305.         if (fMatched[i])
  306.             return true;
  307.     }
  308.     return false;
  309. }
  310. void XPathMatcher::matched(const XMLCh* const content,
  311.                            DatatypeValidator* const dv,
  312.                            const bool isNil) {
  313.     return;
  314. }
  315. /**
  316.   * End of file XPathMatcher.cpp
  317.   */