2 * Copyright 1999-2001,2004 The Apache Software Foundation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Revision 1.1 2004/11/23 13:38:52 lpadovan
20 * * basic infrastructure for collecting statistics
22 * Revision 1.10 2004/09/08 13:55:33 peiyongz
23 * Apache License Version 2.0
25 * Revision 1.9 2003/05/30 09:36:36 gareth
26 * Use new macros for iostream.h and std:: issues.
28 * Revision 1.8 2003/03/17 21:03:52 peiyongz
31 * Revision 1.7 2002/05/28 20:24:15 tng
32 * XMLUni::fgEmptyString which is defined as "EMPTY" is incorrectly used as an empty string, should use XMLUni::fgZeroLenString instead
34 * Revision 1.6 2002/02/01 22:40:44 peiyongz
37 * Revision 1.5 2001/05/11 13:24:57 tng
40 * Revision 1.4 2001/05/03 16:00:03 tng
41 * Schema: samples update with schema
43 * Revision 1.3 2000/10/10 23:55:53 andyh
44 * XMLFormatter patch, contributed by Bill Schindler. Fix problems with
45 * output to multi-byte encodings.
47 * Revision 1.2 2000/08/09 22:20:38 jpolast
48 * updates for changes to sax2 core functionality.
50 * Revision 1.1 2000/08/02 19:16:14 jpolast
51 * initial checkin of SAX2Print
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
61 #include <xercesc/util/XMLUniDefs.hpp>
62 #include <xercesc/sax2/Attributes.hpp>
63 #include "SAX2Print.hpp"
66 // ---------------------------------------------------------------------------
69 // Note: This is the 'safe' way to do these strings. If you compiler supports
70 // L"" style strings, and portability is not a concern, you can use
71 // those types constants directly.
72 // ---------------------------------------------------------------------------
73 static const XMLCh gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
74 static const XMLCh gEndPI[] = { chQuestion, chCloseAngle, chNull };
75 static const XMLCh gStartPI[] = { chOpenAngle, chQuestion, chNull };
76 static const XMLCh gXMLDecl1[] =
78 chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
79 , chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
80 , chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
81 , chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
82 , chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
83 , chDoubleQuote, chNull
86 static const XMLCh gXMLDecl2[] =
88 chDoubleQuote, chQuestion, chCloseAngle
95 // ---------------------------------------------------------------------------
96 // SAX2PrintHandlers: Constructors and Destructor
97 // ---------------------------------------------------------------------------
98 SAX2PrintHandlers::SAX2PrintHandlers( const char* const encodingName
99 , const XMLFormatter::UnRepFlags unRepFlags
100 , const bool expandNamespaces) :
107 , XMLFormatter::NoEscapes
110 fExpandNS ( expandNamespaces )
113 // Go ahead and output an XML Decl with our known encoding. This
114 // is not the best answer, but its the best we can do until we
115 // have SAX2 support.
118 //fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
121 SAX2PrintHandlers::~SAX2PrintHandlers()
126 // ---------------------------------------------------------------------------
127 // SAX2PrintHandlers: Overrides of the output formatter target interface
128 // ---------------------------------------------------------------------------
129 void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite)
133 void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite,
134 const unsigned int count,
135 XMLFormatter* const formatter)
137 // For this one, just dump them to the standard output
138 // Surprisingly, Solaris was the only platform on which
139 // required the char* cast to print out the string correctly.
140 // Without the cast, it was printing the pointer value in hex.
141 // Quite annoying, considering every other platform printed
142 // the string with the explicit cast to char* below.
143 XERCES_STD_QUALIFIER cout.write((char *) toWrite, (int) count);
144 XERCES_STD_QUALIFIER cout.flush();
148 // ---------------------------------------------------------------------------
149 // SAX2PrintHandlers: Overrides of the SAX ErrorHandler interface
150 // ---------------------------------------------------------------------------
151 void SAX2PrintHandlers::error(const SAXParseException& e)
153 XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
154 << ", line " << e.getLineNumber()
155 << ", char " << e.getColumnNumber()
156 << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
159 void SAX2PrintHandlers::fatalError(const SAXParseException& e)
161 XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
162 << ", line " << e.getLineNumber()
163 << ", char " << e.getColumnNumber()
164 << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
167 void SAX2PrintHandlers::warning(const SAXParseException& e)
169 XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
170 << ", line " << e.getLineNumber()
171 << ", char " << e.getColumnNumber()
172 << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
176 // ---------------------------------------------------------------------------
177 // SAX2PrintHandlers: Overrides of the SAX DTDHandler interface
178 // ---------------------------------------------------------------------------
179 void SAX2PrintHandlers::unparsedEntityDecl(const XMLCh* const name
180 , const XMLCh* const publicId
181 , const XMLCh* const systemId
182 , const XMLCh* const notationName)
184 // Not used at this time
188 void SAX2PrintHandlers::notationDecl(const XMLCh* const name
189 , const XMLCh* const publicId
190 , const XMLCh* const systemId)
192 // Not used at this time
196 // ---------------------------------------------------------------------------
197 // SAX2PrintHandlers: Overrides of the SAX DocumentHandler interface
198 // ---------------------------------------------------------------------------
199 void SAX2PrintHandlers::characters(const XMLCh* const chars
200 , const unsigned int length)
202 //fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
206 void SAX2PrintHandlers::endDocument()
211 void SAX2PrintHandlers::endElement(const XMLCh* const uri,
212 const XMLCh* const localname,
213 const XMLCh* const qname)
216 // No escapes are legal here
217 fFormatter << XMLFormatter::NoEscapes << gEndElement ;
220 if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
221 fFormatter << uri << chColon;
222 fFormatter << localname << chCloseAngle;
225 fFormatter << qname << chCloseAngle;
230 void SAX2PrintHandlers::ignorableWhitespace( const XMLCh* const chars
231 ,const unsigned int length)
233 //fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
237 void SAX2PrintHandlers::processingInstruction(const XMLCh* const target
238 , const XMLCh* const data)
241 fFormatter << XMLFormatter::NoEscapes << gStartPI << target;
243 fFormatter << chSpace << data;
244 fFormatter << XMLFormatter::NoEscapes << gEndPI;
249 void SAX2PrintHandlers::startDocument()
254 void SAX2PrintHandlers::startElement(const XMLCh* const uri,
255 const XMLCh* const localname,
256 const XMLCh* const qname,
257 const Attributes& attributes)
260 // The name has to be representable without any escapes
261 fFormatter << XMLFormatter::NoEscapes << chOpenAngle ;
264 if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
265 fFormatter << uri << chColon;
266 fFormatter << localname ;
269 fFormatter << qname ;
271 unsigned int len = attributes.getLength();
272 for (unsigned int index = 0; index < len; index++)
275 // Again the name has to be completely representable. But the
276 // attribute can have refs and requires the attribute style
279 fFormatter << XMLFormatter::NoEscapes << chSpace ;
282 if (XMLString::compareIString(attributes.getURI(index),XMLUni::fgZeroLenString) != 0)
283 fFormatter << attributes.getURI(index) << chColon;
284 fFormatter << attributes.getLocalName(index) ;
287 fFormatter << attributes.getQName(index) ;
289 fFormatter << chEqual << chDoubleQuote
290 << XMLFormatter::AttrEscapes
291 << attributes.getValue(index)
292 << XMLFormatter::NoEscapes
295 fFormatter << chCloseAngle;