| API Docs for SAX and DOM |

Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members

# XMLFormatter.hpp

Go to the documentation of this file.

    00001 /*
    00002  * The Apache Software License, Version 1.1
    00003  *
    00004  * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
    00005  * reserved.
    00006  *
    00007  * Redistribution and use in source and binary forms, with or without
    00008  * modification, are permitted provided that the following conditions
    00009  * are met:
    00010  *
    00011  * 1. Redistributions of source code must retain the above copyright
    00012  *    notice, this list of conditions and the following disclaimer.
    00013  *
    00014  * 2. Redistributions in binary form must reproduce the above copyright
    00015  *    notice, this list of conditions and the following disclaimer in
    00016  *    the documentation and/or other materials provided with the
    00017  *    distribution.
    00018  *
    00019  * 3. The end-user documentation included with the redistribution,
    00020  *    if any, must include the following acknowledgment:
    00021  *       "This product includes software developed by the
    00022  *        Apache Software Foundation (http://www.apache.org/)."
    00023  *    Alternately, this acknowledgment may appear in the software itself,
    00024  *    if and wherever such third-party acknowledgments normally appear.
    00025  *
    00026  * 4. The names "Xerces" and "Apache Software Foundation" must
    00027  *    not be used to endorse or promote products derived from this
    00028  *    software without prior written permission. For written
    00029  *    permission, please contact apache\@apache.org.
    00030  *
    00031  * 5. Products derived from this software may not be called "Apache",
    00032  *    nor may "Apache" appear in their name, without prior written
    00033  *    permission of the Apache Software Foundation.
    00034  *
    00035  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    00037  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    00038  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    00039  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00040  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00041  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    00042  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00043  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    00044  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    00045  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    00046  * SUCH DAMAGE.
    00047  * ====================================================================
    00048  *
    00049  * This software consists of voluntary contributions made by many
    00050  * individuals on behalf of the Apache Software Foundation, and was
    00051  * originally based on software copyright (c) 1999, International
    00052  * Business Machines, Inc., http://www.ibm.com .  For more information
    00053  * on the Apache Software Foundation, please see
    00054  * <http://www.apache.org/>.
    00055  */
    00056
    00057 /*
    00058  * $Log: XMLFormatter.hpp,v $
    00059  * Revision 1.14  2003/05/16 21:36:55  knoaman
    00060  * Memory manager implementation: Modify constructors to pass in the memory manager.
    00061  *
    00062  * Revision 1.13  2003/05/15 18:26:07  knoaman
    00063  * Partial implementation of the configurable memory manager.
    00064  *
    00065  * Revision 1.12  2003/03/17 03:19:52  peiyongz
    00066  * Bug#18051 memory leakage in XMLFormatter
    00067  *
    00068  * Revision 1.11  2003/03/16 06:00:43  peiyongz
    00069  * Bug#17983 Formatter does not escape control characters
    00070  *
    00071  * Revision 1.10  2003/03/11 12:58:36  tng
    00072  * Fix compilation error on AIX.
    00073  *
    00074  * Revision 1.9  2003/03/07 21:42:37  tng
    00075  * [Bug 17589] Refactoring ... .  Patch from Jacques Legare.
    00076  *
    00077  * Revision 1.8  2003/03/07 18:08:10  tng
    00078  * Return a reference instead of void for operator=
    00079  *
    00080  * Revision 1.7  2003/01/31 00:30:48  jberry
    00081  * Syntax error in declaration
    00082  *
    00083  * Revision 1.6  2003/01/28 18:32:33  peiyongz
    00084  * Bug#13694: Allow Xerces to write the BOM to XML files
    00085  *
    00086  * Revision 1.5  2003/01/24 20:20:22  tng
    00087  * Add method flush to XMLFormatTarget
    00088  *
    00089  * Revision 1.4  2002/11/04 15:00:21  tng
    00090  * C++ Namespace Support.
    00091  *
    00092  * Revision 1.3  2002/07/30 16:29:16  tng
    00093  * [Bug 8550] No explanation of XMLFormatter escape options.
    00094  *
    00095  * Revision 1.2  2002/06/21 19:31:23  peiyongz
    00096  * getTranscoder() added;
    00097  *
    00098  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
    00099  * sane_include
    00100  *
    00101  * Revision 1.7  2000/10/17 19:25:38  andyh
    00102  * XMLFormatTarget, removed version of writeChars with no length.  Can not be
    00103  * safely used, and obscured other errors.
    00104  *
    00105  * Revision 1.6  2000/10/10 23:54:58  andyh
    00106  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
    00107  * output to multi-byte encodings.
    00108  *
    00109  * Revision 1.5  2000/04/07 01:01:56  roddey
    00110  * Fixed an error message so that it indicated the correct radix for the rep
    00111  * token. Get all of the basic output formatting functionality in place for
    00112  * at least ICU and Win32 transcoders.
    00113  *
    00114  * Revision 1.4  2000/04/06 23:50:38  roddey
    00115  * Now the low level formatter handles doing char refs for
    00116  * unrepresentable chars (in addition to the replacement char style
    00117  * already done.)
    00118  *
    00119  * Revision 1.3  2000/04/06 19:09:21  roddey
    00120  * Some more improvements to output formatting. Now it will correctly
    00121  * handle doing the 'replacement char' style of dealing with chars
    00122  * that are unrepresentable.
    00123  *
    00124  * Revision 1.2  2000/04/05 00:20:16  roddey
    00125  * More updates for the low level formatted output support
    00126  *
    00127  * Revision 1.1  2000/03/28 19:43:17  roddey
    00128  * Fixes for signed/unsigned warnings. New work for two way transcoding
    00129  * stuff.
    00130  *
    00131  */
    00132
    00133 #if !defined(XMLFORMATTER_HPP)
    00134 #define XMLFORMATTER_HPP
    00135
    00136 #include <xercesc/util/PlatformUtils.hpp>
    00137
    00138 XERCES_CPP_NAMESPACE_BEGIN
    00139
    00140 class XMLFormatTarget;
    00141 class XMLTranscoder;
    00142
    00152 class  XMLFormatter : public XMemory
    00153 {
    00154 public:
    00155     // -----------------------------------------------------------------------
    00156     //  Class types
    00157     // -----------------------------------------------------------------------
    00235     enum EscapeFlags
    00236     {
    00237         NoEscapes
    00238         , StdEscapes
    00239         , AttrEscapes
    00240         , CharEscapes
    00241
    00242         // Special values, don't use directly
    00243         , EscapeFlags_Count
    00244         , DefaultEscape     = 999
    00245     };
    00246
    00263     enum UnRepFlags
    00264     {
    00265         UnRep_Fail
    00266         , UnRep_CharRef
    00267         , UnRep_Replace
    00268
    00269         , DefaultUnRep      = 999
    00270     };
    00272
    00273
    00274     // -----------------------------------------------------------------------
    00275     //  Constructors and Destructor
    00276     // -----------------------------------------------------------------------
    00285     XMLFormatter
    00286     (
    00287         const   XMLCh* const            outEncoding
    00288         , const XMLCh* const            docVersion
    00289         ,       XMLFormatTarget* const  target
    00290         , const EscapeFlags             escapeFlags = NoEscapes
    00291         , const UnRepFlags              unrepFlags = UnRep_Fail
    00292         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00293     );
    00294
    00295     XMLFormatter
    00296     (
    00297         const   char* const             outEncoding
    00298         , const char* const             docVersion
    00299         ,       XMLFormatTarget* const  target
    00300         , const EscapeFlags             escapeFlags = NoEscapes
    00301         , const UnRepFlags              unrepFlags = UnRep_Fail
    00302         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
    00303     );
    00304
    00305     ~XMLFormatter();
    00307
    00308
    00309     // -----------------------------------------------------------------------
    00310     //  Formatting methods
    00311     // -----------------------------------------------------------------------
    00321     void formatBuf
    00322     (
    00323         const   XMLCh* const    toFormat
    00324         , const unsigned int    count
    00325         , const EscapeFlags     escapeFlags = DefaultEscape
    00326         , const UnRepFlags      unrepFlags = DefaultUnRep
    00327     );
    00328
    00332     XMLFormatter& operator<<
    00333     (
    00334         const   XMLCh* const    toFormat
    00335     );
    00336
    00337     XMLFormatter& operator<<
    00338     (
    00339         const   XMLCh           toFormat
    00340     );
    00341
    00342     void writeBOM(const XMLByte* const toFormat
    00343                 , const unsigned int   count);
    00344
    00346
    00347     // -----------------------------------------------------------------------
    00348     //  Getter methods
    00349     // -----------------------------------------------------------------------
    00356     const XMLCh* getEncodingName() const;
    00357
    00361     inline const XMLTranscoder*   getTranscoder() const;
    00362
    00364
    00365     // -----------------------------------------------------------------------
    00366     //  Setter methods
    00367     // -----------------------------------------------------------------------
    00373     void setEscapeFlags
    00374     (
    00375         const   EscapeFlags     newFlags
    00376     );
    00377
    00381     void setUnRepFlags
    00382     (
    00383         const   UnRepFlags      newFlags
    00384     );
    00385
    00390     XMLFormatter& operator<<
    00391     (
    00392         const   EscapeFlags     newFlags
    00393     );
    00394
    00399     XMLFormatter& operator<<
    00400     (
    00401         const   UnRepFlags      newFlags
    00402     );
    00404
    00405
    00406 private :
    00407     // -----------------------------------------------------------------------
    00408     //  Unimplemented constructors and operators
    00409     // -----------------------------------------------------------------------
    00410     XMLFormatter();
    00411     XMLFormatter(const XMLFormatter&);
    00412     XMLFormatter& operator=(const XMLFormatter&);
    00413
    00414
    00415     // -----------------------------------------------------------------------
    00416     //  Private class constants
    00417     // -----------------------------------------------------------------------
    00418     enum Constants
    00419     {
    00420         kTmpBufSize     = 16 * 1024
    00421     };
    00422
    00423
    00424     // -----------------------------------------------------------------------
    00425     //  Private helper methods
    00426     // -----------------------------------------------------------------------
    00427     const XMLByte* getCharRef(unsigned int & count,
    00428                               XMLByte*      &ref,
    00429                               const XMLCh *  stdRef);
    00430
    00431     const void writeCharRef(const XMLCh &toWrite);
    00432
    00433     bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
    00434                     , const XMLCh                     toCheck);
    00435
    00436
    00437     unsigned int handleUnEscapedChars(const XMLCh *                  srcPtr,
    00438                                       const unsigned int             count,
    00439                                       const UnRepFlags               unrepFlags);
    00440
    00441     void specialFormat
    00442     (
    00443         const   XMLCh* const    toFormat
    00444         , const unsigned int    count
    00445         , const EscapeFlags     escapeFlags
    00446     );
    00447
    00448
    00449     // -----------------------------------------------------------------------
    00450     //  Private, non-virtual methods
    00451     //
    00452     //  fEscapeFlags
    00453     //      The escape flags we were told to use in formatting. These are
    00454     //      defaults set in the ctor, which can be overridden on a particular
    00455     //      call.
    00456     //
    00457     //  fOutEncoding
    00458     //      This the name of the output encoding. Saved mainly for meaningful
    00459     //      error messages.
    00460     //
    00461     //  fTarget
    00462     //      This is the target object for the formatting operation.
    00463     //
    00464     //  fUnRepFlags
    00465     //      The unrepresentable flags that indicate how to react when a
    00466     //      character cannot be represented in the target encoding.
    00467     //
    00468     //  fXCoder
    00469     //      This the transcoder that we will use. It is created using the
    00470     //      encoding name we were told to use.
    00471     //
    00472     //  fTmpBuf
    00473     //      An output buffer that we use to transcode chars into before we
    00474     //      send them off to be output.
    00475     //
    00476     //  fAposRef
    00477     //  fAmpRef
    00478     //  fGTRef
    00479     //  fLTRef
    00480     //  fQuoteRef
    00481     //      These are character refs for the standard char refs, in the
    00482     //      output encoding. They are faulted in as required, by transcoding
    00483     //      them from fixed Unicode versions.
    00484     //
    00485     //  fIsXML11
    00486     //      for performance reason, we do not store the actual version string
    00487     //      and do the string comparison again and again.
    00488     //
    00489     // -----------------------------------------------------------------------
    00490     EscapeFlags                 fEscapeFlags;
    00491     XMLCh*                      fOutEncoding;
    00492     XMLFormatTarget*            fTarget;
    00493     UnRepFlags                  fUnRepFlags;
    00494     XMLTranscoder*              fXCoder;
    00495     XMLByte                     fTmpBuf[kTmpBufSize + 4];
    00496     XMLByte*                    fAposRef;
    00497     unsigned int                fAposLen;
    00498     XMLByte*                    fAmpRef;
    00499     unsigned int                fAmpLen;
    00500     XMLByte*                    fGTRef;
    00501     unsigned int                fGTLen;
    00502     XMLByte*                    fLTRef;
    00503     unsigned int                fLTLen;
    00504     XMLByte*                    fQuoteRef;
    00505     unsigned int                fQuoteLen;
    00506     bool                        fIsXML11;
    00507     MemoryManager*              fMemoryManager;
    00508 };
    00509
    00510
    00511 class  XMLFormatTarget : public XMemory
    00512 {
    00513 public:
    00514     // -----------------------------------------------------------------------
    00515     //  Constructors and Destructor
    00516     // -----------------------------------------------------------------------
    00517     virtual ~XMLFormatTarget() {}
    00518
    00519
    00520     // -----------------------------------------------------------------------
    00521     //  Virtual interface
    00522     // -----------------------------------------------------------------------
    00523     virtual void writeChars
    00524     (
    00525           const XMLByte* const      toWrite
    00526         , const unsigned int        count
    00527         ,       XMLFormatter* const formatter
    00528     ) = 0;
    00529
    00530     virtual void flush() {};
    00531
    00532
    00533 protected :
    00534     // -----------------------------------------------------------------------
    00535     //  Hidden constructors and operators
    00536     // -----------------------------------------------------------------------
    00537     XMLFormatTarget() {}
    00538     XMLFormatTarget(const XMLFormatTarget&) {}
    00539     void operator=(const XMLFormatTarget&) {}
    00540 };
    00541
    00542
    00543 // ---------------------------------------------------------------------------
    00544 //  XMLFormatter: Getter methods
    00545 // ---------------------------------------------------------------------------
    00546 inline const XMLCh* XMLFormatter::getEncodingName() const
    00547 {
    00548     return fOutEncoding;
    00549 }
    00550
    00551 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
    00552 {
    00553     return fXCoder;
    00554 }
    00555
    00556 // ---------------------------------------------------------------------------
    00557 //  XMLFormatter: Setter methods
    00558 // ---------------------------------------------------------------------------
    00559 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
    00560 {
    00561     fEscapeFlags = newFlags;
    00562 }
    00563
    00564 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
    00565 {
    00566     fUnRepFlags = newFlags;
    00567 }
    00568
    00569
    00570 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
    00571 {
    00572     fEscapeFlags = newFlags;
    00573     return *this;
    00574 }
    00575
    00576 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
    00577 {
    00578     fUnRepFlags = newFlags;
    00579     return *this;
    00580 }
    00581
    00582 XERCES_CPP_NAMESPACE_END
    00583
    00584 #endif

Copyright © 2000 The Apache Software Foundation. All Rights Reserved.