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: SAX2PrintHandlers.cpp,v $
|
---|
19 | * Revision 1.10 2004/09/08 13:55:33 peiyongz
|
---|
20 | * Apache License Version 2.0
|
---|
21 | *
|
---|
22 | * Revision 1.9 2003/05/30 09:36:36 gareth
|
---|
23 | * Use new macros for iostream.h and std:: issues.
|
---|
24 | *
|
---|
25 | * Revision 1.8 2003/03/17 21:03:52 peiyongz
|
---|
26 | * Bug#17983
|
---|
27 | *
|
---|
28 | * Revision 1.7 2002/05/28 20:24:15 tng
|
---|
29 | * XMLUni::fgEmptyString which is defined as "EMPTY" is incorrectly used as an empty string, should use XMLUni::fgZeroLenString instead
|
---|
30 | *
|
---|
31 | * Revision 1.6 2002/02/01 22:40:44 peiyongz
|
---|
32 | * sane_include
|
---|
33 | *
|
---|
34 | * Revision 1.5 2001/05/11 13:24:57 tng
|
---|
35 | * Copyright update.
|
---|
36 | *
|
---|
37 | * Revision 1.4 2001/05/03 16:00:03 tng
|
---|
38 | * Schema: samples update with schema
|
---|
39 | *
|
---|
40 | * Revision 1.3 2000/10/10 23:55:53 andyh
|
---|
41 | * XMLFormatter patch, contributed by Bill Schindler. Fix problems with
|
---|
42 | * output to multi-byte encodings.
|
---|
43 | *
|
---|
44 | * Revision 1.2 2000/08/09 22:20:38 jpolast
|
---|
45 | * updates for changes to sax2 core functionality.
|
---|
46 | *
|
---|
47 | * Revision 1.1 2000/08/02 19:16:14 jpolast
|
---|
48 | * initial checkin of SAX2Print
|
---|
49 | *
|
---|
50 | *
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | // ---------------------------------------------------------------------------
|
---|
56 | // Includes
|
---|
57 | // ---------------------------------------------------------------------------
|
---|
58 | #include <xercesc/util/XMLUniDefs.hpp>
|
---|
59 | #include <xercesc/sax2/Attributes.hpp>
|
---|
60 | #include "SAX2Print.hpp"
|
---|
61 |
|
---|
62 |
|
---|
63 | // ---------------------------------------------------------------------------
|
---|
64 | // Local const data
|
---|
65 | //
|
---|
66 | // Note: This is the 'safe' way to do these strings. If you compiler supports
|
---|
67 | // L"" style strings, and portability is not a concern, you can use
|
---|
68 | // those types constants directly.
|
---|
69 | // ---------------------------------------------------------------------------
|
---|
70 | static const XMLCh gEndElement[] = { chOpenAngle, chForwardSlash, chNull };
|
---|
71 | static const XMLCh gEndPI[] = { chQuestion, chCloseAngle, chNull };
|
---|
72 | static const XMLCh gStartPI[] = { chOpenAngle, chQuestion, chNull };
|
---|
73 | static const XMLCh gXMLDecl1[] =
|
---|
74 | {
|
---|
75 | chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l
|
---|
76 | , chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i
|
---|
77 | , chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1, chPeriod
|
---|
78 | , chDigit_0, chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c
|
---|
79 | , chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual
|
---|
80 | , chDoubleQuote, chNull
|
---|
81 | };
|
---|
82 |
|
---|
83 | static const XMLCh gXMLDecl2[] =
|
---|
84 | {
|
---|
85 | chDoubleQuote, chQuestion, chCloseAngle
|
---|
86 | , chLF, chNull
|
---|
87 | };
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | // ---------------------------------------------------------------------------
|
---|
93 | // SAX2PrintHandlers: Constructors and Destructor
|
---|
94 | // ---------------------------------------------------------------------------
|
---|
95 | SAX2PrintHandlers::SAX2PrintHandlers( const char* const encodingName
|
---|
96 | , const XMLFormatter::UnRepFlags unRepFlags
|
---|
97 | , const bool expandNamespaces) :
|
---|
98 |
|
---|
99 | fFormatter
|
---|
100 | (
|
---|
101 | encodingName
|
---|
102 | , 0
|
---|
103 | , this
|
---|
104 | , XMLFormatter::NoEscapes
|
---|
105 | , unRepFlags
|
---|
106 | ),
|
---|
107 | fExpandNS ( expandNamespaces )
|
---|
108 | {
|
---|
109 | //
|
---|
110 | // Go ahead and output an XML Decl with our known encoding. This
|
---|
111 | // is not the best answer, but its the best we can do until we
|
---|
112 | // have SAX2 support.
|
---|
113 | //
|
---|
114 | fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
|
---|
115 | }
|
---|
116 |
|
---|
117 | SAX2PrintHandlers::~SAX2PrintHandlers()
|
---|
118 | {
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | // ---------------------------------------------------------------------------
|
---|
123 | // SAX2PrintHandlers: Overrides of the output formatter target interface
|
---|
124 | // ---------------------------------------------------------------------------
|
---|
125 | void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite)
|
---|
126 | {
|
---|
127 | }
|
---|
128 |
|
---|
129 | void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite,
|
---|
130 | const unsigned int count,
|
---|
131 | XMLFormatter* const formatter)
|
---|
132 | {
|
---|
133 | // For this one, just dump them to the standard output
|
---|
134 | // Surprisingly, Solaris was the only platform on which
|
---|
135 | // required the char* cast to print out the string correctly.
|
---|
136 | // Without the cast, it was printing the pointer value in hex.
|
---|
137 | // Quite annoying, considering every other platform printed
|
---|
138 | // the string with the explicit cast to char* below.
|
---|
139 | XERCES_STD_QUALIFIER cout.write((char *) toWrite, (int) count);
|
---|
140 | XERCES_STD_QUALIFIER cout.flush();
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | // ---------------------------------------------------------------------------
|
---|
145 | // SAX2PrintHandlers: Overrides of the SAX ErrorHandler interface
|
---|
146 | // ---------------------------------------------------------------------------
|
---|
147 | void SAX2PrintHandlers::error(const SAXParseException& e)
|
---|
148 | {
|
---|
149 | XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
|
---|
150 | << ", line " << e.getLineNumber()
|
---|
151 | << ", char " << e.getColumnNumber()
|
---|
152 | << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
153 | }
|
---|
154 |
|
---|
155 | void SAX2PrintHandlers::fatalError(const SAXParseException& e)
|
---|
156 | {
|
---|
157 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
|
---|
158 | << ", line " << e.getLineNumber()
|
---|
159 | << ", char " << e.getColumnNumber()
|
---|
160 | << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void SAX2PrintHandlers::warning(const SAXParseException& e)
|
---|
164 | {
|
---|
165 | XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
|
---|
166 | << ", line " << e.getLineNumber()
|
---|
167 | << ", char " << e.getColumnNumber()
|
---|
168 | << "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | // ---------------------------------------------------------------------------
|
---|
173 | // SAX2PrintHandlers: Overrides of the SAX DTDHandler interface
|
---|
174 | // ---------------------------------------------------------------------------
|
---|
175 | void SAX2PrintHandlers::unparsedEntityDecl(const XMLCh* const name
|
---|
176 | , const XMLCh* const publicId
|
---|
177 | , const XMLCh* const systemId
|
---|
178 | , const XMLCh* const notationName)
|
---|
179 | {
|
---|
180 | // Not used at this time
|
---|
181 | }
|
---|
182 |
|
---|
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 |
|
---|
192 | // ---------------------------------------------------------------------------
|
---|
193 | // SAX2PrintHandlers: Overrides of the SAX DocumentHandler interface
|
---|
194 | // ---------------------------------------------------------------------------
|
---|
195 | void SAX2PrintHandlers::characters(const XMLCh* const chars
|
---|
196 | , const unsigned int length)
|
---|
197 | {
|
---|
198 | fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | void SAX2PrintHandlers::endDocument()
|
---|
203 | {
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | void SAX2PrintHandlers::endElement(const XMLCh* const uri,
|
---|
208 | const XMLCh* const localname,
|
---|
209 | const XMLCh* const qname)
|
---|
210 | {
|
---|
211 | // No escapes are legal here
|
---|
212 | fFormatter << XMLFormatter::NoEscapes << gEndElement ;
|
---|
213 | if ( fExpandNS )
|
---|
214 | {
|
---|
215 | if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
|
---|
216 | fFormatter << uri << chColon;
|
---|
217 | fFormatter << localname << chCloseAngle;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | fFormatter << qname << chCloseAngle;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | void SAX2PrintHandlers::ignorableWhitespace( const XMLCh* const chars
|
---|
225 | ,const unsigned int length)
|
---|
226 | {
|
---|
227 | fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | void SAX2PrintHandlers::processingInstruction(const XMLCh* const target
|
---|
232 | , const XMLCh* const data)
|
---|
233 | {
|
---|
234 | fFormatter << XMLFormatter::NoEscapes << gStartPI << target;
|
---|
235 | if (data)
|
---|
236 | fFormatter << chSpace << data;
|
---|
237 | fFormatter << XMLFormatter::NoEscapes << gEndPI;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | void SAX2PrintHandlers::startDocument()
|
---|
242 | {
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | void SAX2PrintHandlers::startElement(const XMLCh* const uri,
|
---|
247 | const XMLCh* const localname,
|
---|
248 | const XMLCh* const qname,
|
---|
249 | const Attributes& attributes)
|
---|
250 | {
|
---|
251 | // The name has to be representable without any escapes
|
---|
252 | fFormatter << XMLFormatter::NoEscapes << chOpenAngle ;
|
---|
253 | if ( fExpandNS )
|
---|
254 | {
|
---|
255 | if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
|
---|
256 | fFormatter << uri << chColon;
|
---|
257 | fFormatter << localname ;
|
---|
258 | }
|
---|
259 | else
|
---|
260 | fFormatter << qname ;
|
---|
261 |
|
---|
262 | unsigned int len = attributes.getLength();
|
---|
263 | for (unsigned int index = 0; index < len; index++)
|
---|
264 | {
|
---|
265 | //
|
---|
266 | // Again the name has to be completely representable. But the
|
---|
267 | // attribute can have refs and requires the attribute style
|
---|
268 | // escaping.
|
---|
269 | //
|
---|
270 | fFormatter << XMLFormatter::NoEscapes << chSpace ;
|
---|
271 | if ( fExpandNS )
|
---|
272 | {
|
---|
273 | if (XMLString::compareIString(attributes.getURI(index),XMLUni::fgZeroLenString) != 0)
|
---|
274 | fFormatter << attributes.getURI(index) << chColon;
|
---|
275 | fFormatter << attributes.getLocalName(index) ;
|
---|
276 | }
|
---|
277 | else
|
---|
278 | fFormatter << attributes.getQName(index) ;
|
---|
279 |
|
---|
280 | fFormatter << chEqual << chDoubleQuote
|
---|
281 | << XMLFormatter::AttrEscapes
|
---|
282 | << attributes.getValue(index)
|
---|
283 | << XMLFormatter::NoEscapes
|
---|
284 | << chDoubleQuote;
|
---|
285 | }
|
---|
286 | fFormatter << chCloseAngle;
|
---|
287 | }
|
---|