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

Home

Readme
Installation
Build

API Docs
Samples
Programming
Migration
FAQs

Releases
Feedback
Bug-Todo

Download
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.7  2000/10/17 19:25:38  andyh

00060  * XMLFormatTarget, removed version of writeChars with no length.  Can not be

00061  * safely used, and obscured other errors.

00062  *
00063  * Revision 1.6  2000/10/10 23:54:58  andyh

00064  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with

00065  * output to multi-byte encodings.

00066  *
00067  * Revision 1.5  2000/04/07 01:01:56  roddey

00068  * Fixed an error message so that it indicated the correct radix for the rep

00069  * token. Get all of the basic output formatting functionality in place for

00070  * at least ICU and Win32 transcoders.

00071  *
00072  * Revision 1.4  2000/04/06 23:50:38  roddey

00073  * Now the low level formatter handles doing char refs for

00074  * unrepresentable chars (in addition to the replacement char style

00075  * already done.)

00076  *
00077  * Revision 1.3  2000/04/06 19:09:21  roddey

00078  * Some more improvements to output formatting. Now it will correctly

00079  * handle doing the 'replacement char' style of dealing with chars

00080  * that are unrepresentable.

00081  *
00082  * Revision 1.2  2000/04/05 00:20:16  roddey

00083  * More updates for the low level formatted output support

00084  *
00085  * Revision 1.1  2000/03/28 19:43:17  roddey

00086  * Fixes for signed/unsigned warnings. New work for two way transcoding

00087  * stuff.

00088  *
00089  */
00090 
00091 #if !defined(XMLFORMATTER_HPP)
00092 #define XMLFORMATTER_HPP
00093 
00094 #include <util/XercesDefs.hpp>
00095 
00096 class XMLFormatTarget;
00097 class XMLTranscoder;
00098 
00108 class  XMLFormatter
00109 {
00110 public:
00111     // -----------------------------------------------------------------------
00112     //  Class types
00113     // -----------------------------------------------------------------------
00114     enum EscapeFlags
00115     {
00116         NoEscapes
00117         , StdEscapes
00118         , AttrEscapes
00119         , CharEscapes
00120 
00121         // Special values, don't use directly
00122         , EscapeFlags_Count
00123         , DefaultEscape     = 999
00124     };
00125 
00126     enum UnRepFlags
00127     {
00128         UnRep_Fail
00129         , UnRep_CharRef
00130         , UnRep_Replace
00131 
00132         , DefaultUnRep      = 999
00133     };
00134 
00135 
00136     // -----------------------------------------------------------------------
00137     //  Constructors and Destructor
00138     // -----------------------------------------------------------------------
00139     XMLFormatter
00140     (
00141         const   XMLCh* const            outEncoding
00142         ,       XMLFormatTarget* const  target
00143         , const EscapeFlags             escapeFlags = NoEscapes
00144         , const UnRepFlags              unrepFlags = UnRep_Fail
00145     );
00146 
00147     XMLFormatter
00148     (
00149         const   char* const             outEncoding
00150         ,       XMLFormatTarget* const  target
00151         , const EscapeFlags             escapeFlags = NoEscapes
00152         , const UnRepFlags              unrepFlags = UnRep_Fail
00153     );
00154 
00155     ~XMLFormatter();
00156 
00157 
00158     // -----------------------------------------------------------------------
00159     //  Formatting methods
00160     // -----------------------------------------------------------------------
00161     void formatBuf
00162     (
00163         const   XMLCh* const    toFormat
00164         , const unsigned int    count
00165         , const EscapeFlags     escapeFlags = DefaultEscape
00166         , const UnRepFlags      unrepFlags = DefaultUnRep
00167     );
00168 
00169     XMLFormatter& operator<<
00170     (
00171         const   XMLCh* const    toFormat
00172     );
00173 
00174     XMLFormatter& operator<<
00175     (
00176         const   XMLCh           toFormat
00177     );
00178 
00179 
00180     // -----------------------------------------------------------------------
00181     //  Getter methods
00182     // -----------------------------------------------------------------------
00183     const XMLCh* getEncodingName() const;
00184 
00185 
00186     // -----------------------------------------------------------------------
00187     //  Setter methods
00188     // -----------------------------------------------------------------------
00189     void setEscapeFlags
00190     (
00191         const   EscapeFlags     newFlags
00192     );
00193 
00194     void setUnRepFlags
00195     (
00196         const   UnRepFlags      newFlags
00197     );
00198 
00199     XMLFormatter& operator<<
00200     (
00201         const   EscapeFlags     newFlags
00202     );
00203 
00204     XMLFormatter& operator<<
00205     (
00206         const   UnRepFlags      newFlags
00207     );
00208 
00209 
00210 private :
00211     // -----------------------------------------------------------------------
00212     //  Unimplemented constructors and operators
00213     // -----------------------------------------------------------------------
00214     XMLFormatter();
00215     XMLFormatter(const XMLFormatter&);
00216     void operator=(const XMLFormatter&);
00217 
00218 
00219     // -----------------------------------------------------------------------
00220     //  Private class constants
00221     // -----------------------------------------------------------------------
00222     enum Constants
00223     {
00224         kTmpBufSize     = 16 * 1024
00225     };
00226 
00227 
00228     // -----------------------------------------------------------------------
00229     //  Private helper methods
00230     // -----------------------------------------------------------------------
00231     const XMLByte* getAposRef(unsigned int & count);
00232     const XMLByte* getAmpRef(unsigned int & count);
00233     const XMLByte* getGTRef(unsigned int & count);
00234     const XMLByte* getLTRef(unsigned int & count);
00235     const XMLByte* getQuoteRef(unsigned int & count);
00236 
00237     void specialFormat
00238     (
00239         const   XMLCh* const    toFormat
00240         , const unsigned int    count
00241         , const EscapeFlags     escapeFlags
00242     );
00243 
00244 
00245     // -----------------------------------------------------------------------
00246     //  Private, non-virtual methods
00247     //
00248     //  fEscapeFlags
00249     //      The escape flags we were told to use in formatting. These are
00250     //      defaults set in the ctor, which can be overridden on a particular
00251     //      call.
00252     //
00253     //  fOutEncoding
00254     //      This the name of the output encoding. Saved mainly for meaningful
00255     //      error messages.
00256     //
00257     //  fTarget
00258     //      This is the target object for the formatting operation.
00259     //
00260     //  fUnRepFlags
00261     //      The unrepresentable flags that indicate how to react when a
00262     //      character cannot be represented in the target encoding.
00263     //
00264     //  fXCoder
00265     //      This the transcoder that we will use. It is created using the
00266     //      encoding name we were told to use.
00267     //
00268     //  fTmpBuf
00269     //      An output buffer that we use to transcode chars into before we
00270     //      send them off to be output.
00271     //
00272     //  fAposRef
00273     //  fAmpRef
00274     //  fGTRef
00275     //  fLTRef
00276     //  fQuoteRef
00277     //      These are character refs for the standard char refs, in the
00278     //      output encoding. They are faulted in as required, by transcoding
00279     //      them from fixed Unicode versions.
00280     // -----------------------------------------------------------------------
00281     EscapeFlags                 fEscapeFlags;
00282     XMLCh*                      fOutEncoding;
00283     XMLFormatTarget*            fTarget;
00284     UnRepFlags                  fUnRepFlags;
00285     XMLTranscoder*              fXCoder;
00286     XMLByte                     fTmpBuf[kTmpBufSize + 4];
00287 
00288     XMLByte*                    fAposRef;
00289     unsigned int                fAposLen;
00290     XMLByte*                    fAmpRef;
00291     unsigned int                fAmpLen;
00292     XMLByte*                    fGTRef;
00293     unsigned int                fGTLen;
00294     XMLByte*                    fLTRef;
00295     unsigned int                fLTLen;
00296     XMLByte*                    fQuoteRef;
00297     unsigned int                fQuoteLen;
00298 };
00299 
00300 
00301 class  XMLFormatTarget
00302 {
00303 public:
00304     // -----------------------------------------------------------------------
00305     //  Constructors and Destructor
00306     // -----------------------------------------------------------------------
00307     virtual ~XMLFormatTarget() {}
00308 
00309 
00310     // -----------------------------------------------------------------------
00311     //  Virtual interface
00312     // -----------------------------------------------------------------------
00313     virtual void writeChars
00314     (
00315         const   XMLByte* const      toWrite
00316         , const unsigned int        count
00317         ,       XMLFormatter* const formatter
00318     ) = 0;
00319 
00320 
00321 protected :
00322     // -----------------------------------------------------------------------
00323     //  Hidden constructors and operators
00324     // -----------------------------------------------------------------------
00325     XMLFormatTarget() {}
00326     XMLFormatTarget(const XMLFormatTarget&) {}
00327     void operator=(const XMLFormatTarget&) {}
00328 };
00329 
00330 
00331 // ---------------------------------------------------------------------------
00332 //  XMLFormatter: Getter methods
00333 // ---------------------------------------------------------------------------
00334 inline const XMLCh* XMLFormatter::getEncodingName() const

00335 {
00336     return fOutEncoding;
00337 }
00338 
00339 
00340 // ---------------------------------------------------------------------------
00341 //  XMLFormatter: Setter methods
00342 // ---------------------------------------------------------------------------
00343 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)

00344 {
00345     fEscapeFlags = newFlags;
00346 }
00347 
00348 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)

00349 {
00350     fUnRepFlags = newFlags;
00351 }
00352 
00353 
00354 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)

00355 {
00356     fEscapeFlags = newFlags;
00357     return *this;
00358 }
00359 
00360 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)

00361 {
00362     fUnRepFlags = newFlags;
00363     return *this;
00364 }
00365 
00366 
00367 #endif


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