XMLFormatter.hpp
Go to the documentation of this file.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: XMLFormatter.hpp 932889 2010-04-11 13:10:10Z borisk $
*/
#if !defined(XERCESC_INCLUDE_GUARD_XMLFORMATTER_HPP)
#define XERCESC_INCLUDE_GUARD_XMLFORMATTER_HPP
#include <xercesc/util/PlatformUtils.hpp>
XERCES_CPP_NAMESPACE_BEGIN
class XMLFormatTarget;
class XMLTranscoder;
class XMLPARSER_EXPORT XMLFormatter : public XMemory
{
public:
// -----------------------------------------------------------------------
// Class types
// -----------------------------------------------------------------------
enum EscapeFlags
{
NoEscapes
, StdEscapes
, AttrEscapes
, CharEscapes
// Special values, don't use directly
, EscapeFlags_Count
, DefaultEscape = 999
};
enum UnRepFlags
{
UnRep_Fail
, UnRep_CharRef
, UnRep_Replace
, DefaultUnRep = 999
};
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
XMLFormatter
(
const XMLCh* const outEncoding
, const XMLCh* const docVersion
, XMLFormatTarget* const target
, const EscapeFlags escapeFlags = NoEscapes
, const UnRepFlags unrepFlags = UnRep_Fail
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
XMLFormatter
(
const char* const outEncoding
, const char* const docVersion
, XMLFormatTarget* const target
, const EscapeFlags escapeFlags = NoEscapes
, const UnRepFlags unrepFlags = UnRep_Fail
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
XMLFormatter
(
const XMLCh* const outEncoding
, XMLFormatTarget* const target
, const EscapeFlags escapeFlags = NoEscapes
, const UnRepFlags unrepFlags = UnRep_Fail
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
XMLFormatter
(
const char* const outEncoding
, XMLFormatTarget* const target
, const EscapeFlags escapeFlags = NoEscapes
, const UnRepFlags unrepFlags = UnRep_Fail
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
~XMLFormatter();
// -----------------------------------------------------------------------
// Formatting methods
// -----------------------------------------------------------------------
void formatBuf
(
const XMLCh* const toFormat
, const XMLSize_t count
, const EscapeFlags escapeFlags = DefaultEscape
, const UnRepFlags unrepFlags = DefaultUnRep
);
XMLFormatter& operator<<
(
const XMLCh* const toFormat
);
XMLFormatter& operator<<
(
const XMLCh toFormat
);
void writeBOM(const XMLByte* const toFormat
, const XMLSize_t count);
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
const XMLCh* getEncodingName() const;
inline const XMLTranscoder* getTranscoder() const;
inline XMLTranscoder* getTranscoder();
// -----------------------------------------------------------------------
// Setter methods
// -----------------------------------------------------------------------
void setEscapeFlags
(
const EscapeFlags newFlags
);
void setUnRepFlags
(
const UnRepFlags newFlags
);
XMLFormatter& operator<<
(
const EscapeFlags newFlags
);
XMLFormatter& operator<<
(
const UnRepFlags newFlags
);
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
EscapeFlags getEscapeFlags() const;
UnRepFlags getUnRepFlags() const;
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
XMLFormatter();
XMLFormatter(const XMLFormatter&);
XMLFormatter& operator=(const XMLFormatter&);
// -----------------------------------------------------------------------
// Private class constants
// -----------------------------------------------------------------------
enum Constants
{
kTmpBufSize = 16 * 1024
};
// -----------------------------------------------------------------------
// Private helper methods
// -----------------------------------------------------------------------
const XMLByte* getCharRef(XMLSize_t &count,
XMLByte* &ref,
const XMLCh * stdRef);
void writeCharRef(const XMLCh &toWrite);
void writeCharRef(XMLSize_t toWrite);
bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
, const XMLCh toCheck);
XMLSize_t handleUnEscapedChars(const XMLCh * srcPtr,
const XMLSize_t count,
const UnRepFlags unrepFlags);
void specialFormat
(
const XMLCh* const toFormat
, const XMLSize_t count
, const EscapeFlags escapeFlags
);
// -----------------------------------------------------------------------
// Private, non-virtual methods
//
// fEscapeFlags
// The escape flags we were told to use in formatting. These are
// defaults set in the ctor, which can be overridden on a particular
// call.
//
// fOutEncoding
// This the name of the output encoding. Saved mainly for meaningful
// error messages.
//
// fTarget
// This is the target object for the formatting operation.
//
// fUnRepFlags
// The unrepresentable flags that indicate how to react when a
// character cannot be represented in the target encoding.
//
// fXCoder
// This the transcoder that we will use. It is created using the
// encoding name we were told to use.
//
// fTmpBuf
// An output buffer that we use to transcode chars into before we
// send them off to be output.
//
// fAposRef
// fAmpRef
// fGTRef
// fLTRef
// fQuoteRef
// These are character refs for the standard char refs, in the
// output encoding. They are faulted in as required, by transcoding
// them from fixed Unicode versions.
//
// fIsXML11
// for performance reason, we do not store the actual version string
// and do the string comparison again and again.
//
// -----------------------------------------------------------------------
EscapeFlags fEscapeFlags;
XMLCh* fOutEncoding;
XMLFormatTarget* fTarget;
UnRepFlags fUnRepFlags;
XMLTranscoder* fXCoder;
XMLByte fTmpBuf[kTmpBufSize + 4];
XMLByte* fAposRef;
XMLSize_t fAposLen;
XMLByte* fAmpRef;
XMLSize_t fAmpLen;
XMLByte* fGTRef;
XMLSize_t fGTLen;
XMLByte* fLTRef;
XMLSize_t fLTLen;
XMLByte* fQuoteRef;
XMLSize_t fQuoteLen;
bool fIsXML11;
MemoryManager* fMemoryManager;
};
class XMLPARSER_EXPORT XMLFormatTarget : public XMemory
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
virtual ~XMLFormatTarget() {}
// -----------------------------------------------------------------------
// Virtual interface
// -----------------------------------------------------------------------
virtual void writeChars
(
const XMLByte* const toWrite
, const XMLSize_t count
, XMLFormatter* const formatter
) = 0;
virtual void flush() {};
protected :
// -----------------------------------------------------------------------
// Hidden constructors and operators
// -----------------------------------------------------------------------
XMLFormatTarget() {};
private:
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
XMLFormatTarget(const XMLFormatTarget&);
XMLFormatTarget& operator=(const XMLFormatTarget&);
};
// ---------------------------------------------------------------------------
// XMLFormatter: Getter methods
// ---------------------------------------------------------------------------
inline const XMLCh* XMLFormatter::getEncodingName() const
{
return fOutEncoding;
}
inline const XMLTranscoder* XMLFormatter::getTranscoder() const
{
return fXCoder;
}
inline XMLTranscoder* XMLFormatter::getTranscoder()
{
return fXCoder;
}
// ---------------------------------------------------------------------------
// XMLFormatter: Setter methods
// ---------------------------------------------------------------------------
inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
{
fEscapeFlags = newFlags;
}
inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
{
fUnRepFlags = newFlags;
}
inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
{
fEscapeFlags = newFlags;
return *this;
}
inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
{
fUnRepFlags = newFlags;
return *this;
}
// ---------------------------------------------------------------------------
// XMLFormatter: Getter methods
// ---------------------------------------------------------------------------
inline XMLFormatter::EscapeFlags XMLFormatter::getEscapeFlags() const
{
return fEscapeFlags;
}
inline XMLFormatter::UnRepFlags XMLFormatter::getUnRepFlags() const
{
return fUnRepFlags;
}
XERCES_CPP_NAMESPACE_END
#endif
XMLFormatTarget::flush
virtual void flush()
Definition: XMLFormatter.hpp:461
XMLFormatter::setEscapeFlags
void setEscapeFlags(const EscapeFlags newFlags)
Definition: XMLFormatter.hpp:500
XMemory
This class makes it possible to override the C++ memory management by adding new/delete operators to ...
Definition: XMemory.hpp:40
XMLPlatformUtils::fgMemoryManager
static MemoryManager * fgMemoryManager
The configurable memory manager.
Definition: PlatformUtils.hpp:121
XMLFormatter::operator<<
XMLFormatter & operator<<(const XMLCh *const toFormat)
XMLFormatTarget::~XMLFormatTarget
virtual ~XMLFormatTarget()
Definition: XMLFormatter.hpp:448
XMLFormatter::getEncodingName
const XMLCh * getEncodingName() const
Definition: XMLFormatter.hpp:482
XMLFormatter::setUnRepFlags
void setUnRepFlags(const UnRepFlags newFlags)
Definition: XMLFormatter.hpp:505
XMLFormatter
This class provides the basic formatting capabilities that are required to turn the Unicode based XML...
Definition: XMLFormatter.hpp:41
XMLFormatter::getEscapeFlags
EscapeFlags getEscapeFlags() const
Definition: XMLFormatter.hpp:526
XMLFormatTarget::XMLFormatTarget
XMLFormatTarget()
Definition: XMLFormatter.hpp:468
XMLPARSER_EXPORT
#define XMLPARSER_EXPORT
Definition: XercesDefs.hpp:163
XMLTranscoder
XMLTranscoder is for transcoding non-local code page encodings, i.e.
Definition: TransService.hpp:215
PlatformUtils.hpp
MemoryManager
Configurable memory manager.
Definition: MemoryManager.hpp:39
XMLCh
uint16_t XMLCh
Definition: Xerces_autoconf_config.hpp:118
XERCES_CPP_NAMESPACE_BEGIN
#define XERCES_CPP_NAMESPACE_BEGIN
Definition: XercesDefs.hpp:112
XMLFormatter::getUnRepFlags
UnRepFlags getUnRepFlags() const
Definition: XMLFormatter.hpp:531
XMLFormatter::EscapeFlags
EscapeFlags
EscapeFlags - Different styles of escape flags to control various formatting.
Definition: XMLFormatter.hpp:124
XMLByte
unsigned char XMLByte
Definition: XercesDefs.hpp:65
XMLSize_t
size_t XMLSize_t
Definition: Xerces_autoconf_config.hpp:110
XERCES_CPP_NAMESPACE_END
#define XERCES_CPP_NAMESPACE_END
Definition: XercesDefs.hpp:113
XMLFormatter::UnRepFlags
UnRepFlags
UnRepFlags.
Definition: XMLFormatter.hpp:152
XMLFormatTarget
Definition: XMLFormatter.hpp:442
XMLFormatter::getTranscoder
const XMLTranscoder * getTranscoder() const
Definition: XMLFormatter.hpp:487