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  * 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  * $Log: XMLFormatter.hpp,v $
00019  * Revision 1.21  2004/09/08 13:55:59  peiyongz
00020  * Apache License Version 2.0
00021  *
00022  * Revision 1.20  2004/01/29 11:46:29  cargilld
00023  * Code cleanup changes to get rid of various compiler diagnostic messages.
00024  *
00025  * Revision 1.19  2003/12/01 23:23:25  neilg
00026  * fix for bug 25118; thanks to Jeroen Witmond
00027  *
00028  * Revision 1.18  2003/10/10 02:06:09  neilg
00029  * fix for bug 21780; thanks to David Cargill
00030  *
00031  * Revision 1.17  2003/09/22 08:50:04  gareth
00032  * doc fix
00033  *
00034  * Revision 1.16  2003/09/08 21:48:36  peiyongz
00035  * Restore pre2.3 constructors
00036  *
00037  * Revision 1.15  2003/05/30 16:11:43  gareth
00038  * Fixes so we compile under VC7.1. Patch by Alberto Massari.
00039  *
00040  * Revision 1.14  2003/05/16 21:36:55  knoaman
00041  * Memory manager implementation: Modify constructors to pass in the memory manager.
00042  *
00043  * Revision 1.13  2003/05/15 18:26:07  knoaman
00044  * Partial implementation of the configurable memory manager.
00045  *
00046  * Revision 1.12  2003/03/17 03:19:52  peiyongz
00047  * Bug#18051 memory leakage in XMLFormatter
00048  *
00049  * Revision 1.11  2003/03/16 06:00:43  peiyongz
00050  * Bug#17983 Formatter does not escape control characters
00051  *
00052  * Revision 1.10  2003/03/11 12:58:36  tng
00053  * Fix compilation error on AIX.
00054  *
00055  * Revision 1.9  2003/03/07 21:42:37  tng
00056  * [Bug 17589] Refactoring ... .  Patch from Jacques Legare.
00057  *
00058  * Revision 1.8  2003/03/07 18:08:10  tng
00059  * Return a reference instead of void for operator=
00060  *
00061  * Revision 1.7  2003/01/31 00:30:48  jberry
00062  * Syntax error in declaration
00063  *
00064  * Revision 1.6  2003/01/28 18:32:33  peiyongz
00065  * Bug#13694: Allow Xerces to write the BOM to XML files
00066  *
00067  * Revision 1.5  2003/01/24 20:20:22  tng
00068  * Add method flush to XMLFormatTarget
00069  *
00070  * Revision 1.4  2002/11/04 15:00:21  tng
00071  * C++ Namespace Support.
00072  *
00073  * Revision 1.3  2002/07/30 16:29:16  tng
00074  * [Bug 8550] No explanation of XMLFormatter escape options.
00075  *
00076  * Revision 1.2  2002/06/21 19:31:23  peiyongz
00077  * getTranscoder() added;
00078  *
00079  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
00080  * sane_include
00081  *
00082  * Revision 1.7  2000/10/17 19:25:38  andyh
00083  * XMLFormatTarget, removed version of writeChars with no length.  Can not be
00084  * safely used, and obscured other errors.
00085  *
00086  * Revision 1.6  2000/10/10 23:54:58  andyh
00087  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
00088  * output to multi-byte encodings.
00089  *
00090  * Revision 1.5  2000/04/07 01:01:56  roddey
00091  * Fixed an error message so that it indicated the correct radix for the rep
00092  * token. Get all of the basic output formatting functionality in place for
00093  * at least ICU and Win32 transcoders.
00094  *
00095  * Revision 1.4  2000/04/06 23:50:38  roddey
00096  * Now the low level formatter handles doing char refs for
00097  * unrepresentable chars (in addition to the replacement char style
00098  * already done.)
00099  *
00100  * Revision 1.3  2000/04/06 19:09:21  roddey
00101  * Some more improvements to output formatting. Now it will correctly
00102  * handle doing the 'replacement char' style of dealing with chars
00103  * that are unrepresentable.
00104  *
00105  * Revision 1.2  2000/04/05 00:20:16  roddey
00106  * More updates for the low level formatted output support
00107  *
00108  * Revision 1.1  2000/03/28 19:43:17  roddey
00109  * Fixes for signed/unsigned warnings. New work for two way transcoding
00110  * stuff.
00111  *
00112  */
00113 
00114 #if !defined(XMLFORMATTER_HPP)
00115 #define XMLFORMATTER_HPP
00116 
00117 #include <xercesc/util/PlatformUtils.hpp>
00118 
00119 XERCES_CPP_NAMESPACE_BEGIN
00120 
00121 class XMLFormatTarget;
00122 class XMLTranscoder;
00123 
00133 class  XMLFormatter : public XMemory
00134 {
00135 public:
00136     // -----------------------------------------------------------------------
00137     //  Class types
00138     // -----------------------------------------------------------------------
00216     enum EscapeFlags
00217     {
00218         NoEscapes
00219         , StdEscapes
00220         , AttrEscapes
00221         , CharEscapes
00222 
00223         // Special values, don't use directly
00224         , EscapeFlags_Count
00225         , DefaultEscape     = 999
00226     };
00227 
00244     enum UnRepFlags
00245     {
00246         UnRep_Fail
00247         , UnRep_CharRef
00248         , UnRep_Replace
00249 
00250         , DefaultUnRep      = 999
00251     };
00253 
00254 
00255     // -----------------------------------------------------------------------
00256     //  Constructors and Destructor
00257     // -----------------------------------------------------------------------
00269     XMLFormatter
00270     (
00271         const   XMLCh* const            outEncoding
00272         , const XMLCh* const            docVersion
00273         ,       XMLFormatTarget* const  target
00274         , const EscapeFlags             escapeFlags = NoEscapes
00275         , const UnRepFlags              unrepFlags = UnRep_Fail
00276         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00277     );
00278 
00279     XMLFormatter
00280     (
00281         const   char* const             outEncoding
00282         , const char* const             docVersion
00283         ,       XMLFormatTarget* const  target
00284         , const EscapeFlags             escapeFlags = NoEscapes
00285         , const UnRepFlags              unrepFlags = UnRep_Fail
00286         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00287     );
00288 
00289     XMLFormatter
00290     (
00291         const   XMLCh* const            outEncoding
00292         ,       XMLFormatTarget* const  target
00293         , const EscapeFlags             escapeFlags = NoEscapes
00294         , const UnRepFlags              unrepFlags = UnRep_Fail
00295         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00296     );
00297 
00298     XMLFormatter
00299     (
00300         const   char* const             outEncoding
00301         ,       XMLFormatTarget* const  target
00302         , const EscapeFlags             escapeFlags = NoEscapes
00303         , const UnRepFlags              unrepFlags = UnRep_Fail
00304         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00305     );
00306     
00307     ~XMLFormatter();
00309 
00310 
00311     // -----------------------------------------------------------------------
00312     //  Formatting methods
00313     // -----------------------------------------------------------------------
00323     void formatBuf
00324     (
00325         const   XMLCh* const    toFormat
00326         , const unsigned int    count
00327         , const EscapeFlags     escapeFlags = DefaultEscape
00328         , const UnRepFlags      unrepFlags = DefaultUnRep
00329     );
00330 
00334     XMLFormatter& operator<<
00335     (
00336         const   XMLCh* const    toFormat
00337     );
00338 
00339     XMLFormatter& operator<<
00340     (
00341         const   XMLCh           toFormat
00342     );
00343 
00344     void writeBOM(const XMLByte* const toFormat
00345                 , const unsigned int   count);
00346 
00348 
00349     // -----------------------------------------------------------------------
00350     //  Getter methods
00351     // -----------------------------------------------------------------------
00358     const XMLCh* getEncodingName() const;
00359 
00363     inline const XMLTranscoder*   getTranscoder() const;
00364 
00366 
00367     // -----------------------------------------------------------------------
00368     //  Setter methods
00369     // -----------------------------------------------------------------------
00375     void setEscapeFlags
00376     (
00377         const   EscapeFlags     newFlags
00378     );
00379 
00383     void setUnRepFlags
00384     (
00385         const   UnRepFlags      newFlags
00386     );
00387 
00392     XMLFormatter& operator<<
00393     (
00394         const   EscapeFlags     newFlags
00395     );
00396 
00401     XMLFormatter& operator<<
00402     (
00403         const   UnRepFlags      newFlags
00404     );
00406 
00407 
00408 private :
00409     // -----------------------------------------------------------------------
00410     //  Unimplemented constructors and operators
00411     // -----------------------------------------------------------------------
00412     XMLFormatter();
00413     XMLFormatter(const XMLFormatter&);
00414     XMLFormatter& operator=(const XMLFormatter&);
00415 
00416 
00417     // -----------------------------------------------------------------------
00418     //  Private class constants
00419     // -----------------------------------------------------------------------
00420     enum Constants
00421     {
00422         kTmpBufSize     = 16 * 1024
00423     };
00424 
00425 
00426     // -----------------------------------------------------------------------
00427     //  Private helper methods
00428     // -----------------------------------------------------------------------
00429     const XMLByte* getCharRef(unsigned int & count, 
00430                               XMLByte*      &ref, 
00431                               const XMLCh *  stdRef);  
00432  
00433     void writeCharRef(const XMLCh &toWrite);
00434     void writeCharRef(unsigned long toWrite);
00435 
00436     bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
00437                     , const XMLCh                     toCheck);
00438                               
00439 
00440     unsigned int handleUnEscapedChars(const XMLCh *                  srcPtr, 
00441                                       const unsigned int             count, 
00442                                       const UnRepFlags               unrepFlags);
00443 
00444     void specialFormat
00445     (
00446         const   XMLCh* const    toFormat
00447         , const unsigned int    count
00448         , const EscapeFlags     escapeFlags
00449     );
00450 
00451 
00452     // -----------------------------------------------------------------------
00453     //  Private, non-virtual methods
00454     //
00455     //  fEscapeFlags
00456     //      The escape flags we were told to use in formatting. These are
00457     //      defaults set in the ctor, which can be overridden on a particular
00458     //      call.
00459     //
00460     //  fOutEncoding
00461     //      This the name of the output encoding. Saved mainly for meaningful
00462     //      error messages.
00463     //
00464     //  fTarget
00465     //      This is the target object for the formatting operation.
00466     //
00467     //  fUnRepFlags
00468     //      The unrepresentable flags that indicate how to react when a
00469     //      character cannot be represented in the target encoding.
00470     //
00471     //  fXCoder
00472     //      This the transcoder that we will use. It is created using the
00473     //      encoding name we were told to use.
00474     //
00475     //  fTmpBuf
00476     //      An output buffer that we use to transcode chars into before we
00477     //      send them off to be output.
00478     //
00479     //  fAposRef
00480     //  fAmpRef
00481     //  fGTRef
00482     //  fLTRef
00483     //  fQuoteRef
00484     //      These are character refs for the standard char refs, in the
00485     //      output encoding. They are faulted in as required, by transcoding
00486     //      them from fixed Unicode versions.
00487     //
00488     //  fIsXML11
00489     //      for performance reason, we do not store the actual version string
00490     //      and do the string comparison again and again.
00491     //
00492     // -----------------------------------------------------------------------
00493     EscapeFlags                 fEscapeFlags;
00494     XMLCh*                      fOutEncoding;
00495     XMLFormatTarget*            fTarget;
00496     UnRepFlags                  fUnRepFlags;
00497     XMLTranscoder*              fXCoder;
00498     XMLByte                     fTmpBuf[kTmpBufSize + 4];
00499     XMLByte*                    fAposRef;
00500     unsigned int                fAposLen;
00501     XMLByte*                    fAmpRef;
00502     unsigned int                fAmpLen;
00503     XMLByte*                    fGTRef;
00504     unsigned int                fGTLen;
00505     XMLByte*                    fLTRef;
00506     unsigned int                fLTLen;
00507     XMLByte*                    fQuoteRef;
00508     unsigned int                fQuoteLen;
00509     bool                        fIsXML11;
00510     MemoryManager*              fMemoryManager;
00511 };
00512 
00513 
00514 class  XMLFormatTarget : public XMemory
00515 {
00516 public:
00517     // -----------------------------------------------------------------------
00518     //  Constructors and Destructor
00519     // -----------------------------------------------------------------------
00520     virtual ~XMLFormatTarget() {}
00521 
00522 
00523     // -----------------------------------------------------------------------
00524     //  Virtual interface
00525     // -----------------------------------------------------------------------
00526     virtual void writeChars
00527     (
00528           const XMLByte* const      toWrite
00529         , const unsigned int        count
00530         ,       XMLFormatter* const formatter
00531     ) = 0;
00532 
00533     virtual void flush() {};
00534 
00535 
00536 protected :
00537     // -----------------------------------------------------------------------
00538     //  Hidden constructors and operators
00539     // -----------------------------------------------------------------------
00540     XMLFormatTarget() {};
00541 
00542 private:
00543     // -----------------------------------------------------------------------
00544     //  Unimplemented constructors and operators
00545     // -----------------------------------------------------------------------
00546     XMLFormatTarget(const XMLFormatTarget&);
00547     XMLFormatTarget& operator=(const XMLFormatTarget&);
00548 };
00549 
00550 
00551 // ---------------------------------------------------------------------------
00552 //  XMLFormatter: Getter methods
00553 // ---------------------------------------------------------------------------
00554 inline const XMLCh* XMLFormatter::getEncodingName() const
00555 {
00556     return fOutEncoding;
00557 }
00558 
00559 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
00560 {
00561     return fXCoder;
00562 }
00563 
00564 // ---------------------------------------------------------------------------
00565 //  XMLFormatter: Setter methods
00566 // ---------------------------------------------------------------------------
00567 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
00568 {
00569     fEscapeFlags = newFlags;
00570 }
00571 
00572 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
00573 {
00574     fUnRepFlags = newFlags;
00575 }
00576 
00577 
00578 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
00579 {
00580     fEscapeFlags = newFlags;
00581     return *this;
00582 }
00583 
00584 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
00585 {
00586     fUnRepFlags = newFlags;
00587     return *this;
00588 }
00589 
00590 XERCES_CPP_NAMESPACE_END
00591 
00592 #endif


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