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

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2001-2003 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.  * $Id: XercesXPath.hpp,v 1.6 2003/05/22 02:10:52 knoaman Exp $
  58.  */
  59. #if !defined(XERCESXPATH_HPP)
  60. #define XERCESXPATH_HPP
  61. // ---------------------------------------------------------------------------
  62. //  Includes
  63. // ---------------------------------------------------------------------------
  64. #include <xercesc/util/QName.hpp>
  65. #include <xercesc/util/RefVectorOf.hpp>
  66. #include <xercesc/util/ValueVectorOf.hpp>
  67. XERCES_CPP_NAMESPACE_BEGIN
  68. // ---------------------------------------------------------------------------
  69. //  Forward Declarations
  70. // ---------------------------------------------------------------------------
  71. class XMLStringPool;
  72. class NamespaceScope;
  73. class VALIDATORS_EXPORT XercesNodeTest : public XMemory
  74. {
  75. public:
  76.     // -----------------------------------------------------------------------
  77.     //  Constants
  78.     // -----------------------------------------------------------------------
  79.     enum {
  80.         QNAME = 1,
  81.         WILDCARD = 2,
  82.         NODE = 3,
  83.         NAMESPACE= 4
  84.     };
  85.     // -----------------------------------------------------------------------
  86.     //  Constructors/Destructor
  87.     // -----------------------------------------------------------------------
  88.     XercesNodeTest(const short type,
  89.                    MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
  90.     XercesNodeTest(const QName* const qName);
  91.     XercesNodeTest(const XMLCh* const prefix, const unsigned int uriId,
  92.                    MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
  93.     XercesNodeTest(const XercesNodeTest& other);
  94.     ~XercesNodeTest() { delete fName; }
  95.     // -----------------------------------------------------------------------
  96.     //  Operators
  97.     // -----------------------------------------------------------------------
  98.     XercesNodeTest& operator= (const XercesNodeTest& other);
  99.     bool operator== (const XercesNodeTest& other) const;
  100.     bool operator!= (const XercesNodeTest& other) const;
  101. // -----------------------------------------------------------------------
  102.     //  Getter methods
  103.     // -----------------------------------------------------------------------
  104.     short getType() const { return fType; }
  105.     QName* getName() const { return fName; }
  106. private:
  107.     // -----------------------------------------------------------------------
  108.     //  Data members
  109.     // -----------------------------------------------------------------------
  110.     short  fType;
  111.     QName* fName;
  112. };
  113. /**
  114.   * A location path step comprised of an axis and node test.
  115.   */
  116. class VALIDATORS_EXPORT XercesStep : public XMemory
  117. {
  118. public:
  119.     // -----------------------------------------------------------------------
  120.     //  Constants
  121.     // -----------------------------------------------------------------------
  122.     enum { // Axis type
  123.         CHILD = 1,
  124.         ATTRIBUTE = 2,
  125.         SELF = 3,
  126.         DESCENDANT = 4
  127.     };
  128.     // -----------------------------------------------------------------------
  129.     //  Constructors/Destructor
  130.     // -----------------------------------------------------------------------
  131.     XercesStep(const unsigned short axisType, XercesNodeTest* const nodeTest);
  132.     XercesStep(const XercesStep& other);
  133.     ~XercesStep() { delete fNodeTest; }
  134.     // -----------------------------------------------------------------------
  135.     //  Operators
  136.     // -----------------------------------------------------------------------
  137.     XercesStep& operator= (const XercesStep& other);
  138.     bool operator== (const XercesStep& other) const;
  139.     bool operator!= (const XercesStep& other) const;
  140. // -----------------------------------------------------------------------
  141.     //  Getter methods
  142.     // -----------------------------------------------------------------------
  143.     unsigned short getAxisType() const { return fAxisType; }
  144.     XercesNodeTest* getNodeTest() const { return fNodeTest; }
  145. private:
  146.     // -----------------------------------------------------------------------
  147.     //  Data members
  148.     // -----------------------------------------------------------------------
  149.     unsigned short  fAxisType;
  150.     XercesNodeTest* fNodeTest;
  151. };
  152. /**
  153.   * A location path representation for an XPath expression.
  154.   */
  155. class VALIDATORS_EXPORT XercesLocationPath : public XMemory
  156. {
  157. public:
  158.     // -----------------------------------------------------------------------
  159.     //  Constructors/Destructor
  160.     // -----------------------------------------------------------------------
  161.     XercesLocationPath(RefVectorOf<XercesStep>* const steps);
  162.     ~XercesLocationPath() { delete fSteps; }
  163.     // -----------------------------------------------------------------------
  164.     //  Operators
  165.     // -----------------------------------------------------------------------
  166.     bool operator== (const XercesLocationPath& other) const;
  167.     bool operator!= (const XercesLocationPath& other) const;
  168.     // -----------------------------------------------------------------------
  169.     //  Access methods
  170.     // -----------------------------------------------------------------------
  171.     unsigned int getStepSize() const;
  172.     void addStep(XercesStep* const aStep);
  173.     XercesStep* getStep(const unsigned int index) const;
  174. private:
  175.     // -----------------------------------------------------------------------
  176.     //  Unimplemented contstructors and operators
  177.     // -----------------------------------------------------------------------
  178.     XercesLocationPath(const XercesLocationPath& other);
  179.     XercesLocationPath& operator= (const XercesLocationPath& other);
  180.     // -----------------------------------------------------------------------
  181.     //  Data members
  182.     // -----------------------------------------------------------------------
  183.     RefVectorOf<XercesStep>* fSteps;
  184. };
  185. class VALIDATORS_EXPORT XercesXPath : public XMemory
  186. {
  187. public:
  188.     // -----------------------------------------------------------------------
  189.     //  Constants
  190.     // -----------------------------------------------------------------------
  191.     /**
  192.       * [28] ExprToken ::= '(' | ')' | '[' | ']' | '.' | '..' | '@' | ',' | '::'
  193.       *                  | NameTest | NodeType | Operator | FunctionName
  194.       *                  | AxisName | Literal | Number | VariableReference
  195.       */
  196.     enum {
  197.         EXPRTOKEN_OPEN_PAREN                  =  0,
  198.         EXPRTOKEN_CLOSE_PAREN                 =  1,
  199.         EXPRTOKEN_OPEN_BRACKET                =  2,
  200.         EXPRTOKEN_CLOSE_BRACKET               =  3,
  201.         EXPRTOKEN_PERIOD                      =  4,
  202.         EXPRTOKEN_DOUBLE_PERIOD               =  5,
  203.         EXPRTOKEN_ATSIGN                      =  6,
  204.         EXPRTOKEN_COMMA                       =  7,
  205.         EXPRTOKEN_DOUBLE_COLON                =  8,
  206.         EXPRTOKEN_NAMETEST_ANY                =  9,
  207.         EXPRTOKEN_NAMETEST_NAMESPACE          = 10,
  208.         EXPRTOKEN_NAMETEST_QNAME              = 11,
  209.         EXPRTOKEN_NODETYPE_COMMENT            = 12,
  210.         EXPRTOKEN_NODETYPE_TEXT               = 13,
  211.         EXPRTOKEN_NODETYPE_PI                 = 14,
  212.         EXPRTOKEN_NODETYPE_NODE               = 15,
  213.         EXPRTOKEN_OPERATOR_AND                = 16,
  214.         EXPRTOKEN_OPERATOR_OR                 = 17,
  215.         EXPRTOKEN_OPERATOR_MOD                = 18,
  216.         EXPRTOKEN_OPERATOR_DIV                = 19,
  217.         EXPRTOKEN_OPERATOR_MULT               = 20,
  218.         EXPRTOKEN_OPERATOR_SLASH              = 21,
  219.         EXPRTOKEN_OPERATOR_DOUBLE_SLASH       = 22,
  220.         EXPRTOKEN_OPERATOR_UNION              = 23,
  221.         EXPRTOKEN_OPERATOR_PLUS               = 24,
  222.         EXPRTOKEN_OPERATOR_MINUS              = 25,
  223.         EXPRTOKEN_OPERATOR_EQUAL              = 26,
  224.         EXPRTOKEN_OPERATOR_NOT_EQUAL          = 27,
  225.         EXPRTOKEN_OPERATOR_LESS               = 28,
  226.         EXPRTOKEN_OPERATOR_LESS_EQUAL         = 29,
  227.         EXPRTOKEN_OPERATOR_GREATER            = 30,
  228.         EXPRTOKEN_OPERATOR_GREATER_EQUAL      = 31,
  229.         EXPRTOKEN_FUNCTION_NAME               = 32,
  230.         EXPRTOKEN_AXISNAME_ANCESTOR           = 33,
  231.         EXPRTOKEN_AXISNAME_ANCESTOR_OR_SELF   = 34,
  232.         EXPRTOKEN_AXISNAME_ATTRIBUTE          = 35,
  233.         EXPRTOKEN_AXISNAME_CHILD              = 36,
  234.         EXPRTOKEN_AXISNAME_DESCENDANT         = 37,
  235.         EXPRTOKEN_AXISNAME_DESCENDANT_OR_SELF = 38,
  236.         EXPRTOKEN_AXISNAME_FOLLOWING          = 39,
  237.         EXPRTOKEN_AXISNAME_FOLLOWING_SIBLING  = 40,
  238.         EXPRTOKEN_AXISNAME_NAMESPACE          = 41,
  239.         EXPRTOKEN_AXISNAME_PARENT             = 42,
  240.         EXPRTOKEN_AXISNAME_PRECEDING          = 43,
  241.         EXPRTOKEN_AXISNAME_PRECEDING_SIBLING  = 44,
  242.         EXPRTOKEN_AXISNAME_SELF               = 45,
  243.         EXPRTOKEN_LITERAL                     = 46,
  244.         EXPRTOKEN_NUMBER                      = 47,
  245.         EXPRTOKEN_VARIABLE_REFERENCE          = 48
  246.     };
  247.     // -----------------------------------------------------------------------
  248.     //  Constructors/Destructor
  249.     // -----------------------------------------------------------------------
  250.     XercesXPath(const XMLCh* const xpathExpr,
  251.                 XMLStringPool* const stringPool,
  252.                 NamespaceScope* const scopeContext,
  253.                 const unsigned int emptyNamespaceId,
  254.                 const bool isSelector = false,
  255.                 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
  256. ~XercesXPath();
  257.     // -----------------------------------------------------------------------
  258.     //  Operators
  259.     // -----------------------------------------------------------------------
  260.     bool operator== (const XercesXPath& other) const;
  261.     bool operator!= (const XercesXPath& other) const;
  262.     // -----------------------------------------------------------------------
  263.     //  Constructors/Destructor
  264.     // -----------------------------------------------------------------------
  265.     RefVectorOf<XercesLocationPath>* getLocationPaths() const;
  266. private:
  267.     // -----------------------------------------------------------------------
  268.     //  Unimplemented contstructors and operators
  269.     // -----------------------------------------------------------------------
  270.     XercesXPath(const XercesXPath& other);
  271.     XercesXPath& operator= (const XercesXPath& other);
  272.     // -----------------------------------------------------------------------
  273.     //  Helper methods
  274.     // -----------------------------------------------------------------------
  275.     void cleanUp();
  276.     void checkForSelectedAttributes();
  277.     void parseExpression(XMLStringPool* const stringPool,
  278.                          NamespaceScope* const scopeContext);
  279.     // -----------------------------------------------------------------------
  280.     //  Data members
  281.     // -----------------------------------------------------------------------
  282.     unsigned int                     fEmptyNamespaceId;
  283.     XMLCh*                           fExpression;
  284.     RefVectorOf<XercesLocationPath>* fLocationPaths;
  285.     MemoryManager*                   fMemoryManager;
  286. };
  287. class VALIDATORS_EXPORT XPathScanner : public XMemory
  288. {
  289. public:
  290.     // -----------------------------------------------------------------------
  291.     //  Constants
  292.     // -----------------------------------------------------------------------
  293.     enum {
  294.         CHARTYPE_INVALID            =  0,   // invalid XML character
  295.         CHARTYPE_OTHER              =  1,   // not special - one of "#%&;?^`{}~" or DEL
  296.         CHARTYPE_WHITESPACE         =  2,   // one of "tnr " (0x09, 0x0A, 0x0D, 0x20)
  297.         CHARTYPE_EXCLAMATION        =  3,   // '!' (0x21)
  298.         CHARTYPE_QUOTE              =  4,   // '"' or ''' (0x22 and 0x27)
  299.         CHARTYPE_DOLLAR             =  5,   // '$' (0x24)
  300.         CHARTYPE_OPEN_PAREN         =  6,   // '(' (0x28)
  301.         CHARTYPE_CLOSE_PAREN        =  7,   // ')' (0x29)
  302.         CHARTYPE_STAR               =  8,   // '*' (0x2A)
  303.         CHARTYPE_PLUS               =  9,   // '+' (0x2B)
  304.         CHARTYPE_COMMA              = 10,   // ',' (0x2C)
  305.         CHARTYPE_MINUS              = 11,   // '-' (0x2D)
  306.         CHARTYPE_PERIOD             = 12,   // '.' (0x2E)
  307.         CHARTYPE_SLASH              = 13,   // '/' (0x2F)
  308.         CHARTYPE_DIGIT              = 14,   // '0'-'9' (0x30 to 0x39)
  309.         CHARTYPE_COLON              = 15,   // ':' (0x3A)
  310.         CHARTYPE_LESS               = 16,   // '<' (0x3C)
  311.         CHARTYPE_EQUAL              = 17,   // '=' (0x3D)
  312.         CHARTYPE_GREATER            = 18,   // '>' (0x3E)
  313.         CHARTYPE_ATSIGN             = 19,   // '@' (0x40)
  314.         CHARTYPE_LETTER             = 20,   // 'A'-'Z' or 'a'-'z' (0x41 to 0x5A and 0x61 to 0x7A)
  315.         CHARTYPE_OPEN_BRACKET       = 21,   // '[' (0x5B)
  316.         CHARTYPE_CLOSE_BRACKET      = 22,   // ']' (0x5D)
  317.         CHARTYPE_UNDERSCORE         = 23,   // '_' (0x5F)
  318.         CHARTYPE_UNION              = 24,   // '|' (0x7C)
  319.         CHARTYPE_NONASCII           = 25   // Non-ASCII Unicode codepoint (>= 0x80)
  320.     };
  321.     // -----------------------------------------------------------------------
  322.     //  Constructors/Destructor
  323.     // -----------------------------------------------------------------------
  324.     XPathScanner(XMLStringPool* const stringPool);
  325.     virtual ~XPathScanner() {}
  326.     // -----------------------------------------------------------------------
  327.     //  Scan methods
  328.     // -----------------------------------------------------------------------
  329.     bool scanExpression(const XMLCh* const data, int currentOffset,
  330.                         const int endOffset, ValueVectorOf<int>* const tokens);
  331. protected:
  332.     // -----------------------------------------------------------------------
  333.     //  Helper methods
  334.     // -----------------------------------------------------------------------
  335.     /**
  336.       * This method adds the specified token to the token list. By default,
  337.       * this method allows all tokens. However, subclasses can can override
  338.       * this method in order to disallow certain tokens from being used in the
  339.       * scanned XPath expression. This is a convenient way of allowing only
  340.       * a subset of XPath.
  341.       */
  342.     virtual void addToken(ValueVectorOf<int>* const tokens, const int aToken);
  343. private:
  344.     // -----------------------------------------------------------------------
  345.     //  Unimplemented contstructors and operators
  346.     // -----------------------------------------------------------------------
  347.     XPathScanner(const XPathScanner& other);
  348.     XPathScanner& operator= (const XPathScanner& other);
  349.     // -----------------------------------------------------------------------
  350.     //  Helper methods
  351.     // -----------------------------------------------------------------------
  352.     void init();
  353.     // -----------------------------------------------------------------------
  354.     //  Scan methods
  355.     // -----------------------------------------------------------------------
  356.     int scanNCName(const XMLCh* const data, const int endOffset,
  357.                    int currentOffset);
  358.     int scanNumber(const XMLCh* const data, const int endOffset,
  359.                    int currentOffset, ValueVectorOf<int>* const tokens);
  360.     // -----------------------------------------------------------------------
  361.     //  Data members
  362.     // -----------------------------------------------------------------------
  363.     int fAndSymbol;
  364.     int fOrSymbol;
  365.     int fModSymbol;
  366.     int fDivSymbol;
  367.     int fCommentSymbol;
  368.     int fTextSymbol;
  369.     int fPISymbol;
  370.     int fNodeSymbol;
  371.     int fAncestorSymbol;
  372.     int fAncestorOrSelfSymbol;
  373.     int fAttributeSymbol;
  374.     int fChildSymbol;
  375.     int fDescendantSymbol;
  376.     int fDescendantOrSelfSymbol;
  377.     int fFollowingSymbol;
  378.     int fFollowingSiblingSymbol;
  379.     int fNamespaceSymbol;
  380.     int fParentSymbol;
  381.     int fPrecedingSymbol;
  382.     int fPrecedingSiblingSymbol;
  383.     int fSelfSymbol;
  384.     XMLStringPool* fStringPool;
  385.     static const XMLByte fASCIICharMap[128];
  386. };
  387. class VALIDATORS_EXPORT XPathScannerForSchema: public XPathScanner
  388. {
  389. public:
  390.     // -----------------------------------------------------------------------
  391.     //  Constructors/Destructor
  392.     // -----------------------------------------------------------------------
  393.     XPathScannerForSchema(XMLStringPool* const stringPool);
  394.     ~XPathScannerForSchema() {}
  395. protected:
  396.     // -----------------------------------------------------------------------
  397.     //  Helper methods
  398.     // -----------------------------------------------------------------------
  399.     void addToken(ValueVectorOf<int>* const tokens, const int aToken);
  400. private:
  401.     // -----------------------------------------------------------------------
  402.     //  Unimplemented contstructors and operators
  403.     // -----------------------------------------------------------------------
  404.     XPathScannerForSchema(const XPathScannerForSchema& other);
  405.     XPathScannerForSchema& operator= (const XPathScannerForSchema& other);
  406. };
  407. // ---------------------------------------------------------------------------
  408. //  XercesLocationPath: Access methods
  409. // ---------------------------------------------------------------------------
  410. inline unsigned int XercesLocationPath::getStepSize() const {
  411.     if (fSteps)
  412.         return fSteps->size();
  413.     return 0;
  414. }
  415. inline void XercesLocationPath::addStep(XercesStep* const aStep) {
  416.     fSteps->addElement(aStep);
  417. }
  418. inline XercesStep* XercesLocationPath::getStep(const unsigned int index) const {
  419.     if (fSteps)
  420.         return fSteps->elementAt(index);
  421.     return 0;
  422. }
  423. // ---------------------------------------------------------------------------
  424. //  XercesScanner: Helper methods
  425. // ---------------------------------------------------------------------------
  426. inline void XPathScanner::addToken(ValueVectorOf<int>* const tokens,
  427.                                    const int aToken) {
  428.     tokens->addElement(aToken);
  429. }
  430. // ---------------------------------------------------------------------------
  431. //  XercesXPath: Getter methods
  432. // ---------------------------------------------------------------------------
  433. inline RefVectorOf<XercesLocationPath>* XercesXPath::getLocationPaths() const {
  434.     return fLocationPaths;
  435. }
  436. XERCES_CPP_NAMESPACE_END
  437. #endif
  438. /**
  439.   * End of file XercesPath.hpp
  440.   */