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.3  2002/07/30 16:29:16  tng
00060  * [Bug 8550] No explanation of XMLFormatter escape options.
00061  *
00062  * Revision 1.2  2002/06/21 19:31:23  peiyongz
00063  * getTranscoder() added;
00064  *
00065  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
00066  * sane_include
00067  *
00068  * Revision 1.7  2000/10/17 19:25:38  andyh
00069  * XMLFormatTarget, removed version of writeChars with no length.  Can not be
00070  * safely used, and obscured other errors.
00071  *
00072  * Revision 1.6  2000/10/10 23:54:58  andyh
00073  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
00074  * output to multi-byte encodings.
00075  *
00076  * Revision 1.5  2000/04/07 01:01:56  roddey
00077  * Fixed an error message so that it indicated the correct radix for the rep
00078  * token. Get all of the basic output formatting functionality in place for
00079  * at least ICU and Win32 transcoders.
00080  *
00081  * Revision 1.4  2000/04/06 23:50:38  roddey
00082  * Now the low level formatter handles doing char refs for
00083  * unrepresentable chars (in addition to the replacement char style
00084  * already done.)
00085  *
00086  * Revision 1.3  2000/04/06 19:09:21  roddey
00087  * Some more improvements to output formatting. Now it will correctly
00088  * handle doing the 'replacement char' style of dealing with chars
00089  * that are unrepresentable.
00090  *
00091  * Revision 1.2  2000/04/05 00:20:16  roddey
00092  * More updates for the low level formatted output support
00093  *
00094  * Revision 1.1  2000/03/28 19:43:17  roddey
00095  * Fixes for signed/unsigned warnings. New work for two way transcoding
00096  * stuff.
00097  *
00098  */
00099 
00100 #if !defined(XMLFORMATTER_HPP)
00101 #define XMLFORMATTER_HPP
00102 
00103 #include <xercesc/util/XercesDefs.hpp>
00104 
00105 class XMLFormatTarget;
00106 class XMLTranscoder;
00107 
00117 class  XMLFormatter
00118 {
00119 public:
00120     // -----------------------------------------------------------------------
00121     //  Class types
00122     // -----------------------------------------------------------------------
00125 
00200     enum EscapeFlags
00201     {
00202         NoEscapes
00203         , StdEscapes
00204         , AttrEscapes
00205         , CharEscapes
00206 
00207         // Special values, don't use directly
00208         , EscapeFlags_Count
00209         , DefaultEscape     = 999
00210     };
00211 
00228     enum UnRepFlags
00229     {
00230         UnRep_Fail
00231         , UnRep_CharRef
00232         , UnRep_Replace
00233 
00234         , DefaultUnRep      = 999
00235     };
00237 
00238 
00239     // -----------------------------------------------------------------------
00240     //  Constructors and Destructor
00241     // -----------------------------------------------------------------------
00244 
00250     XMLFormatter
00251     (
00252         const   XMLCh* const            outEncoding
00253         ,       XMLFormatTarget* const  target
00254         , const EscapeFlags             escapeFlags = NoEscapes
00255         , const UnRepFlags              unrepFlags = UnRep_Fail
00256     );
00257 
00258     XMLFormatter
00259     (
00260         const   char* const             outEncoding
00261         ,       XMLFormatTarget* const  target
00262         , const EscapeFlags             escapeFlags = NoEscapes
00263         , const UnRepFlags              unrepFlags = UnRep_Fail
00264     );
00265 
00266     ~XMLFormatter();
00268 
00269 
00270     // -----------------------------------------------------------------------
00271     //  Formatting methods
00272     // -----------------------------------------------------------------------
00275 
00282     void formatBuf
00283     (
00284         const   XMLCh* const    toFormat
00285         , const unsigned int    count
00286         , const EscapeFlags     escapeFlags = DefaultEscape
00287         , const UnRepFlags      unrepFlags = DefaultUnRep
00288     );
00289 
00293     XMLFormatter& operator<<
00294     (
00295         const   XMLCh* const    toFormat
00296     );
00297 
00298     XMLFormatter& operator<<
00299     (
00300         const   XMLCh           toFormat
00301     );
00303 
00304     // -----------------------------------------------------------------------
00305     //  Getter methods
00306     // -----------------------------------------------------------------------
00309 
00312 
00313     const XMLCh* getEncodingName() const;
00314 
00318     inline const XMLTranscoder*   getTranscoder() const;
00320 
00321     // -----------------------------------------------------------------------
00322     //  Setter methods
00323     // -----------------------------------------------------------------------
00326 
00329     void setEscapeFlags
00330     (
00331         const   EscapeFlags     newFlags
00332     );
00333 
00337     void setUnRepFlags
00338     (
00339         const   UnRepFlags      newFlags
00340     );
00341 
00346     XMLFormatter& operator<<
00347     (
00348         const   EscapeFlags     newFlags
00349     );
00350 
00355     XMLFormatter& operator<<
00356     (
00357         const   UnRepFlags      newFlags
00358     );
00360 
00361 
00362 private :
00363     // -----------------------------------------------------------------------
00364     //  Unimplemented constructors and operators
00365     // -----------------------------------------------------------------------
00366     XMLFormatter();
00367     XMLFormatter(const XMLFormatter&);
00368     void operator=(const XMLFormatter&);
00369 
00370 
00371     // -----------------------------------------------------------------------
00372     //  Private class constants
00373     // -----------------------------------------------------------------------
00374     enum Constants
00375     {
00376         kTmpBufSize     = 16 * 1024
00377     };
00378 
00379 
00380     // -----------------------------------------------------------------------
00381     //  Private helper methods
00382     // -----------------------------------------------------------------------
00383     const XMLByte* getAposRef(unsigned int & count);
00384     const XMLByte* getAmpRef(unsigned int & count);
00385     const XMLByte* getGTRef(unsigned int & count);
00386     const XMLByte* getLTRef(unsigned int & count);
00387     const XMLByte* getQuoteRef(unsigned int & count);
00388 
00389     void specialFormat
00390     (
00391         const   XMLCh* const    toFormat
00392         , const unsigned int    count
00393         , const EscapeFlags     escapeFlags
00394     );
00395 
00396 
00397     // -----------------------------------------------------------------------
00398     //  Private, non-virtual methods
00399     //
00400     //  fEscapeFlags
00401     //      The escape flags we were told to use in formatting. These are
00402     //      defaults set in the ctor, which can be overridden on a particular
00403     //      call.
00404     //
00405     //  fOutEncoding
00406     //      This the name of the output encoding. Saved mainly for meaningful
00407     //      error messages.
00408     //
00409     //  fTarget
00410     //      This is the target object for the formatting operation.
00411     //
00412     //  fUnRepFlags
00413     //      The unrepresentable flags that indicate how to react when a
00414     //      character cannot be represented in the target encoding.
00415     //
00416     //  fXCoder
00417     //      This the transcoder that we will use. It is created using the
00418     //      encoding name we were told to use.
00419     //
00420     //  fTmpBuf
00421     //      An output buffer that we use to transcode chars into before we
00422     //      send them off to be output.
00423     //
00424     //  fAposRef
00425     //  fAmpRef
00426     //  fGTRef
00427     //  fLTRef
00428     //  fQuoteRef
00429     //      These are character refs for the standard char refs, in the
00430     //      output encoding. They are faulted in as required, by transcoding
00431     //      them from fixed Unicode versions.
00432     // -----------------------------------------------------------------------
00433     EscapeFlags                 fEscapeFlags;
00434     XMLCh*                      fOutEncoding;
00435     XMLFormatTarget*            fTarget;
00436     UnRepFlags                  fUnRepFlags;
00437     XMLTranscoder*              fXCoder;
00438     XMLByte                     fTmpBuf[kTmpBufSize + 4];
00439 
00440     XMLByte*                    fAposRef;
00441     unsigned int                fAposLen;
00442     XMLByte*                    fAmpRef;
00443     unsigned int                fAmpLen;
00444     XMLByte*                    fGTRef;
00445     unsigned int                fGTLen;
00446     XMLByte*                    fLTRef;
00447     unsigned int                fLTLen;
00448     XMLByte*                    fQuoteRef;
00449     unsigned int                fQuoteLen;
00450 };
00451 
00452 
00453 class  XMLFormatTarget
00454 {
00455 public:
00456     // -----------------------------------------------------------------------
00457     //  Constructors and Destructor
00458     // -----------------------------------------------------------------------
00459     virtual ~XMLFormatTarget() {}
00460 
00461 
00462     // -----------------------------------------------------------------------
00463     //  Virtual interface
00464     // -----------------------------------------------------------------------
00465     virtual void writeChars
00466     (
00467         const   XMLByte* const      toWrite
00468         , const unsigned int        count
00469         ,       XMLFormatter* const formatter
00470     ) = 0;
00471 
00472 
00473 protected :
00474     // -----------------------------------------------------------------------
00475     //  Hidden constructors and operators
00476     // -----------------------------------------------------------------------
00477     XMLFormatTarget() {}
00478     XMLFormatTarget(const XMLFormatTarget&) {}
00479     void operator=(const XMLFormatTarget&) {}
00480 };
00481 
00482 
00483 // ---------------------------------------------------------------------------
00484 //  XMLFormatter: Getter methods
00485 // ---------------------------------------------------------------------------
00486 inline const XMLCh* XMLFormatter::getEncodingName() const
00487 {
00488     return fOutEncoding;
00489 }
00490 
00491 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
00492 {
00493     return fXCoder;
00494 }
00495 
00496 // ---------------------------------------------------------------------------
00497 //  XMLFormatter: Setter methods
00498 // ---------------------------------------------------------------------------
00499 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
00500 {
00501     fEscapeFlags = newFlags;
00502 }
00503 
00504 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
00505 {
00506     fUnRepFlags = newFlags;
00507 }
00508 
00509 
00510 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
00511 {
00512     fEscapeFlags = newFlags;
00513     return *this;
00514 }
00515 
00516 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
00517 {
00518     fUnRepFlags = newFlags;
00519     return *this;
00520 }
00521 
00522 
00523 #endif


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