XMLFormatter.hpp

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: XMLFormatter.hpp 568078 2007-08-21 11:43:25Z amassari $
00020  */
00021 
00022 #if !defined(XMLFORMATTER_HPP)
00023 #define XMLFORMATTER_HPP
00024 
00025 #include <xercesc/util/PlatformUtils.hpp>
00026 
00027 XERCES_CPP_NAMESPACE_BEGIN
00028 
00029 class XMLFormatTarget;
00030 class XMLTranscoder;
00031 
00041 class XMLPARSER_EXPORT XMLFormatter : public XMemory
00042 {
00043 public:
00044     // -----------------------------------------------------------------------
00045     //  Class types
00046     // -----------------------------------------------------------------------
00124     enum EscapeFlags
00125     {
00126         NoEscapes
00127         , StdEscapes
00128         , AttrEscapes
00129         , CharEscapes
00130 
00131         // Special values, don't use directly
00132         , EscapeFlags_Count
00133         , DefaultEscape     = 999
00134     };
00135 
00152     enum UnRepFlags
00153     {
00154         UnRep_Fail
00155         , UnRep_CharRef
00156         , UnRep_Replace
00157 
00158         , DefaultUnRep      = 999
00159     };
00161 
00162 
00163     // -----------------------------------------------------------------------
00164     //  Constructors and Destructor
00165     // -----------------------------------------------------------------------
00177     XMLFormatter
00178     (
00179         const   XMLCh* const            outEncoding
00180         , const XMLCh* const            docVersion
00181         ,       XMLFormatTarget* const  target
00182         , const EscapeFlags             escapeFlags = NoEscapes
00183         , const UnRepFlags              unrepFlags = UnRep_Fail
00184         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00185     );
00186 
00187     XMLFormatter
00188     (
00189         const   char* const             outEncoding
00190         , const char* const             docVersion
00191         ,       XMLFormatTarget* const  target
00192         , const EscapeFlags             escapeFlags = NoEscapes
00193         , const UnRepFlags              unrepFlags = UnRep_Fail
00194         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00195     );
00196 
00197     XMLFormatter
00198     (
00199         const   XMLCh* const            outEncoding
00200         ,       XMLFormatTarget* const  target
00201         , const EscapeFlags             escapeFlags = NoEscapes
00202         , const UnRepFlags              unrepFlags = UnRep_Fail
00203         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00204     );
00205 
00206     XMLFormatter
00207     (
00208         const   char* const             outEncoding
00209         ,       XMLFormatTarget* const  target
00210         , const EscapeFlags             escapeFlags = NoEscapes
00211         , const UnRepFlags              unrepFlags = UnRep_Fail
00212         ,       MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
00213     );
00214     
00215     ~XMLFormatter();
00217 
00218 
00219     // -----------------------------------------------------------------------
00220     //  Formatting methods
00221     // -----------------------------------------------------------------------
00231     void formatBuf
00232     (
00233         const   XMLCh* const    toFormat
00234         , const unsigned int    count
00235         , const EscapeFlags     escapeFlags = DefaultEscape
00236         , const UnRepFlags      unrepFlags = DefaultUnRep
00237     );
00238 
00242     XMLFormatter& operator<<
00243     (
00244         const   XMLCh* const    toFormat
00245     );
00246 
00247     XMLFormatter& operator<<
00248     (
00249         const   XMLCh           toFormat
00250     );
00251 
00252     void writeBOM(const XMLByte* const toFormat
00253                 , const unsigned int   count);
00254 
00256 
00257     // -----------------------------------------------------------------------
00258     //  Getter methods
00259     // -----------------------------------------------------------------------
00266     const XMLCh* getEncodingName() const;
00267 
00271     inline const XMLTranscoder*   getTranscoder() const;
00272 
00274 
00275     // -----------------------------------------------------------------------
00276     //  Setter methods
00277     // -----------------------------------------------------------------------
00283     void setEscapeFlags
00284     (
00285         const   EscapeFlags     newFlags
00286     );
00287 
00291     void setUnRepFlags
00292     (
00293         const   UnRepFlags      newFlags
00294     );
00295 
00300     XMLFormatter& operator<<
00301     (
00302         const   EscapeFlags     newFlags
00303     );
00304 
00309     XMLFormatter& operator<<
00310     (
00311         const   UnRepFlags      newFlags
00312     );
00314 
00315 
00316     // -----------------------------------------------------------------------
00317     //  Getter methods
00318     // -----------------------------------------------------------------------
00324     EscapeFlags getEscapeFlags() const;
00325 
00329     UnRepFlags getUnRepFlags() const;
00331 
00332 private :
00333     // -----------------------------------------------------------------------
00334     //  Unimplemented constructors and operators
00335     // -----------------------------------------------------------------------
00336     XMLFormatter();
00337     XMLFormatter(const XMLFormatter&);
00338     XMLFormatter& operator=(const XMLFormatter&);
00339 
00340 
00341     // -----------------------------------------------------------------------
00342     //  Private class constants
00343     // -----------------------------------------------------------------------
00344     enum Constants
00345     {
00346         kTmpBufSize     = 16 * 1024
00347     };
00348 
00349 
00350     // -----------------------------------------------------------------------
00351     //  Private helper methods
00352     // -----------------------------------------------------------------------
00353     const XMLByte* getCharRef(unsigned int & count, 
00354                               XMLByte*      &ref, 
00355                               const XMLCh *  stdRef);  
00356  
00357     void writeCharRef(const XMLCh &toWrite);
00358     void writeCharRef(unsigned long toWrite);
00359 
00360     bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
00361                     , const XMLCh                     toCheck);
00362                               
00363 
00364     unsigned int handleUnEscapedChars(const XMLCh *                  srcPtr, 
00365                                       const unsigned int             count, 
00366                                       const UnRepFlags               unrepFlags);
00367 
00368     void specialFormat
00369     (
00370         const   XMLCh* const    toFormat
00371         , const unsigned int    count
00372         , const EscapeFlags     escapeFlags
00373     );
00374 
00375 
00376     // -----------------------------------------------------------------------
00377     //  Private, non-virtual methods
00378     //
00379     //  fEscapeFlags
00380     //      The escape flags we were told to use in formatting. These are
00381     //      defaults set in the ctor, which can be overridden on a particular
00382     //      call.
00383     //
00384     //  fOutEncoding
00385     //      This the name of the output encoding. Saved mainly for meaningful
00386     //      error messages.
00387     //
00388     //  fTarget
00389     //      This is the target object for the formatting operation.
00390     //
00391     //  fUnRepFlags
00392     //      The unrepresentable flags that indicate how to react when a
00393     //      character cannot be represented in the target encoding.
00394     //
00395     //  fXCoder
00396     //      This the transcoder that we will use. It is created using the
00397     //      encoding name we were told to use.
00398     //
00399     //  fTmpBuf
00400     //      An output buffer that we use to transcode chars into before we
00401     //      send them off to be output.
00402     //
00403     //  fAposRef
00404     //  fAmpRef
00405     //  fGTRef
00406     //  fLTRef
00407     //  fQuoteRef
00408     //      These are character refs for the standard char refs, in the
00409     //      output encoding. They are faulted in as required, by transcoding
00410     //      them from fixed Unicode versions.
00411     //
00412     //  fIsXML11
00413     //      for performance reason, we do not store the actual version string
00414     //      and do the string comparison again and again.
00415     //
00416     // -----------------------------------------------------------------------
00417     EscapeFlags                 fEscapeFlags;
00418     XMLCh*                      fOutEncoding;
00419     XMLFormatTarget*            fTarget;
00420     UnRepFlags                  fUnRepFlags;
00421     XMLTranscoder*              fXCoder;
00422     XMLByte                     fTmpBuf[kTmpBufSize + 4];
00423     XMLByte*                    fAposRef;
00424     unsigned int                fAposLen;
00425     XMLByte*                    fAmpRef;
00426     unsigned int                fAmpLen;
00427     XMLByte*                    fGTRef;
00428     unsigned int                fGTLen;
00429     XMLByte*                    fLTRef;
00430     unsigned int                fLTLen;
00431     XMLByte*                    fQuoteRef;
00432     unsigned int                fQuoteLen;
00433     bool                        fIsXML11;
00434     MemoryManager*              fMemoryManager;
00435 };
00436 
00437 
00438 class XMLPARSER_EXPORT XMLFormatTarget : public XMemory
00439 {
00440 public:
00441     // -----------------------------------------------------------------------
00442     //  Constructors and Destructor
00443     // -----------------------------------------------------------------------
00444     virtual ~XMLFormatTarget() {}
00445 
00446 
00447     // -----------------------------------------------------------------------
00448     //  Virtual interface
00449     // -----------------------------------------------------------------------
00450     virtual void writeChars
00451     (
00452           const XMLByte* const      toWrite
00453         , const unsigned int        count
00454         ,       XMLFormatter* const formatter
00455     ) = 0;
00456 
00457     virtual void flush() {};
00458 
00459 
00460 protected :
00461     // -----------------------------------------------------------------------
00462     //  Hidden constructors and operators
00463     // -----------------------------------------------------------------------
00464     XMLFormatTarget() {};
00465 
00466 private:
00467     // -----------------------------------------------------------------------
00468     //  Unimplemented constructors and operators
00469     // -----------------------------------------------------------------------
00470     XMLFormatTarget(const XMLFormatTarget&);
00471     XMLFormatTarget& operator=(const XMLFormatTarget&);
00472 };
00473 
00474 
00475 // ---------------------------------------------------------------------------
00476 //  XMLFormatter: Getter methods
00477 // ---------------------------------------------------------------------------
00478 inline const XMLCh* XMLFormatter::getEncodingName() const
00479 {
00480     return fOutEncoding;
00481 }
00482 
00483 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
00484 {
00485     return fXCoder;
00486 }
00487 
00488 // ---------------------------------------------------------------------------
00489 //  XMLFormatter: Setter methods
00490 // ---------------------------------------------------------------------------
00491 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
00492 {
00493     fEscapeFlags = newFlags;
00494 }
00495 
00496 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
00497 {
00498     fUnRepFlags = newFlags;
00499 }
00500 
00501 
00502 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
00503 {
00504     fEscapeFlags = newFlags;
00505     return *this;
00506 }
00507 
00508 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
00509 {
00510     fUnRepFlags = newFlags;
00511     return *this;
00512 }
00513 
00514 // ---------------------------------------------------------------------------
00515 //  XMLFormatter: Getter methods
00516 // ---------------------------------------------------------------------------
00517 inline XMLFormatter::EscapeFlags XMLFormatter::getEscapeFlags() const
00518 {
00519     return fEscapeFlags;
00520 }
00521 
00522 inline XMLFormatter::UnRepFlags XMLFormatter::getUnRepFlags() const
00523 {
00524     return fUnRepFlags;
00525 }
00526 
00527 XERCES_CPP_NAMESPACE_END
00528 
00529 #endif

Generated on Thu Feb 19 11:35:44 2009 for Xerces-C++ by  doxygen 1.5.4