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

Home

Readme
Installation
Build

API Docs
Samples
Programming
Migration
FAQs

Releases
Feedback

PDF Document

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


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