GME  13
XercesDOMParser.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 
00029 // ---------------------------------------------------------------------------
00030 //  Includes
00031 // ---------------------------------------------------------------------------
00032 #include <xercesc/sax/EntityResolver.hpp>
00033 #include <xercesc/sax/ErrorHandler.hpp>
00034 #include <xercesc/sax/SAXParseException.hpp>
00035 #include <xercesc/util/IOException.hpp>
00036 #include <xercesc/internal/XMLScanner.hpp>
00037 #include <xercesc/parsers/XercesDOMParser.hpp>
00038 #include <xercesc/validators/common/GrammarResolver.hpp>
00039 #include <xercesc/framework/XMLGrammarPool.hpp>
00040 #include <xercesc/framework/XMLSchemaDescription.hpp>
00041 #include <xercesc/util/Janitor.hpp>
00042 #include <xercesc/util/OutOfMemoryException.hpp>
00043 #include <xercesc/util/XMLEntityResolver.hpp>
00044 
00045 XERCES_CPP_NAMESPACE_BEGIN
00046 
00047 
00048 
00049 // ---------------------------------------------------------------------------
00050 //  XercesDOMParser: Constructors and Destructor
00051 // ---------------------------------------------------------------------------
00052 XercesDOMParser::XercesDOMParser( XMLValidator* const   valToAdopt
00053                                 , MemoryManager* const  manager
00054                                 , XMLGrammarPool* const gramPool):
00055 
00056 AbstractDOMParser(valToAdopt, manager, gramPool)
00057 , fEntityResolver(0)
00058 , fXMLEntityResolver(0)
00059 , fErrorHandler(0)
00060 {
00061 }
00062 
00063 
00064 XercesDOMParser::~XercesDOMParser()
00065 {
00066 }
00067 
00068 
00069 // ---------------------------------------------------------------------------
00070 //  XercesDOMParser: Getter methods
00071 // ---------------------------------------------------------------------------
00072 bool XercesDOMParser::isCachingGrammarFromParse() const
00073 {
00074     return getScanner()->isCachingGrammarFromParse();
00075 }
00076 
00077 bool XercesDOMParser::isUsingCachedGrammarInParse() const
00078 {
00079     return getScanner()->isUsingCachedGrammarInParse();
00080 }
00081 
00082 Grammar* XercesDOMParser::getGrammar(const XMLCh* const nameSpaceKey)
00083 {
00084     return getGrammarResolver()->getGrammar(nameSpaceKey);
00085 }
00086 
00087 Grammar* XercesDOMParser::getRootGrammar()
00088 {
00089     return getScanner()->getRootGrammar();
00090 }
00091 
00092 const XMLCh* XercesDOMParser::getURIText(unsigned int uriId) const
00093 {
00094     return getScanner()->getURIText(uriId);
00095 }
00096 
00097 XMLFilePos XercesDOMParser::getSrcOffset() const
00098 {
00099     return getScanner()->getSrcOffset();
00100 }
00101 
00102 bool XercesDOMParser::getIgnoreCachedDTD() const
00103 {
00104     return getScanner()->getIgnoreCachedDTD();
00105 }
00106 
00107 // ---------------------------------------------------------------------------
00108 //  XercesDOMParser: Setter methods
00109 // ---------------------------------------------------------------------------
00110 void XercesDOMParser::setErrorHandler(ErrorHandler* const handler)
00111 {
00112     fErrorHandler = handler;
00113     XMLScanner* scanner = getScanner();
00114     if (fErrorHandler) {
00115         scanner->setErrorReporter(this);
00116         scanner->setErrorHandler(fErrorHandler);
00117     }
00118     else {
00119         scanner->setErrorReporter(0);
00120         scanner->setErrorHandler(0);
00121     }
00122 }
00123 
00124 void XercesDOMParser::setEntityResolver(EntityResolver* const handler)
00125 {
00126     fEntityResolver = handler;
00127     if (fEntityResolver) {
00128         getScanner()->setEntityHandler(this);
00129         fXMLEntityResolver = 0;
00130     }
00131     else {
00132         getScanner()->setEntityHandler(0);
00133     }
00134 }
00135 
00136 void XercesDOMParser::setXMLEntityResolver(XMLEntityResolver* const handler)
00137 {
00138     fXMLEntityResolver = handler;
00139     if (fXMLEntityResolver) {
00140         getScanner()->setEntityHandler(this);
00141         fEntityResolver = 0;
00142     }
00143     else {
00144         getScanner()->setEntityHandler(0);
00145     }
00146 }
00147 
00148 void XercesDOMParser::cacheGrammarFromParse(const bool newState)
00149 {
00150     getScanner()->cacheGrammarFromParse(newState);
00151 
00152     if (newState)
00153         getScanner()->useCachedGrammarInParse(newState);
00154 }
00155 
00156 void XercesDOMParser::useCachedGrammarInParse(const bool newState)
00157 {
00158     if (newState || !getScanner()->isCachingGrammarFromParse())
00159         getScanner()->useCachedGrammarInParse(newState);
00160 }
00161 
00162 void XercesDOMParser::setIgnoreCachedDTD(const bool newValue)
00163 {
00164     getScanner()->setIgnoredCachedDTD(newValue);
00165 }
00166 
00167 // ---------------------------------------------------------------------------
00168 //  XercesDOMParser: Utilities
00169 // ---------------------------------------------------------------------------
00170 void XercesDOMParser::resetDocumentPool()
00171 {
00172     resetPool();
00173 }
00174 
00175 
00176 // ---------------------------------------------------------------------------
00177 //  XercesDOMParser: Implementation of the XMLErrorReporter interface
00178 // ---------------------------------------------------------------------------
00179 void XercesDOMParser::error( const   unsigned int
00180                              , const XMLCh* const
00181                              , const XMLErrorReporter::ErrTypes  errType
00182                              , const XMLCh* const                errorText
00183                              , const XMLCh* const                systemId
00184                              , const XMLCh* const                publicId
00185                              , const XMLFileLoc                  lineNum
00186                              , const XMLFileLoc                  colNum)
00187 {
00188     SAXParseException toThrow = SAXParseException
00189         (
00190         errorText
00191         , publicId
00192         , systemId
00193         , lineNum
00194         , colNum
00195         , getMemoryManager()
00196         );
00197 
00198     //
00199     //  If there is an error handler registered, call it, otherwise ignore
00200     //  all but the fatal errors.
00201     //
00202     if (!fErrorHandler)
00203     {
00204         if (errType == XMLErrorReporter::ErrType_Fatal)
00205             throw toThrow;
00206         return;
00207     }
00208 
00209     if (errType == XMLErrorReporter::ErrType_Warning)
00210         fErrorHandler->warning(toThrow);
00211     else if (errType >= XMLErrorReporter::ErrType_Fatal)
00212         fErrorHandler->fatalError(toThrow);
00213     else
00214         fErrorHandler->error(toThrow);
00215 }
00216 
00217 void XercesDOMParser::resetErrors()
00218 {
00219 }
00220 
00221 
00222 // ---------------------------------------------------------------------------
00223 //  XercesDOMParser: Implementation of XMLEntityHandler interface
00224 // ---------------------------------------------------------------------------
00225 InputSource*
00226 XercesDOMParser::resolveEntity(XMLResourceIdentifier* resourceIdentifier)
00227 {
00228     //
00229     //  Just map it to the SAX entity resolver. If there is not one installed,
00230     //  return a null pointer to cause the default resolution.
00231     //
00232     if (fEntityResolver)
00233         return fEntityResolver->resolveEntity(resourceIdentifier->getPublicId(),
00234                                                 resourceIdentifier->getSystemId());
00235     if (fXMLEntityResolver)
00236         return fXMLEntityResolver->resolveEntity(resourceIdentifier);
00237 
00238     return 0;
00239 }
00240 
00241 typedef JanitorMemFunCall<XercesDOMParser>  ResetParseType;
00242 
00243 // ---------------------------------------------------------------------------
00244 //  XercesDOMParser: Grammar preparsing methods
00245 // ---------------------------------------------------------------------------
00246 Grammar* XercesDOMParser::loadGrammar(const char* const systemId,
00247                                       const Grammar::GrammarType grammarType,
00248                                       const bool toCache)
00249 {
00250     // Avoid multiple entrance
00251     if (getParseInProgress())
00252         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00253 
00254     ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
00255 
00256     Grammar* grammar = 0;
00257 
00258     try
00259     {
00260         setParseInProgress(true);
00261         if (grammarType == Grammar::DTDGrammarType)
00262             getScanner()->setDocTypeHandler(0);
00263         grammar = getScanner()->loadGrammar(systemId, grammarType, toCache);
00264     }
00265     catch(const OutOfMemoryException&)
00266     {
00267         resetParse.release();
00268 
00269         throw;
00270     }
00271 
00272     return grammar;
00273 }
00274 
00275 Grammar* XercesDOMParser::loadGrammar(const XMLCh* const systemId,
00276                                       const Grammar::GrammarType grammarType,
00277                                       const bool toCache)
00278 {
00279     // Avoid multiple entrance
00280     if (getParseInProgress())
00281         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00282 
00283     ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
00284 
00285     Grammar* grammar = 0;
00286 
00287     try
00288     {
00289         setParseInProgress(true);
00290         if (grammarType == Grammar::DTDGrammarType)
00291             getScanner()->setDocTypeHandler(0);
00292         grammar = getScanner()->loadGrammar(systemId, grammarType, toCache);
00293     }
00294     catch(const OutOfMemoryException&)
00295     {
00296         resetParse.release();
00297 
00298         throw;
00299     }
00300 
00301     return grammar;
00302 }
00303 
00304 Grammar* XercesDOMParser::loadGrammar(const InputSource& source,
00305                                       const Grammar::GrammarType grammarType,
00306                                       const bool toCache)
00307 {
00308     // Avoid multiple entrance
00309     if (getParseInProgress())
00310         ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);
00311 
00312     ResetParseType  resetParse(this, &XercesDOMParser::resetParse);
00313 
00314     Grammar* grammar = 0;
00315 
00316     try
00317     {
00318         setParseInProgress(true);
00319         if (grammarType == Grammar::DTDGrammarType)
00320             getScanner()->setDocTypeHandler(0);
00321         grammar = getScanner()->loadGrammar(source, grammarType, toCache);
00322     }
00323     catch(const OutOfMemoryException&)
00324     {
00325         resetParse.release();
00326 
00327         throw;
00328     }
00329 
00330     return grammar;
00331 }
00332 
00333 void XercesDOMParser::resetParse()
00334 {
00335     if (getScanner()->getDocTypeHandler() == 0)
00336     {
00337         getScanner()->setDocTypeHandler(this);
00338     }
00339 
00340     setParseInProgress(false);
00341 }
00342 
00343 void XercesDOMParser::resetCachedGrammarPool()
00344 {
00345     getGrammarResolver()->resetCachedGrammar();
00346     getScanner()->resetCachedGrammar();
00347 }
00348 
00349 XERCES_CPP_NAMESPACE_END