Logo ROOT  
Reference Guide
TSAXParser.cxx
Go to the documentation of this file.
1// @(#)root/xmlparser:$Id$
2// Author: Jose Lo 12/1/2005
3
4/*************************************************************************
5 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13\class TSAXParser
14\ingroup IO
15
16TSAXParser is a subclass of TXMLParser, it is a wraper class to
17libxml library.
18SAX (Simple API for XML) is an event based interface, which doesn't
19maintain the DOM tree in memory, in other words, it's much more
20efficient for large document.
21TSAXParserCallback contains a number of callback routines to the
22parser in a xmlSAXHandler structure. The parser will then parse the
23document and call the appropriate callback when certain conditions
24occur.
25*/
26
27/*************************************************************************
28 This source is based on libxml++, a C++ wrapper for the libxml XML
29 parser library.Copyright (C) 2000 by Ari Johnson
30
31 libxml++ are copyright (C) 2000 by Ari Johnson, and are covered by the
32 GNU Lesser General Public License, which should be included with
33 libxml++ as the file COPYING.
34 *************************************************************************/
35
36#include "TSAXParser.h"
37#include "TXMLAttr.h"
38#include "Varargs.h"
39#include "TList.h"
40#include "TClass.h"
41
42#include <libxml/parser.h>
43#include <libxml/parserInternals.h>
44
45
46class TSAXParserCallback {
47public:
48 static void StartDocument(void *fParser);
49 static void EndDocument(void *fParser);
50 static void StartElement(void *fParser, const xmlChar *name, const xmlChar **p);
51 static void EndElement(void *fParser, const xmlChar *name);
52 static void Characters(void *fParser, const xmlChar *ch, Int_t len);
53 static void Comment(void *fParser, const xmlChar *value);
54 static void CdataBlock(void *fParser, const xmlChar *value, Int_t len);
55 static void Warning(void *fParser, const char *fmt, ...);
56 static void Error(void *fParser, const char *fmt, ...);
57 static void FatalError(void *fParser, const char *fmt, ...);
58};
59
60
62
63////////////////////////////////////////////////////////////////////////////////
64/// Create SAX parser.
65
67{
68 fSAXHandler = new xmlSAXHandler;
69 memset(fSAXHandler, 0, sizeof(xmlSAXHandler));
70
71 fSAXHandler->startDocument =
72 (startDocumentSAXFunc)TSAXParserCallback::StartDocument;
73 fSAXHandler->endDocument =
74 (endDocumentSAXFunc)TSAXParserCallback::EndDocument;
75 fSAXHandler->startElement =
76 (startElementSAXFunc)TSAXParserCallback::StartElement;
77 fSAXHandler->endElement =
78 (endElementSAXFunc)TSAXParserCallback::EndElement;
79 fSAXHandler->characters =
80 (charactersSAXFunc)TSAXParserCallback::Characters;
81 fSAXHandler->comment =
82 (commentSAXFunc)TSAXParserCallback::Comment;
83 fSAXHandler->cdataBlock =
84 (cdataBlockSAXFunc)TSAXParserCallback::CdataBlock;
85 fSAXHandler->warning =
86 (warningSAXFunc)TSAXParserCallback::Warning;
87 fSAXHandler->error =
88 (errorSAXFunc)TSAXParserCallback::Error;
89 fSAXHandler->fatalError =
90 (fatalErrorSAXFunc)TSAXParserCallback::FatalError;
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// TSAXParser desctructor
95
97{
99
100 delete fSAXHandler;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Emit a signal for OnStartDocument.
105
107{
108 Emit("OnStartDocument()");
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Emit a signal for OnEndDocument.
113
115{
116 Emit("OnEndDocument()");
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Emit a signal for OnStarElement, where name is the Element's name and
121/// attribute is a TList of (TObjString*, TObjString *) TPair's.
122/// The TPair's key is the attribute's name and value is the attribute's
123/// value.
124
125void TSAXParser::OnStartElement(const char *name, const TList *attributes)
126{
127 Long_t args[2];
128 args[0] = (Long_t)name;
129 args[1] = (Long_t)attributes;
130
131 Emit("OnStartElement(const char *, const TList *)", args);
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Emit a signal for OnEndElement, where name is the Element's name.
136
138{
139 Emit("OnEndElement(const char *)", name);
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Emit a signal for OnCharacters, where characters are the characters
144/// outside of tags.
145
146void TSAXParser::OnCharacters(const char *characters)
147{
148 Emit("OnCharacters(const char *)", characters);
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Emit a signal for OnComment, where text is the comment.
153
155{
156 Emit("OnComment(const char *)", text);
157}
158
159////////////////////////////////////////////////////////////////////////////////
160/// Emit a signal for OnWarning, where text is the warning.
161
163{
164 Emit("OnWarning(const char *)", text);
165}
166
167////////////////////////////////////////////////////////////////////////////////
168/// Emit a signal for OnError, where text is the error and it returns the
169/// Parse Error Code, see TXMLParser.
170
172{
173 Emit("OnError(const char *)", text);
174 return -3;
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// Emit a signal for OnFactalError, where text is the error and it
179/// returns the Parse Error Code, see TXMLParser.
180
182{
183 Emit("OnFatalError(const char *)", text);
184 return -4;
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Emit a signal for OnCdataBlock.
189
190void TSAXParser::OnCdataBlock(const char *text, Int_t len)
191{
192 Long_t args[2];
193 args[0] = (Long_t)text;
194 args[1] = len;
195
196 Emit("OnCdataBlock(const char *, Int_t)", args);
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// This function parses the xml file, by initializing the parser and checks
201/// whether the parse context is created or not, it will check as well
202/// whether the document is well formated.
203/// It returns the parse error code, see TXMLParser.
204
206{
207 if (!fContext) {
208 return -2;
209 }
210
211 xmlSAXHandlerPtr oldSAX = fContext->sax;
212 fContext->sax = fSAXHandler;
213 fContext->userData = this;
214
216
217 xmlParseDocument(fContext);
218
219 fContext->sax = oldSAX;
220
221 if (!fContext->wellFormed && fParseCode == 0) {
222 fParseCode = -5;
223 }
224
226
227 return fParseCode;
228}
229
230////////////////////////////////////////////////////////////////////////////////
231/// It creates the parse context of the xml file, where the xml file name is
232/// filename. If context is created sucessfully, it will call Parse()
233/// It returns parse error code, see TXMLParser.
234
235Int_t TSAXParser::ParseFile(const char *filename)
236{
237 // Attempt to parse a second file while a parse is in progress.
238 if (fContext) {
239 return -1;
240 }
241
242 fContext = xmlCreateFileParserCtxt(filename);
243 return Parse();
244}
245
246////////////////////////////////////////////////////////////////////////////////
247/// It parse the contents, instead of a file.
248/// It will return error if is attempted to parse a second file while
249/// a parse is in progres.
250/// It returns parse code error, see TXMLParser.
251
252Int_t TSAXParser::ParseBuffer(const char *contents, Int_t len)
253{
254 // Attempt to parse a second file while a parse is in progress.
255 if (fContext) {
256 return -1;
257 }
258
259 fContext = xmlCreateMemoryParserCtxt(contents, len);
260 return Parse();
261}
262
263
264//--- TSAXParserCallback -------------------------------------------------------
265
266////////////////////////////////////////////////////////////////////////////////
267/// StartDocument Callback function.
268
269void TSAXParserCallback::StartDocument(void *fParser)
270{
271 TSAXParser *parser = (TSAXParser*)fParser;
272 parser->OnStartDocument();
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// EndDocument callback function.
277
278void TSAXParserCallback::EndDocument(void *fParser)
279{
280 TSAXParser *parser = (TSAXParser*)fParser;
281 parser->OnEndDocument();
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// StartElement callback function, where name is the name of the element
286/// and p contains the attributes for the start tag.
287
288void TSAXParserCallback::StartElement(void *fParser, const xmlChar *name,
289 const xmlChar **p)
290{
291 TSAXParser *parser = (TSAXParser*)fParser;
292 TList *attributes = new TList;
293
294 if (p) {
295 for (const xmlChar **cur = p; cur && *cur; cur += 2) {
296 attributes->Add(new TXMLAttr((const char*)*cur,
297 (const char*)*(cur + 1)));
298 }
299 }
300
301 parser->OnStartElement((const char*) name, attributes);
302
303 attributes->Delete();
304 delete attributes;
305}
306
307////////////////////////////////////////////////////////////////////////////////
308/// EndElement callback function, where name is the name of the element.
309
310void TSAXParserCallback::EndElement(void *fParser, const xmlChar *name)
311{
312 TSAXParser *parser = (TSAXParser*)fParser;
313 parser->OnEndElement((const char*) name);
314}
315
316////////////////////////////////////////////////////////////////////////////////
317/// Character callback function. It is called when there are characters that
318/// are outside of tags get parsed and the context will be stored in ch,
319/// len is the length of ch.
320
321void TSAXParserCallback::Characters(void *fParser, const xmlChar *ch,
322 Int_t len)
323{
324 TSAXParser *parser = (TSAXParser*)fParser;
325
326 char *str = new char[len+1];
327 strlcpy(str, (const char*) ch, len+1);
328 str[len] = '\0';
329
330 parser->OnCharacters(str);
331
332 delete [] str;
333}
334
335////////////////////////////////////////////////////////////////////////////////
336/// Comment callback function.
337/// Comment of the xml file will be parsed to value.
338
339void TSAXParserCallback::Comment(void *fParser, const xmlChar *value)
340{
341 TSAXParser *parser = (TSAXParser*)fParser;
342 parser->OnComment((const char*) value);
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Warning callback function. Warnings while parsing a xml file will
347/// be stored at fmt.
348
349void TSAXParserCallback::Warning(void * fParser, const char *va_(fmt), ...)
350{
351 TSAXParser *parser = (TSAXParser*)fParser;
352
353 va_list arg;
354 char buffer[2048];
355
356 va_start(arg, va_(fmt));
357 vsnprintf(buffer, 2048, va_(fmt), arg);
358 va_end(arg);
359
360 TString buff(buffer);
361
362 parser->OnWarning(buff.Data());
363}
364
365////////////////////////////////////////////////////////////////////////////////
366/// Error callback function. Errors while parsing a xml file will be stored
367/// at fmt.
368
369void TSAXParserCallback::Error(void *fParser, const char *va_(fmt), ...)
370{
371 Int_t errorcode;
372 TSAXParser *parser = (TSAXParser*)fParser;
373
374 va_list arg;
375 char buffer[2048];
376
377 va_start(arg, va_(fmt));
378 vsnprintf(buffer, 2048, va_(fmt), arg);
379 va_end(arg);
380
381 TString buff(buffer);
382
383 errorcode = parser->OnError(buff.Data());
384 if (errorcode < 0) { //When error occurs, write fErrorCode
385 parser->SetParseCode(errorcode);
386 }
387
388 if (errorcode < 0 && parser->GetStopOnError()) {
389 //When GetStopOnError is enabled, stop the parse when an error occurs
390 parser->StopParser();
391 }
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// FactalError callback function. Factal errors while parsing a xml file
396/// will be stored at fmt.
397
398void TSAXParserCallback::FatalError(void *fParser, const char *va_(fmt), ...)
399{
400 Int_t errorcode;
401 TSAXParser *parser = (TSAXParser*)fParser;
402
403 va_list arg;
404 char buffer[2048];
405
406 va_start(arg, va_(fmt));
407 vsnprintf(buffer, 2048, va_(fmt), arg);
408 va_end(arg);
409
410 TString buff(buffer);
411
412 errorcode = parser->OnFatalError(buff);
413 if (errorcode < 0) {
414 parser->SetParseCode(errorcode);
415 parser->StopParser();
416 }
417}
418
419////////////////////////////////////////////////////////////////////////////////
420/// CdataBlock Callback function.
421
422void TSAXParserCallback::CdataBlock(void *fParser, const xmlChar *value,
423 Int_t len)
424{
425 TSAXParser *parser = (TSAXParser*)fParser;
426 parser->OnCdataBlock((const char*)value, len);
427}
428
429////////////////////////////////////////////////////////////////////////////////
430/// A default TSAXParser to a user-defined Handler connection function.
431/// This function makes connection between various function from TSAXParser
432/// with the user-define SAX Handler, whose functions has to be exactly the
433/// same as in TSAXParser.
434///
435/// \param[in] handler Name User-defined SAX Handler class name
436/// \param[in] handler Pointer to the user-defined SAX Handler
437///
438/// See SAXHandler.C tutorial.
439
440void TSAXParser::ConnectToHandler(const char *handlerName, void *handler)
441{
442 const TString kFunctionsName [] = {
443 "OnStartDocument()",
444 "OnEndDocument()",
445 "OnStartElement(const char *, const TList *)",
446 "OnEndElement(const char *)",
447 "OnCharacters(const char *)",
448 "OnComment(const char *)",
449 "OnWarning(const char *)",
450 "OnError(const char *)",
451 "OnFatalError(const char *)",
452 "OnCdataBlock(const char *, Int_t)"
453 };
454
455 TClass *cl = TClass::GetClass(handlerName);
456
457 for (Int_t i = 0; i < 10; i++) {
458 if (CheckConnectArgs(this, this->IsA(), kFunctionsName[i],
459 cl, kFunctionsName[i]) != -1)
460 Connect(kFunctionsName[i], handlerName, handler, kFunctionsName[i]);
461 }
462}
int Int_t
Definition: RtypesCore.h:43
long Long_t
Definition: RtypesCore.h:52
#define ClassImp(name)
Definition: Rtypes.h:361
void Error(const char *location, const char *msgfmt,...)
void Warning(const char *location, const char *msgfmt,...)
char name[80]
Definition: TGX11.cxx:109
#define va_(arg)
Definition: Varargs.h:41
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2948
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
static Int_t CheckConnectArgs(TQObject *sender, TClass *sender_class, const char *signal, TClass *receiver_class, const char *slot)
Checking of consitency of sender/receiver methods/arguments.
Definition: TQObject.cxx:180
void Emit(const char *signal, const T &arg)
Activate signal with single parameter.
Definition: TQObject.h:164
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition: TQObject.cxx:866
TSAXParser is a subclass of TXMLParser, it is a wraper class to libxml library.
Definition: TSAXParser.h:23
virtual void OnWarning(const char *text)
Emit a signal for OnWarning, where text is the warning.
Definition: TSAXParser.cxx:162
virtual void OnCdataBlock(const char *text, Int_t len)
Emit a signal for OnCdataBlock.
Definition: TSAXParser.cxx:190
virtual Int_t ParseBuffer(const char *contents, Int_t len)
It parse the contents, instead of a file.
Definition: TSAXParser.cxx:252
virtual Int_t Parse()
This function parses the xml file, by initializing the parser and checks whether the parse context is...
Definition: TSAXParser.cxx:205
virtual ~TSAXParser()
TSAXParser desctructor.
Definition: TSAXParser.cxx:96
virtual Int_t OnFatalError(const char *text)
Emit a signal for OnFactalError, where text is the error and it returns the Parse Error Code,...
Definition: TSAXParser.cxx:181
virtual Int_t ParseFile(const char *filename)
It creates the parse context of the xml file, where the xml file name is filename.
Definition: TSAXParser.cxx:235
virtual void OnCharacters(const char *characters)
Emit a signal for OnCharacters, where characters are the characters outside of tags.
Definition: TSAXParser.cxx:146
TSAXParser()
Create SAX parser.
Definition: TSAXParser.cxx:66
virtual void OnComment(const char *text)
Emit a signal for OnComment, where text is the comment.
Definition: TSAXParser.cxx:154
virtual void OnStartElement(const char *name, const TList *attr)
Emit a signal for OnStarElement, where name is the Element's name and attribute is a TList of (TObjSt...
Definition: TSAXParser.cxx:125
virtual void OnEndDocument()
Emit a signal for OnEndDocument.
Definition: TSAXParser.cxx:114
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
Definition: TSAXParser.cxx:440
virtual void OnStartDocument()
Emit a signal for OnStartDocument.
Definition: TSAXParser.cxx:106
virtual void OnEndElement(const char *name)
Emit a signal for OnEndElement, where name is the Element's name.
Definition: TSAXParser.cxx:137
virtual Int_t OnError(const char *text)
Emit a signal for OnError, where text is the error and it returns the Parse Error Code,...
Definition: TSAXParser.cxx:171
_xmlSAXHandler * fSAXHandler
libxml2 SAX handler
Definition: TSAXParser.h:28
Basic string class.
Definition: TString.h:131
TXMLAttribute is the attribute of an Element.
Definition: TXMLAttr.h:18
virtual void InitializeContext()
Initialize parser parameters, such as, disactivate non-standards libxml1 features,...
Definition: TXMLParser.cxx:153
virtual void SetParseCode(Int_t code)
Set the parse code:
Definition: TXMLParser.cxx:182
_xmlParserCtxt * fContext
Parse the xml file.
Definition: TXMLParser.h:31
Int_t fParseCode
To keep track of the errorcodes.
Definition: TXMLParser.h:37
virtual void ReleaseUnderlying()
To release any existing document.
Definition: TXMLParser.cxx:93
virtual void StopParser()
Stops parsing.
Definition: TXMLParser.cxx:167
TText * text