- Main Page
- Related Pages
- Classes
- Files

# XMLFormatter.hpp

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: XMLFormatter.hpp 932887 2010-04-11 13:04:59Z borisk $
    00020  */
    00021
    00022 #if !defined(XERCESC_INCLUDE_GUARD_XMLFORMATTER_HPP)
    00023 #define XERCESC_INCLUDE_GUARD_XMLFORMATTER_HPP
    00024
    00025 #include <xercesc/util/PlatformUtils.hpp>
    00026
    00027 XERCES_CPP_NAMESPACE_BEGIN
    00028
    00029 class XMLFormatTarget;
    00030 class XMLTranscoder;
    00031
    00041 class XMLPARSER_EXPORT XMLFormatter : public XMemory
    00042 {
    00043 public:
    00044     // -----------------------------------------------------------------------
    00045     //  Class types
    00046     // -----------------------------------------------------------------------
    00124     enum EscapeFlags
    00125     {
    00126         NoEscapes
    00127         , StdEscapes
    00128         , AttrEscapes
    00129         , CharEscapes
    00130
    00131         // Special values, don't use directly
    00132         , EscapeFlags_Count
    00133         , DefaultEscape     = 999
    00134     };
    00135
    00152     enum UnRepFlags
    00153     {
    00154         UnRep_Fail
    00155         , UnRep_CharRef
    00156         , UnRep_Replace
    00157
    00158         , DefaultUnRep      = 999
    00159     };
    00161
    00162
    00163     // -----------------------------------------------------------------------
    00164     //  Constructors and Destructor
    00165     // -----------------------------------------------------------------------
    00177     XMLFormatter
    00178     (
    00179         const   XMLCh* const            outEncoding
    00180         , const XMLCh* const            docVersion
    00181         ,       XMLFormatTarget* const  target
    00182         , const EscapeFlags             escapeFlags = NoEscapes
    00183         , const UnRepFlags              unrepFlags = UnRep_Fail
    00184         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00185     );
    00186
    00187     XMLFormatter
    00188     (
    00189         const   char* const             outEncoding
    00190         , const char* const             docVersion
    00191         ,       XMLFormatTarget* const  target
    00192         , const EscapeFlags             escapeFlags = NoEscapes
    00193         , const UnRepFlags              unrepFlags = UnRep_Fail
    00194         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00195     );
    00196
    00197     XMLFormatter
    00198     (
    00199         const   XMLCh* const            outEncoding
    00200         ,       XMLFormatTarget* const  target
    00201         , const EscapeFlags             escapeFlags = NoEscapes
    00202         , const UnRepFlags              unrepFlags = UnRep_Fail
    00203         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00204     );
    00205
    00206     XMLFormatter
    00207     (
    00208         const   char* const             outEncoding
    00209         ,       XMLFormatTarget* const  target
    00210         , const EscapeFlags             escapeFlags = NoEscapes
    00211         , const UnRepFlags              unrepFlags = UnRep_Fail
    00212         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00213     );
    00214
    00215     ~XMLFormatter();
    00217
    00218
    00219     // -----------------------------------------------------------------------
    00220     //  Formatting methods
    00221     // -----------------------------------------------------------------------
    00231     void formatBuf
    00232     (
    00233         const   XMLCh* const    toFormat
    00234         , const XMLSize_t       count
    00235         , const EscapeFlags     escapeFlags = DefaultEscape
    00236         , const UnRepFlags      unrepFlags = DefaultUnRep
    00237     );
    00238
    00242     XMLFormatter& operator<<
    00243     (
    00244         const   XMLCh* const    toFormat
    00245     );
    00246
    00247     XMLFormatter& operator<<
    00248     (
    00249         const   XMLCh           toFormat
    00250     );
    00251
    00252     void writeBOM(const XMLByte* const toFormat
    00253                 , const XMLSize_t      count);
    00254
    00256
    00257     // -----------------------------------------------------------------------
    00258     //  Getter methods
    00259     // -----------------------------------------------------------------------
    00266     const XMLCh* getEncodingName() const;
    00267
    00271     inline const XMLTranscoder*   getTranscoder() const;
    00272
    00276     inline XMLTranscoder*   getTranscoder();
    00277
    00279
    00280     // -----------------------------------------------------------------------
    00281     //  Setter methods
    00282     // -----------------------------------------------------------------------
    00288     void setEscapeFlags
    00289     (
    00290         const   EscapeFlags     newFlags
    00291     );
    00292
    00296     void setUnRepFlags
    00297     (
    00298         const   UnRepFlags      newFlags
    00299     );
    00300
    00305     XMLFormatter& operator<<
    00306     (
    00307         const   EscapeFlags     newFlags
    00308     );
    00309
    00314     XMLFormatter& operator<<
    00315     (
    00316         const   UnRepFlags      newFlags
    00317     );
    00319
    00320     // -----------------------------------------------------------------------
    00321     //  Getter methods
    00322     // -----------------------------------------------------------------------
    00328     EscapeFlags getEscapeFlags() const;
    00329
    00333     UnRepFlags getUnRepFlags() const;
    00335
    00336 private :
    00337     // -----------------------------------------------------------------------
    00338     //  Unimplemented constructors and operators
    00339     // -----------------------------------------------------------------------
    00340     XMLFormatter();
    00341     XMLFormatter(const XMLFormatter&);
    00342     XMLFormatter& operator=(const XMLFormatter&);
    00343
    00344
    00345     // -----------------------------------------------------------------------
    00346     //  Private class constants
    00347     // -----------------------------------------------------------------------
    00348     enum Constants
    00349     {
    00350         kTmpBufSize     = 16 * 1024
    00351     };
    00352
    00353
    00354     // -----------------------------------------------------------------------
    00355     //  Private helper methods
    00356     // -----------------------------------------------------------------------
    00357     const XMLByte* getCharRef(XMLSize_t     &count,
    00358                               XMLByte*      &ref,
    00359                               const XMLCh *  stdRef);
    00360
    00361     void writeCharRef(const XMLCh &toWrite);
    00362     void writeCharRef(XMLSize_t toWrite);
    00363
    00364     bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
    00365                     , const XMLCh                     toCheck);
    00366
    00367
    00368     XMLSize_t handleUnEscapedChars(const XMLCh *      srcPtr,
    00369                                    const XMLSize_t    count,
    00370                                    const UnRepFlags   unrepFlags);
    00371
    00372     void specialFormat
    00373     (
    00374         const   XMLCh* const    toFormat
    00375         , const XMLSize_t       count
    00376         , const EscapeFlags     escapeFlags
    00377     );
    00378
    00379
    00380     // -----------------------------------------------------------------------
    00381     //  Private, non-virtual methods
    00382     //
    00383     //  fEscapeFlags
    00384     //      The escape flags we were told to use in formatting. These are
    00385     //      defaults set in the ctor, which can be overridden on a particular
    00386     //      call.
    00387     //
    00388     //  fOutEncoding
    00389     //      This the name of the output encoding. Saved mainly for meaningful
    00390     //      error messages.
    00391     //
    00392     //  fTarget
    00393     //      This is the target object for the formatting operation.
    00394     //
    00395     //  fUnRepFlags
    00396     //      The unrepresentable flags that indicate how to react when a
    00397     //      character cannot be represented in the target encoding.
    00398     //
    00399     //  fXCoder
    00400     //      This the transcoder that we will use. It is created using the
    00401     //      encoding name we were told to use.
    00402     //
    00403     //  fTmpBuf
    00404     //      An output buffer that we use to transcode chars into before we
    00405     //      send them off to be output.
    00406     //
    00407     //  fAposRef
    00408     //  fAmpRef
    00409     //  fGTRef
    00410     //  fLTRef
    00411     //  fQuoteRef
    00412     //      These are character refs for the standard char refs, in the
    00413     //      output encoding. They are faulted in as required, by transcoding
    00414     //      them from fixed Unicode versions.
    00415     //
    00416     //  fIsXML11
    00417     //      for performance reason, we do not store the actual version string
    00418     //      and do the string comparison again and again.
    00419     //
    00420     // -----------------------------------------------------------------------
    00421     EscapeFlags                 fEscapeFlags;
    00422     XMLCh*                      fOutEncoding;
    00423     XMLFormatTarget*            fTarget;
    00424     UnRepFlags                  fUnRepFlags;
    00425     XMLTranscoder*              fXCoder;
    00426     XMLByte                     fTmpBuf[kTmpBufSize + 4];
    00427     XMLByte*                    fAposRef;
    00428     XMLSize_t                   fAposLen;
    00429     XMLByte*                    fAmpRef;
    00430     XMLSize_t                   fAmpLen;
    00431     XMLByte*                    fGTRef;
    00432     XMLSize_t                   fGTLen;
    00433     XMLByte*                    fLTRef;
    00434     XMLSize_t                   fLTLen;
    00435     XMLByte*                    fQuoteRef;
    00436     XMLSize_t                   fQuoteLen;
    00437     bool                        fIsXML11;
    00438     MemoryManager*              fMemoryManager;
    00439 };
    00440
    00441
    00442 class XMLPARSER_EXPORT XMLFormatTarget : public XMemory
    00443 {
    00444 public:
    00445     // -----------------------------------------------------------------------
    00446     //  Constructors and Destructor
    00447     // -----------------------------------------------------------------------
    00448     virtual ~XMLFormatTarget() {}
    00449
    00450
    00451     // -----------------------------------------------------------------------
    00452     //  Virtual interface
    00453     // -----------------------------------------------------------------------
    00454     virtual void writeChars
    00455     (
    00456           const XMLByte* const      toWrite
    00457         , const XMLSize_t           count
    00458         ,       XMLFormatter* const formatter
    00459     ) = 0;
    00460
    00461     virtual void flush() {};
    00462
    00463
    00464 protected :
    00465     // -----------------------------------------------------------------------
    00466     //  Hidden constructors and operators
    00467     // -----------------------------------------------------------------------
    00468     XMLFormatTarget() {};
    00469
    00470 private:
    00471     // -----------------------------------------------------------------------
    00472     //  Unimplemented constructors and operators
    00473     // -----------------------------------------------------------------------
    00474     XMLFormatTarget(const XMLFormatTarget&);
    00475     XMLFormatTarget& operator=(const XMLFormatTarget&);
    00476 };
    00477
    00478
    00479 // ---------------------------------------------------------------------------
    00480 //  XMLFormatter: Getter methods
    00481 // ---------------------------------------------------------------------------
    00482 inline const XMLCh* XMLFormatter::getEncodingName() const
    00483 {
    00484     return fOutEncoding;
    00485 }
    00486
    00487 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
    00488 {
    00489     return fXCoder;
    00490 }
    00491
    00492 inline XMLTranscoder* XMLFormatter::getTranscoder()
    00493 {
    00494     return fXCoder;
    00495 }
    00496
    00497 // ---------------------------------------------------------------------------
    00498 //  XMLFormatter: Setter methods
    00499 // ---------------------------------------------------------------------------
    00500 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
    00501 {
    00502     fEscapeFlags = newFlags;
    00503 }
    00504
    00505 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
    00506 {
    00507     fUnRepFlags = newFlags;
    00508 }
    00509
    00510
    00511 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
    00512 {
    00513     fEscapeFlags = newFlags;
    00514     return *this;
    00515 }
    00516
    00517 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
    00518 {
    00519     fUnRepFlags = newFlags;
    00520     return *this;
    00521 }
    00522
    00523 // ---------------------------------------------------------------------------
    00524 //  XMLFormatter: Getter methods
    00525 // ---------------------------------------------------------------------------
    00526 inline XMLFormatter::EscapeFlags XMLFormatter::getEscapeFlags() const
    00527 {
    00528     return fEscapeFlags;
    00529 }
    00530
    00531 inline XMLFormatter::UnRepFlags XMLFormatter::getUnRepFlags() const
    00532 {
    00533     return fUnRepFlags;
    00534 }
    00535
    00536 XERCES_CPP_NAMESPACE_END
    00537
    00538 #endif