SAXParser.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:31k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-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) 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: SAXParser.cpp,v $
  58.  * Revision 1.23  2001/11/20 18:51:44  tng
  59.  * Schema: schemaLocation and noNamespaceSchemaLocation to be specified outside the instance document.  New methods setExternalSchemaLocation and setExternalNoNamespaceSchemaLocation are added (for SAX2, two new properties are added).
  60.  *
  61.  * Revision 1.22  2001/10/25 19:46:15  tng
  62.  * Comment outside root element should also be reported.
  63.  *
  64.  * Revision 1.21  2001/08/01 19:11:02  tng
  65.  * Add full schema constraint checking flag to the samples and the parser.
  66.  *
  67.  * Revision 1.20  2001/06/03 19:26:20  jberry
  68.  * Add support for querying error count following parse; enables simple parse without requiring error handler.
  69.  *
  70.  * Revision 1.19  2001/05/11 13:26:22  tng
  71.  * Copyright update.
  72.  *
  73.  * Revision 1.18  2001/05/03 19:09:23  knoaman
  74.  * Support Warning/Error/FatalError messaging.
  75.  * Validity constraints errors are treated as errors, with the ability by user to set
  76.  * validity constraints as fatal errors.
  77.  *
  78.  * Revision 1.17  2001/03/30 16:46:57  tng
  79.  * Schema: Use setDoSchema instead of setSchemaValidation which makes more sense.
  80.  *
  81.  * Revision 1.16  2001/03/21 21:56:08  tng
  82.  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  83.  *
  84.  * Revision 1.15  2001/02/15 15:56:29  tng
  85.  * Schema: Add setSchemaValidation and getSchemaValidation for DOMParser and SAXParser.
  86.  * Add feature "http://apache.org/xml/features/validation/schema" for SAX2XMLReader.
  87.  * New data field  fSchemaValidation in XMLScanner as the flag.
  88.  *
  89.  * Revision 1.14  2000/09/05 23:38:26  andyh
  90.  * Added advanced callback support for XMLDecl()
  91.  *
  92.  * Revision 1.13  2000/06/19 18:12:56  rahulj
  93.  * Suppress the comments, characters, ignoreableWhitespaces before
  94.  * root element. Only allow the PI's to get through. Still need to come
  95.  * to a consensus on this.
  96.  *
  97.  * Revision 1.12  2000/06/17 02:00:55  rahulj
  98.  * Also pass any PI's, comment's, character's occuring before root
  99.  * element to the registered document Handler. Defect identified
  100.  * by John Smirl and Rich Taylor.
  101.  *
  102.  * Revision 1.11  2000/05/15 22:31:18  andyh
  103.  * Replace #include<memory.h> with <string.h> everywhere.
  104.  *
  105.  * Revision 1.10  2000/04/12 22:58:30  roddey
  106.  * Added support for 'auto validate' mode.
  107.  *
  108.  * Revision 1.9  2000/04/11 19:17:58  roddey
  109.  * If a SAX error handler is installed, then the resetErrors() event handler
  110.  * should call the one on the installed SAX error handler.
  111.  *
  112.  * Revision 1.8  2000/04/05 18:56:17  roddey
  113.  * Init the fDTDHandler member. Enable installation of DTDHandler
  114.  * on SAX parser.
  115.  *
  116.  * Revision 1.7  2000/03/03 01:29:34  roddey
  117.  * Added a scanReset()/parseReset() method to the scanner and
  118.  * parsers, to allow for reset after early exit from a progressive parse.
  119.  * Added calls to new Terminate() call to all of the samples. Improved
  120.  * documentation in SAX and DOM parsers.
  121.  *
  122.  * Revision 1.6  2000/03/02 19:54:33  roddey
  123.  * This checkin includes many changes done while waiting for the
  124.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  125.  * available elsewhere.
  126.  *
  127.  * Revision 1.5  2000/02/17 03:54:26  rahulj
  128.  * Added some new getters to query the parser state and
  129.  * clarified the documentation.
  130.  *
  131.  * Revision 1.4  2000/02/06 07:47:56  rahulj
  132.  * Year 2K copyright swat.
  133.  *
  134.  * Revision 1.3  2000/01/12 00:15:22  roddey
  135.  * Changes to deal with multiply nested, relative pathed, entities and to deal
  136.  * with the new URL class changes.
  137.  *
  138.  * Revision 1.2  1999/12/15 19:57:48  roddey
  139.  * Got rid of redundant 'const' on boolean return value. Some compilers choke
  140.  * on this and its useless.
  141.  *
  142.  * Revision 1.1.1.1  1999/11/09 01:07:50  twl
  143.  * Initial checkin
  144.  *
  145.  * Revision 1.6  1999/11/08 20:44:53  rahul
  146.  * Swat for adding in Product name and CVS comment log variable.
  147.  *
  148.  */
  149. // ---------------------------------------------------------------------------
  150. //  Includes
  151. // ---------------------------------------------------------------------------
  152. #include <util/IOException.hpp>
  153. #include <sax/DocumentHandler.hpp>
  154. #include <sax/DTDHandler.hpp>
  155. #include <sax/ErrorHandler.hpp>
  156. #include <sax/EntityResolver.hpp>
  157. #include <sax/SAXParseException.hpp>
  158. #include <internal/XMLScanner.hpp>
  159. #include <parsers/SAXParser.hpp>
  160. #include <string.h>
  161. // ---------------------------------------------------------------------------
  162. //  SAXParser: Constructors and Destructor
  163. // ---------------------------------------------------------------------------
  164. SAXParser::SAXParser(XMLValidator* const valToAdopt) :
  165.     fDocHandler(0)
  166.     , fDTDHandler(0)
  167.     , fElemDepth(0)
  168.     , fEntityResolver(0)
  169.     , fErrorHandler(0)
  170.     , fAdvDHCount(0)
  171.     , fAdvDHList(0)
  172.     , fAdvDHListSize(32)
  173.     , fParseInProgress(false)
  174.     , fScanner(0)
  175. {
  176.     // Create our scanner and tell it what validator to use
  177.     fScanner = new XMLScanner(valToAdopt);
  178.     // Create the initial advanced handler list array and zero it out
  179.     fAdvDHList = new XMLDocumentHandler*[fAdvDHListSize];
  180.     memset(fAdvDHList, 0, sizeof(void*) * fAdvDHListSize);
  181. }
  182. SAXParser::~SAXParser()
  183. {
  184.     delete [] fAdvDHList;
  185.     delete fScanner;
  186. }
  187. // ---------------------------------------------------------------------------
  188. //  SAXParser: Advanced document handler list maintenance methods
  189. // ---------------------------------------------------------------------------
  190. void SAXParser::installAdvDocHandler(XMLDocumentHandler* const toInstall)
  191. {
  192.     // See if we need to expand and do so now if needed
  193.     if (fAdvDHCount == fAdvDHListSize)
  194.     {
  195.         // Calc a new size and allocate the new temp buffer
  196.         const unsigned int newSize = (unsigned int)(fAdvDHListSize * 1.5);
  197.         XMLDocumentHandler** newList = new XMLDocumentHandler*[newSize];
  198.         // Copy over the old data to the new list and zero out the rest
  199.         memcpy(newList, fAdvDHList, sizeof(void*) * fAdvDHListSize);
  200.         memset
  201.         (
  202.             &newList[fAdvDHListSize]
  203.             , 0
  204.             , sizeof(void*) * (newSize - fAdvDHListSize)
  205.         );
  206.         // And now clean up the old array and store the new stuff
  207.         delete [] fAdvDHList;
  208.         fAdvDHList = newList;
  209.         fAdvDHListSize = newSize;
  210.     }
  211.     // Add this new guy into the empty slot
  212.     fAdvDHList[fAdvDHCount++] = toInstall;
  213.     //
  214.     //  Install ourself as the document handler with the scanner. We might
  215.     //  already be, but its not worth checking, just do it.
  216.     //
  217.     fScanner->setDocHandler(this);
  218. }
  219. bool SAXParser::removeAdvDocHandler(XMLDocumentHandler* const toRemove)
  220. {
  221.     // If our count is zero, can't be any installed
  222.     if (!fAdvDHCount)
  223.         return false;
  224.     //
  225.     //  Search the array until we find this handler. If we find a null entry
  226.     //  first, we can stop there before the list is kept contiguous.
  227.     //
  228.     unsigned int index;
  229.     for (index = 0; index < fAdvDHCount; index++)
  230.     {
  231.         //
  232.         //  We found it. We have to keep the list contiguous, so we have to
  233.         //  copy down any used elements after this one.
  234.         //
  235.         if (fAdvDHList[index] == toRemove)
  236.         {
  237.             //
  238.             //  Optimize if only one entry (pretty common). Otherwise, we
  239.             //  have to copy them down to compact them.
  240.             //
  241.             if (fAdvDHCount > 1)
  242.             {
  243.                 index++;
  244.                 while (index < fAdvDHCount)
  245.                     fAdvDHList[index - 1] = fAdvDHList[index];
  246.             }
  247.             // Bump down the count and zero out the last one
  248.             fAdvDHCount--;
  249.             fAdvDHList[fAdvDHCount] = 0;
  250.             //
  251.             //  If this leaves us with no advanced handlers and there is
  252.             //  no SAX doc handler installed on us, then remove us from the
  253.             //  scanner as the document handler.
  254.             //
  255.             if (!fAdvDHCount && !fDocHandler)
  256.                 fScanner->setDocHandler(0);
  257.             return true;
  258.         }
  259.     }
  260.     // Never found it
  261.     return false;
  262. }
  263. // ---------------------------------------------------------------------------
  264. //  SAXParser: Getter methods
  265. // ---------------------------------------------------------------------------
  266. const XMLValidator& SAXParser::getValidator() const
  267. {
  268.     return *fScanner->getValidator();
  269. }
  270. bool SAXParser::getDoNamespaces() const
  271. {
  272.     return fScanner->getDoNamespaces();
  273. }
  274. bool SAXParser::getExitOnFirstFatalError() const
  275. {
  276.     return fScanner->getExitOnFirstFatal();
  277. }
  278. bool SAXParser::getValidationConstraintFatal() const
  279. {
  280.     return fScanner->getValidationConstraintFatal();
  281. }
  282. SAXParser::ValSchemes SAXParser::getValidationScheme() const
  283. {
  284.     const XMLScanner::ValSchemes scheme = fScanner->getValidationScheme();
  285.     if (scheme == XMLScanner::Val_Always)
  286.         return Val_Always;
  287.     else if (scheme == XMLScanner::Val_Never)
  288.         return Val_Never;
  289.     return Val_Auto;
  290. }
  291. bool SAXParser::getDoSchema() const
  292. {
  293.     return fScanner->getDoSchema();
  294. }
  295. bool SAXParser::getValidationSchemaFullChecking() const
  296. {
  297.     return fScanner->getValidationSchemaFullChecking();
  298. }
  299. int SAXParser::getErrorCount() const
  300. {
  301.     return fScanner->getErrorCount();
  302. }
  303. XMLCh* SAXParser::getExternalSchemaLocation() const
  304. {
  305.     return fScanner->getExternalSchemaLocation();
  306. }
  307. XMLCh* SAXParser::getExternalNoNamespaceSchemaLocation() const
  308. {
  309.     return fScanner->getExternalNoNamespaceSchemaLocation();
  310. }
  311. // ---------------------------------------------------------------------------
  312. //  SAXParser: Setter methods
  313. // ---------------------------------------------------------------------------
  314. void SAXParser::setDoNamespaces(const bool newState)
  315. {
  316.     fScanner->setDoNamespaces(newState);
  317. }
  318. void SAXParser::setExitOnFirstFatalError(const bool newState)
  319. {
  320.     fScanner->setExitOnFirstFatal(newState);
  321. }
  322. void SAXParser::setValidationConstraintFatal(const bool newState)
  323. {
  324.     fScanner->setValidationConstraintFatal(newState);
  325. }
  326. void SAXParser::setValidationScheme(const ValSchemes newScheme)
  327. {
  328.     if (newScheme == Val_Never)
  329.         fScanner->setValidationScheme(XMLScanner::Val_Never);
  330.     else if (newScheme == Val_Always)
  331.         fScanner->setValidationScheme(XMLScanner::Val_Always);
  332.     else
  333.         fScanner->setValidationScheme(XMLScanner::Val_Auto);
  334. }
  335. void SAXParser::setDoSchema(const bool newState)
  336. {
  337.     fScanner->setDoSchema(newState);
  338. }
  339. void SAXParser::setValidationSchemaFullChecking(const bool schemaFullChecking)
  340. {
  341.     fScanner->setValidationSchemaFullChecking(schemaFullChecking);
  342. }
  343. void SAXParser::setExternalSchemaLocation(const XMLCh* const schemaLocation)
  344. {
  345.     fScanner->setExternalSchemaLocation(schemaLocation);
  346. }
  347. void SAXParser::setExternalNoNamespaceSchemaLocation(const XMLCh* const noNamespaceSchemaLocation)
  348. {
  349.     fScanner->setExternalNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
  350. }
  351. void SAXParser::setExternalSchemaLocation(const char* const schemaLocation)
  352. {
  353.     fScanner->setExternalSchemaLocation(schemaLocation);
  354. }
  355. void SAXParser::setExternalNoNamespaceSchemaLocation(const char* const noNamespaceSchemaLocation)
  356. {
  357.     fScanner->setExternalNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
  358. }
  359. // ---------------------------------------------------------------------------
  360. //  SAXParser: Overrides of the SAX Parser interface
  361. // ---------------------------------------------------------------------------
  362. void SAXParser::parse(const InputSource& source, const bool reuseGrammar)
  363. {
  364.     // Avoid multiple entrance
  365.     if (fParseInProgress)
  366.         ThrowXML(IOException, XMLExcepts::Gen_ParseInProgress);
  367.     try
  368.     {
  369.         fParseInProgress = true;
  370.         fScanner->scanDocument(source, reuseGrammar);
  371.         fParseInProgress = false;
  372.     }
  373.     catch (...)
  374.     {
  375.         fParseInProgress = false;
  376.         throw;
  377.     }
  378. }
  379. void SAXParser::parse(const XMLCh* const systemId, const bool reuseGrammar)
  380. {
  381.     // Avoid multiple entrance
  382.     if (fParseInProgress)
  383.         ThrowXML(IOException, XMLExcepts::Gen_ParseInProgress);
  384.     try
  385.     {
  386.         fParseInProgress = true;
  387.         fScanner->scanDocument(systemId, reuseGrammar);
  388.         fParseInProgress = false;
  389.     }
  390.     catch (...)
  391.     {
  392.         fParseInProgress = false;
  393.         throw;
  394.     }
  395. }
  396. void SAXParser::parse(const char* const systemId, const bool reuseGrammar)
  397. {
  398.     // Avoid multiple entrance
  399.     if (fParseInProgress)
  400.         ThrowXML(IOException, XMLExcepts::Gen_ParseInProgress);
  401.     try
  402.     {
  403.         fParseInProgress = true;
  404.         fScanner->scanDocument(systemId, reuseGrammar);
  405.         fParseInProgress = false;
  406.     }
  407.     catch (...)
  408.     {
  409.         fParseInProgress = false;
  410.         throw;
  411.     }
  412. }
  413. void SAXParser::setDocumentHandler(DocumentHandler* const handler)
  414. {
  415.     fDocHandler = handler;
  416.     if (fDocHandler)
  417.     {
  418.         //
  419.         //  Make sure we are set as the document handler with the scanner.
  420.         //  We may already be (if advanced handlers are installed), but its
  421.         //  not worthing checking, just do it.
  422.         //
  423.         fScanner->setDocHandler(this);
  424.     }
  425.      else
  426.     {
  427.         //
  428.         //  If we don't have any advanced handlers either, then deinstall us
  429.         //  from the scanner because we don't need document events anymore.
  430.         //
  431.         if (!fAdvDHCount)
  432.             fScanner->setDocHandler(0);
  433.     }
  434. }
  435. void SAXParser::setDTDHandler(DTDHandler* const handler)
  436. {
  437.     fDTDHandler = handler;
  438.     if (fDTDHandler)
  439.         fScanner->setDocTypeHandler(this);
  440.     else
  441.         fScanner->setDocTypeHandler(0);
  442. }
  443. void SAXParser::setErrorHandler(ErrorHandler* const handler)
  444. {
  445.     //
  446.     //  Store the handler. Then either install or deinstall us as the
  447.     //  error reporter on the scanner.
  448.     //
  449.     fErrorHandler = handler;
  450.     if (fErrorHandler) {
  451.         fScanner->setErrorReporter(this);
  452.         fScanner->setErrorHandler(fErrorHandler);
  453.     }
  454.     else {
  455.         fScanner->setErrorReporter(0);
  456.         fScanner->setErrorHandler(0);
  457.     }
  458. }
  459. void SAXParser::setEntityResolver(EntityResolver* const resolver)
  460. {
  461.     fEntityResolver = resolver;
  462.     if (fEntityResolver) {
  463.         fScanner->setEntityHandler(this);
  464.         fScanner->setEntityResolver(fEntityResolver);
  465.     }
  466.     else {
  467.         fScanner->setEntityHandler(0);
  468.         fScanner->setEntityResolver(0);
  469.     }
  470. }
  471. // ---------------------------------------------------------------------------
  472. //  SAXParser: Progressive parse methods
  473. // ---------------------------------------------------------------------------
  474. bool SAXParser::parseFirst( const   XMLCh* const    systemId
  475.                             ,       XMLPScanToken&  toFill
  476.                             , const bool            reuseGrammar)
  477. {
  478.     //
  479.     //  Avoid multiple entrance. We cannot enter here while a regular parse
  480.     //  is in progress.
  481.     //
  482.     if (fParseInProgress)
  483.         ThrowXML(IOException, XMLExcepts::Gen_ParseInProgress);
  484.     return fScanner->scanFirst(systemId, toFill, reuseGrammar);
  485. }
  486. bool SAXParser::parseFirst( const   char* const     systemId
  487.                             ,       XMLPScanToken&  toFill
  488.                             , const bool            reuseGrammar)
  489. {
  490.     //
  491.     //  Avoid multiple entrance. We cannot enter here while a regular parse
  492.     //  is in progress.
  493.     //
  494.     if (fParseInProgress)
  495.         ThrowXML(IOException, XMLExcepts::Gen_ParseInProgress);
  496.     return fScanner->scanFirst(systemId, toFill, reuseGrammar);
  497. }
  498. bool SAXParser::parseFirst( const   InputSource&    source
  499.                             ,       XMLPScanToken&  toFill
  500.                             , const bool            reuseGrammar)
  501. {
  502.     //
  503.     //  Avoid multiple entrance. We cannot enter here while a regular parse
  504.     //  is in progress.
  505.     //
  506.     if (fParseInProgress)
  507.         ThrowXML(IOException, XMLExcepts::Gen_ParseInProgress);
  508.     return fScanner->scanFirst(source, toFill, reuseGrammar);
  509. }
  510. bool SAXParser::parseNext(XMLPScanToken& token)
  511. {
  512.     return fScanner->scanNext(token);
  513. }
  514. void SAXParser::parseReset(XMLPScanToken& token)
  515. {
  516.     // Reset the scanner
  517.     fScanner->scanReset(token);
  518. }
  519. // ---------------------------------------------------------------------------
  520. //  SAXParser: Overrides of the XMLDocumentHandler interface
  521. // ---------------------------------------------------------------------------
  522. void SAXParser::docCharacters(  const   XMLCh* const    chars
  523.                                 , const unsigned int    length
  524.                                 , const bool            cdataSection)
  525. {
  526.     // Suppress the chars before the root element.
  527.     if (!fElemDepth)
  528.         return;
  529.     // Just map to the SAX document handler
  530.     if (fDocHandler)
  531.         fDocHandler->characters(chars, length);
  532.     //
  533.     //  If there are any installed advanced handlers, then lets call them
  534.     //  with this info.
  535.     //
  536.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  537.         fAdvDHList[index]->docCharacters(chars, length, cdataSection);
  538. }
  539. void SAXParser::docComment(const XMLCh* const commentText)
  540. {
  541.     //
  542.     //  SAX has no way to report this. But, if there are any installed
  543.     //  advanced handlers, then lets call them with this info.
  544.     //
  545.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  546.         fAdvDHList[index]->docComment(commentText);
  547. }
  548. void SAXParser::XMLDecl( const  XMLCh* const    versionStr
  549.                         , const XMLCh* const    encodingStr
  550.                         , const XMLCh* const    standaloneStr
  551.                         , const XMLCh* const    actualEncodingStr
  552.                         )
  553. {
  554.     //
  555.     //  SAX has no way to report this. But, if there are any installed
  556.     //  advanced handlers, then lets call them with this info.
  557.     //
  558.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  559.         fAdvDHList[index]->XMLDecl( versionStr,
  560.                                     encodingStr,
  561.                                     standaloneStr,
  562.                                     actualEncodingStr );
  563. }
  564. void SAXParser::docPI(  const   XMLCh* const    target
  565.                         , const XMLCh* const    data)
  566. {
  567.     // Just map to the SAX document handler
  568.     if (fDocHandler)
  569.         fDocHandler->processingInstruction(target, data);
  570.     //
  571.     //  If there are any installed advanced handlers, then lets call them
  572.     //  with this info.
  573.     //
  574.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  575.         fAdvDHList[index]->docPI(target, data);
  576. }
  577. void SAXParser::endDocument()
  578. {
  579.     if (fDocHandler)
  580.         fDocHandler->endDocument();
  581.     //
  582.     //  If there are any installed advanced handlers, then lets call them
  583.     //  with this info.
  584.     //
  585.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  586.         fAdvDHList[index]->endDocument();
  587. }
  588. void SAXParser::endElement( const   XMLElementDecl& elemDecl
  589.                             , const unsigned int    uriId
  590.                             , const bool            isRoot)
  591. {
  592.     // Just map to the SAX document handler
  593.     if (fDocHandler)
  594.         fDocHandler->endElement(elemDecl.getFullName());
  595.     //
  596.     //  If there are any installed advanced handlers, then lets call them
  597.     //  with this info.
  598.     //
  599.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  600.         fAdvDHList[index]->endElement(elemDecl, uriId, isRoot);
  601.     //
  602.     //  Dump the element depth down again. Don't let it underflow in case
  603.     //  of malformed XML.
  604.     //
  605.     if (fElemDepth)
  606.         fElemDepth--;
  607. }
  608. void SAXParser::endEntityReference(const XMLEntityDecl& entityDecl)
  609. {
  610.     //
  611.     //  SAX has no way to report this event. But, if there are any installed
  612.     //  advanced handlers, then lets call them with this info.
  613.     //
  614.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  615.         fAdvDHList[index]->endEntityReference(entityDecl);
  616. }
  617. void SAXParser::ignorableWhitespace(const   XMLCh* const    chars
  618.                                     , const unsigned int    length
  619.                                     , const bool            cdataSection)
  620. {
  621.     // Do not report the whitespace before the root element.
  622.     if (!fElemDepth)
  623.         return;
  624.     // Just map to the SAX document handler
  625.     if (fDocHandler)
  626.         fDocHandler->ignorableWhitespace(chars, length);
  627.     //
  628.     //  If there are any installed advanced handlers, then lets call them
  629.     //  with this info.
  630.     //
  631.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  632.         fAdvDHList[index]->ignorableWhitespace(chars, length, cdataSection);
  633. }
  634. void SAXParser::resetDocument()
  635. {
  636.     // Just map to the SAX document handler
  637.     if (fDocHandler)
  638.         fDocHandler->resetDocument();
  639.     //
  640.     //  If there are any installed advanced handlers, then lets call them
  641.     //  with this info.
  642.     //
  643.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  644.         fAdvDHList[index]->resetDocument();
  645.     // Make sure our element depth flag gets set back to zero
  646.     fElemDepth = 0;
  647. }
  648. void SAXParser::startDocument()
  649. {
  650.     // Just map to the SAX document handler
  651.     if (fDocHandler)
  652.     {
  653.         fDocHandler->setDocumentLocator(fScanner->getLocator());
  654.         fDocHandler->startDocument();
  655.     }
  656.     //
  657.     //  If there are any installed advanced handlers, then lets call them
  658.     //  with this info.
  659.     //
  660.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  661.         fAdvDHList[index]->startDocument();
  662. }
  663. void SAXParser::
  664. startElement(   const   XMLElementDecl&         elemDecl
  665.                 , const unsigned int            elemURLId
  666.                 , const XMLCh* const            elemPrefix
  667.                 , const RefVectorOf<XMLAttr>&   attrList
  668.                 , const unsigned int            attrCount
  669.                 , const bool                    isEmpty
  670.                 , const bool                    isRoot)
  671. {
  672.     // Bump the element depth counter if not empty
  673.     if (!isEmpty)
  674.         fElemDepth++;
  675.     if (fDocHandler)
  676.     {
  677.         fAttrList.setVector(&attrList, attrCount);
  678.         fDocHandler->startElement(elemDecl.getFullName(), fAttrList);
  679.         // If its empty, send the end tag event now
  680.         if (isEmpty)
  681.             fDocHandler->endElement(elemDecl.getFullName());
  682.     }
  683.     //
  684.     //  If there are any installed advanced handlers, then lets call them
  685.     //  with this info.
  686.     //
  687.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  688.     {
  689.         fAdvDHList[index]->startElement
  690.         (
  691.             elemDecl
  692.             , elemURLId
  693.             , elemPrefix
  694.             , attrList
  695.             , attrCount
  696.             , isEmpty
  697.             , isRoot
  698.         );
  699.     }
  700. }
  701. void SAXParser::startEntityReference(const XMLEntityDecl& entityDecl)
  702. {
  703.     //
  704.     //  SAX has no way to report this. But, If there are any installed
  705.     //  advanced handlers, then lets call them with this info.
  706.     //
  707.     for (unsigned int index = 0; index < fAdvDHCount; index++)
  708.         fAdvDHList[index]->startEntityReference(entityDecl);
  709. }
  710. // ---------------------------------------------------------------------------
  711. //  SAXParser: Overrides of the DocTypeHandler interface
  712. // ---------------------------------------------------------------------------
  713. void SAXParser::attDef( const   DTDElementDecl& elemDecl
  714.                         , const DTDAttDef&      attDef
  715.                         , const bool            ignoring)
  716. {
  717.     // Unused by SAX DTDHandler interface at this time
  718. }
  719. void SAXParser::doctypeComment(const XMLCh* const)
  720. {
  721.     // Unused by SAX DTDHandler interface at this time
  722. }
  723. void SAXParser::doctypeDecl(const   DTDElementDecl& elemDecl
  724.                             , const XMLCh* const    publicId
  725.                             , const XMLCh* const    systemId
  726.                             , const bool            hasIntSubset)
  727. {
  728.     // Unused by SAX DTDHandler interface at this time
  729. }
  730. void SAXParser::doctypePI(  const   XMLCh* const
  731.                             , const XMLCh* const)
  732. {
  733.     // Unused by SAX DTDHandler interface at this time
  734. }
  735. void SAXParser::doctypeWhitespace(  const   XMLCh* const    chars
  736.                                     , const unsigned int    length)
  737. {
  738.     // Unused by SAX DTDHandler interface at this time
  739. }
  740. void SAXParser::elementDecl(const DTDElementDecl&, const bool)
  741. {
  742.     // Unused by SAX DTDHandler interface at this time
  743. }
  744. void SAXParser::endAttList(const DTDElementDecl&)
  745. {
  746.     // Unused by SAX DTDHandler interface at this time
  747. }
  748. void SAXParser::endIntSubset()
  749. {
  750.     // Unused by SAX DTDHandler interface at this time
  751. }
  752. void SAXParser::endExtSubset()
  753. {
  754.     // Unused by SAX DTDHandler interface at this time
  755. }
  756. void SAXParser::entityDecl( const   DTDEntityDecl&  entityDecl
  757.                             , const bool            isPEDecl
  758.                             , const bool            isIgnored)
  759. {
  760.     //
  761.     //  If we have a DTD handler, and this entity is not ignored, and
  762.     //  its an unparsed entity, then send this one.
  763.     //
  764.     if (fDTDHandler && !isIgnored)
  765.     {
  766.         if (entityDecl.isUnparsed())
  767.         {
  768.             fDTDHandler->unparsedEntityDecl
  769.             (
  770.                 entityDecl.getName()
  771.                 , entityDecl.getPublicId()
  772.                 , entityDecl.getSystemId()
  773.                 , entityDecl.getNotationName()
  774.             );
  775.         }
  776.     }
  777. }
  778. void SAXParser::resetDocType()
  779. {
  780.     // Just map to the DTD handler
  781.     if (fDTDHandler)
  782.         fDTDHandler->resetDocType();
  783. }
  784. void SAXParser::notationDecl(   const   XMLNotationDecl&    notDecl
  785.                                 , const bool                isIgnored)
  786. {
  787.     if (fDTDHandler && !isIgnored)
  788.     {
  789.         fDTDHandler->notationDecl
  790.         (
  791.             notDecl.getName()
  792.             , notDecl.getPublicId()
  793.             , notDecl.getSystemId()
  794.         );
  795.     }
  796. }
  797. void SAXParser::startAttList(const DTDElementDecl&)
  798. {
  799.     // Unused by SAX DTDHandler interface at this time
  800. }
  801. void SAXParser::startIntSubset()
  802. {
  803.     // Unused by SAX DTDHandler interface at this time
  804. }
  805. void SAXParser::startExtSubset()
  806. {
  807.     // Unused by SAX DTDHandler interface at this time
  808. }
  809. void SAXParser::TextDecl(   const  XMLCh* const
  810.                             , const XMLCh* const)
  811. {
  812.     // Unused by SAX DTDHandler interface at this time
  813. }
  814. // ---------------------------------------------------------------------------
  815. //  SAXParser: Overrides of the XMLErrorReporter interface
  816. // ---------------------------------------------------------------------------
  817. void SAXParser::resetErrors()
  818. {
  819.     if (fErrorHandler)
  820.         fErrorHandler->resetErrors();
  821. }
  822. void SAXParser::error(  const   unsigned int                code
  823.                         , const XMLCh* const                msgDomain
  824.                         , const XMLErrorReporter::ErrTypes  errType
  825.                         , const XMLCh* const                errorText
  826.                         , const XMLCh* const                systemId
  827.                         , const XMLCh* const                publicId
  828.                         , const unsigned int                lineNum
  829.                         , const unsigned int                colNum)
  830. {
  831.     SAXParseException toThrow = SAXParseException
  832.     (
  833.         errorText
  834.         , publicId
  835.         , systemId
  836.         , lineNum
  837.         , colNum
  838.     );
  839.     if (!fErrorHandler)
  840.     {
  841.         if (errType == XMLErrorReporter::ErrType_Fatal)
  842.             throw toThrow;
  843.         else
  844.             return;
  845.     }
  846.     if (errType == XMLErrorReporter::ErrType_Warning)
  847.         fErrorHandler->warning(toThrow);
  848.     else if (errType == XMLErrorReporter::ErrType_Fatal)
  849.         fErrorHandler->fatalError(toThrow);
  850.     else
  851.         fErrorHandler->error(toThrow);
  852. }
  853. // ---------------------------------------------------------------------------
  854. //  SAXParser: Handlers for the XMLEntityHandler interface
  855. // ---------------------------------------------------------------------------
  856. void SAXParser::endInputSource(const InputSource&)
  857. {
  858. }
  859. bool SAXParser::expandSystemId(const XMLCh* const, XMLBuffer&)
  860. {
  861.     return false;
  862. }
  863. void SAXParser::resetEntities()
  864. {
  865.     // Nothing to do for this one
  866. }
  867. InputSource*
  868. SAXParser::resolveEntity(   const   XMLCh* const    publicId
  869.                             , const XMLCh* const    systemId)
  870. {
  871.     // Just map to the SAX entity resolver handler
  872.     if (fEntityResolver)
  873.         return fEntityResolver->resolveEntity(publicId, systemId);
  874.     return 0;
  875. }
  876. void SAXParser::startInputSource(const InputSource&)
  877. {
  878.     // Nothing to do for this one
  879. }
  880. // ---------------------------------------------------------------------------
  881. //  SAXParser: Deprecated methods
  882. // ---------------------------------------------------------------------------
  883. bool SAXParser::getDoValidation() const
  884. {
  885.     //
  886.     //  We don't want to tie the public parser classes to the enum used
  887.     //  by the scanner, so we use a separate one and map.
  888.     //
  889.     //  DON'T mix the new and old methods!!
  890.     //
  891.     const XMLScanner::ValSchemes scheme = fScanner->getValidationScheme();
  892.     if (scheme == XMLScanner::Val_Always)
  893.         return true;
  894.     return false;
  895. }
  896. void SAXParser::setDoValidation(const bool newState)
  897. {
  898.     fScanner->setDoValidation
  899.     (
  900.         newState ? XMLScanner::Val_Always : XMLScanner::Val_Never
  901.     );
  902. }