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

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