Xerces-C++: XMLFormatter.hpp Source File

| API Docs for SAX and DOM |
| Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages # XMLFormatter.hpp Go to the documentation of this file.
    00001 /*
    00002  * Copyright 1999-2000,2004 The Apache Software Foundation.
    00003  *
    00004  * Licensed under the Apache License, Version 2.0 (the "License");
    00005  * you may not use this file except in compliance with the License.
    00006  * You may obtain a copy of the License at
    00007  *
    00008  *      http://www.apache.org/licenses/LICENSE-2.0
    00009  *
    00010  * Unless required by applicable law or agreed to in writing, software
    00011  * distributed under the License is distributed on an "AS IS" BASIS,
    00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    00013  * See the License for the specific language governing permissions and
    00014  * limitations under the License.
    00015  */
    00016
    00017 /*
    00018  * $Id: XMLFormatter.hpp 202337 2005-06-29 08:35:12Z amassari $
    00019  */
    00020
    00021 #if !defined(XMLFORMATTER_HPP)
    00022 #define XMLFORMATTER_HPP
    00023
    00024 #include <xercesc/util/PlatformUtils.hpp>
    00025
    00026 XERCES_CPP_NAMESPACE_BEGIN
    00027
    00028 class XMLFormatTarget;
    00029 class XMLTranscoder;
    00030
    00040 class  XMLFormatter : public XMemory
    00041 {
    00042 public:
    00043     // -----------------------------------------------------------------------
    00044     //  Class types
    00045     // -----------------------------------------------------------------------
    00123     enum EscapeFlags
    00124     {
    00125         NoEscapes
    00126         , StdEscapes
    00127         , AttrEscapes
    00128         , CharEscapes
    00129
    00130         // Special values, don't use directly
    00131         , EscapeFlags_Count
    00132         , DefaultEscape     = 999
    00133     };
    00134
    00151     enum UnRepFlags
    00152     {
    00153         UnRep_Fail
    00154         , UnRep_CharRef
    00155         , UnRep_Replace
    00156
    00157         , DefaultUnRep      = 999
    00158     };
    00160
    00161
    00162     // -----------------------------------------------------------------------
    00163     //  Constructors and Destructor
    00164     // -----------------------------------------------------------------------
    00176     XMLFormatter
    00177     (
    00178         const   XMLCh* const            outEncoding
    00179         , const XMLCh* const            docVersion
    00180         ,       XMLFormatTarget* const  target
    00181         , const EscapeFlags             escapeFlags = NoEscapes
    00182         , const UnRepFlags              unrepFlags = UnRep_Fail
    00183         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00184     );
    00185
    00186     XMLFormatter
    00187     (
    00188         const   char* const             outEncoding
    00189         , const char* const             docVersion
    00190         ,       XMLFormatTarget* const  target
    00191         , const EscapeFlags             escapeFlags = NoEscapes
    00192         , const UnRepFlags              unrepFlags = UnRep_Fail
    00193         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00194     );
    00195
    00196     XMLFormatter
    00197     (
    00198         const   XMLCh* const            outEncoding
    00199         ,       XMLFormatTarget* const  target
    00200         , const EscapeFlags             escapeFlags = NoEscapes
    00201         , const UnRepFlags              unrepFlags = UnRep_Fail
    00202         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00203     );
    00204
    00205     XMLFormatter
    00206     (
    00207         const   char* const             outEncoding
    00208         ,       XMLFormatTarget* const  target
    00209         , const EscapeFlags             escapeFlags = NoEscapes
    00210         , const UnRepFlags              unrepFlags = UnRep_Fail
    00211         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00212     );
    00213
    00214     ~XMLFormatter();
    00216
    00217
    00218     // -----------------------------------------------------------------------
    00219     //  Formatting methods
    00220     // -----------------------------------------------------------------------
    00230     void formatBuf
    00231     (
    00232         const   XMLCh* const    toFormat
    00233         , const unsigned int    count
    00234         , const EscapeFlags     escapeFlags = DefaultEscape
    00235         , const UnRepFlags      unrepFlags = DefaultUnRep
    00236     );
    00237
    00241     XMLFormatter& operator<<
    00242     (
    00243         const   XMLCh* const    toFormat
    00244     );
    00245
    00246     XMLFormatter& operator<<
    00247     (
    00248         const   XMLCh           toFormat
    00249     );
    00250
    00251     void writeBOM(const XMLByte* const toFormat
    00252                 , const unsigned int   count);
    00253
    00255
    00256     // -----------------------------------------------------------------------
    00257     //  Getter methods
    00258     // -----------------------------------------------------------------------
    00265     const XMLCh* getEncodingName() const;
    00266
    00270     inline const XMLTranscoder*   getTranscoder() const;
    00271
    00273
    00274     // -----------------------------------------------------------------------
    00275     //  Setter methods
    00276     // -----------------------------------------------------------------------
    00282     void setEscapeFlags
    00283     (
    00284         const   EscapeFlags     newFlags
    00285     );
    00286
    00290     void setUnRepFlags
    00291     (
    00292         const   UnRepFlags      newFlags
    00293     );
    00294
    00299     XMLFormatter& operator<<
    00300     (
    00301         const   EscapeFlags     newFlags
    00302     );
    00303
    00308     XMLFormatter& operator<<
    00309     (
    00310         const   UnRepFlags      newFlags
    00311     );
    00313
    00314
    00315     // -----------------------------------------------------------------------
    00316     //  Getter methods
    00317     // -----------------------------------------------------------------------
    00323     EscapeFlags getEscapeFlags() const;
    00324
    00328     UnRepFlags getUnRepFlags() const;
    00330
    00331 private :
    00332     // -----------------------------------------------------------------------
    00333     //  Unimplemented constructors and operators
    00334     // -----------------------------------------------------------------------
    00335     XMLFormatter();
    00336     XMLFormatter(const XMLFormatter&);
    00337     XMLFormatter& operator=(const XMLFormatter&);
    00338
    00339
    00340     // -----------------------------------------------------------------------
    00341     //  Private class constants
    00342     // -----------------------------------------------------------------------
    00343     enum Constants
    00344     {
    00345         kTmpBufSize     = 16 * 1024
    00346     };
    00347
    00348
    00349     // -----------------------------------------------------------------------
    00350     //  Private helper methods
    00351     // -----------------------------------------------------------------------
    00352     const XMLByte* getCharRef(unsigned int & count,
    00353                               XMLByte*      &ref,
    00354                               const XMLCh *  stdRef);
    00355
    00356     void writeCharRef(const XMLCh &toWrite);
    00357     void writeCharRef(unsigned long toWrite);
    00358
    00359     bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
    00360                     , const XMLCh                     toCheck);
    00361
    00362
    00363     unsigned int handleUnEscapedChars(const XMLCh *                  srcPtr,
    00364                                       const unsigned int             count,
    00365                                       const UnRepFlags               unrepFlags);
    00366
    00367     void specialFormat
    00368     (
    00369         const   XMLCh* const    toFormat
    00370         , const unsigned int    count
    00371         , const EscapeFlags     escapeFlags
    00372     );
    00373
    00374
    00375     // -----------------------------------------------------------------------
    00376     //  Private, non-virtual methods
    00377     //
    00378     //  fEscapeFlags
    00379     //      The escape flags we were told to use in formatting. These are
    00380     //      defaults set in the ctor, which can be overridden on a particular
    00381     //      call.
    00382     //
    00383     //  fOutEncoding
    00384     //      This the name of the output encoding. Saved mainly for meaningful
    00385     //      error messages.
    00386     //
    00387     //  fTarget
    00388     //      This is the target object for the formatting operation.
    00389     //
    00390     //  fUnRepFlags
    00391     //      The unrepresentable flags that indicate how to react when a
    00392     //      character cannot be represented in the target encoding.
    00393     //
    00394     //  fXCoder
    00395     //      This the transcoder that we will use. It is created using the
    00396     //      encoding name we were told to use.
    00397     //
    00398     //  fTmpBuf
    00399     //      An output buffer that we use to transcode chars into before we
    00400     //      send them off to be output.
    00401     //
    00402     //  fAposRef
    00403     //  fAmpRef
    00404     //  fGTRef
    00405     //  fLTRef
    00406     //  fQuoteRef
    00407     //      These are character refs for the standard char refs, in the
    00408     //      output encoding. They are faulted in as required, by transcoding
    00409     //      them from fixed Unicode versions.
    00410     //
    00411     //  fIsXML11
    00412     //      for performance reason, we do not store the actual version string
    00413     //      and do the string comparison again and again.
    00414     //
    00415     // -----------------------------------------------------------------------
    00416     EscapeFlags                 fEscapeFlags;
    00417     XMLCh*                      fOutEncoding;
    00418     XMLFormatTarget*            fTarget;
    00419     UnRepFlags                  fUnRepFlags;
    00420     XMLTranscoder*              fXCoder;
    00421     XMLByte                     fTmpBuf[kTmpBufSize + 4];
    00422     XMLByte*                    fAposRef;
    00423     unsigned int                fAposLen;
    00424     XMLByte*                    fAmpRef;
    00425     unsigned int                fAmpLen;
    00426     XMLByte*                    fGTRef;
    00427     unsigned int                fGTLen;
    00428     XMLByte*                    fLTRef;
    00429     unsigned int                fLTLen;
    00430     XMLByte*                    fQuoteRef;
    00431     unsigned int                fQuoteLen;
    00432     bool                        fIsXML11;
    00433     MemoryManager*              fMemoryManager;
    00434 };
    00435
    00436
    00437 class  XMLFormatTarget : public XMemory
    00438 {
    00439 public:
    00440     // -----------------------------------------------------------------------
    00441     //  Constructors and Destructor
    00442     // -----------------------------------------------------------------------
    00443     virtual ~XMLFormatTarget() {}
    00444
    00445
    00446     // -----------------------------------------------------------------------
    00447     //  Virtual interface
    00448     // -----------------------------------------------------------------------
    00449     virtual void writeChars
    00450     (
    00451           const XMLByte* const      toWrite
    00452         , const unsigned int        count
    00453         ,       XMLFormatter* const formatter
    00454     ) = 0;
    00455
    00456     virtual void flush() {};
    00457
    00458
    00459 protected :
    00460     // -----------------------------------------------------------------------
    00461     //  Hidden constructors and operators
    00462     // -----------------------------------------------------------------------
    00463     XMLFormatTarget() {};
    00464
    00465 private:
    00466     // -----------------------------------------------------------------------
    00467     //  Unimplemented constructors and operators
    00468     // -----------------------------------------------------------------------
    00469     XMLFormatTarget(const XMLFormatTarget&);
    00470     XMLFormatTarget& operator=(const XMLFormatTarget&);
    00471 };
    00472
    00473
    00474 // ---------------------------------------------------------------------------
    00475 //  XMLFormatter: Getter methods
    00476 // ---------------------------------------------------------------------------
    00477 inline const XMLCh* XMLFormatter::getEncodingName() const
    00478 {
    00479     return fOutEncoding;
    00480 }
    00481
    00482 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
    00483 {
    00484     return fXCoder;
    00485 }
    00486
    00487 // ---------------------------------------------------------------------------
    00488 //  XMLFormatter: Setter methods
    00489 // ---------------------------------------------------------------------------
    00490 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
    00491 {
    00492     fEscapeFlags = newFlags;
    00493 }
    00494
    00495 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
    00496 {
    00497     fUnRepFlags = newFlags;
    00498 }
    00499
    00500
    00501 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
    00502 {
    00503     fEscapeFlags = newFlags;
    00504     return *this;
    00505 }
    00506
    00507 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
    00508 {
    00509     fUnRepFlags = newFlags;
    00510     return *this;
    00511 }
    00512
    00513 // ---------------------------------------------------------------------------
    00514 //  XMLFormatter: Getter methods
    00515 // ---------------------------------------------------------------------------
    00516 inline XMLFormatter::EscapeFlags XMLFormatter::getEscapeFlags() const
    00517 {
    00518     return fEscapeFlags;
    00519 }
    00520
    00521 inline XMLFormatter::UnRepFlags XMLFormatter::getUnRepFlags() const
    00522 {
    00523     return fUnRepFlags;
    00524 }
    00525
    00526 XERCES_CPP_NAMESPACE_END
    00527
    00528 #endif

Copyright © 1994-2004 The Apache Software Foundation. All Rights Reserved.