GME  13
SAXParser.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: SAXParser.cpp 882548 2009-11-20 13:44:14Z borisk $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/parsers/SAXParser.hpp>
00027 #include <xercesc/internal/XMLScannerResolver.hpp>
00028 #include <xercesc/framework/XMLValidator.hpp>
00029 #include <xercesc/util/IOException.hpp>
00030 #include <xercesc/sax/DocumentHandler.hpp>
00031 #include <xercesc/sax/DTDHandler.hpp>
00032 #include <xercesc/sax/ErrorHandler.hpp>
00033 #include <xercesc/sax/EntityResolver.hpp>
00034 #include <xercesc/sax/SAXParseException.hpp>
00035 #include <xercesc/validators/common/GrammarResolver.hpp>
00036 #include <xercesc/framework/XMLGrammarPool.hpp>
00037 #include <xercesc/framework/XMLSchemaDescription.hpp>
00038 #include <xercesc/util/Janitor.hpp>
00039 #include <xercesc/util/OutOfMemoryException.hpp>
00040 #include <xercesc/util/XMLEntityResolver.hpp>
00041 #include <string.h>
00042 
00043 XERCES_CPP_NAMESPACE_BEGIN
00044 
00045 
00046 // ---------------------------------------------------------------------------
00047 //  SAXParser: Constructors and Destructor
00048 // ---------------------------------------------------------------------------
00049 
00050 
00051 typedef JanitorMemFunCall<SAXParser>    CleanupType;
00052 typedef JanitorMemFunCall<SAXParser>    ResetInProgressType;
00053 
00054 
00055 SAXParser::SAXParser( XMLValidator* const   valToAdopt
00056                     , MemoryManager* const  manager
00057                     , XMLGrammarPool* const gramPool):
00058 
00059     fParseInProgress(false)
00060     , fElemDepth(0)
00061     , fAdvDHCount(0)
00062     , fAdvDHListSize(32)
00063     , fDocHandler(0)
00064     , fDTDHandler(0)
00065     , fEntityResolver(0)
00066     , fXMLEntityResolver(0)
00067     , fErrorHandler(0)
00068     , fPSVIHandler(0)
00069     , fAdvDHList(0)
00070     , fScanner(0)
00071     , fGrammarResolver(0)
00072     , fURIStringPool(0)
00073     , fValidator(valToAdopt)
00074     , fMemoryManager(manager)
00075     , fGrammarPool(gramPool)
00076     , fElemQNameBuf(1023, manager)
00077 {
00078     CleanupType cleanup(this, &SAXParser::cleanUp);
00079 
00080     try
00081     {
00082         initialize();
00083     }
00084     catch(const OutOfMemoryException&)
00085     {
00086         // Don't cleanup when out of memory, since executing the
00087         // code can cause problems.
00088         cleanup.release();
00089 
00090         throw;
00091     }
00092 
00093     cleanup.release();
00094 }
00095 
00096 
00097 SAXParser::~SAXParser()
00098 {
00099     cleanUp();
00100 }
00101 
00102 // ---------------------------------------------------------------------------
00103 //  SAXParser: Initialize/CleanUp methods
00104 // ---------------------------------------------------------------------------
00105 void SAXParser::initialize()
00106 {
00107     // Create grammar resolver and string pool to pass to scanner
00108     fGrammarResolver = new (fMemoryManager) GrammarResolver(fGrammarPool, fMemoryManager);
00109     fURIStringPool = fGrammarResolver->getStringPool();
00110 
00111     // Create our scanner and tell it what validator to use
00112     fScanner = XMLScannerResolver::getDefaultScanner(fValidator, fGrammarResolver, fMemoryManager);
00113     fScanner->setURIStringPool(fURIStringPool);
00114 
00115     // Create the initial advanced handler list array and zero it out
00116     fAdvDHList = (XMLDocumentHandler**) fMemoryManager->allocate
00117     (
00118         fAdvDHListSize * sizeof(XMLDocumentHandler*)
00119     );//new XMLDocumentHandler*[fAdvDHListSize];
00120     memset(fAdvDHList, 0, sizeof(void*) * fAdvDHListSize);
00121 }
00122 
00123 void SAXParser::cleanUp()
00124 {
00125     fMemoryManager->deallocate(fAdvDHList);//delete [] fAdvDHList;
00126     delete fScanner;
00127     delete fGrammarResolver;
00128     // grammar pool must do this
00129     //delete fURIStringPool;
00130 
00131     if (fValidator)
00132         delete fValidator;
00133 }
00134 
00135 
00136 // ---------------------------------------------------------------------------
00137 //  SAXParser: Advanced document handler list maintenance methods
00138 // ---------------------------------------------------------------------------
00139 void SAXParser::installAdvDocHandler(XMLDocumentHandler* const toInstall)
00140 {
00141     // See if we need to expand and do so now if needed
00142     if (fAdvDHCount == fAdvDHListSize)
00143     {
00144         // Calc a new size and allocate the new temp buffer
00145         const XMLSize_t newSize = (XMLSize_t)(fAdvDHListSize * 1.5);
00146         XMLDocumentHandler** newList = (XMLDocumentHandler**) fMemoryManager->allocate
00147         (
00148             newSize * sizeof(XMLDocumentHandler*)
00149         );//new XMLDocumentHandler*[newSize];
00150 
00151         // Copy over the old data to the new list and zero out the rest
00152         memcpy(newList, fAdvDHList, sizeof(void*) * fAdvDHListSize);
00153         memset
00154         (
00155             &newList[fAdvDHListSize]
00156             , 0
00157             , sizeof(void*) * (newSize - fAdvDHListSize)
00158         );
00159 
00160         // And now clean up the old array and store the new stuff
00161         fMemoryManager->deallocate(fAdvDHList);//delete [] fAdvDHList;
00162         fAdvDHList = newList;
00163         fAdvDHListSize = newSize;
00164     }
00165 
00166     // Add this new guy into the empty slot
00167     fAdvDHList[fAdvDHCount++] = toInstall;
00168 
00169     //
00170     //  Install ourself as the document handler with the scanner. We might
00171     //  already be, but its not worth checking, just do it.
00172     //
00173     fScanner->setDocHandler(this);
00174 }
00175 
00176 
00177 bool SAXParser::removeAdvDocHandler(XMLDocumentHandler* const toRemove)
00178 {
00179     // If our count is zero, can't be any installed
00180     if (!fAdvDHCount)
00181         return false;
00182 
00183     //
00184     //  Search the array until we find this handler. If we find a null entry
00185     //  first, we can stop there before the list is kept contiguous.
00186     //
00187     XMLSize_t index;
00188     for (index = 0; index < fAdvDHCount; index++)
00189     {
00190         //
00191         //  We found it. We have to keep the list contiguous, so we have to
00192         //  copy down any used elements after this one.
00193         //
00194         if (fAdvDHList[index] == toRemove)
00195         {
00196             //
00197             //  Optimize if only one entry (pretty common). Otherwise, we
00198             //  have to copy them down to compact them.
00199             //
00200             if (fAdvDHCount > 1)
00201             {
00202                 index++;
00203                 while (index < fAdvDHCount)
00204                     fAdvDHList[index - 1] = fAdvDHList[index];
00205             }
00206 
00207             // Bump down the count and zero out the last one
00208             fAdvDHCount--;
00209             fAdvDHList[fAdvDHCount] = 0;
00210 
00211             //
00212             //  If this leaves us with no advanced handlers and there is
00213             //  no SAX doc handler installed on us, then remove us from the
00214             //  scanner as the document handler.
00215             //
00216             if (!fAdvDHCount && !fDocHandler)
00217                 fScanner->setDocHandler(0);
00218 
00219             return true;
00220         }
00221     }
00222 
00223     // Never found it
00224     return false;
00225 }
00226 
00227 
00228 // ---------------------------------------------------------------------------
00229 //  SAXParser: Getter methods
00230 // ---------------------------------------------------------------------------
00231 const XMLValidator& SAXParser::getValidator() const
00232 {
00233     return *fScanner->getValidator();
00234 }
00235 
00236 
00237 bool SAXParser::getDoNamespaces() const
00238 {
00239     return fScanner->getDoNamespaces();
00240 }
00241 
00242 bool SAXParser::getGenerateSyntheticAnnotations() const
00243 {
00244     return fScanner->getGenerateSyntheticAnnotations();
00245 }
00246 
00247 bool SAXParser::getValidateAnnotations() const
00248 {
00249     return fScanner->getValidateAnnotations();
00250 }
00251 
00252 bool SAXParser::getExitOnFirstFatalError() const
00253 {
00254     return fScanner->getExitOnFirstFatal();
00255 }
00256 
00257 bool SAXParser::getValidationConstraintFatal() const
00258 {
00259     return fScanner->getValidationConstraintFatal();
00260 }
00261 
00262 
00263 SAXParser::ValSchemes SAXParser::getValidationScheme() const
00264 {
00265     const XMLScanner::ValSchemes scheme = fScanner->getValidationScheme();
00266 
00267     if (scheme == XMLScanner::Val_Always)
00268         return Val_Always;
00269     else if (scheme == XMLScanner::Val_Never)
00270         return Val_Never;
00271 
00272     return Val_Auto;
00273 }
00274 
00275 bool SAXParser::getDoSchema() const
00276 {
00277     return fScanner->getDoSchema();
00278 }
00279 
00280 bool SAXParser::getValidationSchemaFullChecking() const
00281 {
00282     return fScanner->getValidationSchemaFullChecking();
00283 }
00284 
00285 bool SAXParser::getIdentityConstraintChecking() const
00286 {
00287     return fScanner->getIdentityConstraintChecking();
00288 }
00289 
00290 int SAXParser::getErrorCount() const
00291 {
00292     return fScanner->getErrorCount();
00293 }
00294 
00295 XMLCh* SAXParser::getExternalSchemaLocation() const
00296 {
00297     return fScanner->getExternalSchemaLocation();
00298 }
00299 
00300 XMLCh* SAXParser::getExternalNoNamespaceSchemaLocation() const
00301 {
00302     return fScanner->getExternalNoNamespaceSchemaLocation();
00303 }
00304 
00305 SecurityManager* SAXParser::getSecurityManager() const
00306 {
00307     return fScanner->getSecurityManager();
00308 }
00309 
00310 XMLSize_t SAXParser::getLowWaterMark() const
00311 {
00312     return fScanner->getLowWaterMark();
00313 }
00314 
00315 bool SAXParser::getLoadExternalDTD() const
00316 {
00317     return fScanner->getLoadExternalDTD();
00318 }
00319 
00320 bool SAXParser::getLoadSchema() const
00321 {
00322     return fScanner->getLoadSchema();
00323 }
00324 
00325 bool SAXParser::isCachingGrammarFromParse() const
00326 {
00327     return fScanner->isCachingGrammarFromParse();
00328 }
00329 
00330 bool SAXParser::isUsingCachedGrammarInParse() const
00331 {
00332     return fScanner->isUsingCachedGrammarInParse();
00333 }
00334 
00335 bool SAXParser::getCalculateSrcOfs() const
00336 {
00337     return fScanner->getCalculateSrcOfs();
00338 }
00339 
00340 bool SAXParser::getStandardUriConformant() const
00341 {
00342     return fScanner->getStandardUriConformant();
00343 }
00344 
00345 Grammar* SAXParser::getGrammar(const XMLCh* const nameSpaceKey)
00346 {
00347     return fGrammarResolver->getGrammar(nameSpaceKey);
00348 }
00349 
00350 Grammar* SAXParser::getRootGrammar()
00351 {
00352     return fScanner->getRootGrammar();
00353 }
00354 
00355 const XMLCh* SAXParser::getURIText(unsigned int uriId) const
00356 {
00357     return fScanner->getURIText(uriId);
00358 }
00359 
00360 XMLFilePos SAXParser::getSrcOffset() const
00361 {
00362     return fScanner->getSrcOffset();
00363 }
00364 
00365 bool SAXParser::getIgnoreCachedDTD() const
00366 {
00367     return fScanner->getIgnoreCachedDTD();
00368 }
00369 
00370 bool SAXParser::getIgnoreAnnotations() const
00371 {
00372     return fScanner->getIgnoreAnnotations();
00373 }
00374 
00375 bool SAXParser::getDisableDefaultEntityResolution() const
00376 {
00377     return fScanner->getDisableDefaultEntityResolution();
00378 }
00379 
00380 bool SAXParser::getSkipDTDValidation() const
00381 {
00382     return fScanner->getSkipDTDValidation();
00383 }
00384 
00385 bool SAXParser::getHandleMultipleImports() const
00386 {
00387     return fScanner->getHandleMultipleImports();
00388 }
00389 
00390 // ---------------------------------------------------------------------------
00391 //  SAXParser: Setter methods
00392 // ---------------------------------------------------------------------------
00393 void SAXParser::setDoNamespaces(const bool newState)
00394 {
00395     fScanner->setDoNamespaces(newState);
00396 }
00397 
00398 void SAXParser::setGenerateSyntheticAnnotations(const bool newState)
00399 {
00400     fScanner->setGenerateSyntheticAnnotations(newState);
00401 }
00402 
00403 void SAXParser::setValidateAnnotations(const bool newState)
00404 {
00405     fScanner->setValidateAnnotations(newState);
00406 }
00407 
00408 void SAXParser::setExitOnFirstFatalError(const bool newState)
00409 {
00410     fScanner->setExitOnFirstFatal(newState);
00411 }
00412 
00413 
00414 void SAXParser::setValidationConstraintFatal(const bool newState)
00415 {
00416     fScanner->setValidationConstraintFatal(newState);
00417 }
00418 
00419 
00420 void SAXParser::setValidationScheme(const ValSchemes newScheme)
00421 {
00422     if (newScheme == Val_Never)
00423         fScanner->setValidationScheme(XMLScanner::Val_Never);
00424     else if (newScheme == Val_Always)
00425         fScanner->setValidationScheme(XMLScanner::Val_Always);
00426     else
00427         fScanner->setValidationScheme(XMLScanner::Val_Auto);
00428 }
00429 
00430 void SAXParser::setDoSchema(const bool newState)
00431 {
00432     fScanner->setDoSchema(newState);
00433 }
00434 
00435 void SAXParser::setValidationSchemaFullChecking(const bool schemaFullChecking)
00436 {
00437     fScanner->setValidationSchemaFullChecking(schemaFullChecking);
00438 }
00439 
00440 void SAXParser::setIdentityConstraintChecking(const bool identityConstraintChecking)
00441 {
00442     fScanner->setIdentityConstraintChecking(identityConstraintChecking);
00443 }
00444 
00445 void SAXParser::setExternalSchemaLocation(const XMLCh* const schemaLocation)
00446 {
00447     fScanner->setExternalSchemaLocation(schemaLocation);
00448 }
00449 void SAXParser::setExternalNoNamespaceSchemaLocation(const XMLCh* const noNamespaceSchemaLocation)
00450 {
00451     fScanner->setExternalNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
00452 }
00453 
00454 void SAXParser::setExternalSchemaLocation(const char* const schemaLocation)
00455 {
00456     fScanner->setExternalSchemaLocation(schemaLocation);
00457 }
00458 void SAXParser::setExternalNoNamespaceSchemaLocation(const char* const noNamespaceSchemaLocation)
00459 {
00460     fScanner->setExternalNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
00461 }
00462 
00463 void SAXParser::setSecurityManager(SecurityManager* const securityManager)
00464 {
00465     // since this could impact various components, don't permit it to change
00466     // during a parse
00467     if (fParseInProgress)
00468         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00469 
00470     fScanner->setSecurityManager(securityManager);
00471 }
00472 
00473 void SAXParser::setLowWaterMark(XMLSize_t lwm)
00474 {
00475     fScanner->setLowWaterMark(lwm);
00476 }
00477 
00478 void SAXParser::setLoadExternalDTD(const bool newState)
00479 {
00480     fScanner->setLoadExternalDTD(newState);
00481 }
00482 
00483 void SAXParser::setLoadSchema(const bool newState)
00484 {
00485     fScanner->setLoadSchema(newState);
00486 }
00487 
00488 void SAXParser::cacheGrammarFromParse(const bool newState)
00489 {
00490     fScanner->cacheGrammarFromParse(newState);
00491 
00492     if (newState)
00493         fScanner->useCachedGrammarInParse(newState);
00494 }
00495 
00496 void SAXParser::useCachedGrammarInParse(const bool newState)
00497 {
00498     if (newState || !fScanner->isCachingGrammarFromParse())
00499         fScanner->useCachedGrammarInParse(newState);
00500 }
00501 
00502 void SAXParser::setCalculateSrcOfs(const bool newState)
00503 {
00504     fScanner->setCalculateSrcOfs(newState);
00505 }
00506 
00507 void SAXParser::setStandardUriConformant(const bool newState)
00508 {
00509     fScanner->setStandardUriConformant(newState);
00510 }
00511 
00512 void SAXParser::useScanner(const XMLCh* const scannerName)
00513 {
00514     XMLScanner* tempScanner = XMLScannerResolver::resolveScanner
00515     (
00516         scannerName
00517         , fValidator
00518         , fGrammarResolver
00519         , fMemoryManager
00520     );
00521 
00522     if (tempScanner) {
00523 
00524         tempScanner->setParseSettings(fScanner);
00525         tempScanner->setURIStringPool(fURIStringPool);
00526         delete fScanner;
00527         fScanner = tempScanner;
00528     }
00529 }
00530 
00531 void SAXParser::setInputBufferSize(const XMLSize_t bufferSize)
00532 {
00533     fScanner->setInputBufferSize(bufferSize);
00534 }
00535 
00536 void SAXParser::setIgnoreCachedDTD(const bool newValue)
00537 {
00538     fScanner->setIgnoredCachedDTD(newValue);
00539 }
00540 
00541 void SAXParser::setIgnoreAnnotations(const bool newValue)
00542 {
00543     fScanner->setIgnoreAnnotations(newValue);
00544 }
00545 
00546 void SAXParser::setDisableDefaultEntityResolution(const bool newValue)
00547 {
00548     fScanner->setDisableDefaultEntityResolution(newValue);
00549 }
00550 
00551 void SAXParser::setSkipDTDValidation(const bool newValue)
00552 {
00553     fScanner->setSkipDTDValidation(newValue);
00554 }
00555 
00556 void SAXParser::setHandleMultipleImports(const bool newValue)
00557 {
00558     fScanner->setHandleMultipleImports(newValue);
00559 }
00560 
00561 // ---------------------------------------------------------------------------
00562 //  SAXParser: Overrides of the SAX Parser interface
00563 // ---------------------------------------------------------------------------
00564 void SAXParser::parse(const InputSource& source)
00565 {
00566     // Avoid multiple entrance
00567     if (fParseInProgress)
00568         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00569 
00570     ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
00571 
00572     try
00573     {
00574         fParseInProgress = true;
00575         fScanner->scanDocument(source);
00576     }
00577     catch(const OutOfMemoryException&)
00578     {
00579         resetInProgress.release();
00580 
00581         throw;
00582     }
00583 }
00584 
00585 void SAXParser::parse(const XMLCh* const systemId)
00586 {
00587     // Avoid multiple entrance
00588     if (fParseInProgress)
00589         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00590 
00591     ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
00592 
00593     try
00594     {
00595         fParseInProgress = true;
00596         fScanner->scanDocument(systemId);
00597     }
00598     catch(const OutOfMemoryException&)
00599     {
00600         resetInProgress.release();
00601 
00602         throw;
00603     }
00604 }
00605 
00606 void SAXParser::parse(const char* const systemId)
00607 {
00608     // Avoid multiple entrance
00609     if (fParseInProgress)
00610         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00611 
00612     ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
00613 
00614     try
00615     {
00616         fParseInProgress = true;
00617         fScanner->scanDocument(systemId);
00618     }
00619     catch(const OutOfMemoryException&)
00620     {
00621         resetInProgress.release();
00622 
00623         throw;
00624     }
00625 }
00626 
00627 void SAXParser::setDocumentHandler(DocumentHandler* const handler)
00628 {
00629     fDocHandler = handler;
00630     if (fDocHandler)
00631     {
00632         //
00633         //  Make sure we are set as the document handler with the scanner.
00634         //  We may already be (if advanced handlers are installed), but its
00635         //  not worthing checking, just do it.
00636         //
00637         fScanner->setDocHandler(this);
00638     }
00639      else
00640     {
00641         //
00642         //  If we don't have any advanced handlers either, then deinstall us
00643         //  from the scanner because we don't need document events anymore.
00644         //
00645         if (!fAdvDHCount)
00646             fScanner->setDocHandler(0);
00647     }
00648 }
00649 
00650 
00651 void SAXParser::setDTDHandler(DTDHandler* const handler)
00652 {
00653     fDTDHandler = handler;
00654     if (fDTDHandler)
00655         fScanner->setDocTypeHandler(this);
00656     else
00657         fScanner->setDocTypeHandler(0);
00658 }
00659 
00660 
00661 void SAXParser::setErrorHandler(ErrorHandler* const handler)
00662 {
00663     //
00664     //  Store the handler. Then either install or deinstall us as the
00665     //  error reporter on the scanner.
00666     //
00667     fErrorHandler = handler;
00668     if (fErrorHandler) {
00669         fScanner->setErrorReporter(this);
00670         fScanner->setErrorHandler(fErrorHandler);
00671     }
00672     else {
00673         fScanner->setErrorReporter(0);
00674         fScanner->setErrorHandler(0);
00675     }
00676 }
00677 
00678 
00679 void SAXParser::setPSVIHandler(PSVIHandler* const handler)
00680 {
00681     fPSVIHandler = handler;
00682     if (fPSVIHandler) {
00683         fScanner->setPSVIHandler(fPSVIHandler);
00684     }
00685     else {
00686         fScanner->setPSVIHandler(0);
00687     }
00688 }
00689 
00690 void SAXParser::setEntityResolver(EntityResolver* const resolver)
00691 {
00692     fEntityResolver = resolver;
00693     if (fEntityResolver) {
00694         fScanner->setEntityHandler(this);
00695         fXMLEntityResolver = 0;
00696     }
00697     else {
00698         fScanner->setEntityHandler(0);
00699     }
00700 }
00701 
00702 void SAXParser::setXMLEntityResolver(XMLEntityResolver* const resolver)
00703 {
00704     fXMLEntityResolver = resolver;
00705     if (fXMLEntityResolver) {
00706         fScanner->setEntityHandler(this);
00707         fEntityResolver = 0;
00708     }
00709     else {
00710         fScanner->setEntityHandler(0);
00711     }
00712 }
00713 
00714 // ---------------------------------------------------------------------------
00715 //  SAXParser: Progressive parse methods
00716 // ---------------------------------------------------------------------------
00717 bool SAXParser::parseFirst( const   XMLCh* const    systemId
00718                             ,       XMLPScanToken&  toFill)
00719 {
00720     //
00721     //  Avoid multiple entrance. We cannot enter here while a regular parse
00722     //  is in progress.
00723     //
00724     if (fParseInProgress)
00725         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00726 
00727     return fScanner->scanFirst(systemId, toFill);
00728 }
00729 
00730 bool SAXParser::parseFirst( const   char* const     systemId
00731                             ,       XMLPScanToken&  toFill)
00732 {
00733     //
00734     //  Avoid multiple entrance. We cannot enter here while a regular parse
00735     //  is in progress.
00736     //
00737     if (fParseInProgress)
00738         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00739 
00740     return fScanner->scanFirst(systemId, toFill);
00741 }
00742 
00743 bool SAXParser::parseFirst( const   InputSource&    source
00744                             ,       XMLPScanToken&  toFill)
00745 {
00746     //
00747     //  Avoid multiple entrance. We cannot enter here while a regular parse
00748     //  is in progress.
00749     //
00750     if (fParseInProgress)
00751         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00752 
00753     return fScanner->scanFirst(source, toFill);
00754 }
00755 
00756 bool SAXParser::parseNext(XMLPScanToken& token)
00757 {
00758     return fScanner->scanNext(token);
00759 }
00760 
00761 void SAXParser::parseReset(XMLPScanToken& token)
00762 {
00763     // Reset the scanner
00764     fScanner->scanReset(token);
00765 }
00766 
00767 
00768 // ---------------------------------------------------------------------------
00769 //  SAXParser: Overrides of the XMLDocumentHandler interface
00770 // ---------------------------------------------------------------------------
00771 void SAXParser::docCharacters(  const   XMLCh* const    chars
00772                                 , const XMLSize_t       length
00773                                 , const bool            cdataSection)
00774 {
00775     // Suppress the chars before the root element.
00776     if (fElemDepth)
00777     {
00778         // Just map to the SAX document handler
00779         if (fDocHandler)
00780             fDocHandler->characters(chars, length);
00781     }
00782 
00783     //
00784     //  If there are any installed advanced handlers, then lets call them
00785     //  with this info.
00786     //
00787     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00788         fAdvDHList[index]->docCharacters(chars, length, cdataSection);
00789 }
00790 
00791 
00792 void SAXParser::docComment(const XMLCh* const commentText)
00793 {
00794     //
00795     //  SAX has no way to report this. But, if there are any installed
00796     //  advanced handlers, then lets call them with this info.
00797     //
00798     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00799         fAdvDHList[index]->docComment(commentText);
00800 }
00801 
00802 
00803 void SAXParser::XMLDecl( const  XMLCh* const    versionStr
00804                         , const XMLCh* const    encodingStr
00805                         , const XMLCh* const    standaloneStr
00806                         , const XMLCh* const    actualEncodingStr
00807                         )
00808 {
00809     //
00810     //  SAX has no way to report this. But, if there are any installed
00811     //  advanced handlers, then lets call them with this info.
00812     //
00813     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00814         fAdvDHList[index]->XMLDecl( versionStr,
00815                                     encodingStr,
00816                                     standaloneStr,
00817                                     actualEncodingStr );
00818 }
00819 
00820 
00821 void SAXParser::docPI(  const   XMLCh* const    target
00822                         , const XMLCh* const    data)
00823 {
00824     // Just map to the SAX document handler
00825     if (fDocHandler)
00826         fDocHandler->processingInstruction(target, data);
00827 
00828     //
00829     //  If there are any installed advanced handlers, then lets call them
00830     //  with this info.
00831     //
00832     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00833         fAdvDHList[index]->docPI(target, data);
00834 }
00835 
00836 
00837 void SAXParser::endDocument()
00838 {
00839     if (fDocHandler)
00840         fDocHandler->endDocument();
00841 
00842     //
00843     //  If there are any installed advanced handlers, then lets call them
00844     //  with this info.
00845     //
00846     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00847         fAdvDHList[index]->endDocument();
00848 }
00849 
00850 
00851 void SAXParser::endElement( const   XMLElementDecl& elemDecl
00852                             , const unsigned int    uriId
00853                             , const bool            isRoot
00854                             , const XMLCh* const    elemPrefix)
00855 {
00856     // Just map to the SAX document handler
00857     if (fDocHandler) {
00858         if (fScanner->getDoNamespaces()) {
00859 
00860             if (elemPrefix && *elemPrefix) {
00861 
00862                 fElemQNameBuf.set(elemPrefix);
00863                 fElemQNameBuf.append(chColon);
00864                 fElemQNameBuf.append(elemDecl.getBaseName());
00865                 fDocHandler->endElement(fElemQNameBuf.getRawBuffer());
00866             }
00867             else {
00868                 fDocHandler->endElement(elemDecl.getBaseName());
00869             }
00870         }
00871         else
00872             fDocHandler->endElement(elemDecl.getFullName());
00873 
00874     }
00875 
00876     //
00877     //  If there are any installed advanced handlers, then lets call them
00878     //  with this info.
00879     //
00880     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00881         fAdvDHList[index]->endElement(elemDecl, uriId, isRoot, elemPrefix);
00882 
00883     //
00884     //  Dump the element depth down again. Don't let it underflow in case
00885     //  of malformed XML.
00886     //
00887     if (fElemDepth)
00888         fElemDepth--;
00889 }
00890 
00891 
00892 void SAXParser::endEntityReference(const XMLEntityDecl& entityDecl)
00893 {
00894     //
00895     //  SAX has no way to report this event. But, if there are any installed
00896     //  advanced handlers, then lets call them with this info.
00897     //
00898     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00899         fAdvDHList[index]->endEntityReference(entityDecl);
00900 }
00901 
00902 
00903 void SAXParser::ignorableWhitespace(const   XMLCh* const    chars
00904                                     , const XMLSize_t       length
00905                                     , const bool            cdataSection)
00906 {
00907     // Do not report the whitespace before the root element.
00908     if (!fElemDepth)
00909         return;
00910 
00911     // Just map to the SAX document handler
00912     if (fDocHandler)
00913         fDocHandler->ignorableWhitespace(chars, length);
00914 
00915     //
00916     //  If there are any installed advanced handlers, then lets call them
00917     //  with this info.
00918     //
00919     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00920         fAdvDHList[index]->ignorableWhitespace(chars, length, cdataSection);
00921 }
00922 
00923 
00924 void SAXParser::resetDocument()
00925 {
00926     // Just map to the SAX document handler
00927     if (fDocHandler)
00928         fDocHandler->resetDocument();
00929 
00930     //
00931     //  If there are any installed advanced handlers, then lets call them
00932     //  with this info.
00933     //
00934     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00935         fAdvDHList[index]->resetDocument();
00936 
00937     // Make sure our element depth flag gets set back to zero
00938     fElemDepth = 0;
00939 }
00940 
00941 
00942 void SAXParser::startDocument()
00943 {
00944     // Just map to the SAX document handler
00945     if (fDocHandler)
00946         fDocHandler->setDocumentLocator(fScanner->getLocator());
00947     if(fDocHandler)
00948         fDocHandler->startDocument();
00949 
00950     //
00951     //  If there are any installed advanced handlers, then lets call them
00952     //  with this info.
00953     //
00954     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
00955         fAdvDHList[index]->startDocument();
00956 }
00957 
00958 
00959 void SAXParser::
00960 startElement(   const   XMLElementDecl&         elemDecl
00961                 , const unsigned int            elemURLId
00962                 , const XMLCh* const            elemPrefix
00963                 , const RefVectorOf<XMLAttr>&   attrList
00964                 , const XMLSize_t               attrCount
00965                 , const bool                    isEmpty
00966                 , const bool                    isRoot)
00967 {
00968     // Bump the element depth counter if not empty
00969     if (!isEmpty)
00970         fElemDepth++;
00971 
00972     if (fDocHandler)
00973     {
00974         fAttrList.setVector(&attrList, attrCount);
00975         if (fScanner->getDoNamespaces()) {
00976 
00977             if (elemPrefix && *elemPrefix) {
00978 
00979                 fElemQNameBuf.set(elemPrefix);
00980                 fElemQNameBuf.append(chColon);
00981                 fElemQNameBuf.append(elemDecl.getBaseName());
00982                 fDocHandler->startElement(fElemQNameBuf.getRawBuffer(), fAttrList);
00983 
00984                 // If its empty, send the end tag event now
00985                 if (isEmpty && fDocHandler)
00986                     fDocHandler->endElement(fElemQNameBuf.getRawBuffer());
00987             }
00988             else {
00989 
00990                 fDocHandler->startElement(elemDecl.getBaseName(), fAttrList);
00991 
00992                 // If its empty, send the end tag event now
00993                 if (isEmpty && fDocHandler)
00994                     fDocHandler->endElement(elemDecl.getBaseName());
00995             }
00996         }
00997         else {
00998             fDocHandler->startElement(elemDecl.getFullName(), fAttrList);
00999 
01000             // If its empty, send the end tag event now
01001             if (isEmpty && fDocHandler)
01002                 fDocHandler->endElement(elemDecl.getFullName());
01003         }
01004     }
01005 
01006     //
01007     //  If there are any installed advanced handlers, then lets call them
01008     //  with this info.
01009     //
01010     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
01011     {
01012         fAdvDHList[index]->startElement
01013         (
01014             elemDecl
01015             , elemURLId
01016             , elemPrefix
01017             , attrList
01018             , attrCount
01019             , isEmpty
01020             , isRoot
01021         );
01022     }
01023 }
01024 
01025 
01026 void SAXParser::startEntityReference(const XMLEntityDecl& entityDecl)
01027 {
01028     //
01029     //  SAX has no way to report this. But, If there are any installed
01030     //  advanced handlers, then lets call them with this info.
01031     //
01032     for (XMLSize_t index = 0; index < fAdvDHCount; index++)
01033         fAdvDHList[index]->startEntityReference(entityDecl);
01034 }
01035 
01036 
01037 
01038 // ---------------------------------------------------------------------------
01039 //  SAXParser: Overrides of the DocTypeHandler interface
01040 // ---------------------------------------------------------------------------
01041 void SAXParser::attDef( const   DTDElementDecl&
01042                         , const DTDAttDef&
01043                         , const bool)
01044 {
01045     // Unused by SAX DTDHandler interface at this time
01046 }
01047 
01048 
01049 void SAXParser::doctypeComment(const XMLCh* const)
01050 {
01051     // Unused by SAX DTDHandler interface at this time
01052 }
01053 
01054 
01055 void SAXParser::doctypeDecl(const   DTDElementDecl&
01056                             , const XMLCh* const
01057                             , const XMLCh* const
01058                             , const bool
01059                             , const bool)
01060 {
01061     // Unused by SAX DTDHandler interface at this time
01062 }
01063 
01064 
01065 void SAXParser::doctypePI(  const   XMLCh* const
01066                             , const XMLCh* const)
01067 {
01068     // Unused by SAX DTDHandler interface at this time
01069 }
01070 
01071 
01072 void SAXParser::doctypeWhitespace(  const   XMLCh* const
01073                                     , const XMLSize_t)
01074 {
01075     // Unused by SAX DTDHandler interface at this time
01076 }
01077 
01078 
01079 void SAXParser::elementDecl(const DTDElementDecl&, const bool)
01080 {
01081     // Unused by SAX DTDHandler interface at this time
01082 }
01083 
01084 
01085 void SAXParser::endAttList(const DTDElementDecl&)
01086 {
01087     // Unused by SAX DTDHandler interface at this time
01088 }
01089 
01090 
01091 void SAXParser::endIntSubset()
01092 {
01093     // Unused by SAX DTDHandler interface at this time
01094 }
01095 
01096 
01097 void SAXParser::endExtSubset()
01098 {
01099     // Unused by SAX DTDHandler interface at this time
01100 }
01101 
01102 
01103 void SAXParser::entityDecl( const   DTDEntityDecl&  entityDecl
01104                             , const bool
01105                             , const bool            isIgnored)
01106 {
01107     //
01108     //  If we have a DTD handler, and this entity is not ignored, and
01109     //  its an unparsed entity, then send this one.
01110     //
01111     if (fDTDHandler && !isIgnored)
01112     {
01113         if (entityDecl.isUnparsed())
01114         {
01115             fDTDHandler->unparsedEntityDecl
01116             (
01117                 entityDecl.getName()
01118                 , entityDecl.getPublicId()
01119                 , entityDecl.getSystemId()
01120                 , entityDecl.getNotationName()
01121             );
01122         }
01123     }
01124 }
01125 
01126 
01127 void SAXParser::resetDocType()
01128 {
01129     // Just map to the DTD handler
01130     if (fDTDHandler)
01131         fDTDHandler->resetDocType();
01132 }
01133 
01134 
01135 void SAXParser::notationDecl(   const   XMLNotationDecl&    notDecl
01136                                 , const bool                isIgnored)
01137 {
01138     if (fDTDHandler && !isIgnored)
01139     {
01140         fDTDHandler->notationDecl
01141         (
01142             notDecl.getName()
01143             , notDecl.getPublicId()
01144             , notDecl.getSystemId()
01145         );
01146     }
01147 }
01148 
01149 
01150 void SAXParser::startAttList(const DTDElementDecl&)
01151 {
01152     // Unused by SAX DTDHandler interface at this time
01153 }
01154 
01155 
01156 void SAXParser::startIntSubset()
01157 {
01158     // Unused by SAX DTDHandler interface at this time
01159 }
01160 
01161 
01162 void SAXParser::startExtSubset()
01163 {
01164     // Unused by SAX DTDHandler interface at this time
01165 }
01166 
01167 
01168 void SAXParser::TextDecl(   const  XMLCh* const
01169                             , const XMLCh* const)
01170 {
01171     // Unused by SAX DTDHandler interface at this time
01172 }
01173 
01174 
01175 // ---------------------------------------------------------------------------
01176 //  SAXParser: Overrides of the XMLErrorReporter interface
01177 // ---------------------------------------------------------------------------
01178 void SAXParser::resetErrors()
01179 {
01180     if (fErrorHandler)
01181         fErrorHandler->resetErrors();
01182 }
01183 
01184 
01185 void SAXParser::error(  const   unsigned int
01186                         , const XMLCh* const
01187                         , const XMLErrorReporter::ErrTypes  errType
01188                         , const XMLCh* const                errorText
01189                         , const XMLCh* const                systemId
01190                         , const XMLCh* const                publicId
01191                         , const XMLFileLoc                  lineNum
01192                         , const XMLFileLoc                  colNum)
01193 {
01194     SAXParseException toThrow = SAXParseException
01195     (
01196         errorText
01197         , publicId
01198         , systemId
01199         , lineNum
01200         , colNum
01201         , fMemoryManager
01202     );
01203 
01204     if (!fErrorHandler)
01205     {
01206         if (errType == XMLErrorReporter::ErrType_Fatal)
01207             throw toThrow;
01208         else
01209             return;
01210     }
01211 
01212     if (errType == XMLErrorReporter::ErrType_Warning)
01213         fErrorHandler->warning(toThrow);
01214     else if (errType == XMLErrorReporter::ErrType_Fatal)
01215         fErrorHandler->fatalError(toThrow);
01216     else
01217         fErrorHandler->error(toThrow);
01218 }
01219 
01220 
01221 
01222 // ---------------------------------------------------------------------------
01223 //  SAXParser: Handlers for the XMLEntityHandler interface
01224 // ---------------------------------------------------------------------------
01225 void SAXParser::endInputSource(const InputSource&)
01226 {
01227 }
01228 
01229 bool SAXParser::expandSystemId(const XMLCh* const, XMLBuffer&)
01230 {
01231     return false;
01232 }
01233 
01234 
01235 void SAXParser::resetEntities()
01236 {
01237     // Nothing to do for this one
01238 }
01239 
01240 InputSource*
01241 SAXParser::resolveEntity(  XMLResourceIdentifier* resourceIdentifier )
01242 {
01243     // Just map to the SAX entity resolver handler
01244     if (fEntityResolver)
01245         return fEntityResolver->resolveEntity(resourceIdentifier->getPublicId(),
01246                                                 resourceIdentifier->getSystemId());
01247     if (fXMLEntityResolver)
01248         return fXMLEntityResolver->resolveEntity(resourceIdentifier);
01249     return 0;
01250 }
01251 
01252 
01253 void SAXParser::startInputSource(const InputSource&)
01254 {
01255     // Nothing to do for this one
01256 }
01257 
01258 
01259 // ---------------------------------------------------------------------------
01260 //  SAXParser: Grammar preparsing methods
01261 // ---------------------------------------------------------------------------
01262 Grammar* SAXParser::loadGrammar(const char* const systemId,
01263                                 const Grammar::GrammarType grammarType,
01264                                 const bool toCache)
01265 {
01266     // Avoid multiple entrance
01267     if (fParseInProgress)
01268         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
01269 
01270     ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
01271 
01272     Grammar* grammar = 0;
01273     try
01274     {
01275         fParseInProgress = true;
01276         grammar = fScanner->loadGrammar(systemId, grammarType, toCache);
01277     }
01278     catch(const OutOfMemoryException&)
01279     {
01280         resetInProgress.release();
01281 
01282         throw;
01283     }
01284 
01285     return grammar;
01286 }
01287 
01288 Grammar* SAXParser::loadGrammar(const XMLCh* const systemId,
01289                                 const Grammar::GrammarType grammarType,
01290                                 const bool toCache)
01291 {
01292     // Avoid multiple entrance
01293     if (fParseInProgress)
01294         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
01295 
01296     ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
01297 
01298     Grammar* grammar = 0;
01299     try
01300     {
01301         fParseInProgress = true;
01302         grammar = fScanner->loadGrammar(systemId, grammarType, toCache);
01303     }
01304     catch(const OutOfMemoryException&)
01305     {
01306         resetInProgress.release();
01307 
01308         throw;
01309     }
01310 
01311     return grammar;
01312 }
01313 
01314 Grammar* SAXParser::loadGrammar(const InputSource& source,
01315                                 const Grammar::GrammarType grammarType,
01316                                 const bool toCache)
01317 {
01318     // Avoid multiple entrance
01319     if (fParseInProgress)
01320         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
01321 
01322     ResetInProgressType     resetInProgress(this, &SAXParser::resetInProgress);
01323 
01324     Grammar* grammar = 0;
01325     try
01326     {
01327         fParseInProgress = true;
01328         grammar = fScanner->loadGrammar(source, grammarType, toCache);
01329     }
01330     catch(const OutOfMemoryException&)
01331     {
01332         resetInProgress.release();
01333 
01334         throw;
01335     }
01336 
01337     return grammar;
01338 }
01339 
01340 void SAXParser::resetInProgress()
01341 {
01342     fParseInProgress = false;
01343 }
01344 
01345 void SAXParser::resetCachedGrammarPool()
01346 {
01347     fGrammarResolver->resetCachedGrammar();
01348     fScanner->resetCachedGrammar();
01349 }
01350 
01351 XERCES_CPP_NAMESPACE_END