1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id$
20  */
21 
22 // ---------------------------------------------------------------------------
23 //  Includes
24 // ---------------------------------------------------------------------------
25 #include <xercesc/util/regx/TokenFactory.hpp>
26 #include <xercesc/util/regx/TokenInc.hpp>
27 #include <xercesc/util/regx/XMLRangeFactory.hpp>
28 #include <xercesc/util/regx/ASCIIRangeFactory.hpp>
29 #include <xercesc/util/regx/UnicodeRangeFactory.hpp>
30 #include <xercesc/util/regx/BlockRangeFactory.hpp>
31 #include <xercesc/util/regx/RangeTokenMap.hpp>
32 #include <xercesc/util/regx/RegxDefs.hpp>
33 
34 XERCES_CPP_NAMESPACE_BEGIN
35 
36 
37 // ---------------------------------------------------------------------------
38 //  TokenFactory: Constructors and Destructor
39 // ---------------------------------------------------------------------------
TokenFactory(MemoryManager * const manager)40 TokenFactory::TokenFactory(MemoryManager* const manager) :
41     fTokens(new (manager) RefVectorOf<Token> (16, true, manager))
42     , fEmpty(0)
43     , fLineBegin(0)
44     , fLineEnd(0)
45     , fDot(0)
46     , fMemoryManager(manager)
47 {
48 
49 }
50 
~TokenFactory()51 TokenFactory::~TokenFactory() {
52 
53     delete fTokens;
54     fTokens = 0;
55 }
56 
57 // ---------------------------------------------------------------------------
58 //  TokenFactory - Factory methods
59 // ---------------------------------------------------------------------------
createToken(const Token::tokType tkType)60 Token* TokenFactory::createToken(const Token::tokType tkType) {
61 
62     if (tkType == Token::T_EMPTY && fEmpty != 0)
63         return fEmpty;
64 
65     Token* tmpTok = new (fMemoryManager) Token(tkType, fMemoryManager);
66 
67     if (tkType == Token::T_EMPTY) {
68         fEmpty = tmpTok;
69     }
70 
71     fTokens->addElement(tmpTok);
72 
73     return tmpTok;
74 }
75 
76 
createParenthesis(Token * const token,const int noGroups)77 ParenToken* TokenFactory::createParenthesis(Token* const token,
78                                             const int noGroups) {
79 
80     ParenToken* tmpTok = new (fMemoryManager) ParenToken(Token::T_PAREN, token, noGroups, fMemoryManager);
81 
82     fTokens->addElement(tmpTok);
83     return tmpTok;
84 }
85 
createClosure(Token * const token,bool isNonGreedy)86 ClosureToken* TokenFactory::createClosure(Token* const token,
87                                           bool isNonGreedy) {
88 
89     ClosureToken* tmpTok = isNonGreedy ? new (fMemoryManager) ClosureToken(Token::T_NONGREEDYCLOSURE, token, fMemoryManager)
90                                        : new (fMemoryManager) ClosureToken(Token::T_CLOSURE, token, fMemoryManager);
91 
92     fTokens->addElement(tmpTok);
93     return tmpTok;
94 }
95 
createConcat(Token * const token1,Token * const token2)96 ConcatToken* TokenFactory::createConcat(Token* const token1,
97                                         Token* const token2) {
98 
99     ConcatToken* tmpTok = new (fMemoryManager) ConcatToken(token1, token2, fMemoryManager);
100 
101     fTokens->addElement(tmpTok);
102     return tmpTok;
103 }
104 
createUnion(const bool isConcat)105 UnionToken* TokenFactory::createUnion(const bool isConcat) {
106 
107     UnionToken* tmpTok = isConcat ? new (fMemoryManager) UnionToken(Token::T_CONCAT, fMemoryManager)
108                                   : new (fMemoryManager) UnionToken(Token::T_UNION, fMemoryManager);
109 
110     fTokens->addElement(tmpTok);
111     return tmpTok;
112 }
113 
createRange(const bool isNegRange)114 RangeToken* TokenFactory::createRange(const bool isNegRange){
115 
116 
117     RangeToken* tmpTok = isNegRange ? new (fMemoryManager) RangeToken(Token::T_NRANGE, fMemoryManager)
118                                    : new (fMemoryManager) RangeToken(Token::T_RANGE, fMemoryManager);
119 
120     fTokens->addElement(tmpTok);
121     return tmpTok;
122 }
123 
createChar(const XMLUInt32 ch,const bool isAnchor)124 CharToken* TokenFactory::createChar(const XMLUInt32 ch, const bool isAnchor) {
125 
126     CharToken* tmpTok = isAnchor ? new (fMemoryManager) CharToken(Token::T_ANCHOR, ch, fMemoryManager)
127                                 : new (fMemoryManager) CharToken(Token::T_CHAR, ch, fMemoryManager);
128 
129     fTokens->addElement(tmpTok);
130     return tmpTok;
131 }
132 
createBackReference(const int noRefs)133 StringToken* TokenFactory::createBackReference(const int noRefs) {
134 
135     StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_BACKREFERENCE, 0, noRefs, fMemoryManager);
136 
137     fTokens->addElement(tmpTok);
138     return tmpTok;
139 }
140 
createString(const XMLCh * const literal)141 StringToken* TokenFactory::createString(const XMLCh* const literal) {
142 
143     StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_STRING, literal, 0, fMemoryManager);
144 
145     fTokens->addElement(tmpTok);
146     return tmpTok;
147 }
148 
149 // ---------------------------------------------------------------------------
150 //  TokenFactory - Getter methods
151 // ---------------------------------------------------------------------------
staticGetRange(const XMLCh * const keyword,const bool complement)152 RangeToken* TokenFactory::staticGetRange(const XMLCh* const keyword,
153                                    const bool complement) {
154 
155     return RangeTokenMap::instance()->getRange(keyword, complement);
156 }
157 
getLineBegin()158 Token* TokenFactory::getLineBegin() {
159 
160     if (fLineBegin == 0)
161         fLineBegin = createChar(chCaret, true);
162 
163     return fLineBegin;
164 }
165 
getLineEnd()166 Token* TokenFactory::getLineEnd() {
167 
168     if (fLineEnd == 0)
169         fLineEnd = createChar(chDollarSign, true);
170 
171     return fLineEnd;
172 }
173 
getDot()174 Token* TokenFactory::getDot() {
175 
176     if (fDot == 0)
177         fDot = createToken(Token::T_DOT);
178 
179     return fDot;
180 }
181 
182 /*
183 #if HAVE_CONFIG_H
184 #    include <config.h>
185 #endif
186 
187 #if XERCES_USE_TRANSCODER_ICU
188    #include <unicode/uchar.h>
189 #endif
190 
191 #include <stdio.h>
192 void TokenFactory::printUnicode() {
193 
194 #if XERCES_USE_TRANSCODER_ICU
195     //
196     //  Write it out to a temp file to be read back into this source later.
197     //
198     printf("Printing\n");
199     //sprintf(msg, "Printing\n");
200     FILE* outFl = fopen("table.out", "wt+");
201     fprintf(outFl, "const XMLByte fgUniCharsTable[0x10000] =\n{    ");
202     for (unsigned int index = 0; index <= 0xFFFF; index += 16)
203     {
204         fprintf(outFl
205                 , "    , 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"
206                 , (unsigned int)u_charType(index)
207                 , (unsigned int)u_charType(index+1)
208                 , (unsigned int)u_charType(index+2)
209                 , (unsigned int)u_charType(index+3)
210                 , (unsigned int)u_charType(index+4)
211                 , (unsigned int)u_charType(index+5)
212                 , (unsigned int)u_charType(index+6)
213                 , (unsigned int)u_charType(index+7)
214                 , (unsigned int)u_charType(index+8)
215                 , (unsigned int)u_charType(index+9)
216                 , (unsigned int)u_charType(index+10)
217                 , (unsigned int)u_charType(index+11)
218                 , (unsigned int)u_charType(index+12)
219                 , (unsigned int)u_charType(index+13)
220                 , (unsigned int)u_charType(index+14)
221                 , (unsigned int)u_charType(index+15));
222     }
223     fprintf(outFl, "};\n");
224 
225     fclose(outFl);
226 #endif
227 }
228 */
229 
230 XERCES_CPP_NAMESPACE_END
231 
232 /**
233   * End of file TokenFactory.cpp
234   */
235