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


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