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

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: RegxParser.hpp,v 1.5 2001/05/11 21:51:02 knoaman Exp $
  58.  */
  59. /*
  60.  * A regular expression parser
  61.  */
  62. #if !defined(REGXPARSER_HPP)
  63. #define REGXPARSER_HPP
  64. // ---------------------------------------------------------------------------
  65. //  Includes
  66. // ---------------------------------------------------------------------------
  67. #include <util/RefVectorOf.hpp>
  68. #include <util/XMLUniDefs.hpp>
  69. #include <util/Mutexes.hpp>
  70. // ---------------------------------------------------------------------------
  71. //  Forward Declaration
  72. // ---------------------------------------------------------------------------
  73. class Token;
  74. class RangeToken;
  75. class TokenFactory;
  76. class XMLUTIL_EXPORT RegxParser {
  77. public:
  78. // -----------------------------------------------------------------------
  79.     //  Public constant data
  80.     // -----------------------------------------------------------------------
  81. enum {
  82. T_CHAR = 0,
  83. T_EOF = 1,
  84. T_OR = 2,
  85. T_STAR = 3,
  86. T_PLUS = 4,
  87. T_QUESTION = 5,
  88. T_LPAREN = 6,
  89. T_RPAREN = 7,
  90. T_DOT = 8,
  91. T_LBRACKET = 9,
  92. T_BACKSOLIDUS = 10,
  93. T_CARET = 11,
  94. T_DOLLAR = 12,
  95. T_LPAREN2 = 13,
  96. T_LOOKAHEAD = 14,
  97. T_NEGATIVELOOKAHEAD = 15,
  98. T_LOOKBEHIND = 16,
  99. T_NEGATIVELOOKBEHIND = 17,
  100. T_INDEPENDENT = 18,
  101. T_SET_OPERATIONS = 19,
  102. T_POSIX_CHARCLASS_START = 20,
  103. T_COMMENT = 21,
  104. T_MODIFIERS = 22,
  105. T_CONDITION = 23,
  106. T_XMLSCHEMA_CC_SUBTRACTION = 24
  107. };
  108. static const unsigned short S_NORMAL;
  109. static const unsigned short S_INBRACKETS;
  110. static const unsigned short S_INXBRACKETS;
  111. // -----------------------------------------------------------------------
  112.     //  Public Constructors and Destructor
  113.     // -----------------------------------------------------------------------
  114. RegxParser();
  115. virtual ~RegxParser();
  116.     // -----------------------------------------------------------------------
  117.     //  Getter methods
  118.     // -----------------------------------------------------------------------
  119.     unsigned short getParseContext() const;
  120.     unsigned short getState() const;
  121.     XMLInt32       getCharData() const;
  122.     int            getNoParen() const;
  123. int            getOffset() const;
  124. bool           hasBackReferences() const;
  125.     TokenFactory*  getTokenFactory() const;
  126. // -----------------------------------------------------------------------
  127.     //  Setter methods
  128.     // -----------------------------------------------------------------------
  129. void setParseContext(const unsigned short value);
  130.     void setTokenFactory(TokenFactory* const tokFactory);
  131. // -----------------------------------------------------------------------
  132.     //  Public Parsing methods
  133.     // -----------------------------------------------------------------------
  134. Token* parse(const XMLCh* const regxStr, const int options);
  135. protected:
  136.     // -----------------------------------------------------------------------
  137.     //  Protected Helper methods
  138.     // -----------------------------------------------------------------------
  139.     virtual bool        checkQuestion(const int off);
  140. virtual XMLInt32    decodeEscaped();
  141.     // -----------------------------------------------------------------------
  142.     //  Protected Parsing/Processing methods
  143.     // -----------------------------------------------------------------------
  144. void                processNext();
  145. Token*              parseRegx();
  146. virtual Token*      processCaret();
  147.     virtual Token*      processDollar();
  148. virtual Token*      processLook(const unsigned short tokType);
  149.     virtual Token*      processBacksolidus_A();
  150.     virtual Token*      processBacksolidus_z();
  151.     virtual Token*      processBacksolidus_Z();
  152.     virtual Token*      processBacksolidus_b();
  153.     virtual Token*      processBacksolidus_B();
  154.     virtual Token*      processBacksolidus_lt();
  155.     virtual Token*      processBacksolidus_gt();
  156.     virtual Token*      processBacksolidus_c();
  157.     virtual Token*      processBacksolidus_C();
  158.     virtual Token*      processBacksolidus_i();
  159.     virtual Token*      processBacksolidus_I();
  160.     virtual Token*      processBacksolidus_g();
  161.     virtual Token*      processBacksolidus_X();
  162.     virtual Token*      processBackReference();
  163. virtual Token*      processStar(Token* const tok);
  164. virtual Token*      processPlus(Token* const tok);
  165. virtual Token*      processQuestion(Token* const tok);
  166.     virtual Token*      processParen();
  167.     virtual Token*      processParen2();
  168.     virtual Token*      processCondition();
  169.     virtual Token*      processModifiers();
  170.     virtual Token*      processIndependent();
  171.     virtual RangeToken* parseCharacterClass(const bool useNRange);
  172.     virtual RangeToken* parseSetOperations();
  173. virtual XMLInt32    processCInCharacterClass(RangeToken* const tok,
  174.                                                  const XMLInt32 ch);
  175.     RangeToken*         processBacksolidus_pP(const XMLInt32 ch);
  176.     // -----------------------------------------------------------------------
  177.     //  Protected PreCreated RangeToken access methods
  178.     // -----------------------------------------------------------------------
  179. virtual Token*      getTokenForShorthand(const XMLInt32 ch);
  180. private:
  181.     // -----------------------------------------------------------------------
  182.     //  Private parsing/processing methods
  183.     // -----------------------------------------------------------------------
  184.     Token* parseTerm();
  185. Token* parseFactor();
  186. Token* parseAtom();
  187. // -----------------------------------------------------------------------
  188.     //  Private data types
  189.     // -----------------------------------------------------------------------
  190.     class ReferencePosition
  191.     {
  192.         public :
  193.             ReferencePosition(const int refNo, const int position);
  194.             int fReferenceNo;
  195. int fPosition;
  196.     };
  197.     // -----------------------------------------------------------------------
  198.     //  Private Helper methods
  199.     // -----------------------------------------------------------------------
  200.     bool isSet(const int flag);
  201. int hexChar(const XMLInt32 ch);
  202. // -----------------------------------------------------------------------
  203.     //  Private data members
  204. // -----------------------------------------------------------------------
  205. bool                            fHasBackReferences;
  206. int                             fOptions;
  207. int                             fOffset;
  208. int                             fNoGroups;
  209. int                             fParseContext;
  210. int                             fStringLen;
  211. unsigned short                  fState;
  212. XMLInt32                        fCharData;
  213. XMLCh*                          fString;
  214. RefVectorOf<ReferencePosition>* fReferences;
  215.     TokenFactory*                   fTokenFactory;
  216. XMLMutex fMutex;
  217. };
  218. // ---------------------------------------------------------------------------
  219. //  RegxParser: Getter Methods
  220. // ---------------------------------------------------------------------------
  221. inline unsigned short RegxParser::getParseContext() const {
  222.     return fParseContext;
  223. }
  224. inline unsigned short RegxParser::getState() const {
  225. return fState;
  226. }
  227. inline XMLInt32 RegxParser::getCharData() const {
  228.     return fCharData;
  229. }
  230. inline int RegxParser::getNoParen() const {
  231.     return fNoGroups;
  232. }
  233. inline int RegxParser::getOffset() const {
  234. return fOffset;
  235. }
  236. inline bool RegxParser::hasBackReferences() const {
  237. return fHasBackReferences;
  238. }
  239. inline TokenFactory* RegxParser::getTokenFactory() const {
  240.     return fTokenFactory;
  241. }
  242. // ---------------------------------------------------------------------------
  243. //  RegxParser: Setter Methods
  244. // ---------------------------------------------------------------------------
  245. inline void RegxParser::setParseContext(const unsigned short value) {
  246. fParseContext = value;
  247. }
  248. inline void RegxParser::setTokenFactory(TokenFactory* const tokFactory) {
  249.     fTokenFactory = tokFactory;
  250. }
  251. // ---------------------------------------------------------------------------
  252. //  RegxParser: Helper Methods
  253. // ---------------------------------------------------------------------------
  254. inline bool RegxParser::isSet(const int flag) {
  255.     return (fOptions & flag) == flag;
  256. }
  257. inline int RegxParser::hexChar(const XMLInt32 ch) {
  258. if (ch < chDigit_0 || ch > chLatin_f)
  259. return -1;
  260. if (ch <= chDigit_9)
  261. return ch - chDigit_0;
  262. if (ch < chLatin_A)
  263. return -1;
  264. if (ch <= chLatin_F)
  265. return ch - chLatin_A + 10;
  266. if (ch < chLatin_a)
  267. return -1;
  268. return ch - chLatin_a + 10;
  269. }
  270. #endif
  271. /**
  272.   * End file RegxParser.hpp
  273.   */