Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

sax2formatter.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //-----------------------------------------------------------------------------
00006 //  SAX2Formatter.HPP
00007 //----------------------------------------------------------------------------- 
00013 //_____________________________________________________________________________
00014 //
00015 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00016 //
00017 //  This file is part of the XDFLengine project.
00018 //
00019 //  The XDFLengine is free software; you can redistribute it and/or modify
00020 //  it under the terms of the GNU General Public License as published by
00021 //  the Free Software Foundation; either version 2 of the License, or
00022 //  (at your option) any later version.
00023 //
00024 //  This program is distributed in the hope that it will be useful,
00025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 //  GNU General Public License for more details.
00028 //
00029 //  You should have received a copy of the GNU General Public License
00030 //  along with this program; if not, write to the Free Software
00031 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00032 //  USA.
00033 //
00034 //  For more information, 
00035 //      contact  : guillaume@baurand.net 
00036 //      or visit : http://xdflengine.sourceforge.net
00037 //
00038 //============================================================================= 
00039 
00040 // ---------------------------------------------------------------------------
00041 //  Includes
00042 // ---------------------------------------------------------------------------
00043 #include "sax2formatter.hpp"
00044 
00045 
00046 
00047 #include <xercesc/util/XMLUniDefs.hpp>
00048 #include <xercesc/sax2/Attributes.hpp>
00049 
00050 
00051 BEGIN_XDFLENGINE_NS
00052 
00053 
00054 // ---------------------------------------------------------------------------
00055 //  Local const data
00056 //
00057 //  Note: This is the 'safe' way to do these strings. If you compiler supports
00058 //        L"" style strings, and portability is not a concern, you can use
00059 //        those types constants directly.
00060 // ---------------------------------------------------------------------------
00061 
00062 
00063 static const XMLCh  gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
00064 static const XMLCh  gEndPI[] = { chQuestion, chCloseAngle, chNull };
00065 static const XMLCh  gStartPI[] = { chOpenAngle, chQuestion, chNull };
00066 static const XMLCh  gXMLDecl1[] =
00067 {
00068         chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
00069     ,   chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
00070     ,   chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
00071     ,   chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
00072     ,   chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
00073     ,   chDoubleQuote, chNull
00074 };
00075 
00076 static const XMLCh  gXMLDecl2[] =
00077 {
00078         chDoubleQuote, chQuestion, chCloseAngle
00079     ,   chLF, chNull
00080 };
00081 
00082 
00083 
00084 
00085 //_____________________________________________________________________
00086 //  SAX2Formatter
00087 //---------------------------------------------------------------------
00088 SAX2Formatter::SAX2Formatter( const   char* const  encodingName , const XMLFormatter::UnRepFlags unRepFlags , const bool expandNamespaces , XMLStreamConsumer* outStreamConsumer) :
00089     fFormatter( (const char *)encodingName , (const char *)0, this , XMLFormatter::NoEscapes , unRepFlags ),
00090     fExpandNS ( expandNamespaces )
00091 {
00092     setConsumer(outStreamConsumer);
00093     fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;   
00094 }
00095 
00096 //_____________________________________________________________________
00097 //  ~SAX2Formatter
00098 //---------------------------------------------------------------------
00099 SAX2Formatter::~SAX2Formatter()
00100 {
00101 }
00102 
00103 
00104 //_____________________________________________________________________
00105 //  setConsumer
00106 //---------------------------------------------------------------------
00107 void SAX2Formatter::setConsumer(XMLStreamConsumer* outStreamConsumer)
00108 {
00109     pStreamConsumer = outStreamConsumer;
00110 }
00111 
00112 
00113 //_____________________________________________________________________
00114 //  writeChars
00115 //---------------------------------------------------------------------
00116 void SAX2Formatter::writeChars(const XMLByte* const toWrite, const unsigned int count, XMLFormatter* const formatter)
00117 {
00118     if(pStreamConsumer)
00119     {
00120         pStreamConsumer->writeData( (const char *) toWrite, (unsigned int) count);
00121     }
00122 }
00123 
00124 
00125 
00126 // ---------------------------------------------------------------------------
00127 //  SAX2Formatter: Overrides of the SAX DTDHandler interface
00128 // ---------------------------------------------------------------------------
00129 void SAX2Formatter::unparsedEntityDecl(const     XMLCh* const name
00130                                           , const   XMLCh* const publicId
00131                                           , const   XMLCh* const systemId
00132                                           , const   XMLCh* const notationName)
00133 {
00134     // Not used at this time
00135 }
00136 
00137 
00138 void SAX2Formatter::notationDecl(const   XMLCh* const name
00139                                     , const XMLCh* const publicId
00140                                     , const XMLCh* const systemId)
00141 {
00142     // Not used at this time
00143 }
00144 
00145 
00146 // ---------------------------------------------------------------------------
00147 //  SAX2Formatter: Overrides of the SAX DocumentHandler interface
00148 // ---------------------------------------------------------------------------
00149 void SAX2Formatter::characters(const     XMLCh* const    chars
00150                                   , const   unsigned int    length)
00151 {
00152     fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
00153 }
00154 
00155 
00156 void SAX2Formatter::endDocument()
00157 {
00158 }
00159 
00160 
00161 void SAX2Formatter::endElement(const XMLCh* const uri,
00162                                  const XMLCh* const localname,
00163                                  const XMLCh* const qname)
00164 {
00165     // No escapes are legal here
00166     fFormatter << XMLFormatter::NoEscapes << gEndElement ;
00167     if ( fExpandNS )
00168     {
00169         if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
00170                 fFormatter  << uri << chColon;
00171         fFormatter << localname << chCloseAngle;
00172     }
00173     else
00174         fFormatter << qname << chCloseAngle;
00175 }
00176 
00177 
00178 void SAX2Formatter::ignorableWhitespace( const   XMLCh* const chars
00179                                             ,const  unsigned int length)
00180 {
00181     fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
00182 }
00183 
00184 
00185 void SAX2Formatter::processingInstruction(const  XMLCh* const target
00186                                             , const XMLCh* const data)
00187 {
00188     fFormatter << XMLFormatter::NoEscapes << gStartPI  << target;
00189     if (data)
00190         fFormatter << chSpace << data;
00191     fFormatter << XMLFormatter::NoEscapes << gEndPI;
00192 }
00193 
00194 
00195 void SAX2Formatter::startDocument()
00196 {
00197 }
00198 
00199 
00200 void SAX2Formatter::startElement(const   XMLCh* const    uri,
00201                                     const   XMLCh* const    localname,
00202                                     const   XMLCh* const    qname,
00203                                     const   Attributes&     attributes)
00204 {
00205     // The name has to be representable without any escapes
00206     fFormatter  << XMLFormatter::NoEscapes << chOpenAngle ;
00207     if ( fExpandNS )
00208     {
00209         if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
00210                 fFormatter  << uri << chColon;
00211         fFormatter << localname ;
00212     }
00213     else
00214         fFormatter << qname ;
00215 
00216     unsigned int len = attributes.getLength();
00217     for (unsigned int index = 0; index < len; index++)
00218     {
00219 
00220         fFormatter  << XMLFormatter::NoEscapes << chSpace ;
00221         if ( fExpandNS )
00222         {
00223             if (XMLString::compareIString(attributes.getURI(index),XMLUni::fgZeroLenString) != 0)
00224                 fFormatter  << attributes.getURI(index) << chColon;
00225             fFormatter  << attributes.getLocalName(index) ;
00226         }
00227         else
00228             fFormatter  << attributes.getQName(index) ;
00229 
00230         fFormatter  << chEqual << chDoubleQuote
00231                     << XMLFormatter::AttrEscapes
00232                     << attributes.getValue(index)
00233                     << XMLFormatter::NoEscapes
00234                     << chDoubleQuote;
00235     }
00236     fFormatter << chCloseAngle;
00237 }
00238 
00239 
00240 END_XDFLENGINE_NS

Generated on Sat Oct 4 13:20:01 2003 for XDFLengine by doxygen1.3-rc2