QName.cpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:17k
源码类别:

词法分析

开发平台:

Visual 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: QName.cpp,v $
  58.  * Revision 1.8  2003/05/16 06:01:52  knoaman
  59.  * Partial implementation of the configurable memory manager.
  60.  *
  61.  * Revision 1.7  2003/05/15 19:04:35  knoaman
  62.  * Partial implementation of the configurable memory manager.
  63.  *
  64.  * Revision 1.6  2002/11/04 15:22:04  tng
  65.  * C++ Namespace Support.
  66.  *
  67.  * Revision 1.5  2002/10/30 21:52:00  tng
  68.  * [Bug 13641] compiler-generated copy-constructor for QName doesn't do the right thing.
  69.  *
  70.  * Revision 1.4  2002/09/24 19:51:24  tng
  71.  * Performance: use XMLString::equals instead of XMLString::compareString
  72.  *
  73.  * Revision 1.3  2002/09/05 16:06:41  tng
  74.  * [Bug 12232] Make operator to be constant.
  75.  *
  76.  * Revision 1.2  2002/04/02 15:31:48  knoaman
  77.  * Modiy QName comparison (operator=).
  78.  *
  79.  * Revision 1.1.1.1  2002/02/01 22:22:11  peiyongz
  80.  * sane_include
  81.  *
  82.  * Revision 1.8  2001/12/06 17:48:36  tng
  83.  * Performance Enhancement.  Added setNPrefix and setNLocalPart methods that allow code to take advantage of the fact that it knows the length of the prefix and local name, when possible.  That can avoid a copy of the prefix into a null-terminated temporary variable before copying into the fPrefix.
  84.  * Also changed the getRawName method so that it would simply return the local part when there is no prefix, instead of allocating another buffer to copy the local part into the fRawName.
  85.  * When there is a prefix, changed the getRawName to copy the prefix and local part into the fRawName using XMLString::moveChars instead of using XMLString::copyString and XMLString::catString.  The catString method has to loop past the prefix portion of the fRawName, which seems like a waste.
  86.  * By Henry Zongaro.
  87.  *
  88.  * Revision 1.7  2001/07/24 18:31:47  knoaman
  89.  * Added support for <group> + extra constraint checking for complexType
  90.  *
  91.  * Revision 1.6  2001/05/11 13:26:28  tng
  92.  * Copyright update.
  93.  *
  94.  * Revision 1.5  2001/04/19 18:17:08  tng
  95.  * Schema: SchemaValidator update, and use QName in Content Model
  96.  *
  97.  * Revision 1.4  2001/03/21 21:56:11  tng
  98.  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  99.  *
  100.  * Revision 1.3  2001/02/27 14:48:35  tng
  101.  * Schema: Add CMAny and ContentLeafNameTypeVector, by Pei Yong Zhang
  102.  *
  103.  * Revision 1.2  2001/02/26 21:56:15  tng
  104.  * Schema: QName can also be constructed with rawName.
  105.  *
  106.  * Revision 1.1  2001/02/26 19:44:25  tng
  107.  * Schema: add utility class QName, by Pei Yong Zhang.
  108.  *
  109.  */
  110. #include <xercesc/util/QName.hpp>
  111. XERCES_CPP_NAMESPACE_BEGIN
  112. // ---------------------------------------------------------------------------
  113. //  QName: Constructors and Destructor
  114. // ---------------------------------------------------------------------------
  115. QName::QName(MemoryManager* const manager) :
  116.     fMemoryManager(manager)
  117.     , fPrefix(0)
  118.     , fPrefixBufSz(0)
  119.     , fLocalPart(0)
  120.     , fLocalPartBufSz(0)
  121.     , fRawName(0)
  122.     , fRawNameBufSz(0)
  123.     , fURIId(0)
  124. {
  125. }
  126. QName::QName( const XMLCh* const   prefix
  127.             , const XMLCh* const   localPart
  128.             , const unsigned int   uriId
  129.             , MemoryManager* const manager) :
  130.     fMemoryManager(manager)
  131.     , fPrefix(0)
  132.     , fPrefixBufSz(0)
  133.     , fLocalPart(0)
  134.     , fLocalPartBufSz(0)
  135.     , fRawName(0)
  136.     , fRawNameBufSz(0)
  137.     , fURIId(0)
  138. {
  139.     try
  140.     {
  141.         //
  142.         //  Just call the local setters to set up everything. Too much
  143.         //  work is required to replicate that functionality here.
  144.         //
  145.         setName(prefix, localPart, uriId);
  146.     }
  147.     catch(...)
  148.     {
  149.         cleanUp();
  150.     }
  151. }
  152. QName::QName( const XMLCh* const rawName
  153.             , const unsigned int uriId
  154.             , MemoryManager* const manager) :
  155.     fMemoryManager(manager)
  156.     , fPrefix(0)
  157.     , fPrefixBufSz(0)
  158.     , fLocalPart(0)
  159.     , fLocalPartBufSz(0)
  160.     , fRawName(0)
  161.     , fRawNameBufSz(0)
  162.     , fURIId(0)
  163. {
  164.     try
  165.     {
  166.         //
  167.         //  Just call the local setters to set up everything. Too much
  168.         //  work is required to replicate that functionality here.
  169.         //
  170.         setName(rawName, uriId);
  171.     }
  172.     catch(...)
  173.     {
  174.         cleanUp();
  175.     }
  176. }
  177. QName::~QName()
  178. {
  179. cleanUp();
  180. }
  181. // ---------------------------------------------------------------------------
  182. //  QName: Copy Constructors
  183. // ---------------------------------------------------------------------------
  184. QName::QName(const QName& qname) :
  185.     fMemoryManager(qname.fMemoryManager)
  186.     , fPrefix(0)
  187.     , fPrefixBufSz(0)
  188.     , fLocalPart(0)
  189.     , fLocalPartBufSz(0)
  190.     , fRawName(0)
  191.     , fRawNameBufSz(0)
  192.     , fURIId(0)
  193. {
  194.     unsigned int newLen;
  195.     newLen = XMLString::stringLen(qname.getLocalPart());
  196.     fLocalPartBufSz = newLen + 8;
  197.     fLocalPart = (XMLCh*) fMemoryManager->allocate
  198.     (
  199.         (fLocalPartBufSz + 1) * sizeof(XMLCh)
  200.     ); //new XMLCh[fLocalPartBufSz + 1];
  201.     XMLString::moveChars(fLocalPart, qname.getLocalPart(), newLen + 1);
  202.     newLen = XMLString::stringLen(qname.getPrefix());
  203.     fPrefixBufSz = newLen + 8;
  204.     fPrefix = (XMLCh*) fMemoryManager->allocate
  205.     (
  206.         (fPrefixBufSz + 1) * sizeof(XMLCh)
  207.     ); //new XMLCh[fPrefixBufSz + 1];
  208.     XMLString::moveChars(fPrefix, qname.getPrefix(), newLen + 1);
  209.     fURIId = qname.getURI();
  210. }
  211. // ---------------------------------------------------------------------------
  212. //  QName: Getter methods
  213. // ---------------------------------------------------------------------------
  214. const XMLCh* QName::getRawName() const
  215. {
  216.     //
  217.     //  If there is no buffer, or if there is but we've not faulted in the
  218.     //  value yet, then we have to do that now.
  219.     //
  220.     if (!fRawName || !*fRawName)
  221.     {
  222.         //
  223.         //  If we have a prefix, then do the prefix:name version. Else, its
  224.         //  just the name.
  225.         //
  226.         if (*fPrefix)
  227.         {
  228.             //
  229.             //  Calculate the worst case size buffer we will need. We use the
  230.             //  current high water marks of the prefix and name buffers, so it
  231.             //  might be a little wasteful of memory but we don't have to do
  232.             //  string len operations on the two strings.
  233.             //
  234.             const unsigned int neededLen = fPrefixBufSz + fLocalPartBufSz + 1;
  235.             //
  236.             //  If no buffer, or the current one is too small, then allocate one
  237.             //  and get rid of any old one.
  238.             //
  239.             if (!fRawName || (neededLen > fRawNameBufSz))
  240.             {
  241.                 fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  242.                 // We have to cast off the const'ness to do this
  243.                 ((QName*)this)->fRawNameBufSz = neededLen;
  244.                 ((QName*)this)->fRawName = (XMLCh*) fMemoryManager->allocate
  245.                 (
  246.                     (neededLen + 1) * sizeof(XMLCh)
  247.                 ); //new XMLCh[neededLen + 1];
  248.                 // Make sure its initially empty
  249.                 *fRawName = 0;
  250.             }
  251.             const unsigned int prefixLen = XMLString::stringLen(fPrefix);
  252.             XMLString::moveChars(fRawName, fPrefix, prefixLen);
  253.             fRawName[prefixLen] = chColon;
  254.             XMLString::copyString(&fRawName[prefixLen+1], fLocalPart);
  255.         }
  256.          else
  257.         {
  258.             return fLocalPart;
  259.         }
  260.     }
  261.     return fRawName;
  262. }
  263. XMLCh* QName::getRawName()
  264. {
  265.     //
  266.     //  If there is no buffer, or if there is but we've not faulted in the
  267.     //  value yet, then we have to do that now.
  268.     //
  269.     if (!fRawName || !*fRawName)
  270.     {
  271.         //
  272.         //  If we have a prefix, then do the prefix:name version. Else, its
  273.         //  just the name.
  274.         //
  275.         if (*fPrefix)
  276.         {
  277.             //
  278.             //  Calculate the worst case size buffer we will need. We use the
  279.             //  current high water marks of the prefix and name buffers, so it
  280.             //  might be a little wasteful of memory but we don't have to do
  281.             //  string len operations on the two strings.
  282.             //
  283.             const unsigned int neededLen = fPrefixBufSz + fLocalPartBufSz + 1;
  284.             //
  285.             //  If no buffer, or the current one is too small, then allocate one
  286.             //  and get rid of any old one.
  287.             //
  288.             if (!fRawName || (neededLen > fRawNameBufSz))
  289.             {
  290.                 fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  291.                 // We have to cast off the const'ness to do this
  292.                 ((QName*)this)->fRawNameBufSz = neededLen;
  293.                 ((QName*)this)->fRawName = (XMLCh*) fMemoryManager->allocate
  294.                 (
  295.                     (neededLen + 1) * sizeof(XMLCh)
  296.                 ); //new XMLCh[neededLen + 1];
  297.                 // Make sure its initially empty
  298.                 *fRawName = 0;
  299.             }
  300.             const unsigned int prefixLen = XMLString::stringLen(fPrefix);
  301.             XMLString::moveChars(fRawName, fPrefix, prefixLen);
  302.             fRawName[prefixLen] = chColon;
  303.             XMLString::copyString(&fRawName[prefixLen+1], fLocalPart);
  304.         }
  305.          else
  306.         {
  307.             return fLocalPart;
  308.         }
  309.     }
  310.     return fRawName;
  311. }
  312. // ---------------------------------------------------------------------------
  313. //  QName: Setter methods
  314. // ---------------------------------------------------------------------------
  315. void QName::setName(const XMLCh* const    prefix
  316.                   , const XMLCh* const    localPart
  317.                   , const unsigned int    uriId)
  318. {
  319.     setPrefix(prefix);
  320.     setLocalPart(localPart);
  321.     // And clean up any QName and leave it undone until/if asked for again
  322.     if (fRawName)
  323.         *fRawName = 0;
  324.     // And finally store the URI id parameter
  325.     fURIId = uriId;
  326. }
  327. void QName::setName(const XMLCh* const    rawName
  328.                   , const unsigned int    uriId)
  329. {
  330.     //set the rawName
  331.     unsigned int newLen;
  332.     newLen = XMLString::stringLen(rawName);
  333.     if (!fRawNameBufSz || (newLen > fRawNameBufSz))
  334.     {
  335.         fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  336.         fRawNameBufSz = newLen + 8;
  337.         fRawName = (XMLCh*) fMemoryManager->allocate
  338.         (
  339.             (fRawNameBufSz + 1) * sizeof(XMLCh)
  340.         ); //new XMLCh[fRawNameBufSz + 1];
  341.     }
  342.     XMLString::moveChars(fRawName, rawName, newLen + 1);
  343.     //find out the prefix and localPart from the rawName
  344.     const int colonInd = XMLString::indexOf(rawName, chColon);
  345.     if (colonInd >= 0)
  346.     {
  347.         setNPrefix(rawName, colonInd);
  348.     }
  349.      else
  350.     {
  351.         // No colon, so we just have a name with no prefix
  352.         setPrefix(XMLUni::fgZeroLenString);
  353.     }
  354.     setNLocalPart(&rawName[colonInd+1], newLen-colonInd-1);
  355.     // And finally store the URI id parameter
  356.     fURIId = uriId;
  357. }
  358. void QName::setPrefix(const XMLCh* prefix)
  359. {
  360.     unsigned int newLen;
  361.     newLen = XMLString::stringLen(prefix);
  362.     if (!fPrefixBufSz || (newLen > fPrefixBufSz))
  363.     {
  364.         fMemoryManager->deallocate(fPrefix); //delete [] fPrefix;
  365.         fPrefixBufSz = newLen + 8;
  366.         fPrefix = (XMLCh*) fMemoryManager->allocate
  367.         (
  368.             (fPrefixBufSz + 1) * sizeof(XMLCh)
  369.         ); //new XMLCh[fPrefixBufSz + 1];
  370.     }
  371.     XMLString::moveChars(fPrefix, prefix, newLen + 1);
  372. }
  373. void QName::setNPrefix(const XMLCh* prefix, const unsigned int newLen)
  374. {
  375.     if (!fPrefixBufSz || (newLen > fPrefixBufSz))
  376.     {
  377.         fMemoryManager->deallocate(fPrefix); //delete [] fPrefix;
  378.         fPrefixBufSz = newLen + 8;
  379.         fPrefix = (XMLCh*) fMemoryManager->allocate
  380.         (
  381.             (fPrefixBufSz + 1) * sizeof(XMLCh)
  382.         ); //new XMLCh[fPrefixBufSz + 1];
  383.     }
  384.     XMLString::moveChars(fPrefix, prefix, newLen);
  385.     fPrefix[newLen] = chNull;
  386. }
  387. void QName::setLocalPart(const XMLCh* localPart)
  388. {
  389.     unsigned int newLen;
  390.     newLen = XMLString::stringLen(localPart);
  391.     if (!fLocalPartBufSz || (newLen > fLocalPartBufSz))
  392.     {
  393.         fMemoryManager->deallocate(fLocalPart); //delete [] fLocalPart;
  394.         fLocalPartBufSz = newLen + 8;
  395.         fLocalPart = (XMLCh*) fMemoryManager->allocate
  396.         (
  397.             (fLocalPartBufSz + 1) * sizeof(XMLCh)
  398.         ); //new XMLCh[fLocalPartBufSz + 1];
  399.     }
  400.     XMLString::moveChars(fLocalPart, localPart, newLen + 1);
  401. }
  402. void QName::setNLocalPart(const XMLCh* localPart, const unsigned int newLen)
  403. {
  404.     if (!fLocalPartBufSz || (newLen > fLocalPartBufSz))
  405.     {
  406.         fMemoryManager->deallocate(fLocalPart); //delete [] fLocalPart;
  407.         fLocalPartBufSz = newLen + 8;
  408.         fLocalPart = (XMLCh*) fMemoryManager->allocate
  409.         (
  410.             (fLocalPartBufSz + 1) * sizeof(XMLCh)
  411.         ); //new XMLCh[fLocalPartBufSz + 1];
  412.     }
  413.     XMLString::moveChars(fLocalPart, localPart, newLen);
  414.     fLocalPart[newLen] = chNull;
  415. }
  416. void QName::setValues(const QName& qname)
  417. {
  418.     setPrefix(qname.getPrefix());
  419.     setLocalPart(qname.getLocalPart());
  420.     setURI(qname.getURI());
  421. }
  422. // -----------------------------------------------------------------------
  423. //  comparison
  424. // -----------------------------------------------------------------------
  425. bool QName::operator==(const QName& qname) const
  426. {
  427.     if (fURIId == 0) // null URI
  428.         return (XMLString::equals(getRawName(),qname.getRawName()));
  429.     return ((fURIId == qname.getURI()) &&
  430.            (XMLString::equals(fLocalPart, qname.getLocalPart())));
  431. }
  432. // ---------------------------------------------------------------------------
  433. //  QName: Private, helper methods
  434. // ---------------------------------------------------------------------------
  435. void QName::cleanUp()
  436. {
  437.     fMemoryManager->deallocate(fLocalPart); //delete [] fLocalPart;
  438.     fMemoryManager->deallocate(fPrefix); //delete [] fPrefix;
  439.     fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  440.     fLocalPart = fPrefix = fRawName = 0;
  441. }
  442. XERCES_CPP_NAMESPACE_END