Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:14:55

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_READERMGR_HPP)
0023 #define XERCESC_INCLUDE_GUARD_READERMGR_HPP
0024 
0025 #include <xercesc/internal/XMLReader.hpp>
0026 #include <xercesc/util/PlatformUtils.hpp>
0027 #include <xercesc/util/RefStackOf.hpp>
0028 #include <xercesc/sax/Locator.hpp>
0029 #include <xercesc/framework/XMLBuffer.hpp>
0030 
0031 XERCES_CPP_NAMESPACE_BEGIN
0032 
0033 class XMLEntityDecl;
0034 class XMLEntityHandler;
0035 class XMLDocumentHandler;
0036 class XMLScanner;
0037 
0038 
0039 // ---------------------------------------------------------------------------
0040 //  This class is used by the scanner. The scanner must deal with expansion
0041 //  of entities, some of which are totally different files (external parsed
0042 //  entities.) It does so by pushing readers onto a stack. The top reader is
0043 //  the one it wants to read out of, but that one must be popped when it is
0044 //  empty. To keep that logic from being all over the place, the scanner
0045 //  talks to the reader manager, which handles the stack and popping off
0046 //  used up readers.
0047 // ---------------------------------------------------------------------------
0048 class XMLPARSER_EXPORT ReaderMgr :   public XMemory
0049                                    , public Locator
0050 {
0051 public :
0052     // -----------------------------------------------------------------------
0053     //  Class specific types
0054     // -----------------------------------------------------------------------
0055     struct LastExtEntityInfo : public XMemory
0056     {
0057         const   XMLCh*          systemId;
0058         const   XMLCh*          publicId;
0059                 XMLFileLoc      lineNumber;
0060                 XMLFileLoc      colNumber;
0061     };
0062 
0063 
0064     // -----------------------------------------------------------------------
0065     //  Constructors and Destructor
0066     // -----------------------------------------------------------------------
0067     ReaderMgr(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0068     ~ReaderMgr();
0069 
0070 
0071     // -----------------------------------------------------------------------
0072     //  Convenience scanning methods
0073     //
0074     //  This are all convenience methods that work in terms of the core
0075     //  character spooling methods.
0076     // -----------------------------------------------------------------------
0077     bool atEOF() const;
0078     bool getName(XMLBuffer& toFill);
0079     bool getQName(XMLBuffer& toFill, int* colonPosition);
0080     bool getNameToken(XMLBuffer& toFill);
0081     XMLCh getNextChar();
0082     bool getNextCharIfNot(const XMLCh chNotToGet, XMLCh& chGotten);
0083     void movePlainContentChars(XMLBuffer &dest);
0084     void getSpaces(XMLBuffer& toFill);
0085     void getUpToCharOrWS(XMLBuffer& toFill, const XMLCh toCheck);
0086     bool isEmpty() const;
0087     bool lookingAtChar(const XMLCh toCheck);
0088     bool lookingAtSpace();
0089     XMLCh peekNextChar();
0090     bool skipIfQuote(XMLCh& chGotten);
0091     void skipPastChar(const XMLCh toSkip);
0092     void skipPastSpaces(bool& skippedSomething, bool inDecl = false);
0093     void skipPastSpaces();
0094     void skipToChar(const XMLCh toSkipTo);
0095     bool skippedChar(const XMLCh toSkip);
0096     bool skippedSpace();
0097     bool skippedString(const XMLCh* const toSkip);
0098     bool skippedStringLong(const XMLCh* const toSkip);
0099     void skipQuotedString(const XMLCh quoteCh);
0100     XMLCh skipUntilIn(const XMLCh* const listToSkip);
0101     XMLCh skipUntilInOrWS(const XMLCh* const listToSkip);
0102     bool peekString(const XMLCh* const toPeek);
0103 
0104 
0105     // -----------------------------------------------------------------------
0106     //  Control methods
0107     // -----------------------------------------------------------------------
0108     void cleanStackBackTo(const XMLSize_t readerNum);
0109     XMLReader* createReader
0110     (
0111         const   InputSource&        src
0112         , const bool                xmlDecl
0113         , const XMLReader::RefFrom  refFrom
0114         , const XMLReader::Types    type
0115         , const XMLReader::Sources  source
0116         , const bool                calcSrsOfs = true
0117         ,       XMLSize_t           lowWaterMark = 100
0118     );
0119     XMLReader* createReader
0120     (
0121         const   XMLCh* const        sysId
0122         , const XMLCh* const        pubId
0123         , const bool                xmlDecl
0124         , const XMLReader::RefFrom  refFrom
0125         , const XMLReader::Types    type
0126         , const XMLReader::Sources  source
0127         ,       InputSource*&       srcToFill
0128         , const bool                calcSrcOfs = true
0129         ,       XMLSize_t           lowWaterMark = 100
0130         , const bool                disableDefaultEntityResolution = false
0131     );
0132     XMLReader* createReader
0133     (
0134         const   XMLCh* const        baseURI
0135         , const XMLCh* const        sysId
0136         , const XMLCh* const        pubId
0137         , const bool                xmlDecl
0138         , const XMLReader::RefFrom  refFrom
0139         , const XMLReader::Types    type
0140         , const XMLReader::Sources  source
0141         ,       InputSource*&       srcToFill
0142         , const bool                calcSrcOfs = true
0143         ,       XMLSize_t           lowWaterMark = 100
0144         , const bool                disableDefaultEntityResolution = false
0145     );
0146     XMLReader* createIntEntReader
0147     (
0148         const   XMLCh* const        sysId
0149         , const XMLReader::RefFrom  refFrom
0150         , const XMLReader::Types    type
0151         , const XMLCh* const        dataBuf
0152         , const XMLSize_t           dataLen
0153         , const bool                copyBuf
0154         , const bool                calcSrcOfs = true
0155         ,       XMLSize_t           lowWaterMark = 100
0156     );
0157     bool isScanningPERefOutOfLiteral() const;
0158     bool pushReader
0159     (
0160                 XMLReader* const        reader
0161         ,       XMLEntityDecl* const    entity
0162     );
0163     bool pushReaderAdoptEntity
0164     (
0165                 XMLReader* const        reader
0166         ,       XMLEntityDecl* const    entity
0167         ,       const bool              adoptEntity = true
0168     );
0169     void reset();
0170 
0171 
0172     // -----------------------------------------------------------------------
0173     //  Getter methods
0174     // -----------------------------------------------------------------------
0175     const XMLCh* getCurrentEncodingStr() const;
0176     const XMLEntityDecl* getCurrentEntity() const;
0177     XMLEntityDecl* getCurrentEntity();
0178     const XMLReader* getCurrentReader() const;
0179     XMLReader* getCurrentReader();
0180     XMLSize_t getCurrentReaderNum() const;
0181     XMLSize_t getReaderDepth() const;
0182     void getLastExtEntityInfo(LastExtEntityInfo& lastInfo) const;
0183     XMLFilePos getSrcOffset() const;
0184     bool getThrowEOE() const;
0185 
0186 
0187     // -----------------------------------------------------------------------
0188     //  Setter methods
0189     // -----------------------------------------------------------------------
0190     void setEntityHandler(XMLEntityHandler* const newHandler);
0191     void setThrowEOE(const bool newValue);
0192     void setXMLVersion(const XMLReader::XMLVersion version);
0193     void setStandardUriConformant(const bool newValue);
0194 
0195     // -----------------------------------------------------------------------
0196     //  Implement the SAX Locator interface
0197     // -----------------------------------------------------------------------
0198     virtual const XMLCh* getPublicId() const;
0199     virtual const XMLCh* getSystemId() const;
0200     virtual XMLFileLoc getLineNumber() const;
0201     virtual XMLFileLoc getColumnNumber() const;
0202 
0203 
0204 private :
0205     // -----------------------------------------------------------------------
0206     //  Private helper methods
0207     // -----------------------------------------------------------------------
0208     const XMLReader* getLastExtEntity(const XMLEntityDecl*& itsEntity) const;
0209     bool popReader();
0210 
0211     // -----------------------------------------------------------------------
0212     //  Unimplemented constructors and operators
0213     // -----------------------------------------------------------------------
0214     ReaderMgr(const ReaderMgr&);
0215     ReaderMgr& operator=(const ReaderMgr&);
0216 
0217     // -----------------------------------------------------------------------
0218     //  Private data types
0219     // -----------------------------------------------------------------------
0220     class ReaderData : public XMemory
0221     {
0222     public  :
0223         // ---------------------------------------------------------------------
0224         //  Constructors and Destructor
0225         // ---------------------------------------------------------------------
0226         ReaderData
0227         (    XMLReader* const     reader
0228            , XMLEntityDecl* const entity
0229            , const bool           adoptEntity
0230         );
0231 
0232         ~ReaderData();
0233 
0234         // ----------------------------------------------------------------------
0235         //  Getter methods
0236         // ----------------------------------------------------------------------
0237         XMLReader* getReader() const;
0238         XMLEntityDecl* getEntity() const;
0239         bool getEntityAdopted() const;
0240 
0241         XMLEntityDecl* releaseEntity();
0242 
0243     private :
0244         // ---------------------------------------------------------------------
0245         //  Unimplemented constructors and operators
0246         // ---------------------------------------------------------------------
0247         ReaderData();
0248         ReaderData(const ReaderData&);
0249         ReaderData& operator=(const ReaderData&);
0250 
0251         // ---------------------------------------------------------------------
0252         //  Private data members
0253         //
0254         //  fReader
0255         //      This is the pointer to the reader object that must be destroyed
0256         //      when this object is destroyed.
0257         //
0258         //  fEntity
0259         //  fEntityAdopted
0260         //      This is the pointer to the entity object that, if adopted, must
0261         //      be destroyed when this object is destroyed.
0262         //
0263         //      Note that we need to keep up with which of the pushed readers
0264         //      are pushed entity values that are being spooled. This is done
0265         //      to avoid the problem of recursive definitions.
0266         // ---------------------------------------------------------------------
0267         XMLReader*     fReader;
0268         XMLEntityDecl* fEntity;
0269         bool           fEntityAdopted;
0270     };
0271 
0272     // -----------------------------------------------------------------------
0273     //  Private data members
0274     //
0275     //  fCurReaderData
0276     //      This is the current top of the reader data stack. We pull it off
0277     //      the stack and store it here for efficiency.
0278     //
0279     //  fCurReader
0280     //      This is the reader of the current top of the reader data stack.
0281     //      It contains the same value as fCurReaderData->fReader or NULL,
0282     //      if fCurReaderData is NULL. We store it here for efficiency.
0283     //
0284     //  fEntityHandler
0285     //      This is the installed entity handler. Its installed via the
0286     //      scanner but he passes it on to us since we need it the most, in
0287     //      process of creating external entity readers.
0288     //
0289     //  fEntityStack
0290     //      This is a storage of orphaned XMLEntityDecl objects. The
0291     //      popReader() function adds a reader manager-adopted entity object
0292     //      to this storage before passing its pointer to the constructor
0293     //      of the being thrown EndOfEntityException exception. This makes
0294     //      sure that the life-time of an entity exposed to the exception
0295     //      handlers is the same as the life-time of reader manager (and so
0296     //      normally the life-time of the scanner which embeds the reader
0297     //      manager).
0298     //
0299     //  fNextReaderNum
0300     //      This is the reader serial number value. Each new reader that is
0301     //      created from this reader is given a successive number. This lets
0302     //      us catch things like partial markup errors and such.
0303     //
0304     //  fReaderStack
0305     //      This is the stack of reader data references. We own all the
0306     //      entries and destroy them when they are used up.
0307     //
0308     //  fThrowEOE
0309     //      This flag controls whether we throw an exception when we hit an
0310     //      end of entity. The scanner doesn't really need to know about ends
0311     //      of entities in the int/ext subsets, so it will turn this flag off
0312     //      until it gets into the content usually.
0313     //
0314     //  fXMLVersion
0315     //      Enum to indicate if each Reader should be created as XML 1.1 or
0316     //      XML 1.0 conformant
0317     //
0318     //  fStandardUriConformant
0319     //      This flag controls whether we force conformant URI
0320     // -----------------------------------------------------------------------
0321     ReaderData*                 fCurReaderData;
0322     XMLReader*                  fCurReader;
0323     XMLEntityHandler*           fEntityHandler;
0324     RefStackOf<XMLEntityDecl>*  fEntityStack;
0325     unsigned int                fNextReaderNum;
0326     RefStackOf<ReaderData>*     fReaderStack;
0327     bool                        fThrowEOE;
0328     XMLReader::XMLVersion       fXMLVersion;
0329     bool                        fStandardUriConformant;
0330     MemoryManager*              fMemoryManager;
0331 };
0332 
0333 
0334 
0335 // ---------------------------------------------------------------------------
0336 //  ReaderMgr: Inlined methods
0337 //
0338 //  NOTE: We cannot put these in alphabetical and type order as we usually
0339 //  do because some of the compilers we have to support are too stupid to
0340 //  understand out of order inlines!
0341 // ---------------------------------------------------------------------------
0342 inline XMLSize_t ReaderMgr::getCurrentReaderNum() const
0343 {
0344     return fCurReader->getReaderNum();
0345 }
0346 
0347 inline const XMLReader* ReaderMgr::getCurrentReader() const
0348 {
0349     return fCurReader;
0350 }
0351 
0352 inline XMLReader* ReaderMgr::getCurrentReader()
0353 {
0354     return fCurReader;
0355 }
0356 
0357 inline bool ReaderMgr::getName(XMLBuffer& toFill)
0358 {
0359     toFill.reset();
0360     return fCurReader->getName(toFill, false);
0361 }
0362 
0363 inline bool ReaderMgr::getQName(XMLBuffer& toFill, int *colonPosition)
0364 {
0365     toFill.reset();
0366     return fCurReader->getQName(toFill, colonPosition);
0367 }
0368 
0369 inline bool ReaderMgr::getNameToken(XMLBuffer& toFill)
0370 {
0371     toFill.reset();
0372     return fCurReader->getName(toFill, true);
0373 }
0374 
0375 inline bool ReaderMgr::getNextCharIfNot(const XMLCh chNotToGet, XMLCh& chGotten)
0376 {
0377     return fCurReader->getNextCharIfNot(chNotToGet, chGotten);
0378 }
0379 
0380 inline void ReaderMgr::movePlainContentChars(XMLBuffer &dest)
0381 {
0382     fCurReader->movePlainContentChars(dest);
0383 }
0384 
0385 inline bool ReaderMgr::getThrowEOE() const
0386 {
0387     return fThrowEOE;
0388 }
0389 
0390 inline XMLFilePos ReaderMgr::getSrcOffset() const
0391 {
0392     return fCurReader? fCurReader->getSrcOffset() : 0;
0393 }
0394 
0395 inline bool ReaderMgr::lookingAtChar(const XMLCh chToCheck)
0396 {
0397     return (chToCheck == peekNextChar());
0398 }
0399 
0400 inline bool ReaderMgr::lookingAtSpace()
0401 {
0402     XMLCh c = peekNextChar();
0403     return fCurReader->isWhitespace(c);
0404 }
0405 
0406 inline void ReaderMgr::setThrowEOE(const bool newValue)
0407 {
0408     fThrowEOE = newValue;
0409 }
0410 
0411 inline void ReaderMgr::setStandardUriConformant(const bool newValue)
0412 {
0413     fStandardUriConformant = newValue;
0414 }
0415 
0416 inline bool ReaderMgr::skippedString(const XMLCh* const toSkip)
0417 {
0418     return fCurReader->skippedString(toSkip);
0419 }
0420 
0421 inline bool ReaderMgr::skippedStringLong(const XMLCh* const toSkip)
0422 {
0423     return fCurReader->skippedStringLong(toSkip);
0424 }
0425 
0426 inline void ReaderMgr::skipToChar(const XMLCh toSkipTo)
0427 {
0428     XMLCh nextCh = 0;
0429     do
0430     {
0431         // Get chars until we find the one to skip
0432         nextCh = getNextChar();
0433     }
0434     // Break out at end of input or the char to skip
0435     while((nextCh != toSkipTo) && nextCh!=0);
0436 }
0437 
0438 inline void ReaderMgr::skipPastChar(const XMLCh toSkipPast)
0439 {
0440     XMLCh nextCh = 0;
0441     do
0442     {
0443         // Get chars until we find the one to skip
0444         nextCh = getNextChar();
0445     }
0446     while((nextCh != toSkipPast) && nextCh!=0);
0447 }
0448 
0449 inline bool ReaderMgr::peekString(const XMLCh* const toPeek)
0450 {
0451     return fCurReader->peekString(toPeek);
0452 }
0453 
0454 inline void ReaderMgr::setEntityHandler(XMLEntityHandler* const newHandler)
0455 {
0456     fEntityHandler = newHandler;
0457 }
0458 
0459 inline void ReaderMgr::setXMLVersion(const XMLReader::XMLVersion version)
0460 {
0461     fXMLVersion = version;
0462     fCurReader->setXMLVersion(version);
0463 }
0464 
0465 //
0466 //  This is a simple class to temporarily change the 'throw at end of entity'
0467 //  flag of the reader manager. There are some places where we need to
0468 //  turn this on and off on a scoped basis.
0469 //
0470 class XMLPARSER_EXPORT ThrowEOEJanitor
0471 {
0472 public :
0473     // -----------------------------------------------------------------------
0474     //  Constructors and destructor
0475     // -----------------------------------------------------------------------
0476     ThrowEOEJanitor(ReaderMgr* mgrTarget, const bool newValue) :
0477 
0478         fOld(mgrTarget->getThrowEOE())
0479         , fMgr(mgrTarget)
0480     {
0481         mgrTarget->setThrowEOE(newValue);
0482     }
0483 
0484     ~ThrowEOEJanitor()
0485     {
0486         fMgr->setThrowEOE(fOld);
0487     };
0488 
0489 private :
0490     // -----------------------------------------------------------------------
0491     //  Unimplemented constructors and operators
0492     // -----------------------------------------------------------------------
0493     ThrowEOEJanitor(const ThrowEOEJanitor&);
0494     ThrowEOEJanitor& operator=(const ThrowEOEJanitor&);
0495 
0496     // -----------------------------------------------------------------------
0497     //  Private data members
0498     //
0499     //  fOld
0500     //      The previous value of the flag, which we replaced during ctor,
0501     //      and will replace during dtor.
0502     //
0503     //  fMgr
0504     //      A pointer to the reader manager we are going to set/reset the
0505     //      flag on.
0506     // -----------------------------------------------------------------------
0507     bool        fOld;
0508     ReaderMgr*  fMgr;
0509 };
0510 
0511 XERCES_CPP_NAMESPACE_END
0512 
0513 #endif