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.2  2002/06/21 19:31:23  peiyongz
00060  * getTranscoder() added;
00061  *
00062  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
00063  * sane_include
00064  *
00065  * Revision 1.7  2000/10/17 19:25:38  andyh
00066  * XMLFormatTarget, removed version of writeChars with no length.  Can not be
00067  * safely used, and obscured other errors.
00068  *
00069  * Revision 1.6  2000/10/10 23:54:58  andyh
00070  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
00071  * output to multi-byte encodings.
00072  *
00073  * Revision 1.5  2000/04/07 01:01:56  roddey
00074  * Fixed an error message so that it indicated the correct radix for the rep
00075  * token. Get all of the basic output formatting functionality in place for
00076  * at least ICU and Win32 transcoders.
00077  *
00078  * Revision 1.4  2000/04/06 23:50:38  roddey
00079  * Now the low level formatter handles doing char refs for
00080  * unrepresentable chars (in addition to the replacement char style
00081  * already done.)
00082  *
00083  * Revision 1.3  2000/04/06 19:09:21  roddey
00084  * Some more improvements to output formatting. Now it will correctly
00085  * handle doing the 'replacement char' style of dealing with chars
00086  * that are unrepresentable.
00087  *
00088  * Revision 1.2  2000/04/05 00:20:16  roddey
00089  * More updates for the low level formatted output support
00090  *
00091  * Revision 1.1  2000/03/28 19:43:17  roddey
00092  * Fixes for signed/unsigned warnings. New work for two way transcoding
00093  * stuff.
00094  *
00095  */
00096 
00097 #if !defined(XMLFORMATTER_HPP)
00098 #define XMLFORMATTER_HPP
00099 
00100 #include <xercesc/util/XercesDefs.hpp>
00101 
00102 class XMLFormatTarget;
00103 class XMLTranscoder;
00104 
00114 class  XMLFormatter
00115 {
00116 public:
00117     // -----------------------------------------------------------------------
00118     //  Class types
00119     // -----------------------------------------------------------------------
00120     enum EscapeFlags
00121     {
00122         NoEscapes
00123         , StdEscapes
00124         , AttrEscapes
00125         , CharEscapes
00126 
00127         // Special values, don't use directly
00128         , EscapeFlags_Count
00129         , DefaultEscape     = 999
00130     };
00131 
00132     enum UnRepFlags
00133     {
00134         UnRep_Fail
00135         , UnRep_CharRef
00136         , UnRep_Replace
00137 
00138         , DefaultUnRep      = 999
00139     };
00140 
00141 
00142     // -----------------------------------------------------------------------
00143     //  Constructors and Destructor
00144     // -----------------------------------------------------------------------
00145     XMLFormatter
00146     (
00147         const   XMLCh* const            outEncoding
00148         ,       XMLFormatTarget* const  target
00149         , const EscapeFlags             escapeFlags = NoEscapes
00150         , const UnRepFlags              unrepFlags = UnRep_Fail
00151     );
00152 
00153     XMLFormatter
00154     (
00155         const   char* const             outEncoding
00156         ,       XMLFormatTarget* const  target
00157         , const EscapeFlags             escapeFlags = NoEscapes
00158         , const UnRepFlags              unrepFlags = UnRep_Fail
00159     );
00160 
00161     ~XMLFormatter();
00162 
00163 
00164     // -----------------------------------------------------------------------
00165     //  Formatting methods
00166     // -----------------------------------------------------------------------
00167     void formatBuf
00168     (
00169         const   XMLCh* const    toFormat
00170         , const unsigned int    count
00171         , const EscapeFlags     escapeFlags = DefaultEscape
00172         , const UnRepFlags      unrepFlags = DefaultUnRep
00173     );
00174 
00175     XMLFormatter& operator<<
00176     (
00177         const   XMLCh* const    toFormat
00178     );
00179 
00180     XMLFormatter& operator<<
00181     (
00182         const   XMLCh           toFormat
00183     );
00184 
00185 
00186     // -----------------------------------------------------------------------
00187     //  Getter methods
00188     // -----------------------------------------------------------------------
00189     const XMLCh* getEncodingName() const;
00190 
00191     inline const XMLTranscoder*   getTranscoder() const;
00192 
00193     // -----------------------------------------------------------------------
00194     //  Setter methods
00195     // -----------------------------------------------------------------------
00196     void setEscapeFlags
00197     (
00198         const   EscapeFlags     newFlags
00199     );
00200 
00201     void setUnRepFlags
00202     (
00203         const   UnRepFlags      newFlags
00204     );
00205 
00206     XMLFormatter& operator<<
00207     (
00208         const   EscapeFlags     newFlags
00209     );
00210 
00211     XMLFormatter& operator<<
00212     (
00213         const   UnRepFlags      newFlags
00214     );
00215 
00216 
00217 private :
00218     // -----------------------------------------------------------------------
00219     //  Unimplemented constructors and operators
00220     // -----------------------------------------------------------------------
00221     XMLFormatter();
00222     XMLFormatter(const XMLFormatter&);
00223     void operator=(const XMLFormatter&);
00224 
00225 
00226     // -----------------------------------------------------------------------
00227     //  Private class constants
00228     // -----------------------------------------------------------------------
00229     enum Constants
00230     {
00231         kTmpBufSize     = 16 * 1024
00232     };
00233 
00234 
00235     // -----------------------------------------------------------------------
00236     //  Private helper methods
00237     // -----------------------------------------------------------------------
00238     const XMLByte* getAposRef(unsigned int & count);
00239     const XMLByte* getAmpRef(unsigned int & count);
00240     const XMLByte* getGTRef(unsigned int & count);
00241     const XMLByte* getLTRef(unsigned int & count);
00242     const XMLByte* getQuoteRef(unsigned int & count);
00243 
00244     void specialFormat
00245     (
00246         const   XMLCh* const    toFormat
00247         , const unsigned int    count
00248         , const EscapeFlags     escapeFlags
00249     );
00250 
00251 
00252     // -----------------------------------------------------------------------
00253     //  Private, non-virtual methods
00254     //
00255     //  fEscapeFlags
00256     //      The escape flags we were told to use in formatting. These are
00257     //      defaults set in the ctor, which can be overridden on a particular
00258     //      call.
00259     //
00260     //  fOutEncoding
00261     //      This the name of the output encoding. Saved mainly for meaningful
00262     //      error messages.
00263     //
00264     //  fTarget
00265     //      This is the target object for the formatting operation.
00266     //
00267     //  fUnRepFlags
00268     //      The unrepresentable flags that indicate how to react when a
00269     //      character cannot be represented in the target encoding.
00270     //
00271     //  fXCoder
00272     //      This the transcoder that we will use. It is created using the
00273     //      encoding name we were told to use.
00274     //
00275     //  fTmpBuf
00276     //      An output buffer that we use to transcode chars into before we
00277     //      send them off to be output.
00278     //
00279     //  fAposRef
00280     //  fAmpRef
00281     //  fGTRef
00282     //  fLTRef
00283     //  fQuoteRef
00284     //      These are character refs for the standard char refs, in the
00285     //      output encoding. They are faulted in as required, by transcoding
00286     //      them from fixed Unicode versions.
00287     // -----------------------------------------------------------------------
00288     EscapeFlags                 fEscapeFlags;
00289     XMLCh*                      fOutEncoding;
00290     XMLFormatTarget*            fTarget;
00291     UnRepFlags                  fUnRepFlags;
00292     XMLTranscoder*              fXCoder;
00293     XMLByte                     fTmpBuf[kTmpBufSize + 4];
00294 
00295     XMLByte*                    fAposRef;
00296     unsigned int                fAposLen;
00297     XMLByte*                    fAmpRef;
00298     unsigned int                fAmpLen;
00299     XMLByte*                    fGTRef;
00300     unsigned int                fGTLen;
00301     XMLByte*                    fLTRef;
00302     unsigned int                fLTLen;
00303     XMLByte*                    fQuoteRef;
00304     unsigned int                fQuoteLen;
00305 };
00306 
00307 
00308 class  XMLFormatTarget
00309 {
00310 public:
00311     // -----------------------------------------------------------------------
00312     //  Constructors and Destructor
00313     // -----------------------------------------------------------------------
00314     virtual ~XMLFormatTarget() {}
00315 
00316 
00317     // -----------------------------------------------------------------------
00318     //  Virtual interface
00319     // -----------------------------------------------------------------------
00320     virtual void writeChars
00321     (
00322         const   XMLByte* const      toWrite
00323         , const unsigned int        count
00324         ,       XMLFormatter* const formatter
00325     ) = 0;
00326 
00327 
00328 protected :
00329     // -----------------------------------------------------------------------
00330     //  Hidden constructors and operators
00331     // -----------------------------------------------------------------------
00332     XMLFormatTarget() {}
00333     XMLFormatTarget(const XMLFormatTarget&) {}
00334     void operator=(const XMLFormatTarget&) {}
00335 };
00336 
00337 
00338 // ---------------------------------------------------------------------------
00339 //  XMLFormatter: Getter methods
00340 // ---------------------------------------------------------------------------
00341 inline const XMLCh* XMLFormatter::getEncodingName() const
00342 {
00343     return fOutEncoding;
00344 }
00345 
00346 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
00347 {
00348     return fXCoder;
00349 }
00350 
00351 // ---------------------------------------------------------------------------
00352 //  XMLFormatter: Setter methods
00353 // ---------------------------------------------------------------------------
00354 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
00355 {
00356     fEscapeFlags = newFlags;
00357 }
00358 
00359 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
00360 {
00361     fUnRepFlags = newFlags;
00362 }
00363 
00364 
00365 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
00366 {
00367     fEscapeFlags = newFlags;
00368     return *this;
00369 }
00370 
00371 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
00372 {
00373     fUnRepFlags = newFlags;
00374     return *this;
00375 }
00376 
00377 
00378 #endif


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