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

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-2003 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) 1999, 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: XMLValidator.cpp,v $
  58.   * Revision 1.4  2003/05/15 18:26:07  knoaman
  59.   * Partial implementation of the configurable memory manager.
  60.   *
  61.   * Revision 1.3  2003/03/09 16:38:04  peiyongz
  62.   * PanicHandler
  63.   *
  64.   * Revision 1.2  2002/11/04 15:00:21  tng
  65.   * C++ Namespace Support.
  66.   *
  67.   * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
  68.   * sane_include
  69.   *
  70.   * Revision 1.13  2001/11/30 22:18:18  peiyongz
  71.   * cleanUp function made member function
  72.   * cleanUp object moved to file scope
  73.   * double mutex lock removed
  74.   *
  75.   * Revision 1.12  2001/11/28 20:32:49  tng
  76.   * Do not increment the error count if it is a warning.
  77.   *
  78.   * Revision 1.11  2001/10/24 23:46:52  peiyongz
  79.   * [Bug 4342] fix the leak.
  80.   *
  81.   * Revision 1.10  2001/06/04 21:07:34  jberry
  82.   * Increment scanner error count from schema validator, not just in scanner itself.
  83.   *
  84.   * Revision 1.9  2001/05/11 13:25:33  tng
  85.   * Copyright update.
  86.   *
  87.   * Revision 1.8  2001/05/03 19:08:58  knoaman
  88.   * Support Warning/Error/FatalError messaging.
  89.   * Validity constraints errors are treated as errors, with the ability by user to set
  90.   * validity constraints as fatal errors.
  91.   *
  92.   * Revision 1.7  2001/03/21 21:56:02  tng
  93.   * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  94.   *
  95.   * Revision 1.6  2000/03/28 19:43:17  roddey
  96.   * Fixes for signed/unsigned warnings. New work for two way transcoding
  97.   * stuff.
  98.   *
  99.   * Revision 1.5  2000/03/02 19:54:25  roddey
  100.   * This checkin includes many changes done while waiting for the
  101.   * 1.1.0 code to be finished. I can't list them all here, but a list is
  102.   * available elsewhere.
  103.   *
  104.   * Revision 1.4  2000/02/06 07:47:49  rahulj
  105.   * Year 2K copyright swat.
  106.   *
  107.   * Revision 1.3  1999/12/08 00:15:06  roddey
  108.   * Some small last minute fixes to get into the 3.0.1 build that is going to be
  109.   * going out anyway for platform fixes.
  110.   *
  111.   * Revision 1.2  1999/12/02 19:02:56  roddey
  112.   * Get rid of a few statically defined XMLMutex objects, and lazy eval them
  113.   * using atomic compare and swap. I somehow let it get by me that we don't
  114.   * want any static/global objects at all.
  115.   *
  116.   * Revision 1.1.1.1  1999/11/09 01:08:37  twl
  117.   * Initial checkin
  118.   *
  119.   * Revision 1.3  1999/11/08 20:44:40  rahul
  120.   * Swat for adding in Product name and CVS comment log variable.
  121.   *
  122.   */
  123. // ---------------------------------------------------------------------------
  124. //  Includes
  125. // ---------------------------------------------------------------------------
  126. #include <xercesc/framework/XMLValidator.hpp>
  127. #include <xercesc/util/Mutexes.hpp>
  128. #include <xercesc/util/PlatformUtils.hpp>
  129. #include <xercesc/util/XMLMsgLoader.hpp>
  130. #include <xercesc/util/XMLRegisterCleanup.hpp>
  131. #include <xercesc/internal/XMLScanner.hpp>
  132. XERCES_CPP_NAMESPACE_BEGIN
  133. // ---------------------------------------------------------------------------
  134. //  Local static functions
  135. // ---------------------------------------------------------------------------
  136. static XMLMutex* sMsgMutex = 0;
  137. static XMLRegisterCleanup msgLoaderCleanup;
  138. static XMLMsgLoader* sMsgLoader = 0;
  139. static XMLRegisterCleanup validatorMutexCleanup;
  140. //
  141. //  We need to fault in this mutex. But, since its used for synchronization
  142. //  itself, we have to do this the low level way using a compare and swap.
  143. //
  144. static XMLMutex& gValidatorMutex()
  145. {
  146.     if (!sMsgMutex)
  147.     {
  148.         XMLMutex* tmpMutex = new XMLMutex;
  149.         if (XMLPlatformUtils::compareAndSwap((void**)&sMsgMutex, tmpMutex, 0))
  150.         {
  151.             // Someone beat us to it, so let's clean up ours
  152.             delete tmpMutex;
  153.         }
  154.         else
  155.         {
  156.             validatorMutexCleanup.registerCleanup(XMLValidator::reinitMsgMutex);
  157.         }
  158.     }
  159.     return *sMsgMutex;
  160. }
  161. static XMLMsgLoader& getMsgLoader()
  162. {
  163. // Lock the mutex
  164. XMLMutexLock lockInit(&gValidatorMutex());
  165.     if (!sMsgLoader)
  166. {
  167. sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgValidityDomain);
  168. if (!sMsgLoader)
  169. XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);
  170.         //
  171.         // Register this XMLMsgLoader for cleanup at Termination.
  172.         //
  173.         msgLoaderCleanup.registerCleanup(XMLValidator::reinitMsgLoader);
  174. }
  175.     return *sMsgLoader;
  176. }
  177. // ---------------------------------------------------------------------------
  178. //  XMLValidator: Error emitting methods
  179. // ---------------------------------------------------------------------------
  180. //
  181. //  These methods are called whenever the scanner wants to emit an error.
  182. //  It handles getting the message loaded, doing token replacement, etc...
  183. //  and then calling the error handler, if its installed.
  184. //
  185. void XMLValidator::emitError(const XMLValid::Codes toEmit)
  186. {
  187.     // Bump the error count if it is not a warning
  188.     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
  189.         fScanner->incrementErrorCount();
  190.     // Call error reporter if we have one
  191.     if (fErrorReporter)
  192.     {
  193.         // Load the message into a local for display
  194.         const unsigned int msgSize = 1023;
  195.         XMLCh errText[msgSize + 1];
  196.         // load the text
  197. if (!getMsgLoader().loadMsg(toEmit, errText, msgSize))
  198. {
  199. // <TBD> Probably should load a default msg here
  200.         }
  201.         //
  202.         //  Create a LastExtEntityInfo structure and get the reader manager
  203.         //  to fill it in for us. This will give us the information about
  204.         //  the last reader on the stack that was an external entity of some
  205.         //  sort (i.e. it will ignore internal entities.
  206.         //
  207.         ReaderMgr::LastExtEntityInfo lastInfo;
  208.         fReaderMgr->getLastExtEntityInfo(lastInfo);
  209.         fErrorReporter->error
  210.         (
  211.             toEmit
  212.             , XMLUni::fgValidityDomain
  213.             , XMLValid::errorType(toEmit)
  214.             , errText
  215.             , lastInfo.systemId
  216.             , lastInfo.publicId
  217.             , lastInfo.lineNumber
  218.             , lastInfo.colNumber
  219.         );
  220.     }
  221.     // Bail out if its fatal an we are to give up on the first fatal error
  222.     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
  223.          || XMLValid::isFatal(toEmit))
  224.     &&  fScanner->getExitOnFirstFatal()
  225.     &&  !fScanner->getInException())
  226.     {
  227.         throw toEmit;
  228.     }
  229. }
  230. void XMLValidator::emitError(const  XMLValid::Codes toEmit
  231.                             , const XMLCh* const    text1
  232.                             , const XMLCh* const    text2
  233.                             , const XMLCh* const    text3
  234.                             , const XMLCh* const    text4)
  235. {
  236.     // Bump the error count if it is not a warning
  237.     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
  238.         fScanner->incrementErrorCount();
  239.     // Call error reporter if we have one
  240.     if (fErrorReporter)
  241.     {
  242.         //
  243.         //  Load the message into alocal and replace any tokens found in
  244.         //  the text.
  245.         //
  246.         const unsigned int maxChars = 2047;
  247.         XMLCh errText[maxChars + 1];
  248.         // load the text
  249. if (!getMsgLoader().loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4))
  250. {
  251. // <TBD> Should probably load a default message here
  252.         }
  253.         //
  254.         //  Create a LastExtEntityInfo structure and get the reader manager
  255.         //  to fill it in for us. This will give us the information about
  256.         //  the last reader on the stack that was an external entity of some
  257.         //  sort (i.e. it will ignore internal entities.
  258.         //
  259.         ReaderMgr::LastExtEntityInfo lastInfo;
  260.         fReaderMgr->getLastExtEntityInfo(lastInfo);
  261.         fErrorReporter->error
  262.         (
  263.             toEmit
  264.             , XMLUni::fgValidityDomain
  265.             , XMLValid::errorType(toEmit)
  266.             , errText
  267.             , lastInfo.systemId
  268.             , lastInfo.publicId
  269.             , lastInfo.lineNumber
  270.             , lastInfo.colNumber
  271.         );
  272.     }
  273.     // Bail out if its fatal an we are to give up on the first fatal error
  274.     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
  275.          || XMLValid::isFatal(toEmit))
  276.     &&  fScanner->getExitOnFirstFatal()
  277.     &&  !fScanner->getInException())
  278.     {
  279.         throw toEmit;
  280.     }
  281. }
  282. void XMLValidator::emitError(const  XMLValid::Codes toEmit
  283.                             , const char* const     text1
  284.                             , const char* const     text2
  285.                             , const char* const     text3
  286.                             , const char* const     text4)
  287. {
  288.     // Bump the error count if it is not a warning
  289.     if (XMLValid::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)
  290.         fScanner->incrementErrorCount();
  291.     // Call error reporter if we have one
  292.     if (fErrorReporter)
  293.     {
  294.         //
  295.         //  Load the message into alocal and replace any tokens found in
  296.         //  the text.
  297.         //
  298.         const unsigned int maxChars = 2047;
  299.         XMLCh errText[maxChars + 1];
  300.         // load the text
  301. if (!getMsgLoader().loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4))
  302. {
  303. // <TBD> Should probably load a default message here
  304.         }
  305.         //
  306.         //  Create a LastExtEntityInfo structure and get the reader manager
  307.         //  to fill it in for us. This will give us the information about
  308.         //  the last reader on the stack that was an external entity of some
  309.         //  sort (i.e. it will ignore internal entities.
  310.         //
  311.         ReaderMgr::LastExtEntityInfo lastInfo;
  312.         fReaderMgr->getLastExtEntityInfo(lastInfo);
  313.         fErrorReporter->error
  314.         (
  315.             toEmit
  316.             , XMLUni::fgValidityDomain
  317.             , XMLValid::errorType(toEmit)
  318.             , errText
  319.             , lastInfo.systemId
  320.             , lastInfo.publicId
  321.             , lastInfo.lineNumber
  322.             , lastInfo.colNumber
  323.         );
  324.     }
  325.     // Bail out if its fatal an we are to give up on the first fatal error
  326.     if (((XMLValid::isError(toEmit) && fScanner->getValidationConstraintFatal())
  327.          || XMLValid::isFatal(toEmit))
  328.     &&  fScanner->getExitOnFirstFatal()
  329.     &&  !fScanner->getInException())
  330.     {
  331.         throw toEmit;
  332.     }
  333. }
  334. // ---------------------------------------------------------------------------
  335. //  XMLValidator: Hidden Constructors
  336. // ---------------------------------------------------------------------------
  337. XMLValidator::XMLValidator(XMLErrorReporter* const errReporter) :
  338.     fBufMgr(0)
  339.     , fErrorReporter(errReporter)
  340.     , fReaderMgr(0)
  341.     , fScanner(0)
  342. {
  343. }
  344. // -----------------------------------------------------------------------
  345. //  Notification that lazy data has been deleted
  346. // -----------------------------------------------------------------------
  347. void XMLValidator::reinitMsgMutex()
  348. {
  349.     delete sMsgMutex;
  350.     sMsgMutex = 0;
  351. }
  352. // -----------------------------------------------------------------------
  353. //  Reinitialise the message loader
  354. // -----------------------------------------------------------------------
  355. void XMLValidator::reinitMsgLoader()
  356. {
  357. delete sMsgLoader;
  358. sMsgLoader = 0;
  359. }
  360. XERCES_CPP_NAMESPACE_END