SAX2PrintHandlers.cpp
Upload User: huihehuasu
Upload Date: 2007-01-10
Package Size: 6948k
Code Size: 11k
Development Platform:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Log: SAX2PrintHandlers.cpp,v $
  58.  * Revision 1.5  2001/05/11 13:24:57  tng
  59.  * Copyright update.
  60.  *
  61.  * Revision 1.4  2001/05/03 16:00:03  tng
  62.  * Schema: samples update with schema
  63.  *
  64.  * Revision 1.3  2000/10/10 23:55:53  andyh
  65.  * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
  66.  * output to multi-byte encodings.
  67.  *
  68.  * Revision 1.2  2000/08/09 22:20:38  jpolast
  69.  * updates for changes to sax2 core functionality.
  70.  *
  71.  * Revision 1.1  2000/08/02 19:16:14  jpolast
  72.  * initial checkin of SAX2Print
  73.  *
  74.  *
  75.  */
  76. // ---------------------------------------------------------------------------
  77. //  Includes
  78. // ---------------------------------------------------------------------------
  79. #include <util/XMLUniDefs.hpp>
  80. #include <sax2/Attributes.hpp>
  81. #include "SAX2Print.hpp"
  82. // ---------------------------------------------------------------------------
  83. //  Local const data
  84. //
  85. //  Note: This is the 'safe' way to do these strings. If you compiler supports
  86. //        L"" style strings, and portability is not a concern, you can use
  87. //        those types constants directly.
  88. // ---------------------------------------------------------------------------
  89. static const XMLCh  gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
  90. static const XMLCh  gEndPI[] = { chQuestion, chCloseAngle, chNull };
  91. static const XMLCh  gStartPI[] = { chOpenAngle, chQuestion, chNull };
  92. static const XMLCh  gXMLDecl1[] =
  93. {
  94.         chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
  95.     ,   chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
  96.     ,   chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
  97.     ,   chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
  98.     ,   chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
  99.     ,   chDoubleQuote, chNull
  100. };
  101. static const XMLCh  gXMLDecl2[] =
  102. {
  103.         chDoubleQuote, chQuestion, chCloseAngle
  104.     ,   chLF, chNull
  105. };
  106. // ---------------------------------------------------------------------------
  107. //  SAX2PrintHandlers: Constructors and Destructor
  108. // ---------------------------------------------------------------------------
  109. SAX2PrintHandlers::SAX2PrintHandlers( const   char* const              encodingName
  110.                                     , const XMLFormatter::UnRepFlags unRepFlags
  111. , const bool  expandNamespaces) :
  112.     fFormatter
  113.     (
  114.         encodingName
  115.         , this
  116.         , XMLFormatter::NoEscapes
  117.         , unRepFlags
  118.     ),
  119. fExpandNS ( expandNamespaces )
  120. {
  121.     //
  122.     //  Go ahead and output an XML Decl with our known encoding. This
  123.     //  is not the best answer, but its the best we can do until we
  124.     //  have SAX2 support.
  125.     //
  126.     fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
  127. }
  128. SAX2PrintHandlers::~SAX2PrintHandlers()
  129. {
  130. }
  131. // ---------------------------------------------------------------------------
  132. //  SAX2PrintHandlers: Overrides of the output formatter target interface
  133. // ---------------------------------------------------------------------------
  134. void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite)
  135. {
  136. }
  137. void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite,
  138.                                    const unsigned int count,
  139.                                    XMLFormatter* const formatter)
  140. {
  141.     // For this one, just dump them to the standard output
  142.     // Surprisingly, Solaris was the only platform on which
  143.     // required the char* cast to print out the string correctly.
  144.     // Without the cast, it was printing the pointer value in hex.
  145.     // Quite annoying, considering every other platform printed
  146.     // the string with the explicit cast to char* below.
  147.   cout.write((char *) toWrite, (int) count);
  148. cout.flush();
  149. }
  150. // ---------------------------------------------------------------------------
  151. //  SAX2PrintHandlers: Overrides of the SAX ErrorHandler interface
  152. // ---------------------------------------------------------------------------
  153. void SAX2PrintHandlers::error(const SAXParseException& e)
  154. {
  155.     cerr << "nError at file " << StrX(e.getSystemId())
  156.  << ", line " << e.getLineNumber()
  157.  << ", char " << e.getColumnNumber()
  158.          << "n  Message: " << StrX(e.getMessage()) << endl;
  159. }
  160. void SAX2PrintHandlers::fatalError(const SAXParseException& e)
  161. {
  162.     cerr << "nFatal Error at file " << StrX(e.getSystemId())
  163.  << ", line " << e.getLineNumber()
  164.  << ", char " << e.getColumnNumber()
  165.          << "n  Message: " << StrX(e.getMessage()) << endl;
  166. }
  167. void SAX2PrintHandlers::warning(const SAXParseException& e)
  168. {
  169.     cerr << "nWarning at file " << StrX(e.getSystemId())
  170.  << ", line " << e.getLineNumber()
  171.  << ", char " << e.getColumnNumber()
  172.          << "n  Message: " << StrX(e.getMessage()) << endl;
  173. }
  174. // ---------------------------------------------------------------------------
  175. //  SAX2PrintHandlers: Overrides of the SAX DTDHandler interface
  176. // ---------------------------------------------------------------------------
  177. void SAX2PrintHandlers::unparsedEntityDecl(const     XMLCh* const name
  178.                                           , const   XMLCh* const publicId
  179.                                           , const   XMLCh* const systemId
  180.                                           , const   XMLCh* const notationName)
  181. {
  182.     // Not used at this time
  183. }
  184. void SAX2PrintHandlers::notationDecl(const   XMLCh* const name
  185.                                     , const XMLCh* const publicId
  186.                                     , const XMLCh* const systemId)
  187. {
  188.     // Not used at this time
  189. }
  190. // ---------------------------------------------------------------------------
  191. //  SAX2PrintHandlers: Overrides of the SAX DocumentHandler interface
  192. // ---------------------------------------------------------------------------
  193. void SAX2PrintHandlers::characters(const     XMLCh* const    chars
  194.                                   , const   unsigned int    length)
  195. {
  196.     fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
  197. }
  198. void SAX2PrintHandlers::endDocument()
  199. {
  200. }
  201. void SAX2PrintHandlers::endElement(const XMLCh* const uri,
  202.  const XMLCh* const localname,
  203.  const XMLCh* const qname)
  204. {
  205.     // No escapes are legal here
  206.     fFormatter << XMLFormatter::NoEscapes << gEndElement ;
  207. if ( fExpandNS )
  208. {
  209. if (XMLString::compareIString(uri,XMLUni::fgEmptyString) != 0)
  210. fFormatter  << uri << chColon;
  211. fFormatter << localname << chCloseAngle;
  212. }
  213. else
  214. fFormatter << qname << chCloseAngle;
  215. }
  216. void SAX2PrintHandlers::ignorableWhitespace( const   XMLCh* const chars
  217.                                             ,const  unsigned int length)
  218. {
  219.     fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
  220. }
  221. void SAX2PrintHandlers::processingInstruction(const  XMLCh* const target
  222.                                             , const XMLCh* const data)
  223. {
  224.     fFormatter << XMLFormatter::NoEscapes << gStartPI  << target;
  225.     if (data)
  226.         fFormatter << chSpace << data;
  227.     fFormatter << XMLFormatter::NoEscapes << gEndPI;
  228. }
  229. void SAX2PrintHandlers::startDocument()
  230. {
  231. }
  232. void SAX2PrintHandlers::startElement(const   XMLCh* const    uri,
  233. const   XMLCh* const    localname,
  234. const   XMLCh* const    qname,
  235.                                     const   Attributes& attributes)
  236. {
  237.     // The name has to be representable without any escapes
  238.     fFormatter  << XMLFormatter::NoEscapes << chOpenAngle ;
  239. if ( fExpandNS )
  240. {
  241. if (XMLString::compareIString(uri,XMLUni::fgEmptyString) != 0)
  242. fFormatter  << uri << chColon;
  243. fFormatter << localname ;
  244. }
  245. else
  246. fFormatter << qname ;
  247.     unsigned int len = attributes.getLength();
  248.     for (unsigned int index = 0; index < len; index++)
  249.     {
  250.         //
  251.         //  Again the name has to be completely representable. But the
  252.         //  attribute can have refs and requires the attribute style
  253.         //  escaping.
  254.         //
  255.         fFormatter  << XMLFormatter::NoEscapes << chSpace ;
  256. if ( fExpandNS )
  257. {
  258. if (XMLString::compareIString(attributes.getURI(index),XMLUni::fgEmptyString) != 0)
  259. fFormatter  << attributes.getURI(index) << chColon;
  260. fFormatter  << attributes.getLocalName(index) ;
  261. }
  262. else
  263. fFormatter  << attributes.getQName(index) ;
  264. fFormatter  << chEqual << chDoubleQuote
  265.                     << XMLFormatter::AttrEscapes
  266.             << attributes.getValue(index)
  267.                     << XMLFormatter::NoEscapes
  268.                     << chDoubleQuote;
  269.     }
  270.     fFormatter << chCloseAngle;
  271. }