http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Release Info

Installation
Download
Build

FAQs
Samples
API Docs

DOM C++ Binding
Programming
Migration Guide

Feedback
Bug-Reporting
PDF Document

CVS Repository
Mail Archive

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.20  2004/01/29 11:46:29  cargilld
00060  * Code cleanup changes to get rid of various compiler diagnostic messages.
00061  *
00062  * Revision 1.19  2003/12/01 23:23:25  neilg
00063  * fix for bug 25118; thanks to Jeroen Witmond
00064  *
00065  * Revision 1.18  2003/10/10 02:06:09  neilg
00066  * fix for bug 21780; thanks to David Cargill
00067  *
00068  * Revision 1.17  2003/09/22 08:50:04  gareth
00069  * doc fix
00070  *
00071  * Revision 1.16  2003/09/08 21:48:36  peiyongz
00072  * Restore pre2.3 constructors
00073  *
00074  * Revision 1.15  2003/05/30 16:11:43  gareth
00075  * Fixes so we compile under VC7.1. Patch by Alberto Massari.
00076  *
00077  * Revision 1.14  2003/05/16 21:36:55  knoaman
00078  * Memory manager implementation: Modify constructors to pass in the memory manager.
00079  *
00080  * Revision 1.13  2003/05/15 18:26:07  knoaman
00081  * Partial implementation of the configurable memory manager.
00082  *
00083  * Revision 1.12  2003/03/17 03:19:52  peiyongz
00084  * Bug#18051 memory leakage in XMLFormatter
00085  *
00086  * Revision 1.11  2003/03/16 06:00:43  peiyongz
00087  * Bug#17983 Formatter does not escape control characters
00088  *
00089  * Revision 1.10  2003/03/11 12:58:36  tng
00090  * Fix compilation error on AIX.
00091  *
00092  * Revision 1.9  2003/03/07 21:42:37  tng
00093  * [Bug 17589] Refactoring ... .  Patch from Jacques Legare.
00094  *
00095  * Revision 1.8  2003/03/07 18:08:10  tng
00096  * Return a reference instead of void for operator=
00097  *
00098  * Revision 1.7  2003/01/31 00:30:48  jberry
00099  * Syntax error in declaration
00100  *
00101  * Revision 1.6  2003/01/28 18:32:33  peiyongz
00102  * Bug#13694: Allow Xerces to write the BOM to XML files
00103  *
00104  * Revision 1.5  2003/01/24 20:20:22  tng
00105  * Add method flush to XMLFormatTarget
00106  *
00107  * Revision 1.4  2002/11/04 15:00:21  tng
00108  * C++ Namespace Support.
00109  *
00110  * Revision 1.3  2002/07/30 16:29:16  tng
00111  * [Bug 8550] No explanation of XMLFormatter escape options.
00112  *
00113  * Revision 1.2  2002/06/21 19:31:23  peiyongz
00114  * getTranscoder() added;
00115  *
00116  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
00117  * sane_include
00118  *
00119  * Revision 1.7  2000/10/17 19:25:38  andyh
00120  * XMLFormatTarget, removed version of writeChars with no length.  Can not be
00121  * safely used, and obscured other errors.
00122  *
00123  * Revision 1.6  2000/10/10 23:54:58  andyh
00124  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
00125  * output to multi-byte encodings.
00126  *
00127  * Revision 1.5  2000/04/07 01:01:56  roddey
00128  * Fixed an error message so that it indicated the correct radix for the rep
00129  * token. Get all of the basic output formatting functionality in place for
00130  * at least ICU and Win32 transcoders.
00131  *
00132  * Revision 1.4  2000/04/06 23:50:38  roddey
00133  * Now the low level formatter handles doing char refs for
00134  * unrepresentable chars (in addition to the replacement char style
00135  * already done.)
00136  *
00137  * Revision 1.3  2000/04/06 19:09:21  roddey
00138  * Some more improvements to output formatting. Now it will correctly
00139  * handle doing the 'replacement char' style of dealing with chars
00140  * that are unrepresentable.
00141  *
00142  * Revision 1.2  2000/04/05 00:20:16  roddey
00143  * More updates for the low level formatted output support
00144  *
00145  * Revision 1.1  2000/03/28 19:43:17  roddey
00146  * Fixes for signed/unsigned warnings. New work for two way transcoding
00147  * stuff.
00148  *
00149  */
00150 
00151 #if !defined(XMLFORMATTER_HPP)
00152 #define XMLFORMATTER_HPP
00153 
00154 #include <xercesc/util/PlatformUtils.hpp>
00155 
00156 XERCES_CPP_NAMESPACE_BEGIN
00157 
00158 class XMLFormatTarget;
00159 class XMLTranscoder;
00160 
00170 class  XMLFormatter : public XMemory
00171 {
00172 public:
00173     // -----------------------------------------------------------------------
00174     //  Class types
00175     // -----------------------------------------------------------------------
00253     enum EscapeFlags
00254     {
00255         NoEscapes
00256         , StdEscapes
00257         , AttrEscapes
00258         , CharEscapes
00259 
00260         // Special values, don't use directly
00261         , EscapeFlags_Count
00262         , DefaultEscape     = 999
00263     };
00264 
00281     enum UnRepFlags
00282     {
00283         UnRep_Fail
00284         , UnRep_CharRef
00285         , UnRep_Replace
00286 
00287         , DefaultUnRep      = 999
00288     };
00290 
00291 
00292     // -----------------------------------------------------------------------
00293     //  Constructors and Destructor
00294     // -----------------------------------------------------------------------
00306     XMLFormatter
00307     (
00308         const   XMLCh* const            outEncoding
00309         , const XMLCh* const            docVersion
00310         ,       XMLFormatTarget* const  target
00311         , const EscapeFlags             escapeFlags = NoEscapes
00312         , const UnRepFlags              unrepFlags = UnRep_Fail
00313         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00314     );
00315 
00316     XMLFormatter
00317     (
00318         const   char* const             outEncoding
00319         , const char* const             docVersion
00320         ,       XMLFormatTarget* const  target
00321         , const EscapeFlags             escapeFlags = NoEscapes
00322         , const UnRepFlags              unrepFlags = UnRep_Fail
00323         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00324     );
00325 
00326     XMLFormatter
00327     (
00328         const   XMLCh* const            outEncoding
00329         ,       XMLFormatTarget* const  target
00330         , const EscapeFlags             escapeFlags = NoEscapes
00331         , const UnRepFlags              unrepFlags = UnRep_Fail
00332         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00333     );
00334 
00335     XMLFormatter
00336     (
00337         const   char* const             outEncoding
00338         ,       XMLFormatTarget* const  target
00339         , const EscapeFlags             escapeFlags = NoEscapes
00340         , const UnRepFlags              unrepFlags = UnRep_Fail
00341         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00342     );
00343     
00344     ~XMLFormatter();
00346 
00347 
00348     // -----------------------------------------------------------------------
00349     //  Formatting methods
00350     // -----------------------------------------------------------------------
00360     void formatBuf
00361     (
00362         const   XMLCh* const    toFormat
00363         , const unsigned int    count
00364         , const EscapeFlags     escapeFlags = DefaultEscape
00365         , const UnRepFlags      unrepFlags = DefaultUnRep
00366     );
00367 
00371     XMLFormatter& operator<<
00372     (
00373         const   XMLCh* const    toFormat
00374     );
00375 
00376     XMLFormatter& operator<<
00377     (
00378         const   XMLCh           toFormat
00379     );
00380 
00381     void writeBOM(const XMLByte* const toFormat
00382                 , const unsigned int   count);
00383 
00385 
00386     // -----------------------------------------------------------------------
00387     //  Getter methods
00388     // -----------------------------------------------------------------------
00395     const XMLCh* getEncodingName() const;
00396 
00400     inline const XMLTranscoder*   getTranscoder() const;
00401 
00403 
00404     // -----------------------------------------------------------------------
00405     //  Setter methods
00406     // -----------------------------------------------------------------------
00412     void setEscapeFlags
00413     (
00414         const   EscapeFlags     newFlags
00415     );
00416 
00420     void setUnRepFlags
00421     (
00422         const   UnRepFlags      newFlags
00423     );
00424 
00429     XMLFormatter& operator<<
00430     (
00431         const   EscapeFlags     newFlags
00432     );
00433 
00438     XMLFormatter& operator<<
00439     (
00440         const   UnRepFlags      newFlags
00441     );
00443 
00444 
00445 private :
00446     // -----------------------------------------------------------------------
00447     //  Unimplemented constructors and operators
00448     // -----------------------------------------------------------------------
00449     XMLFormatter();
00450     XMLFormatter(const XMLFormatter&);
00451     XMLFormatter& operator=(const XMLFormatter&);
00452 
00453 
00454     // -----------------------------------------------------------------------
00455     //  Private class constants
00456     // -----------------------------------------------------------------------
00457     enum Constants
00458     {
00459         kTmpBufSize     = 16 * 1024
00460     };
00461 
00462 
00463     // -----------------------------------------------------------------------
00464     //  Private helper methods
00465     // -----------------------------------------------------------------------
00466     const XMLByte* getCharRef(unsigned int & count, 
00467                               XMLByte*      &ref, 
00468                               const XMLCh *  stdRef);  
00469  
00470     void writeCharRef(const XMLCh &toWrite);
00471     void writeCharRef(unsigned long toWrite);
00472 
00473     bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
00474                     , const XMLCh                     toCheck);
00475                               
00476 
00477     unsigned int handleUnEscapedChars(const XMLCh *                  srcPtr, 
00478                                       const unsigned int             count, 
00479                                       const UnRepFlags               unrepFlags);
00480 
00481     void specialFormat
00482     (
00483         const   XMLCh* const    toFormat
00484         , const unsigned int    count
00485         , const EscapeFlags     escapeFlags
00486     );
00487 
00488 
00489     // -----------------------------------------------------------------------
00490     //  Private, non-virtual methods
00491     //
00492     //  fEscapeFlags
00493     //      The escape flags we were told to use in formatting. These are
00494     //      defaults set in the ctor, which can be overridden on a particular
00495     //      call.
00496     //
00497     //  fOutEncoding
00498     //      This the name of the output encoding. Saved mainly for meaningful
00499     //      error messages.
00500     //
00501     //  fTarget
00502     //      This is the target object for the formatting operation.
00503     //
00504     //  fUnRepFlags
00505     //      The unrepresentable flags that indicate how to react when a
00506     //      character cannot be represented in the target encoding.
00507     //
00508     //  fXCoder
00509     //      This the transcoder that we will use. It is created using the
00510     //      encoding name we were told to use.
00511     //
00512     //  fTmpBuf
00513     //      An output buffer that we use to transcode chars into before we
00514     //      send them off to be output.
00515     //
00516     //  fAposRef
00517     //  fAmpRef
00518     //  fGTRef
00519     //  fLTRef
00520     //  fQuoteRef
00521     //      These are character refs for the standard char refs, in the
00522     //      output encoding. They are faulted in as required, by transcoding
00523     //      them from fixed Unicode versions.
00524     //
00525     //  fIsXML11
00526     //      for performance reason, we do not store the actual version string
00527     //      and do the string comparison again and again.
00528     //
00529     // -----------------------------------------------------------------------
00530     EscapeFlags                 fEscapeFlags;
00531     XMLCh*                      fOutEncoding;
00532     XMLFormatTarget*            fTarget;
00533     UnRepFlags                  fUnRepFlags;
00534     XMLTranscoder*              fXCoder;
00535     XMLByte                     fTmpBuf[kTmpBufSize + 4];
00536     XMLByte*                    fAposRef;
00537     unsigned int                fAposLen;
00538     XMLByte*                    fAmpRef;
00539     unsigned int                fAmpLen;
00540     XMLByte*                    fGTRef;
00541     unsigned int                fGTLen;
00542     XMLByte*                    fLTRef;
00543     unsigned int                fLTLen;
00544     XMLByte*                    fQuoteRef;
00545     unsigned int                fQuoteLen;
00546     bool                        fIsXML11;
00547     MemoryManager*              fMemoryManager;
00548 };
00549 
00550 
00551 class  XMLFormatTarget : public XMemory
00552 {
00553 public:
00554     // -----------------------------------------------------------------------
00555     //  Constructors and Destructor
00556     // -----------------------------------------------------------------------
00557     virtual ~XMLFormatTarget() {}
00558 
00559 
00560     // -----------------------------------------------------------------------
00561     //  Virtual interface
00562     // -----------------------------------------------------------------------
00563     virtual void writeChars
00564     (
00565           const XMLByte* const      toWrite
00566         , const unsigned int        count
00567         ,       XMLFormatter* const formatter
00568     ) = 0;
00569 
00570     virtual void flush() {};
00571 
00572 
00573 protected :
00574     // -----------------------------------------------------------------------
00575     //  Hidden constructors and operators
00576     // -----------------------------------------------------------------------
00577     XMLFormatTarget() {};
00578 
00579 private:
00580     // -----------------------------------------------------------------------
00581     //  Unimplemented constructors and operators
00582     // -----------------------------------------------------------------------
00583     XMLFormatTarget(const XMLFormatTarget&);
00584     XMLFormatTarget& operator=(const XMLFormatTarget&);
00585 };
00586 
00587 
00588 // ---------------------------------------------------------------------------
00589 //  XMLFormatter: Getter methods
00590 // ---------------------------------------------------------------------------
00591 inline const XMLCh* XMLFormatter::getEncodingName() const
00592 {
00593     return fOutEncoding;
00594 }
00595 
00596 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
00597 {
00598     return fXCoder;
00599 }
00600 
00601 // ---------------------------------------------------------------------------
00602 //  XMLFormatter: Setter methods
00603 // ---------------------------------------------------------------------------
00604 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
00605 {
00606     fEscapeFlags = newFlags;
00607 }
00608 
00609 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
00610 {
00611     fUnRepFlags = newFlags;
00612 }
00613 
00614 
00615 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
00616 {
00617     fEscapeFlags = newFlags;
00618     return *this;
00619 }
00620 
00621 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
00622 {
00623     fUnRepFlags = newFlags;
00624     return *this;
00625 }
00626 
00627 XERCES_CPP_NAMESPACE_END
00628 
00629 #endif


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