Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "strlcpy.h"
40#include "TList.h"
41#include "TClass.h"
42
43#include <libxml/parser.h>
44#include <libxml/parserInternals.h>
45
46
48public:
49 static void StartDocument(void *fParser);
50 static void EndDocument(void *fParser);
51 static void StartElement(void *fParser, const xmlChar *name, const xmlChar **p);
52 static void EndElement(void *fParser, const xmlChar *name);
53 static void Characters(void *fParser, const xmlChar *ch, Int_t len);
54 static void Comment(void *fParser, const xmlChar *value);
55 static void CdataBlock(void *fParser, const xmlChar *value, Int_t len);
56 static void Warning(void *fParser, const char *fmt, ...);
57 static void Error(void *fParser, const char *fmt, ...);
58 static void FatalError(void *fParser, const char *fmt, ...);
59};
60
61
63
64////////////////////////////////////////////////////////////////////////////////
65/// Create SAX parser.
66
68{
69 fSAXHandler = new xmlSAXHandler;
70 memset(fSAXHandler, 0, sizeof(xmlSAXHandler));
71
72 fSAXHandler->startDocument =
73 (startDocumentSAXFunc)TSAXParserCallback::StartDocument;
74 fSAXHandler->endDocument =
75 (endDocumentSAXFunc)TSAXParserCallback::EndDocument;
76 fSAXHandler->startElement =
77 (startElementSAXFunc)TSAXParserCallback::StartElement;
78 fSAXHandler->endElement =
79 (endElementSAXFunc)TSAXParserCallback::EndElement;
80 fSAXHandler->characters =
81 (charactersSAXFunc)TSAXParserCallback::Characters;
82 fSAXHandler->comment =
83 (commentSAXFunc)TSAXParserCallback::Comment;
84 fSAXHandler->cdataBlock =
85 (cdataBlockSAXFunc)TSAXParserCallback::CdataBlock;
86 fSAXHandler->warning =
87 (warningSAXFunc)TSAXParserCallback::Warning;
88 fSAXHandler->error =
89 (errorSAXFunc)TSAXParserCallback::Error;
90 fSAXHandler->fatalError =
91 (fatalErrorSAXFunc)TSAXParserCallback::FatalError;
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// TSAXParser desctructor
96
98{
100
101 delete fSAXHandler;
102}
103
104////////////////////////////////////////////////////////////////////////////////
105/// Emit a signal for OnStartDocument.
106
108{
109 Emit("OnStartDocument()");
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Emit a signal for OnEndDocument.
114
116{
117 Emit("OnEndDocument()");
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Emit a signal for OnStarElement, where name is the Element's name and
122/// attribute is a TList of (TObjString*, TObjString *) TPair's.
123/// The TPair's key is the attribute's name and value is the attribute's
124/// value.
125
126void TSAXParser::OnStartElement(const char *name, const TList *attributes)
127{
128 Longptr_t args[2];
129 args[0] = (Longptr_t)name;
130 args[1] = (Longptr_t)attributes;
131
132 Emit("OnStartElement(const char *, const TList *)", args);
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Emit a signal for OnEndElement, where name is the Element's name.
137
139{
140 Emit("OnEndElement(const char *)", name);
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Emit a signal for OnCharacters, where characters are the characters
145/// outside of tags.
146
147void TSAXParser::OnCharacters(const char *characters)
148{
149 Emit("OnCharacters(const char *)", characters);
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Emit a signal for OnComment, where text is the comment.
154
156{
157 Emit("OnComment(const char *)", text);
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Emit a signal for OnWarning, where text is the warning.
162
164{
165 Emit("OnWarning(const char *)", text);
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Emit a signal for OnError, where text is the error and it returns the
170/// Parse Error Code, see TXMLParser.
171
173{
174 TString errmsg;
175 errmsg.Form("Source line: %d %s", fContext->input->line, text);
176
177 Emit("OnError(const char *)", errmsg.Data());
178 return -3;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Emit a signal for OnFactalError, where text is the error and it
183/// returns the Parse Error Code, see TXMLParser.
184
186{
187 TString errmsg;
188 errmsg.Form("Source line: %d %s", fContext->input->line, text);
189
190 Emit("OnFatalError(const char *)", errmsg.Data());
191 return -4;
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Emit a signal for OnCdataBlock.
196
198{
199 Longptr_t args[2];
200 args[0] = (Longptr_t)text;
201 args[1] = len;
202
203 Emit("OnCdataBlock(const char *, Int_t)", args);
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// This function parses the xml file, by initializing the parser and checks
208/// whether the parse context is created or not, it will check as well
209/// whether the document is well formated.
210/// It returns the parse error code, see TXMLParser.
211
213{
214 if (!fContext) {
215 return -2;
216 }
217
218 xmlSAXHandlerPtr oldSAX = fContext->sax;
219 fContext->sax = fSAXHandler;
220 fContext->userData = this;
221
223
224 xmlParseDocument(fContext);
225
226 fContext->sax = oldSAX;
227
228 if (!fContext->wellFormed && fParseCode == 0) {
229 fParseCode = -5;
230 }
231
233
234 return fParseCode;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// It creates the parse context of the xml file, where the xml file name is
239/// filename. If context is created sucessfully, it will call Parse()
240/// It returns parse error code, see TXMLParser.
241
243{
244 // Attempt to parse a second file while a parse is in progress.
245 if (fContext) {
246 return -1;
247 }
248
249 fContext = xmlCreateFileParserCtxt(filename);
250 return Parse();
251}
252
253////////////////////////////////////////////////////////////////////////////////
254/// It parse the contents, instead of a file.
255/// It will return error if is attempted to parse a second file while
256/// a parse is in progres.
257/// It returns parse code error, see TXMLParser.
258
260{
261 // Attempt to parse a second file while a parse is in progress.
262 if (fContext) {
263 return -1;
264 }
265
266 fContext = xmlCreateMemoryParserCtxt(contents, len);
267 return Parse();
268}
269
270
271//--- TSAXParserCallback -------------------------------------------------------
272
273////////////////////////////////////////////////////////////////////////////////
274/// StartDocument Callback function.
275
277{
278 TSAXParser *parser = (TSAXParser*)fParser;
279 parser->OnStartDocument();
280}
281
282////////////////////////////////////////////////////////////////////////////////
283/// EndDocument callback function.
284
286{
287 TSAXParser *parser = (TSAXParser*)fParser;
288 parser->OnEndDocument();
289}
290
291////////////////////////////////////////////////////////////////////////////////
292/// StartElement callback function, where name is the name of the element
293/// and p contains the attributes for the start tag.
294
295void TSAXParserCallback::StartElement(void *fParser, const xmlChar *name,
296 const xmlChar **p)
297{
298 TSAXParser *parser = (TSAXParser*)fParser;
299 TList *attributes = new TList;
300
301 if (p) {
302 for (const xmlChar **cur = p; cur && *cur; cur += 2) {
303 attributes->Add(new TXMLAttr((const char*)*cur,
304 (const char*)*(cur + 1)));
305 }
306 }
307
308 parser->OnStartElement((const char*) name, attributes);
309
310 attributes->Delete();
311 delete attributes;
312}
313
314////////////////////////////////////////////////////////////////////////////////
315/// EndElement callback function, where name is the name of the element.
316
317void TSAXParserCallback::EndElement(void *fParser, const xmlChar *name)
318{
319 TSAXParser *parser = (TSAXParser*)fParser;
320 parser->OnEndElement((const char*) name);
321}
322
323////////////////////////////////////////////////////////////////////////////////
324/// Character callback function. It is called when there are characters that
325/// are outside of tags get parsed and the context will be stored in ch,
326/// len is the length of ch.
327
328void TSAXParserCallback::Characters(void *fParser, const xmlChar *ch,
329 Int_t len)
330{
331 TSAXParser *parser = (TSAXParser*)fParser;
332
333 char *str = new char[len+1];
334 strlcpy(str, (const char*) ch, len+1);
335 str[len] = '\0';
336
337 parser->OnCharacters(str);
338
339 delete [] str;
340}
341
342////////////////////////////////////////////////////////////////////////////////
343/// Comment callback function.
344/// Comment of the xml file will be parsed to value.
345
346void TSAXParserCallback::Comment(void *fParser, const xmlChar *value)
347{
348 TSAXParser *parser = (TSAXParser*)fParser;
349 parser->OnComment((const char*) value);
350}
351
352////////////////////////////////////////////////////////////////////////////////
353/// Warning callback function. Warnings while parsing a xml file will
354/// be stored at fmt.
355
356void TSAXParserCallback::Warning(void * fParser, const char *va_(fmt), ...)
357{
358 TSAXParser *parser = (TSAXParser*)fParser;
359
360 va_list arg;
361 char buffer[2048];
362
363 va_start(arg, va_(fmt));
364 vsnprintf(buffer, 2048, va_(fmt), arg);
365 va_end(arg);
366
367 TString buff(buffer);
368
369 parser->OnWarning(buff.Data());
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Error callback function. Errors while parsing a xml file will be stored
374/// at fmt.
375
376void TSAXParserCallback::Error(void *fParser, const char *va_(fmt), ...)
377{
378 Int_t errorcode;
379 TSAXParser *parser = (TSAXParser*)fParser;
380
381 va_list arg;
382 char buffer[2048];
383
384 va_start(arg, va_(fmt));
385 vsnprintf(buffer, 2048, va_(fmt), arg);
386 va_end(arg);
387
388 TString buff(buffer);
389
390 errorcode = parser->OnError(buff.Data());
391 if (errorcode < 0) { //When error occurs, write fErrorCode
392 parser->SetParseCode(errorcode);
393 }
394
395 if (errorcode < 0 && parser->GetStopOnError()) {
396 //When GetStopOnError is enabled, stop the parse when an error occurs
397 parser->StopParser();
398 }
399}
400
401////////////////////////////////////////////////////////////////////////////////
402/// FactalError callback function. Factal errors while parsing a xml file
403/// will be stored at fmt.
404
405void TSAXParserCallback::FatalError(void *fParser, const char *va_(fmt), ...)
406{
407 Int_t errorcode;
408 TSAXParser *parser = (TSAXParser*)fParser;
409
410 va_list arg;
411 char buffer[2048];
412
413 va_start(arg, va_(fmt));
414 vsnprintf(buffer, 2048, va_(fmt), arg);
415 va_end(arg);
416
417 TString buff(buffer);
418
419 errorcode = parser->OnFatalError(buff);
420 if (errorcode < 0) {
421 parser->SetParseCode(errorcode);
422 parser->StopParser();
423 }
424}
425
426////////////////////////////////////////////////////////////////////////////////
427/// CdataBlock Callback function.
428
429void TSAXParserCallback::CdataBlock(void *fParser, const xmlChar *value,
430 Int_t len)
431{
432 TSAXParser *parser = (TSAXParser*)fParser;
433 parser->OnCdataBlock((const char*)value, len);
434}
435
436////////////////////////////////////////////////////////////////////////////////
437/// A default TSAXParser to a user-defined Handler connection function.
438/// This function makes connection between various function from TSAXParser
439/// with the user-define SAX Handler, whose functions has to be exactly the
440/// same as in TSAXParser.
441///
442/// \param[in] handlerName User-defined SAX Handler class name
443/// \param[in] handler Pointer to the user-defined SAX Handler
444///
445/// See SAXHandler.C tutorial.
446
447void TSAXParser::ConnectToHandler(const char *handlerName, void *handler)
448{
449 const TString kFunctionsName [] = {
450 "OnStartDocument()",
451 "OnEndDocument()",
452 "OnStartElement(const char *, const TList *)",
453 "OnEndElement(const char *)",
454 "OnCharacters(const char *)",
455 "OnComment(const char *)",
456 "OnWarning(const char *)",
457 "OnError(const char *)",
458 "OnFatalError(const char *)",
459 "OnCdataBlock(const char *, Int_t)"
460 };
461
462 TClass *cl = TClass::GetClass(handlerName);
463
464 for (Int_t i = 0; i < 10; i++) {
465 if (CheckConnectArgs(this, this->IsA(), kFunctionsName[i],
466 cl, kFunctionsName[i]) != -1)
467 Connect(kFunctionsName[i], handlerName, handler, kFunctionsName[i]);
468 }
469}
long Longptr_t
Definition RtypesCore.h:75
#define ClassImp(name)
Definition Rtypes.h:382
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char text
char name[80]
Definition TGX11.cxx:110
#define va_(arg)
Definition Varargs.h:35
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
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:3037
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
static Int_t CheckConnectArgs(TQObject *sender, TClass *sender_class, const char *signal, TClass *receiver_class, const char *slot)
Checking of consistency of sender/receiver methods/arguments.
Definition TQObject.cxx:179
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:869
static void Comment(void *fParser, const xmlChar *value)
Comment callback function.
static void StartDocument(void *fParser)
StartDocument Callback function.
static void CdataBlock(void *fParser, const xmlChar *value, Int_t len)
CdataBlock Callback function.
static void Characters(void *fParser, const xmlChar *ch, Int_t len)
Character callback function.
static void Warning(void *fParser, const char *fmt,...)
Warning callback function.
static void StartElement(void *fParser, const xmlChar *name, const xmlChar **p)
StartElement callback function, where name is the name of the element and p contains the attributes f...
static void Error(void *fParser, const char *fmt,...)
Error callback function.
static void EndDocument(void *fParser)
EndDocument callback function.
static void EndElement(void *fParser, const xmlChar *name)
EndElement callback function, where name is the name of the element.
static void FatalError(void *fParser, const char *fmt,...)
FactalError callback function.
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.
virtual void OnCdataBlock(const char *text, Int_t len)
Emit a signal for OnCdataBlock.
virtual Int_t Parse()
This function parses the xml file, by initializing the parser and checks whether the parse context is...
virtual Int_t OnFatalError(const char *text)
Emit a signal for OnFactalError, where text is the error and it returns the Parse Error Code,...
TClass * IsA() const override
Definition TSAXParser.h:55
virtual void OnCharacters(const char *characters)
Emit a signal for OnCharacters, where characters are the characters outside of tags.
TSAXParser()
Create SAX parser.
Int_t ParseBuffer(const char *contents, Int_t len) override
It parse the contents, instead of a file.
virtual void OnComment(const char *text)
Emit a signal for OnComment, where text is the comment.
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...
virtual void OnEndDocument()
Emit a signal for OnEndDocument.
~TSAXParser() override
TSAXParser desctructor.
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
virtual void OnStartDocument()
Emit a signal for OnStartDocument.
virtual void OnEndElement(const char *name)
Emit a signal for OnEndElement, where name is the Element's name.
Int_t ParseFile(const char *filename) override
It creates the parse context of the xml file, where the xml file name is filename.
virtual Int_t OnError(const char *text)
Emit a signal for OnError, where text is the error and it returns the Parse Error Code,...
_xmlSAXHandler * fSAXHandler
libxml2 SAX handler
Definition TSAXParser.h:28
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
TXMLAttribute is the attribute of an Element.
Definition TXMLAttr.h:18
virtual void InitializeContext()
Initialize parser parameters, such as, disactivate non-standards libxml1 features,...
virtual void SetParseCode(Int_t code)
Set the parse code:
_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.
virtual void StopParser()
Stops parsing.