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

Home

Readme
Download
Installation
Build

API Docs
Samples
Schema

FAQs
Programming
Migration

Releases
Bug-Reporting
Feedback

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


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