]> matita.cs.unibo.it Git - helm.git/blob - helm/papers/use_case/stats/parse/SAX2Print/SAX2PrintHandlers.cpp
ocaml 3.09 transition
[helm.git] / helm / papers / use_case / stats / parse / SAX2Print / SAX2PrintHandlers.cpp
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$
19  * Revision 1.1  2004/11/23 13:38:52  lpadovan
20  * * basic infrastructure for collecting statistics
21  *
22  * Revision 1.10  2004/09/08 13:55:33  peiyongz
23  * Apache License Version 2.0
24  *
25  * Revision 1.9  2003/05/30 09:36:36  gareth
26  * Use new macros for iostream.h and std:: issues.
27  *
28  * Revision 1.8  2003/03/17 21:03:52  peiyongz
29  * Bug#17983
30  *
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
33  *
34  * Revision 1.6  2002/02/01 22:40:44  peiyongz
35  * sane_include
36  *
37  * Revision 1.5  2001/05/11 13:24:57  tng
38  * Copyright update.
39  *
40  * Revision 1.4  2001/05/03 16:00:03  tng
41  * Schema: samples update with schema
42  *
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.
46  *
47  * Revision 1.2  2000/08/09 22:20:38  jpolast
48  * updates for changes to sax2 core functionality.
49  *
50  * Revision 1.1  2000/08/02 19:16:14  jpolast
51  * initial checkin of SAX2Print
52  *
53  *
54  */
55
56
57
58 // ---------------------------------------------------------------------------
59 //  Includes
60 // ---------------------------------------------------------------------------
61 #include <xercesc/util/XMLUniDefs.hpp>
62 #include <xercesc/sax2/Attributes.hpp>
63 #include "SAX2Print.hpp"
64
65
66 // ---------------------------------------------------------------------------
67 //  Local const data
68 //
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[] =
77 {
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
84 };
85
86 static const XMLCh  gXMLDecl2[] =
87 {
88         chDoubleQuote, chQuestion, chCloseAngle
89     ,   chLF, chNull
90 };
91
92
93
94
95 // ---------------------------------------------------------------------------
96 //  SAX2PrintHandlers: Constructors and Destructor
97 // ---------------------------------------------------------------------------
98 SAX2PrintHandlers::SAX2PrintHandlers( const   char* const              encodingName
99                                     , const XMLFormatter::UnRepFlags unRepFlags
100                                                                         , const bool                                     expandNamespaces) :
101
102     fFormatter
103     (
104         encodingName
105         , 0
106         , this
107         , XMLFormatter::NoEscapes
108         , unRepFlags
109     ),
110         fExpandNS ( expandNamespaces )
111 {
112     //
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.
116     //
117     //
118     //fFormatter << gXMLDecl1 << fFormatter.getEncodingName() << gXMLDecl2;
119 }
120
121 SAX2PrintHandlers::~SAX2PrintHandlers()
122 {
123 }
124
125
126 // ---------------------------------------------------------------------------
127 //  SAX2PrintHandlers: Overrides of the output formatter target interface
128 // ---------------------------------------------------------------------------
129 void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite)
130 {
131 }
132
133 void SAX2PrintHandlers::writeChars(const XMLByte* const toWrite,
134                                    const unsigned int count,
135                                    XMLFormatter* const formatter)
136 {
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();
145 }
146
147
148 // ---------------------------------------------------------------------------
149 //  SAX2PrintHandlers: Overrides of the SAX ErrorHandler interface
150 // ---------------------------------------------------------------------------
151 void SAX2PrintHandlers::error(const SAXParseException& e)
152 {
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;
157 }
158
159 void SAX2PrintHandlers::fatalError(const SAXParseException& e)
160 {
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;
165 }
166
167 void SAX2PrintHandlers::warning(const SAXParseException& e)
168 {
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;
173 }
174
175
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)
183 {
184     // Not used at this time
185 }
186
187
188 void SAX2PrintHandlers::notationDecl(const   XMLCh* const name
189                                     , const XMLCh* const publicId
190                                     , const XMLCh* const systemId)
191 {
192     // Not used at this time
193 }
194
195
196 // ---------------------------------------------------------------------------
197 //  SAX2PrintHandlers: Overrides of the SAX DocumentHandler interface
198 // ---------------------------------------------------------------------------
199 void SAX2PrintHandlers::characters(const     XMLCh* const    chars
200                                   , const   unsigned int    length)
201 {
202     //fFormatter.formatBuf(chars, length, XMLFormatter::CharEscapes);
203 }
204
205
206 void SAX2PrintHandlers::endDocument()
207 {
208 }
209
210
211 void SAX2PrintHandlers::endElement(const XMLCh* const uri,
212                                                                  const XMLCh* const localname,
213                                                                  const XMLCh* const qname)
214 {
215 #if 0
216     // No escapes are legal here
217     fFormatter << XMLFormatter::NoEscapes << gEndElement ;
218         if ( fExpandNS )
219         {
220                 if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
221                                 fFormatter  << uri << chColon;
222                 fFormatter << localname << chCloseAngle;
223         }
224         else
225                 fFormatter << qname << chCloseAngle;
226 #endif
227 }
228
229
230 void SAX2PrintHandlers::ignorableWhitespace( const   XMLCh* const chars
231                                             ,const  unsigned int length)
232 {
233     //fFormatter.formatBuf(chars, length, XMLFormatter::NoEscapes);
234 }
235
236
237 void SAX2PrintHandlers::processingInstruction(const  XMLCh* const target
238                                             , const XMLCh* const data)
239 {
240 #if 0
241     fFormatter << XMLFormatter::NoEscapes << gStartPI  << target;
242     if (data)
243         fFormatter << chSpace << data;
244     fFormatter << XMLFormatter::NoEscapes << gEndPI;
245 #endif
246 }
247
248
249 void SAX2PrintHandlers::startDocument()
250 {
251 }
252
253
254 void SAX2PrintHandlers::startElement(const   XMLCh* const    uri,
255                                                                         const   XMLCh* const    localname,
256                                                                         const   XMLCh* const    qname,
257                                     const   Attributes&         attributes)
258 {
259 #if 0
260     // The name has to be representable without any escapes
261     fFormatter  << XMLFormatter::NoEscapes << chOpenAngle ;
262         if ( fExpandNS )
263         {
264                 if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
265                                 fFormatter  << uri << chColon;
266                 fFormatter << localname ;
267         }
268         else
269                 fFormatter << qname ;
270
271     unsigned int len = attributes.getLength();
272     for (unsigned int index = 0; index < len; index++)
273     {
274         //
275         //  Again the name has to be completely representable. But the
276         //  attribute can have refs and requires the attribute style
277         //  escaping.
278         //
279         fFormatter  << XMLFormatter::NoEscapes << chSpace ;
280                 if ( fExpandNS )
281                 {
282                         if (XMLString::compareIString(attributes.getURI(index),XMLUni::fgZeroLenString) != 0)
283                                 fFormatter  << attributes.getURI(index) << chColon;
284                         fFormatter  << attributes.getLocalName(index) ;
285                 }
286                 else
287                         fFormatter  << attributes.getQName(index) ;
288
289                 fFormatter  << chEqual << chDoubleQuote
290                     << XMLFormatter::AttrEscapes
291                             << attributes.getValue(index)
292                     << XMLFormatter::NoEscapes
293                     << chDoubleQuote;
294     }
295     fFormatter << chCloseAngle;
296 #endif
297 }