GME  13
Transcoder.cpp
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 
00003 #include "Transcoder.h"
00004 #include <xercesc/util/XMLUniDefs.hpp>
00005 
00006 
00007 static const XMLCh  gXMLDecl1[] =
00008 {
00009         chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
00010     ,   chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
00011     ,   chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
00012     ,   chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
00013     ,   chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
00014     ,   chDoubleQuote, chNull
00015 }; // = `<?xml version="1.0" encoding="` 
00016 
00017 static const XMLCh  gXMLDecl2[] =
00018 {
00019         chDoubleQuote, chQuestion, chCloseAngle
00020     ,   chLF, chNull
00021 }; // = `"?>LF`
00022 
00023 
00024 
00025 
00026 // ---------------------------------------------------------------------------
00027 //  Transcoder: Constructors and Destructor
00028 // ---------------------------------------------------------------------------
00029 Transcoder::Transcoder()
00030         : m_pFormatter(0)
00031 {
00032 }
00033 
00034 Transcoder::~Transcoder()
00035 {
00036         if ( m_pFormatter)
00037         {
00038                 delete m_pFormatter;
00039                 m_pFormatter = 0;
00040         }
00041 
00042         if ( is_open()) close();
00043 }
00044 
00045 void Transcoder::init( const TCHAR * f, const TCHAR * const encodingName)
00046 {
00047         ASSERT( !m_pFormatter);
00048 
00049         XMLPlatformUtils::Initialize();
00050         
00051         m_pFormatter = new XMLFormatter
00052     (
00053         encodingName
00054         , 0
00055         , this
00056         , XMLFormatter::NoEscapes
00057         , XMLFormatter::UnRep_CharRef
00058     );
00059 
00060         ASSERT( !is_open() );
00061 
00062         open(f, std::ios::out | std::ios::trunc | std::ios::binary);
00063         if( fail() || !is_open() )
00064                 HR_THROW(E_INVALID_FILENAME);
00065 
00066         // write BOM
00067         if (_tcsicmp(_T("UTF-16"), encodingName) == 0)
00068         {
00069                 *m_pFormatter << (XMLCh) 0xFEFF;
00070         }
00071     *m_pFormatter << gXMLDecl1 << m_pFormatter->getEncodingName() << gXMLDecl2; //will dump '<?xml version="1.0" encoding="UTF-8"?> 
00072 }
00073 
00074 
00075 void Transcoder::finalize()
00076 {
00077         delete m_pFormatter;
00078         m_pFormatter = 0;
00079 
00080         close();
00081 
00082         XMLPlatformUtils::Terminate();
00083 }
00084 
00085 
00086 Transcoder& 
00087 Transcoder::operator <<( Modes mode)
00088 {
00089     if ( mode == NoEscape)
00090                 *m_pFormatter << XMLFormatter::NoEscapes;
00091         else if ( mode == StdEscape)
00092                 *m_pFormatter << XMLFormatter::StdEscapes;
00093 
00094         return *this;
00095 }
00096 
00097 
00098 Transcoder& 
00099 Transcoder::operator <<( const XMLCh* const toWrite)
00100 {
00101         *m_pFormatter << toWrite;
00102         return *this;
00103 }
00104 
00105 Transcoder& 
00106 Transcoder::operator <<( const char * const toWrite)
00107 {
00108     XMLCh * fUnicodeForm = XMLString::transcode( toWrite);
00109         
00110         operator<<( fUnicodeForm);
00111         
00112         XMLString::release(&fUnicodeForm);
00113 
00114         return *this;
00115 }
00116 
00117 Transcoder& 
00118 Transcoder::operator <<( const char toWrite)
00119 {
00120         wchar_t tmp[2] = { toWrite, 0 };
00121 
00122         operator<<( tmp);
00123 
00124         return *this;
00125 }
00126 
00127 Transcoder& 
00128 Transcoder::operator <<( const std::tstring& toWrite)
00129 {
00130         operator<<( toWrite.c_str());
00131 
00132         return *this;
00133 }
00134 
00135 // ---------------------------------------------------------------------------
00136 //  Transcoder: Overrides of the output formatter target interface
00137 // ---------------------------------------------------------------------------
00138 void Transcoder::writeChars(const   XMLByte* const  toWrite, const XMLSize_t count, XMLFormatter* const   formatter)
00139 {
00140         ASSERT( sizeof( XMLByte) == sizeof( char));
00141         write( (const char * const)toWrite, count);//zolmol: cast from const unsigned char * const
00142 }