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 #include "xml_smart_ptr.h"
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 char * f, const char * 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);
00063         if( fail() || !is_open() )
00064                 HR_THROW(E_INVALID_FILENAME);
00065 
00066     *m_pFormatter << gXMLDecl1 << m_pFormatter->getEncodingName() << gXMLDecl2; //will dump '<?xml version="1.0" encoding="UTF-8"?> 
00067 }
00068 
00069 
00070 void Transcoder::finalize()
00071 {
00072         delete m_pFormatter;
00073         m_pFormatter = 0;
00074 
00075         close();
00076 
00077         XMLPlatformUtils::Terminate();
00078 }
00079 
00080 
00081 Transcoder& 
00082 Transcoder::operator <<( Modes mode)
00083 {
00084     if ( mode == NoEscape)
00085                 *m_pFormatter << XMLFormatter::NoEscapes;
00086         else if ( mode == StdEscape)
00087                 *m_pFormatter << XMLFormatter::StdEscapes;
00088 
00089         return *this;
00090 }
00091 
00092 
00093 Transcoder& 
00094 Transcoder::operator <<( metaid_type toWrite)
00095 {
00096         std::ofstream::operator<<( toWrite);
00097 
00098         return *this;
00099 }
00100 
00101 Transcoder& 
00102 Transcoder::operator <<( const XMLCh* const toWrite)
00103 {
00104         *m_pFormatter << toWrite;
00105         return *this;
00106 }
00107 
00108 Transcoder& 
00109 Transcoder::operator <<( const char * const toWrite)
00110 {
00111     smart_XMLCh fUnicodeForm = XMLString::transcode( toWrite);
00112         
00113         Transcoder::operator<<(static_cast<const XMLCh*>(fUnicodeForm));
00114 
00115         return *this;
00116 }
00117 
00118 Transcoder& 
00119 Transcoder::operator <<( const char toWrite)
00120 {
00121         char tmp[2] = { toWrite, 0 };
00122 
00123         Transcoder::operator<<( tmp);
00124 
00125         return *this;
00126 }
00127 
00128 Transcoder& 
00129 Transcoder::operator <<( const std::string& toWrite)
00130 {
00131         Transcoder::operator<<( toWrite.c_str());
00132 
00133         return *this;
00134 }
00135 
00136 // ---------------------------------------------------------------------------
00137 //  Transcoder: Overrides of the output formatter target interface
00138 // ---------------------------------------------------------------------------
00139 void Transcoder::writeChars(const   XMLByte* const  toWrite, const XMLSize_t count, XMLFormatter* const   formatter)
00140 {
00141         ASSERT( sizeof( XMLByte) == sizeof( char));
00142         write( (const char * const)toWrite, count);//zolmol: cast from const unsigned char * const
00143 }
00144 
00145 bool Transcoder::isOpen()
00146 {
00147         return is_open();
00148 }