PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/xerces-c-src2_4_0/src/xercesc/util/regx/TokenFactory.cpp

http://atf2flightsim.googlecode.com/
C++ | 593 lines | 273 code | 122 blank | 198 comment | 38 complexity | e3687a74e63186e3e2ecb9a53bc5f118 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  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. * $Log: TokenFactory.cpp,v $
  58. * Revision 1.1.1.1 2009/03/14 06:42:32 whitegr
  59. * epics channel archiver
  60. *
  61. * Revision 1.9 2003/10/17 16:44:34 knoaman
  62. * Fix multithreading problem.
  63. *
  64. * Revision 1.8 2003/05/18 14:02:06 knoaman
  65. * Memory manager implementation: pass per instance manager.
  66. *
  67. * Revision 1.7 2003/05/16 21:37:00 knoaman
  68. * Memory manager implementation: Modify constructors to pass in the memory manager.
  69. *
  70. * Revision 1.6 2003/05/16 00:03:10 knoaman
  71. * Partial implementation of the configurable memory manager.
  72. *
  73. * Revision 1.5 2003/03/04 21:11:12 knoaman
  74. * [Bug 17516] Thread safety problems in ../util/ and ../util/regx.
  75. *
  76. * Revision 1.4 2002/12/24 17:59:07 tng
  77. * Build with ICU 2.4
  78. *
  79. * Revision 1.3 2002/11/04 15:17:00 tng
  80. * C++ Namespace Support.
  81. *
  82. * Revision 1.2 2002/03/18 19:29:53 knoaman
  83. * Change constant names to eliminate possible conflict with user defined ones.
  84. *
  85. * Revision 1.1.1.1 2002/02/01 22:22:31 peiyongz
  86. * sane_include
  87. *
  88. * Revision 1.5 2001/06/07 20:55:39 tng
  89. * Fix no newline at the end warning. By Pei Yong Zhang.
  90. *
  91. * Revision 1.4 2001/05/11 13:26:51 tng
  92. * Copyright update.
  93. *
  94. * Revision 1.3 2001/05/03 18:17:52 knoaman
  95. * Some design changes:
  96. * o Changed the TokenFactory from a single static instance, to a
  97. * normal class. Each RegularExpression object will have its own
  98. * instance of TokenFactory, and that instance will be passed to
  99. * other classes that need to use a TokenFactory to create Token
  100. * objects (with the exception of RangeTokenMap).
  101. * o Added a new class RangeTokenMap to map a the different ranges
  102. * in a given category to a specific RangeFactory object. In the old
  103. * design RangeFactory had dual functionality (act as a Map, and as
  104. * a factory for creating RangeToken(s)). The RangeTokenMap will
  105. * have its own copy of the TokenFactory. There will be only one
  106. * instance of the RangeTokenMap class, and that instance will be
  107. * lazily deleted when XPlatformUtils::Terminate is called.
  108. *
  109. * Revision 1.2 2001/03/22 13:23:34 knoaman
  110. * Minor modifications to eliminate compiler warnings.
  111. *
  112. * Revision 1.1 2001/03/02 19:23:00 knoaman
  113. * Schema: Regular expression handling part I
  114. *
  115. */
  116. // ---------------------------------------------------------------------------
  117. // Includes
  118. // ---------------------------------------------------------------------------
  119. #include <xercesc/util/regx/TokenFactory.hpp>
  120. #include <xercesc/util/regx/TokenInc.hpp>
  121. #include <xercesc/util/regx/XMLRangeFactory.hpp>
  122. #include <xercesc/util/regx/ASCIIRangeFactory.hpp>
  123. #include <xercesc/util/regx/UnicodeRangeFactory.hpp>
  124. #include <xercesc/util/regx/BlockRangeFactory.hpp>
  125. #include <xercesc/util/regx/RangeTokenMap.hpp>
  126. #include <xercesc/util/regx/RegxDefs.hpp>
  127. #include <xercesc/util/XMLRegisterCleanup.hpp>
  128. XERCES_CPP_NAMESPACE_BEGIN
  129. // ---------------------------------------------------------------------------
  130. // Static member data initialization
  131. // ---------------------------------------------------------------------------
  132. bool TokenFactory::fRangeInitialized = false;
  133. // ---------------------------------------------------------------------------
  134. // Local static data
  135. // ---------------------------------------------------------------------------
  136. static bool sTokFactoryMutexRegistered = false;
  137. static XMLMutex* sTokFactoryMutex = 0;
  138. static XMLRegisterCleanup tokenFactoryMutexCleanup;
  139. // ---------------------------------------------------------------------------
  140. // Local, static functions
  141. // ---------------------------------------------------------------------------
  142. // Cleanup for the TokenFactory mutex
  143. void TokenFactory::reinitTokenFactoryMutex()
  144. {
  145. delete sTokFactoryMutex;
  146. sTokFactoryMutex = 0;
  147. sTokFactoryMutexRegistered = false;
  148. }
  149. // We need to fault in this mutex. But, since its used for synchronization
  150. // itself, we have to do this the low level way using a compare and swap.
  151. static XMLMutex& gTokenFactoryMutex()
  152. {
  153. if (!sTokFactoryMutex)
  154. {
  155. XMLMutex* tmpMutex = new XMLMutex;
  156. if (XMLPlatformUtils::compareAndSwap((void**)&sTokFactoryMutex, tmpMutex, 0))
  157. {
  158. // Someone beat us to it, so let's clean up ours
  159. delete tmpMutex;
  160. }
  161. // Now lock it and try to register it
  162. XMLMutexLock lock(sTokFactoryMutex);
  163. // If we got here first, then register it and set the registered flag
  164. if (!sTokFactoryMutexRegistered)
  165. {
  166. tokenFactoryMutexCleanup.registerCleanup(TokenFactory::reinitTokenFactoryMutex);
  167. sTokFactoryMutexRegistered = true;
  168. }
  169. }
  170. return *sTokFactoryMutex;
  171. }
  172. // ---------------------------------------------------------------------------
  173. // TokenFactory: Constructors and Destructor
  174. // ---------------------------------------------------------------------------
  175. TokenFactory::TokenFactory(MemoryManager* const manager) :
  176. fTokens(new (manager) RefVectorOf<Token> (16, true, manager))
  177. , fEmpty(0)
  178. , fLineBegin(0)
  179. , fLineBegin2(0)
  180. , fLineEnd(0)
  181. , fStringBegin(0)
  182. , fStringEnd(0)
  183. , fStringEnd2(0)
  184. , fWordEdge(0)
  185. , fNotWordEdge(0)
  186. , fWordEnd(0)
  187. , fWordBegin(0)
  188. , fDot(0)
  189. , fCombiningChar(0)
  190. , fGrapheme(0)
  191. , fMemoryManager(manager)
  192. {
  193. }
  194. TokenFactory::~TokenFactory() {
  195. delete fTokens;
  196. fTokens = 0;
  197. }
  198. // ---------------------------------------------------------------------------
  199. // TokenFactory - Factory methods
  200. // ---------------------------------------------------------------------------
  201. Token* TokenFactory::createToken(const unsigned short tokType) {
  202. if (tokType == Token::T_EMPTY && fEmpty != 0)
  203. return fEmpty;
  204. Token* tmpTok = new (fMemoryManager) Token(tokType);
  205. if (tokType == Token::T_EMPTY) {
  206. fEmpty = tmpTok;
  207. }
  208. fTokens->addElement(tmpTok);
  209. return tmpTok;
  210. }
  211. ParenToken* TokenFactory::createLook(const unsigned short tokType,
  212. Token* const token) {
  213. ParenToken* tmpTok = new (fMemoryManager) ParenToken(tokType, token, 0);
  214. fTokens->addElement(tmpTok);
  215. return tmpTok;
  216. }
  217. ParenToken* TokenFactory::createParenthesis(Token* const token,
  218. const int noGroups) {
  219. ParenToken* tmpTok = new (fMemoryManager) ParenToken(Token::T_PAREN, token, noGroups);
  220. fTokens->addElement(tmpTok);
  221. return tmpTok;
  222. }
  223. ClosureToken* TokenFactory::createClosure(Token* const token,
  224. bool isNonGreedy) {
  225. ClosureToken* tmpTok = isNonGreedy ? new (fMemoryManager) ClosureToken(Token::T_NONGREEDYCLOSURE, token)
  226. : new (fMemoryManager) ClosureToken(Token::T_CLOSURE, token);
  227. fTokens->addElement(tmpTok);
  228. return tmpTok;
  229. }
  230. ConcatToken* TokenFactory::createConcat(Token* const token1,
  231. Token* const token2) {
  232. ConcatToken* tmpTok = new (fMemoryManager) ConcatToken(token1, token2);
  233. fTokens->addElement(tmpTok);
  234. return tmpTok;
  235. }
  236. UnionToken* TokenFactory::createUnion(const bool isConcat) {
  237. UnionToken* tmpTok = isConcat ? new (fMemoryManager) UnionToken(Token::T_CONCAT)
  238. : new (fMemoryManager) UnionToken(Token::T_UNION);
  239. fTokens->addElement(tmpTok);
  240. return tmpTok;
  241. }
  242. RangeToken* TokenFactory::createRange(const bool isNegRange){
  243. RangeToken* tmpTok = isNegRange ? new (fMemoryManager) RangeToken(Token::T_NRANGE, fMemoryManager)
  244. : new (fMemoryManager) RangeToken(Token::T_RANGE, fMemoryManager);
  245. fTokens->addElement(tmpTok);
  246. return tmpTok;
  247. return 0;
  248. }
  249. CharToken* TokenFactory::createChar(const XMLUInt32 ch, const bool isAnchor) {
  250. CharToken* tmpTok = isAnchor ? new (fMemoryManager) CharToken(Token::T_ANCHOR, ch)
  251. : new (fMemoryManager) CharToken(Token::T_CHAR, ch);
  252. fTokens->addElement(tmpTok);
  253. return tmpTok;
  254. }
  255. StringToken* TokenFactory::createBackReference(const int noRefs) {
  256. StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_BACKREFERENCE, 0, noRefs, fMemoryManager);
  257. fTokens->addElement(tmpTok);
  258. return tmpTok;
  259. }
  260. StringToken* TokenFactory::createString(const XMLCh* const literal) {
  261. StringToken* tmpTok = new (fMemoryManager) StringToken(Token::T_STRING, literal, 0, fMemoryManager);
  262. fTokens->addElement(tmpTok);
  263. return tmpTok;
  264. }
  265. ModifierToken* TokenFactory::createModifierGroup(Token* const child,
  266. const int add,
  267. const int mask) {
  268. ModifierToken* tmpTok = new (fMemoryManager) ModifierToken(child, add, mask);
  269. fTokens->addElement(tmpTok);
  270. return tmpTok;
  271. }
  272. ConditionToken* TokenFactory::createCondition(const int refNo,
  273. Token* const condition,
  274. Token* const yesFlow,
  275. Token* const noFlow) {
  276. ConditionToken* tmpTok = new (fMemoryManager) ConditionToken(refNo, condition, yesFlow,
  277. noFlow);
  278. fTokens->addElement(tmpTok);
  279. return tmpTok;
  280. }
  281. // ---------------------------------------------------------------------------
  282. // TokenFactory - Getter methods
  283. // ---------------------------------------------------------------------------
  284. RangeToken* TokenFactory::getRange(const XMLCh* const keyword,
  285. const bool complement) {
  286. if (!fRangeInitialized) {
  287. initializeRegistry();
  288. }
  289. return RangeTokenMap::instance()->getRange(keyword, complement);
  290. }
  291. Token* TokenFactory::getLineBegin() {
  292. if (fLineBegin == 0)
  293. fLineBegin = createChar(chCaret, true);
  294. return fLineBegin;
  295. }
  296. Token* TokenFactory::getLineBegin2() {
  297. if (fLineBegin2 == 0)
  298. fLineBegin2 = createChar(chAt, true);
  299. return fLineBegin2;
  300. }
  301. Token* TokenFactory::getLineEnd() {
  302. if (fLineEnd == 0)
  303. fLineEnd = createChar(chDollarSign, true);
  304. return fLineEnd;
  305. }
  306. Token* TokenFactory::getStringBegin() {
  307. if (fStringBegin == 0)
  308. fStringBegin = createChar(chLatin_A, true);
  309. return fStringBegin;
  310. }
  311. Token* TokenFactory::getStringEnd() {
  312. if (fStringEnd == 0)
  313. fStringEnd = createChar(chLatin_z, true);
  314. return fStringEnd;
  315. }
  316. Token* TokenFactory::getStringEnd2() {
  317. if (fStringEnd2 == 0)
  318. fStringEnd2 = createChar(chLatin_Z, true);
  319. return fStringEnd2;
  320. }
  321. Token* TokenFactory::getWordEdge() {
  322. if (fWordEdge == 0)
  323. fWordEdge = createChar(chLatin_b, true);
  324. return fWordEdge;
  325. }
  326. Token* TokenFactory::getNotWordEdge(){
  327. if (fNotWordEdge == 0)
  328. fNotWordEdge = createChar(chLatin_B, true);
  329. return fNotWordEdge;
  330. }
  331. Token* TokenFactory::getWordBegin() {
  332. if (fWordBegin == 0)
  333. fWordBegin = createChar(chOpenAngle, true);
  334. return fWordBegin;
  335. }
  336. Token* TokenFactory::getWordEnd() {
  337. if (fWordEnd == 0)
  338. fWordEnd = createChar(chCloseAngle, true);
  339. return fWordEnd;
  340. }
  341. Token* TokenFactory::getDot() {
  342. if (fDot == 0)
  343. fDot = createToken(Token::T_DOT);
  344. return fDot;
  345. }
  346. Token* TokenFactory::getCombiningCharacterSequence() {
  347. if (fCombiningChar == 0) {
  348. Token* foo = createClosure(getRange(fgUniMark)); // \pM*
  349. foo = createConcat(getRange(fgUniMark, true), foo); // \PM + \pM*
  350. fCombiningChar = foo;
  351. }
  352. return fCombiningChar;
  353. }
  354. // static final String viramaString =
  355. Token* TokenFactory::getGraphemePattern() {
  356. if (fGrapheme == 0) {
  357. Token* base_char = createRange(); // [{ASSIGNED}]-[{M},{C}]
  358. base_char->mergeRanges(getRange(fgUniAssigned));
  359. base_char->subtractRanges(getRange(fgUniMark));
  360. base_char->subtractRanges(getRange(fgUniControl));
  361. Token* virama = createRange();
  362. virama->addRange(0x094D, 0x094D);
  363. virama->addRange(0x09CD, 0x09CD);
  364. virama->addRange(0x0A4D, 0x0A4D);
  365. virama->addRange(0x0ACD, 0x0ACD);
  366. virama->addRange(0x0B4D, 0x0B4D);
  367. virama->addRange(0x0BCD, 0x0BCD);
  368. virama->addRange(0x0C4D, 0x0C4D);
  369. virama->addRange(0x0CCD, 0x0CCD);
  370. virama->addRange(0x0D4D, 0x0D4D);
  371. virama->addRange(0x0E3A, 0x0E3A);
  372. virama->addRange(0x0F84, 0x0F84);
  373. Token* combiner_wo_virama = createRange();
  374. combiner_wo_virama->mergeRanges(getRange(fgUniMark));
  375. combiner_wo_virama->addRange(0x1160, 0x11FF); // hangul_medial and hangul_final
  376. combiner_wo_virama->addRange(0xFF9F, 0xFF9F); // extras
  377. Token* left = TokenFactory::createUnion(); // base_char?
  378. left->addChild(base_char, this);
  379. left->addChild(createToken(Token::T_EMPTY), this);
  380. Token* foo = createUnion();
  381. foo->addChild(TokenFactory::createConcat(virama,getRange(fgUniLetter)), this);
  382. foo->addChild(combiner_wo_virama, this);
  383. foo = createClosure(foo);
  384. foo = createConcat(left, foo);
  385. fGrapheme = foo;
  386. }
  387. return fGrapheme;
  388. }
  389. // ---------------------------------------------------------------------------
  390. // TokenFactory - Helper methods
  391. // ---------------------------------------------------------------------------
  392. void TokenFactory::initializeRegistry() {
  393. if (fRangeInitialized)
  394. return;
  395. // Use a faux scope to synchronize while we do this
  396. {
  397. XMLMutexLock lockInit(&gTokenFactoryMutex());
  398. if (!fRangeInitialized) {
  399. RangeTokenMap::instance()->initializeRegistry();
  400. // Add categories
  401. RangeTokenMap::instance()->addCategory(fgXMLCategory);
  402. RangeTokenMap::instance()->addCategory(fgASCIICategory);
  403. RangeTokenMap::instance()->addCategory(fgUnicodeCategory);
  404. RangeTokenMap::instance()->addCategory(fgBlockCategory);
  405. // Add xml range factory
  406. RangeFactory* rangeFact = new XMLRangeFactory();
  407. RangeTokenMap::instance()->addRangeMap(fgXMLCategory, rangeFact);
  408. rangeFact->initializeKeywordMap();
  409. // Add ascii range factory
  410. rangeFact = new ASCIIRangeFactory();
  411. RangeTokenMap::instance()->addRangeMap(fgASCIICategory, rangeFact);
  412. rangeFact->initializeKeywordMap();
  413. // Add unicode range factory
  414. rangeFact = new UnicodeRangeFactory();
  415. RangeTokenMap::instance()->addRangeMap(fgUnicodeCategory, rangeFact);
  416. rangeFact->initializeKeywordMap();
  417. // Add block range factory
  418. rangeFact = new BlockRangeFactory();
  419. RangeTokenMap::instance()->addRangeMap(fgBlockCategory, rangeFact);
  420. rangeFact->initializeKeywordMap();
  421. fRangeInitialized = true;
  422. }
  423. }
  424. }
  425. /*
  426. #if defined (XML_USE_ICU_TRANSCODER)
  427. #include <unicode/uchar.h>
  428. #endif
  429. #include <stdio.h>
  430. void TokenFactory::printUnicode() {
  431. #if defined (XML_USE_ICU_TRANSCODER)
  432. //
  433. // Write it out to a temp file to be read back into this source later.
  434. //
  435. printf("Printing\n");
  436. //sprintf(msg, "Printing\n");
  437. FILE* outFl = fopen("table.out", "wt+");
  438. fprintf(outFl, "const XMLByte fgUniCharsTable[0x10000] =\n{ ");
  439. for (unsigned int index = 0; index <= 0xFFFF; index += 16)
  440. {
  441. fprintf(outFl
  442. , " , 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"
  443. , (unsigned int)u_charType(index)
  444. , (unsigned int)u_charType(index+1)
  445. , (unsigned int)u_charType(index+2)
  446. , (unsigned int)u_charType(index+3)
  447. , (unsigned int)u_charType(index+4)
  448. , (unsigned int)u_charType(index+5)
  449. , (unsigned int)u_charType(index+6)
  450. , (unsigned int)u_charType(index+7)
  451. , (unsigned int)u_charType(index+8)
  452. , (unsigned int)u_charType(index+9)
  453. , (unsigned int)u_charType(index+10)
  454. , (unsigned int)u_charType(index+11)
  455. , (unsigned int)u_charType(index+12)
  456. , (unsigned int)u_charType(index+13)
  457. , (unsigned int)u_charType(index+14)
  458. , (unsigned int)u_charType(index+15));
  459. }
  460. fprintf(outFl, "};\n");
  461. fclose(outFl);
  462. #endif
  463. }
  464. */
  465. XERCES_CPP_NAMESPACE_END
  466. /**
  467. * End of file TokenFactory.cpp
  468. */