UnicodeRangeFactory.cpp
上传用户: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.  * $Log: UnicodeRangeFactory.cpp,v $
  58.  * Revision 1.4  2001/10/15 18:30:40  knoaman
  59.  * Add support for 'Pi' and 'Pf'.
  60.  *
  61.  * Revision 1.3  2001/05/11 13:26:51  tng
  62.  * Copyright update.
  63.  *
  64.  * Revision 1.2  2001/05/03 18:17:56  knoaman
  65.  * Some design changes:
  66.  * o Changed the TokenFactory from a single static instance, to a
  67.  *    normal class. Each RegularExpression object will have its own
  68.  *    instance of TokenFactory, and that instance will be passed to
  69.  *    other classes that need to use a TokenFactory to create Token
  70.  *    objects (with the exception of RangeTokenMap).
  71.  * o Added a new class RangeTokenMap to map a the different ranges
  72.  *    in a given category to a specific RangeFactory object. In the old
  73.  *    design RangeFactory had dual functionality (act as a Map, and as
  74.  *    a factory for creating RangeToken(s)). The RangeTokenMap will
  75.  *    have its own copy of the TokenFactory. There will be only one
  76.  *    instance of the RangeTokenMap class, and that instance will be
  77.  *    lazily deleted when XPlatformUtils::Terminate is called.
  78.  *
  79.  * Revision 1.1  2001/03/02 19:26:48  knoaman
  80.  * Schema: Regular expression handling part II
  81.  *
  82.  */
  83. // ---------------------------------------------------------------------------
  84. //  Includes
  85. // ---------------------------------------------------------------------------
  86. #include <util/regx/UnicodeRangeFactory.hpp>
  87. #include <util/regx/TokenFactory.hpp>
  88. #include <util/regx/RangeToken.hpp>
  89. #include <util/regx/RangeTokenMap.hpp>
  90. #include <util/regx/RegxDefs.hpp>
  91. #include <util/regx/XMLUniCharacter.hpp>
  92. // ---------------------------------------------------------------------------
  93. //  Static member data initialization
  94. // ---------------------------------------------------------------------------
  95. bool UnicodeRangeFactory::fRangesCreated       = false;
  96. bool UnicodeRangeFactory::fKeywordsInitialized = false;
  97. // ---------------------------------------------------------------------------
  98. //  Local data
  99. // ---------------------------------------------------------------------------
  100. const int            UNICATEGSIZE     = 37;
  101. const unsigned short CHAR_LETTER      = 30;
  102. const unsigned short CHAR_MARK        = 31;
  103. const unsigned short CHAR_NUMBER      = 32;
  104. const unsigned short CHAR_SEPARATOR   = 33;
  105. const unsigned short CHAR_OTHER       = 34;
  106. const unsigned short CHAR_PUNCTUATION = 35;
  107. const unsigned short CHAR_SYMBOL      = 36;
  108. const XMLCh uniCategNames[][UNICATEGSIZE] =
  109. {
  110.     {chLatin_C, chLatin_n, chNull},
  111.     {chLatin_L, chLatin_u, chNull},
  112.     {chLatin_L, chLatin_l, chNull},
  113.     {chLatin_L, chLatin_t, chNull},
  114.     {chLatin_L, chLatin_m, chNull},
  115.     {chLatin_L, chLatin_o, chNull},
  116.     {chLatin_M, chLatin_n, chNull},
  117.     {chLatin_M, chLatin_e, chNull},
  118.     {chLatin_M, chLatin_c, chNull},
  119.     {chLatin_N, chLatin_d, chNull},
  120.     {chLatin_N, chLatin_l, chNull},
  121.     {chLatin_N, chLatin_o, chNull},
  122.     {chLatin_Z, chLatin_s, chNull},
  123.     {chLatin_Z, chLatin_l, chNull},
  124.     {chLatin_Z, chLatin_p, chNull},
  125.     {chLatin_C, chLatin_c, chNull},
  126.     {chLatin_C, chLatin_f, chNull},
  127.     {chLatin_C, chLatin_o, chNull},
  128.     {chLatin_C, chLatin_s, chNull},
  129.     {chLatin_P, chLatin_d, chNull},
  130.     {chLatin_P, chLatin_s, chNull},
  131.     {chLatin_P, chLatin_e, chNull},
  132.     {chLatin_P, chLatin_c, chNull},
  133.     {chLatin_P, chLatin_o, chNull},
  134.     {chLatin_S, chLatin_m, chNull},
  135.     {chLatin_S, chLatin_c, chNull},
  136.     {chLatin_S, chLatin_k, chNull},
  137.     {chLatin_S, chLatin_o, chNull},
  138.     {chLatin_P, chLatin_i, chNull},
  139.     {chLatin_P, chLatin_f, chNull},
  140.     {chLatin_L, chNull},
  141.     {chLatin_M, chNull},
  142.     {chLatin_N, chNull},
  143.     {chLatin_Z, chNull},
  144.     {chLatin_C, chNull},
  145.     {chLatin_P, chNull},
  146.     {chLatin_S, chNull},
  147. };
  148. // ---------------------------------------------------------------------------
  149. //  UnicodeRangeFactory: Constructors and Destructor
  150. // ---------------------------------------------------------------------------
  151. UnicodeRangeFactory::UnicodeRangeFactory() {
  152. }
  153. UnicodeRangeFactory::~UnicodeRangeFactory() {
  154. }
  155. // ---------------------------------------------------------------------------
  156. //  UnicodeRangeFactory: Range creation methods
  157. // ---------------------------------------------------------------------------
  158. void UnicodeRangeFactory::buildRanges() {
  159.     if (fRangesCreated)
  160.         return;
  161.     if (!fKeywordsInitialized) {
  162.         initializeKeywordMap();
  163.     }
  164.     RangeTokenMap* rangeTokMap = RangeTokenMap::instance();
  165.     TokenFactory* tokFactory = rangeTokMap->getTokenFactory();
  166. RangeToken* ranges[UNICATEGSIZE];
  167.     for (int i=0; i < UNICATEGSIZE; i++) {
  168.         ranges[i] = tokFactory->createRange();
  169.     }
  170.     for (int j=0; j < 0x10000; j++) {
  171.         unsigned short charType = XMLUniCharacter::getType(j);
  172. ranges[charType]->addRange(j, j);
  173. charType = getUniCategory(charType);
  174. ranges[charType]->addRange(j, j);
  175.     }
  176. ranges[XMLUniCharacter::UNASSIGNED]->addRange(0x10000, Token::UTF16_MAX);
  177. for (int k=0; k < UNICATEGSIZE; k++) {
  178.         rangeTokMap->setRangeToken(uniCategNames[k], ranges[k]);
  179.     }
  180.     // Create all range
  181. RangeToken* tok = tokFactory->createRange();
  182. tok->addRange(0, Token::UTF16_MAX);
  183. rangeTokMap->setRangeToken(fgUniAll, tok);
  184.     // Create alpha range
  185.     tok = tokFactory->createRange();
  186.     tok->mergeRanges(ranges[XMLUniCharacter::UPPERCASE_LETTER]);
  187.     tok->mergeRanges(ranges[XMLUniCharacter::LOWERCASE_LETTER]);
  188.     tok->mergeRanges(ranges[XMLUniCharacter::OTHER_LETTER]);
  189.     rangeTokMap->setRangeToken(fgUniIsAlpha, tok);
  190.     // Create alpha-num range
  191.     RangeToken* alnumTok = tokFactory->createRange();
  192.     alnumTok->mergeRanges(tok);
  193.     alnumTok->mergeRanges(ranges[XMLUniCharacter::DECIMAL_DIGIT_NUMBER]);
  194.     rangeTokMap->setRangeToken(fgUniIsAlnum, alnumTok);
  195.     // Create word range
  196.     tok = tokFactory->createRange();
  197.     tok->mergeRanges(alnumTok);
  198.     tok->addRange(chUnderscore, chUnderscore);
  199.     rangeTokMap->setRangeToken(fgUniIsWord, tok);
  200.     // Create assigned range
  201.     tok = ranges[XMLUniCharacter::UNASSIGNED];
  202.     rangeTokMap->setRangeToken(fgUniAssigned,(RangeToken*)RangeToken::complementRanges(tok,
  203.           tokFactory));
  204.     fRangesCreated = true;
  205. }
  206. // ---------------------------------------------------------------------------
  207. //  UnicodeRangeFactory: Initialization methods
  208. // ---------------------------------------------------------------------------
  209. void UnicodeRangeFactory::initializeKeywordMap() {
  210.     if (fKeywordsInitialized)
  211.         return;
  212.     RangeTokenMap* rangeTokMap = RangeTokenMap::instance();
  213. for (int k=0; k < UNICATEGSIZE; k++) {
  214.         rangeTokMap->addKeywordMap(uniCategNames[k], fgUnicodeCategory);
  215.     }
  216. rangeTokMap->addKeywordMap(fgUniAll, fgUnicodeCategory);
  217.     rangeTokMap->addKeywordMap(fgUniIsAlpha, fgUnicodeCategory);
  218.     rangeTokMap->addKeywordMap(fgUniIsAlnum, fgUnicodeCategory);
  219.     rangeTokMap->addKeywordMap(fgUniIsWord, fgUnicodeCategory);
  220.     rangeTokMap->addKeywordMap(fgUniAssigned, fgUnicodeCategory);
  221.     fKeywordsInitialized = true;
  222. }
  223. // ---------------------------------------------------------------------------
  224. //  UnicodeRangeFactory: Helper methods
  225. // ---------------------------------------------------------------------------
  226. unsigned short UnicodeRangeFactory::getUniCategory(const unsigned short type)
  227. {
  228.     switch(type) {
  229.     case XMLUniCharacter::UPPERCASE_LETTER:
  230.     case XMLUniCharacter::LOWERCASE_LETTER:
  231.     case XMLUniCharacter::TITLECASE_LETTER:
  232.     case XMLUniCharacter::MODIFIER_LETTER:
  233.     case XMLUniCharacter::OTHER_LETTER:
  234.         return CHAR_LETTER;
  235.     case XMLUniCharacter::NON_SPACING_MARK:
  236.     case XMLUniCharacter::COMBINING_SPACING_MARK:
  237.     case XMLUniCharacter::ENCLOSING_MARK:
  238.         return CHAR_MARK;
  239.     case XMLUniCharacter::DECIMAL_DIGIT_NUMBER:
  240.     case XMLUniCharacter::LETTER_NUMBER:
  241.     case XMLUniCharacter::OTHER_NUMBER:
  242. return CHAR_NUMBER;
  243.     case XMLUniCharacter::SPACE_SEPARATOR:
  244.     case XMLUniCharacter::LINE_SEPARATOR:
  245.     case XMLUniCharacter::PARAGRAPH_SEPARATOR:
  246. return CHAR_SEPARATOR;
  247.     case XMLUniCharacter::CONTROL:
  248.     case XMLUniCharacter::FORMAT:
  249.     case XMLUniCharacter::SURROGATE:
  250.     case XMLUniCharacter::PRIVATE_USE:
  251.     case XMLUniCharacter::UNASSIGNED:
  252. return CHAR_OTHER;
  253.     case XMLUniCharacter::CONNECTOR_PUNCTUATION:
  254.     case XMLUniCharacter::DASH_PUNCTUATION:
  255.     case XMLUniCharacter::START_PUNCTUATION:
  256.     case XMLUniCharacter::END_PUNCTUATION:
  257.     case XMLUniCharacter::OTHER_PUNCTUATION:
  258.     case XMLUniCharacter::INITIAL_PUNCTUATION:
  259.     case XMLUniCharacter::FINAL_PUNCTUATION:
  260. return CHAR_PUNCTUATION;
  261.     case XMLUniCharacter::MATH_SYMBOL:
  262.     case XMLUniCharacter::CURRENCY_SYMBOL:
  263.     case XMLUniCharacter::MODIFIER_SYMBOL:
  264.     case XMLUniCharacter::OTHER_SYMBOL:
  265. return CHAR_SYMBOL;
  266.     }
  267.     return 0;
  268. }
  269. /**
  270.   * End of file UnicodeRangeFactory.cpp
  271.   */