source: icXML/icXML-devel/samples/src/SAX2Print/SAX2PrintHandlers.cpp @ 2726

Last change on this file since 2726 was 2726, checked in by cameron, 7 years ago

Add original Xerces tests and samples directories

File size: 8.8 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: SAX2PrintHandlers.cpp 676911 2008-07-15 13:27:32Z amassari $
20 */
21
22
23
24// ---------------------------------------------------------------------------
25//  Includes
26// ---------------------------------------------------------------------------
27#include <xercesc/util/XMLUniDefs.hpp>
28#include <xercesc/sax2/Attributes.hpp>
29#include "SAX2Print.hpp"
30
31
32// ---------------------------------------------------------------------------
33//  Local const data
34//
35//  Note: This is the 'safe' way to do these strings. If you compiler supports
36//        L"" style strings, and portability is not a concern, you can use
37//        those types constants directly.
38// ---------------------------------------------------------------------------
39static const XMLCh  gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
40static const XMLCh  gEndPI[] = { chQuestion, chCloseAngle, chNull };
41static const XMLCh  gStartPI[] = { chOpenAngle, chQuestion, chNull };
42static const XMLCh  gXMLDecl1[] =
43{
44        chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
45    ,   chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
46    ,   chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
47    ,   chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
48    ,   chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
49    ,   chDoubleQuote, chNull
50};
51
52static const XMLCh  gXMLDecl2[] =
53{
54        chDoubleQuote, chQuestion, chCloseAngle
55    ,   chLF, chNull
56};
57
58
59
60
61// ---------------------------------------------------------------------------
62//  SAX2PrintHandlers: Constructors and Destructor
63// ---------------------------------------------------------------------------
64SAX2PrintHandlers::SAX2PrintHandlers( const   char* const              encodingName
65                                    , const XMLFormatter::UnRepFlags unRepFlags
66                                                                        , const bool                                     expandNamespaces) :
67
68    fFormatter
69    (
70        encodingName
71        , 0
72        , this
73        , XMLFormatter::NoEscapes
74        , unRepFlags
75    ),
76        fExpandNS ( expandNamespaces )
77{
78    //
79    //  Go ahead and output an XML Decl with our known encoding. This
80    //  is not the best answer, but its the best we can do until we
81    //  have SAX2 support.
82    //
83    fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
84}
85
86SAX2PrintHandlers::~SAX2PrintHandlers()
87{
88}
89
90
91// ---------------------------------------------------------------------------
92//  SAX2PrintHandlers: Overrides of the output formatter target interface
93// ---------------------------------------------------------------------------
94void SAX2PrintHandlers::writeChars(const XMLByte* const /* toWrite */)
95{
96}
97
98void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite,
99                                   const XMLSize_t      count,
100                                   XMLFormatter* const /* formatter */)
101{
102    // For this one, just dump them to the standard output
103    // Surprisingly, Solaris was the only platform on which
104    // required the char* cast to print out the string correctly.
105    // Without the cast, it was printing the pointer value in hex.
106    // Quite annoying, considering every other platform printed
107    // the string with the explicit cast to char* below.
108    XERCES_STD_QUALIFIER cout.write((char *) toWrite, (int) count);
109        XERCES_STD_QUALIFIER cout.flush();
110}
111
112
113// ---------------------------------------------------------------------------
114//  SAX2PrintHandlers: Overrides of the SAX ErrorHandler interface
115// ---------------------------------------------------------------------------
116void SAX2PrintHandlers::error(const SAXParseException& e)
117{
118    XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
119                 << ", line " << e.getLineNumber()
120                 << ", char " << e.getColumnNumber()
121         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
122}
123
124void SAX2PrintHandlers::fatalError(const SAXParseException& e)
125{
126    XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
127                 << ", line " << e.getLineNumber()
128                 << ", char " << e.getColumnNumber()
129         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
130}
131
132void SAX2PrintHandlers::warning(const SAXParseException& e)
133{
134    XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
135                 << ", line " << e.getLineNumber()
136                 << ", char " << e.getColumnNumber()
137         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
138}
139
140
141// ---------------------------------------------------------------------------
142//  SAX2PrintHandlers: Overrides of the SAX DTDHandler interface
143// ---------------------------------------------------------------------------
144void SAX2PrintHandlers::unparsedEntityDecl(const    XMLCh* const /* name */
145                                          , const   XMLCh* const /* publicId */
146                                          , const   XMLCh* const /* systemId */
147                                          , const   XMLCh* const /* notationName */)
148{
149    // Not used at this time
150}
151
152
153void SAX2PrintHandlers::notationDecl(const  XMLCh* const /* name */
154                                    , const XMLCh* const /* publicId */
155                                    , const XMLCh* const /* systemId */)
156{
157    // Not used at this time
158}
159
160
161// ---------------------------------------------------------------------------
162//  SAX2PrintHandlers: Overrides of the SAX DocumentHandler interface
163// ---------------------------------------------------------------------------
164void SAX2PrintHandlers::characters(const     XMLCh* const    chars
165                                  , const   XMLSize_t    length)
166{
167    fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
168}
169
170
171void SAX2PrintHandlers::endDocument()
172{
173}
174
175
176void SAX2PrintHandlers::endElement(const XMLCh* const uri,
177                                                                 const XMLCh* const localname,
178                                                                 const XMLCh* const qname)
179{
180    // No escapes are legal here
181    fFormatter << XMLFormatter::NoEscapes << gEndElement ;
182        if ( fExpandNS )
183        {
184                if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
185                                fFormatter  << uri << chColon;
186                fFormatter << localname << chCloseAngle;
187        }
188        else
189                fFormatter << qname << chCloseAngle;
190}
191
192
193void SAX2PrintHandlers::ignorableWhitespace( const   XMLCh* const chars
194                                            ,const  XMLSize_t length)
195{
196    fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
197}
198
199
200void SAX2PrintHandlers::processingInstruction(const  XMLCh* const target
201                                            , const XMLCh* const data)
202{
203    fFormatter << XMLFormatter::NoEscapes << gStartPI  << target;
204    if (data)
205        fFormatter << chSpace << data;
206    fFormatter << XMLFormatter::NoEscapes << gEndPI;
207}
208
209
210void SAX2PrintHandlers::startDocument()
211{
212}
213
214
215void SAX2PrintHandlers::startElement(const   XMLCh* const    uri,
216                                                                        const   XMLCh* const    localname,
217                                                                        const   XMLCh* const    qname,
218                                    const   Attributes&         attributes)
219{
220    // The name has to be representable without any escapes
221    fFormatter  << XMLFormatter::NoEscapes << chOpenAngle ;
222        if ( fExpandNS )
223        {
224                if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
225                                fFormatter  << uri << chColon;
226                fFormatter << localname ;
227        }
228        else
229                fFormatter << qname ;
230
231    XMLSize_t len = attributes.getLength();
232    for (XMLSize_t index = 0; index < len; index++)
233    {
234        //
235        //  Again the name has to be completely representable. But the
236        //  attribute can have refs and requires the attribute style
237        //  escaping.
238        //
239        fFormatter  << XMLFormatter::NoEscapes << chSpace ;
240                if ( fExpandNS )
241                {
242                        if (XMLString::compareIString(attributes.getURI(index),XMLUni::fgZeroLenString) != 0)
243                                fFormatter  << attributes.getURI(index) << chColon;
244                        fFormatter  << attributes.getLocalName(index) ;
245                }
246                else
247                        fFormatter  << attributes.getQName(index) ;
248
249                fFormatter  << chEqual << chDoubleQuote
250                    << XMLFormatter::AttrEscapes
251                            << attributes.getValue(index)
252                    << XMLFormatter::NoEscapes
253                    << chDoubleQuote;
254    }
255    fFormatter << chCloseAngle;
256}
Note: See TracBrowser for help on using the repository browser.