GME  13
TokenFactory.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: TokenFactory.cpp 678879 2008-07-22 20:05:05Z amassari $
00020  */
00021 
00022 // ---------------------------------------------------------------------------
00023 //  Includes
00024 // ---------------------------------------------------------------------------
00025 #include <xercesc/util/regx/TokenFactory.hpp>
00026 #include <xercesc/util/regx/TokenInc.hpp>
00027 #include <xercesc/util/regx/XMLRangeFactory.hpp>
00028 #include <xercesc/util/regx/ASCIIRangeFactory.hpp>
00029 #include <xercesc/util/regx/UnicodeRangeFactory.hpp>
00030 #include <xercesc/util/regx/BlockRangeFactory.hpp>
00031 #include <xercesc/util/regx/RangeTokenMap.hpp>
00032 #include <xercesc/util/regx/RegxDefs.hpp>
00033 
00034 XERCES_CPP_NAMESPACE_BEGIN
00035 
00036 
00037 // ---------------------------------------------------------------------------
00038 //  TokenFactory: Constructors and Destructor
00039 // ---------------------------------------------------------------------------
00040 TokenFactory::TokenFactory(MemoryManager* const manager) :
00041     fTokens(new (manager) RefVectorOf<Token> (16, true, manager))
00042     , fEmpty(0)
00043     , fLineBegin(0)
00044     , fLineEnd(0)
00045     , fDot(0)
00046     , fMemoryManager(manager)
00047 {
00048 
00049 }
00050 
00051 TokenFactory::~TokenFactory() {
00052 
00053     delete fTokens;
00054     fTokens = 0;
00055 }
00056 
00057 // ---------------------------------------------------------------------------
00058 //  TokenFactory - Factory methods
00059 // ---------------------------------------------------------------------------
00060 Token* TokenFactory::createToken(const Token::tokType tkType) {
00061 
00062     if (tkType == Token::T_EMPTY && fEmpty != 0)
00063         return fEmpty;
00064 
00065     Token* tmpTok = new (fMemoryManager) Token(tkType, fMemoryManager);
00066 
00067     if (tkType == Token::T_EMPTY) {
00068         fEmpty = tmpTok;
00069     }
00070 
00071     fTokens->addElement(tmpTok);
00072 
00073     return tmpTok;
00074 }
00075 
00076 
00077 ParenToken* TokenFactory::createParenthesis(Token* const token,
00078                                             const int noGroups) {
00079 
00080     ParenToken* tmpTok = new (fMemoryManager) ParenToken(Token::T_PAREN, token, noGroups, fMemoryManager);
00081 
00082     fTokens->addElement(tmpTok);
00083     return tmpTok;
00084 }
00085 
00086 ClosureToken* TokenFactory::createClosure(Token* const token,
00087                                           bool isNonGreedy) {
00088 
00089     ClosureToken* tmpTok = isNonGreedy ? new (fMemoryManager) ClosureToken(Token::T_NONGREEDYCLOSURE, token, fMemoryManager)
00090                                        : new (fMemoryManager) ClosureToken(Token::T_CLOSURE, token, fMemoryManager);
00091     
00092     fTokens->addElement(tmpTok);
00093     return tmpTok;
00094 }
00095 
00096 ConcatToken* TokenFactory::createConcat(Token* const token1,
00097                                         Token* const token2) {
00098 
00099     ConcatToken* tmpTok = new (fMemoryManager) ConcatToken(token1, token2, fMemoryManager);
00100     
00101     fTokens->addElement(tmpTok);
00102     return tmpTok;
00103 }
00104 
00105 UnionToken* TokenFactory::createUnion(const bool isConcat) {
00106 
00107     UnionToken* tmpTok = isConcat ? new (fMemoryManager) UnionToken(Token::T_CONCAT, fMemoryManager)
00108                                   : new (fMemoryManager) UnionToken(Token::T_UNION, fMemoryManager);
00109 
00110     fTokens->addElement(tmpTok);
00111     return tmpTok;
00112 }
00113 
00114 RangeToken* TokenFactory::createRange(const bool isNegRange){
00115 
00116 
00117     RangeToken* tmpTok = isNegRange ? new (fMemoryManager) RangeToken(Token::T_NRANGE, fMemoryManager)
00118                                    : new (fMemoryManager) RangeToken(Token::T_RANGE, fMemoryManager);
00119 
00120     fTokens->addElement(tmpTok);
00121     return tmpTok;
00122 }
00123 
00124 CharToken* TokenFactory::createChar(const XMLUInt32 ch, const bool isAnchor) {
00125 
00126     CharToken* tmpTok = isAnchor ? new (fMemoryManager) CharToken(Token::T_ANCHOR, ch, fMemoryManager)
00127                                 : new (fMemoryManager) CharToken(Token::T_CHAR, ch, fMemoryManager);
00128 
00129     fTokens->addElement(tmpTok);
00130     return tmpTok;
00131 }
00132 
00133 StringToken* TokenFactory::createBackReference(const int noRefs) {
00134 
00135     StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_BACKREFERENCE, 0, noRefs, fMemoryManager);
00136 
00137     fTokens->addElement(tmpTok);
00138     return tmpTok;
00139 }
00140 
00141 StringToken* TokenFactory::createString(const XMLCh* const literal) {
00142 
00143     StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_STRING, literal, 0, fMemoryManager);
00144 
00145     fTokens->addElement(tmpTok);
00146     return tmpTok;
00147 }
00148 
00149 // ---------------------------------------------------------------------------
00150 //  TokenFactory - Getter methods
00151 // ---------------------------------------------------------------------------
00152 RangeToken* TokenFactory::staticGetRange(const XMLCh* const keyword,
00153                                    const bool complement) {
00154 
00155     return RangeTokenMap::instance()->getRange(keyword, complement);
00156 }
00157 
00158 Token* TokenFactory::getLineBegin() {
00159 
00160     if (fLineBegin == 0)
00161         fLineBegin = createChar(chCaret, true);
00162 
00163     return fLineBegin;
00164 }
00165 
00166 Token* TokenFactory::getLineEnd() {
00167 
00168     if (fLineEnd == 0)
00169         fLineEnd = createChar(chDollarSign, true);
00170 
00171     return fLineEnd;
00172 }
00173 
00174 Token* TokenFactory::getDot() {
00175 
00176     if (fDot == 0)
00177         fDot = createToken(Token::T_DOT);
00178 
00179     return fDot;
00180 }
00181 
00182 /*
00183 #if HAVE_CONFIG_H
00184 #    include <config.h>
00185 #endif
00186 
00187 #if XERCES_USE_TRANSCODER_ICU
00188    #include <unicode/uchar.h>
00189 #endif
00190 
00191 #include <stdio.h>
00192 void TokenFactory::printUnicode() {
00193 
00194 #if XERCES_USE_TRANSCODER_ICU
00195     //
00196     //  Write it out to a temp file to be read back into this source later.
00197     //
00198     printf("Printing\n");
00199     //sprintf(msg, "Printing\n");
00200     FILE* outFl = fopen("table.out", "wt+");
00201     fprintf(outFl, "const XMLByte fgUniCharsTable[0x10000] =\n{    ");
00202     for (unsigned int index = 0; index <= 0xFFFF; index += 16)
00203     {
00204         fprintf(outFl
00205                 , "    , 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\n"
00206                 , (unsigned int)u_charType(index)
00207                 , (unsigned int)u_charType(index+1)
00208                 , (unsigned int)u_charType(index+2)
00209                 , (unsigned int)u_charType(index+3)
00210                 , (unsigned int)u_charType(index+4)
00211                 , (unsigned int)u_charType(index+5)
00212                 , (unsigned int)u_charType(index+6)
00213                 , (unsigned int)u_charType(index+7)
00214                 , (unsigned int)u_charType(index+8)
00215                 , (unsigned int)u_charType(index+9)
00216                 , (unsigned int)u_charType(index+10)
00217                 , (unsigned int)u_charType(index+11)
00218                 , (unsigned int)u_charType(index+12)
00219                 , (unsigned int)u_charType(index+13)
00220                 , (unsigned int)u_charType(index+14)
00221                 , (unsigned int)u_charType(index+15));
00222     }
00223     fprintf(outFl, "};\n");
00224 
00225     fclose(outFl);
00226 #endif
00227 }
00228 */
00229 
00230 XERCES_CPP_NAMESPACE_END
00231