source: NonGTP/Xerces/xerces/samples/SAXPrint/SAXPrintHandlers.cpp @ 358

Revision 358, 10.1 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: SAXPrintHandlers.cpp,v $
19 * Revision 1.18  2004/09/08 13:55:34  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.17  2003/05/30 09:36:36  gareth
23 * Use new macros for iostream.h and std:: issues.
24 *
25 * Revision 1.16  2003/03/17 21:03:45  peiyongz
26 * Bug#17983
27 *
28 * Revision 1.15  2002/02/01 22:41:17  peiyongz
29 * sane_include
30 *
31 * Revision 1.14  2001/05/11 13:24:58  tng
32 * Copyright update.
33 *
34 * Revision 1.13  2001/05/03 16:00:25  tng
35 * Schema: samples update with schema
36 *
37 * Revision 1.12  2000/10/10 23:55:58  andyh
38 * XMLFormatter patch, contributed by Bill Schindler.  Fix problems with
39 * output to multi-byte encodings.
40 *
41 * Revision 1.11  2000/07/25 22:41:32  aruna1
42 * Char definitions in XMLUni moved to XMLUniDefs
43 *
44 * Revision 1.10  2000/06/17 01:58:06  rahulj
45 * Now output the PI's with no space between ? and target.
46 *
47 * Revision 1.9  2000/05/31 23:58:19  rahulj
48 * Needed an explicit char* cast to get it working under Solaris.
49 *
50 * Revision 1.8  2000/04/07 23:25:53  roddey
51 * A couple more tweaks of the event handler output.
52 *
53 * Revision 1.7  2000/04/06 19:09:51  roddey
54 * Some more improvements to output formatting. Now it will correctly
55 * handle doing the 'replacement char' style of dealing with chars
56 * that are unrepresentable.
57 *
58 * Revision 1.6  2000/04/05 00:20:32  roddey
59 * More updates for the low level formatted output support
60 *
61 * Revision 1.5  2000/03/28 19:43:11  roddey
62 * Fixes for signed/unsigned warnings. New work for two way transcoding
63 * stuff.
64 *
65 * Revision 1.4  2000/03/02 19:53:49  roddey
66 * This checkin includes many changes done while waiting for the
67 * 1.1.0 code to be finished. I can't list them all here, but a list is
68 * available elsewhere.
69 *
70 * Revision 1.3  2000/02/11 03:05:35  abagchi
71 * Removed second parameter from call to StrX constructor
72 *
73 * Revision 1.2  2000/02/06 07:47:24  rahulj
74 * Year 2K copyright swat.
75 *
76 * Revision 1.1.1.1  1999/11/09 01:09:29  twl
77 * Initial checkin
78 *
79 * Revision 1.11  1999/11/08 20:43:42  rahul
80 * Swat for adding in Product name and CVS comment log variable.
81 *
82 */
83
84
85
86// ---------------------------------------------------------------------------
87//  Includes
88// ---------------------------------------------------------------------------
89#include <xercesc/util/XMLUniDefs.hpp>
90#include <xercesc/sax/AttributeList.hpp>
91#include "SAXPrint.hpp"
92
93
94// ---------------------------------------------------------------------------
95//  Local const data
96//
97//  Note: This is the 'safe' way to do these strings. If you compiler supports
98//        L"" style strings, and portability is not a concern, you can use
99//        those types constants directly.
100// ---------------------------------------------------------------------------
101static const XMLCh  gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
102static const XMLCh  gEndPI[] = { chQuestion, chCloseAngle, chNull };
103static const XMLCh  gStartPI[] = { chOpenAngle, chQuestion, chNull };
104static const XMLCh  gXMLDecl1[] =
105{
106        chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
107    ,   chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
108    ,   chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
109    ,   chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
110    ,   chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
111    ,   chDoubleQuote, chNull
112};
113
114static const XMLCh  gXMLDecl2[] =
115{
116        chDoubleQuote, chQuestion, chCloseAngle
117    ,   chLF, chNull
118};
119
120
121
122
123// ---------------------------------------------------------------------------
124//  SAXPrintHandlers: Constructors and Destructor
125// ---------------------------------------------------------------------------
126SAXPrintHandlers::SAXPrintHandlers( const   char* const              encodingName
127                                    , const XMLFormatter::UnRepFlags unRepFlags) :
128
129    fFormatter
130    (
131        encodingName
132        , 0
133        , this
134        , XMLFormatter::NoEscapes
135        , unRepFlags
136    )
137{
138    //
139    //  Go ahead and output an XML Decl with our known encoding. This
140    //  is not the best answer, but its the best we can do until we
141    //  have SAX2 support.
142    //
143    fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
144}
145
146SAXPrintHandlers::~SAXPrintHandlers()
147{
148}
149
150
151// ---------------------------------------------------------------------------
152//  SAXPrintHandlers: Overrides of the output formatter target interface
153// ---------------------------------------------------------------------------
154void SAXPrintHandlers::writeChars(const XMLByte* const toWrite)
155{
156}
157
158void SAXPrintHandlers::writeChars(const XMLByte* const toWrite,
159                                  const unsigned int count,
160                                  XMLFormatter* const formatter)
161{
162    // For this one, just dump them to the standard output
163    // Surprisingly, Solaris was the only platform on which
164    // required the char* cast to print out the string correctly.
165    // Without the cast, it was printing the pointer value in hex.
166    // Quite annoying, considering every other platform printed
167    // the string with the explicit cast to char* below.
168  XERCES_STD_QUALIFIER cout.write((char *) toWrite, (int) count);
169        XERCES_STD_QUALIFIER cout.flush();
170}
171
172
173// ---------------------------------------------------------------------------
174//  SAXPrintHandlers: Overrides of the SAX ErrorHandler interface
175// ---------------------------------------------------------------------------
176void SAXPrintHandlers::error(const SAXParseException& e)
177{
178    XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
179                 << ", line " << e.getLineNumber()
180                 << ", char " << e.getColumnNumber()
181         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
182}
183
184void SAXPrintHandlers::fatalError(const SAXParseException& e)
185{
186    XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
187                 << ", line " << e.getLineNumber()
188                 << ", char " << e.getColumnNumber()
189         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
190}
191
192void SAXPrintHandlers::warning(const SAXParseException& e)
193{
194    XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
195                 << ", line " << e.getLineNumber()
196                 << ", char " << e.getColumnNumber()
197         << "\n  Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
198}
199
200
201// ---------------------------------------------------------------------------
202//  SAXPrintHandlers: Overrides of the SAX DTDHandler interface
203// ---------------------------------------------------------------------------
204void SAXPrintHandlers::unparsedEntityDecl(const     XMLCh* const name
205                                          , const   XMLCh* const publicId
206                                          , const   XMLCh* const systemId
207                                          , const   XMLCh* const notationName)
208{
209    // Not used at this time
210}
211
212
213void SAXPrintHandlers::notationDecl(const   XMLCh* const name
214                                    , const XMLCh* const publicId
215                                    , const XMLCh* const systemId)
216{
217    // Not used at this time
218}
219
220
221// ---------------------------------------------------------------------------
222//  SAXPrintHandlers: Overrides of the SAX DocumentHandler interface
223// ---------------------------------------------------------------------------
224void SAXPrintHandlers::characters(const     XMLCh* const    chars
225                                  , const   unsigned int    length)
226{
227    fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
228}
229
230
231void SAXPrintHandlers::endDocument()
232{
233}
234
235
236void SAXPrintHandlers::endElement(const XMLCh* const name)
237{
238    // No escapes are legal here
239    fFormatter << XMLFormatter::NoEscapes << gEndElement << name << chCloseAngle;
240}
241
242
243void SAXPrintHandlers::ignorableWhitespace( const   XMLCh* const chars
244                                            ,const  unsigned int length)
245{
246    fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
247}
248
249
250void SAXPrintHandlers::processingInstruction(const  XMLCh* const target
251                                            , const XMLCh* const data)
252{
253    fFormatter << XMLFormatter::NoEscapes << gStartPI  << target;
254    if (data)
255        fFormatter << chSpace << data;
256    fFormatter << XMLFormatter::NoEscapes << gEndPI;
257}
258
259
260void SAXPrintHandlers::startDocument()
261{
262}
263
264
265void SAXPrintHandlers::startElement(const   XMLCh* const    name
266                                    ,       AttributeList&  attributes)
267{
268    // The name has to be representable without any escapes
269    fFormatter  << XMLFormatter::NoEscapes
270                << chOpenAngle << name;
271
272    unsigned int len = attributes.getLength();
273    for (unsigned int index = 0; index < len; index++)
274    {
275        //
276        //  Again the name has to be completely representable. But the
277        //  attribute can have refs and requires the attribute style
278        //  escaping.
279        //
280        fFormatter  << XMLFormatter::NoEscapes
281                    << chSpace << attributes.getName(index)
282                    << chEqual << chDoubleQuote
283                    << XMLFormatter::AttrEscapes
284                            << attributes.getValue(index)
285                    << XMLFormatter::NoEscapes
286                    << chDoubleQuote;
287    }
288    fFormatter << chCloseAngle;
289}
Note: See TracBrowser for help on using the repository browser.