source: icXML/icXML-devel/samples/src/SAXPrint/SAXPrintHandlers.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.0 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: SAXPrintHandlers.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/sax/AttributeList.hpp>
29#include "SAXPrint.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//  SAXPrintHandlers: Constructors and Destructor
63// ---------------------------------------------------------------------------
64SAXPrintHandlers::SAXPrintHandlers( const   char* const              encodingName
65                                    , const XMLFormatter::UnRepFlags unRepFlags) :
66
67    fFormatter
68    (
69        encodingName
70        , 0
71        , this
72        , XMLFormatter::NoEscapes
73        , unRepFlags
74    )
75{
76    //
77    //  Go ahead and output an XML Decl with our known encoding. This
78    //  is not the best answer, but its the best we can do until we
79    //  have SAX2 support.
80    //
81    fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
82}
83
84SAXPrintHandlers::~SAXPrintHandlers()
85{
86}
87
88
89// ---------------------------------------------------------------------------
90//  SAXPrintHandlers: Overrides of the output formatter target interface
91// ---------------------------------------------------------------------------
92void SAXPrintHandlers::writeChars(const XMLByte* const /* toWrite */)
93{
94}
95
96void SAXPrintHandlers::writeChars(const XMLByte* const toWrite,
97                                  const XMLSize_t      count,
98                                  XMLFormatter* const /* formatter */)
99{
100    // For this one, just dump them to the standard output
101    // Surprisingly, Solaris was the only platform on which
102    // required the char* cast to print out the string correctly.
103    // Without the cast, it was printing the pointer value in hex.
104    // Quite annoying, considering every other platform printed
105    // the string with the explicit cast to char* below.
106    XERCES_STD_QUALIFIER cout.write((char *) toWrite, (int) count);
107        XERCES_STD_QUALIFIER cout.flush();
108}
109
110
111// ---------------------------------------------------------------------------
112//  SAXPrintHandlers: Overrides of the SAX ErrorHandler interface
113// ---------------------------------------------------------------------------
114void SAXPrintHandlers::error(const SAXParseException& e)
115{
116    XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
117                 << ", line " << e.getLineNumber()
118                 << ", char " << e.getColumnNumber()
119         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
120}
121
122void SAXPrintHandlers::fatalError(const SAXParseException& e)
123{
124    XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
125                 << ", line " << e.getLineNumber()
126                 << ", char " << e.getColumnNumber()
127         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
128}
129
130void SAXPrintHandlers::warning(const SAXParseException& e)
131{
132    XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
133                 << ", line " << e.getLineNumber()
134                 << ", char " << e.getColumnNumber()
135         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
136}
137
138
139// ---------------------------------------------------------------------------
140//  SAXPrintHandlers: Overrides of the SAX DTDHandler interface
141// ---------------------------------------------------------------------------
142void SAXPrintHandlers::unparsedEntityDecl(const     XMLCh* const /* name */
143                                          , const   XMLCh* const /* publicId */
144                                          , const   XMLCh* const /* systemId */
145                                          , const   XMLCh* const /* notationName */)
146{
147    // Not used at this time
148}
149
150
151void SAXPrintHandlers::notationDecl(const   XMLCh* const /* name */
152                                    , const XMLCh* const /* publicId */
153                                    , const XMLCh* const /* systemId */)
154{
155    // Not used at this time
156}
157
158
159// ---------------------------------------------------------------------------
160//  SAXPrintHandlers: Overrides of the SAX DocumentHandler interface
161// ---------------------------------------------------------------------------
162void SAXPrintHandlers::characters(const     XMLCh* const    chars
163                                  , const   XMLSize_t       length)
164{
165    fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
166}
167
168
169void SAXPrintHandlers::endDocument()
170{
171}
172
173
174void SAXPrintHandlers::endElement(const XMLCh* const name)
175{
176    // No escapes are legal here
177    fFormatter << XMLFormatter::NoEscapes << gEndElement << name << chCloseAngle;
178}
179
180
181void SAXPrintHandlers::ignorableWhitespace( const   XMLCh* const chars
182                                            ,const  XMLSize_t    length)
183{
184    fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
185}
186
187
188void SAXPrintHandlers::processingInstruction(const  XMLCh* const target
189                                            , const XMLCh* const data)
190{
191    fFormatter << XMLFormatter::NoEscapes << gStartPI  << target;
192    if (data)
193        fFormatter << chSpace << data;
194    fFormatter << XMLFormatter::NoEscapes << gEndPI;
195}
196
197
198void SAXPrintHandlers::startDocument()
199{
200}
201
202
203void SAXPrintHandlers::startElement(const   XMLCh* const    name
204                                    ,       AttributeList&  attributes)
205{
206    // The name has to be representable without any escapes
207    fFormatter  << XMLFormatter::NoEscapes
208                << chOpenAngle << name;
209
210    XMLSize_t len = attributes.getLength();
211    for (XMLSize_t index = 0; index < len; index++)
212    {
213        //
214        //  Again the name has to be completely representable. But the
215        //  attribute can have refs and requires the attribute style
216        //  escaping.
217        //
218        fFormatter  << XMLFormatter::NoEscapes
219                    << chSpace << attributes.getName(index)
220                    << chEqual << chDoubleQuote
221                    << XMLFormatter::AttrEscapes
222                            << attributes.getValue(index)
223                    << XMLFormatter::NoEscapes
224                    << chDoubleQuote;
225    }
226    fFormatter << chCloseAngle;
227}
Note: See TracBrowser for help on using the repository browser.