| 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  2003/01/31 00:30:48  jberry
    00060  * Syntax error in declaration
    00061  *
    00062  * Revision 1.6  2003/01/28 18:32:33  peiyongz
    00063  * Bug#13694: Allow Xerces to write the BOM to XML files
    00064  *
    00065  * Revision 1.5  2003/01/24 20:20:22  tng
    00066  * Add method flush to XMLFormatTarget
    00067  *
    00068  * Revision 1.4  2002/11/04 15:00:21  tng
    00069  * C++ Namespace Support.
    00070  *
    00071  * Revision 1.3  2002/07/30 16:29:16  tng
    00072  * [Bug 8550] No explanation of XMLFormatter escape options.
    00073  *
    00074  * Revision 1.2  2002/06/21 19:31:23  peiyongz
    00075  * getTranscoder() added;
    00076  *
    00077  * Revision 1.1.1.1  2002/02/01 22:21:52  peiyongz
    00078  * sane_include
    00079  *
    00080  * Revision 1.7  2000/10/17 19:25:38  andyh
    00081  * XMLFormatTarget, removed version of writeChars with no length.  Can not be
    00082  * safely used, and obscured other errors.
    00083  *
    00084  * Revision 1.6  2000/10/10 23:54:58  andyh
    00085  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
    00086  * output to multi-byte encodings.
    00087  *
    00088  * Revision 1.5  2000/04/07 01:01:56  roddey
    00089  * Fixed an error message so that it indicated the correct radix for the rep
    00090  * token. Get all of the basic output formatting functionality in place for
    00091  * at least ICU and Win32 transcoders.
    00092  *
    00093  * Revision 1.4  2000/04/06 23:50:38  roddey
    00094  * Now the low level formatter handles doing char refs for
    00095  * unrepresentable chars (in addition to the replacement char style
    00096  * already done.)
    00097  *
    00098  * Revision 1.3  2000/04/06 19:09:21  roddey
    00099  * Some more improvements to output formatting. Now it will correctly
    00100  * handle doing the 'replacement char' style of dealing with chars
    00101  * that are unrepresentable.
    00102  *
    00103  * Revision 1.2  2000/04/05 00:20:16  roddey
    00104  * More updates for the low level formatted output support
    00105  *
    00106  * Revision 1.1  2000/03/28 19:43:17  roddey
    00107  * Fixes for signed/unsigned warnings. New work for two way transcoding
    00108  * stuff.
    00109  *
    00110  */
    00111
    00112 #if !defined(XMLFORMATTER_HPP)
    00113 #define XMLFORMATTER_HPP
    00114
    00115 #include <xercesc/util/XercesDefs.hpp>
    00116
    00117 XERCES_CPP_NAMESPACE_BEGIN
    00118
    00119 class XMLFormatTarget;
    00120 class XMLTranscoder;
    00121
    00131 class  XMLFormatter
    00132 {
    00133 public:
    00134     // -----------------------------------------------------------------------
    00135     //  Class types
    00136     // -----------------------------------------------------------------------
    00214     enum EscapeFlags
    00215     {
    00216         NoEscapes
    00217         , StdEscapes
    00218         , AttrEscapes
    00219         , CharEscapes
    00220
    00221         // Special values, don't use directly
    00222         , EscapeFlags_Count
    00223         , DefaultEscape     = 999
    00224     };
    00225
    00242     enum UnRepFlags
    00243     {
    00244         UnRep_Fail
    00245         , UnRep_CharRef
    00246         , UnRep_Replace
    00247
    00248         , DefaultUnRep      = 999
    00249     };
    00251
    00252
    00253     // -----------------------------------------------------------------------
    00254     //  Constructors and Destructor
    00255     // -----------------------------------------------------------------------
    00264     XMLFormatter
    00265     (
    00266         const   XMLCh* const            outEncoding
    00267         ,       XMLFormatTarget* const  target
    00268         , const EscapeFlags             escapeFlags = NoEscapes
    00269         , const UnRepFlags              unrepFlags = UnRep_Fail
    00270     );
    00271
    00272     XMLFormatter
    00273     (
    00274         const   char* const             outEncoding
    00275         ,       XMLFormatTarget* const  target
    00276         , const EscapeFlags             escapeFlags = NoEscapes
    00277         , const UnRepFlags              unrepFlags = UnRep_Fail
    00278     );
    00279
    00280     ~XMLFormatter();
    00282
    00283
    00284     // -----------------------------------------------------------------------
    00285     //  Formatting methods
    00286     // -----------------------------------------------------------------------
    00296     void formatBuf
    00297     (
    00298         const   XMLCh* const    toFormat
    00299         , const unsigned int    count
    00300         , const EscapeFlags     escapeFlags = DefaultEscape
    00301         , const UnRepFlags      unrepFlags = DefaultUnRep
    00302     );
    00303
    00307     XMLFormatter& operator<<
    00308     (
    00309         const   XMLCh* const    toFormat
    00310     );
    00311
    00312     XMLFormatter& operator<<
    00313     (
    00314         const   XMLCh           toFormat
    00315     );
    00316
    00317     void writeBOM(const XMLByte* const toFormat
    00318                 , const unsigned int   count);
    00319
    00321
    00322     // -----------------------------------------------------------------------
    00323     //  Getter methods
    00324     // -----------------------------------------------------------------------
    00331     const XMLCh* getEncodingName() const;
    00332
    00336     inline const XMLTranscoder*   getTranscoder() const;
    00338
    00339     // -----------------------------------------------------------------------
    00340     //  Setter methods
    00341     // -----------------------------------------------------------------------
    00347     void setEscapeFlags
    00348     (
    00349         const   EscapeFlags     newFlags
    00350     );
    00351
    00355     void setUnRepFlags
    00356     (
    00357         const   UnRepFlags      newFlags
    00358     );
    00359
    00364     XMLFormatter& operator<<
    00365     (
    00366         const   EscapeFlags     newFlags
    00367     );
    00368
    00373     XMLFormatter& operator<<
    00374     (
    00375         const   UnRepFlags      newFlags
    00376     );
    00378
    00379
    00380 private :
    00381     // -----------------------------------------------------------------------
    00382     //  Unimplemented constructors and operators
    00383     // -----------------------------------------------------------------------
    00384     XMLFormatter();
    00385     XMLFormatter(const XMLFormatter&);
    00386     void operator=(const XMLFormatter&);
    00387
    00388
    00389     // -----------------------------------------------------------------------
    00390     //  Private class constants
    00391     // -----------------------------------------------------------------------
    00392     enum Constants
    00393     {
    00394         kTmpBufSize     = 16 * 1024
    00395     };
    00396
    00397
    00398     // -----------------------------------------------------------------------
    00399     //  Private helper methods
    00400     // -----------------------------------------------------------------------
    00401     const XMLByte* getAposRef(unsigned int & count);
    00402     const XMLByte* getAmpRef(unsigned int & count);
    00403     const XMLByte* getGTRef(unsigned int & count);
    00404     const XMLByte* getLTRef(unsigned int & count);
    00405     const XMLByte* getQuoteRef(unsigned int & count);
    00406
    00407     void specialFormat
    00408     (
    00409         const   XMLCh* const    toFormat
    00410         , const unsigned int    count
    00411         , const EscapeFlags     escapeFlags
    00412     );
    00413
    00414
    00415     // -----------------------------------------------------------------------
    00416     //  Private, non-virtual methods
    00417     //
    00418     //  fEscapeFlags
    00419     //      The escape flags we were told to use in formatting. These are
    00420     //      defaults set in the ctor, which can be overridden on a particular
    00421     //      call.
    00422     //
    00423     //  fOutEncoding
    00424     //      This the name of the output encoding. Saved mainly for meaningful
    00425     //      error messages.
    00426     //
    00427     //  fTarget
    00428     //      This is the target object for the formatting operation.
    00429     //
    00430     //  fUnRepFlags
    00431     //      The unrepresentable flags that indicate how to react when a
    00432     //      character cannot be represented in the target encoding.
    00433     //
    00434     //  fXCoder
    00435     //      This the transcoder that we will use. It is created using the
    00436     //      encoding name we were told to use.
    00437     //
    00438     //  fTmpBuf
    00439     //      An output buffer that we use to transcode chars into before we
    00440     //      send them off to be output.
    00441     //
    00442     //  fAposRef
    00443     //  fAmpRef
    00444     //  fGTRef
    00445     //  fLTRef
    00446     //  fQuoteRef
    00447     //      These are character refs for the standard char refs, in the
    00448     //      output encoding. They are faulted in as required, by transcoding
    00449     //      them from fixed Unicode versions.
    00450     // -----------------------------------------------------------------------
    00451     EscapeFlags                 fEscapeFlags;
    00452     XMLCh*                      fOutEncoding;
    00453     XMLFormatTarget*            fTarget;
    00454     UnRepFlags                  fUnRepFlags;
    00455     XMLTranscoder*              fXCoder;
    00456     XMLByte                     fTmpBuf[kTmpBufSize + 4];
    00457
    00458     XMLByte*                    fAposRef;
    00459     unsigned int                fAposLen;
    00460     XMLByte*                    fAmpRef;
    00461     unsigned int                fAmpLen;
    00462     XMLByte*                    fGTRef;
    00463     unsigned int                fGTLen;
    00464     XMLByte*                    fLTRef;
    00465     unsigned int                fLTLen;
    00466     XMLByte*                    fQuoteRef;
    00467     unsigned int                fQuoteLen;
    00468 };
    00469
    00470
    00471 class  XMLFormatTarget
    00472 {
    00473 public:
    00474     // -----------------------------------------------------------------------
    00475     //  Constructors and Destructor
    00476     // -----------------------------------------------------------------------
    00477     virtual ~XMLFormatTarget() {}
    00478
    00479
    00480     // -----------------------------------------------------------------------
    00481     //  Virtual interface
    00482     // -----------------------------------------------------------------------
    00483     virtual void writeChars
    00484     (
    00485         const   XMLByte* const      toWrite
    00486         , const unsigned int        count
    00487         ,       XMLFormatter* const formatter
    00488     ) = 0;
    00489
    00490     virtual void flush() {};
    00491
    00492
    00493 protected :
    00494     // -----------------------------------------------------------------------
    00495     //  Hidden constructors and operators
    00496     // -----------------------------------------------------------------------
    00497     XMLFormatTarget() {}
    00498     XMLFormatTarget(const XMLFormatTarget&) {}
    00499     void operator=(const XMLFormatTarget&) {}
    00500 };
    00501
    00502
    00503 // ---------------------------------------------------------------------------
    00504 //  XMLFormatter: Getter methods
    00505 // ---------------------------------------------------------------------------
    00506 inline const XMLCh* XMLFormatter::getEncodingName() const
    00507 {
    00508     return fOutEncoding;
    00509 }
    00510
    00511 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
    00512 {
    00513     return fXCoder;
    00514 }
    00515
    00516 // ---------------------------------------------------------------------------
    00517 //  XMLFormatter: Setter methods
    00518 // ---------------------------------------------------------------------------
    00519 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
    00520 {
    00521     fEscapeFlags = newFlags;
    00522 }
    00523
    00524 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
    00525 {
    00526     fUnRepFlags = newFlags;
    00527 }
    00528
    00529
    00530 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
    00531 {
    00532     fEscapeFlags = newFlags;
    00533     return *this;
    00534 }
    00535
    00536 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
    00537 {
    00538     fUnRepFlags = newFlags;
    00539     return *this;
    00540 }
    00541
    00542 XERCES_CPP_NAMESPACE_END
    00543
    00544 #endif

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