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
62
63////////////////////////////////////////////////////////////////////////////////
64/// Create SAX parser.
65
92
93////////////////////////////////////////////////////////////////////////////////
94/// TSAXParser desctructor
95
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 Longptr_t args[2];
128 args[0] = (Longptr_t)name;
129 args[1] = (Longptr_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
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{
174 errmsg.Form("Source line: %d %s", fContext->input->line, text);
175
176 Emit("OnError(const char *)", errmsg.Data());
177 return -3;
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Emit a signal for OnFactalError, where text is the error and it
182/// returns the Parse Error Code, see TXMLParser.
183
185{
187 errmsg.Form("Source line: %d %s", fContext->input->line, text);
188
189 Emit("OnFatalError(const char *)", errmsg.Data());
190 return -4;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Emit a signal for OnCdataBlock.
195
197{
198 Longptr_t args[2];
199 args[0] = (Longptr_t)text;
200 args[1] = len;
201
202 Emit("OnCdataBlock(const char *, Int_t)", args);
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// This function parses the xml file, by initializing the parser and checks
207/// whether the parse context is created or not, it will check as well
208/// whether the document is well formated.
209/// It returns the parse error code, see TXMLParser.
210
212{
213 if (!fContext) {
214 return -2;
215 }
216
218 fContext->sax = fSAXHandler;
219 fContext->userData = this;
220
222
224
225 fContext->sax = oldSAX;
226
227 if (!fContext->wellFormed && fParseCode == 0) {
228 fParseCode = -5;
229 }
230
232
233 return fParseCode;
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// It creates the parse context of the xml file, where the xml file name is
238/// filename. If context is created sucessfully, it will call Parse()
239/// It returns parse error code, see TXMLParser.
240
242{
243 // Attempt to parse a second file while a parse is in progress.
244 if (fContext) {
245 return -1;
246 }
247
249 return Parse();
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// It parse the contents, instead of a file.
254/// It will return error if is attempted to parse a second file while
255/// a parse is in progres.
256/// It returns parse code error, see TXMLParser.
257
259{
260 // Attempt to parse a second file while a parse is in progress.
261 if (fContext) {
262 return -1;
263 }
264
266 return Parse();
267}
268
269
270//--- TSAXParserCallback -------------------------------------------------------
271
272////////////////////////////////////////////////////////////////////////////////
273/// StartDocument Callback function.
274
276{
277 TSAXParser *parser = (TSAXParser*)fParser;
278 parser->OnStartDocument();
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// EndDocument callback function.
283
285{
286 TSAXParser *parser = (TSAXParser*)fParser;
287 parser->OnEndDocument();
288}
289
290////////////////////////////////////////////////////////////////////////////////
291/// StartElement callback function, where name is the name of the element
292/// and p contains the attributes for the start tag.
293
295 const xmlChar **p)
296{
297 TSAXParser *parser = (TSAXParser*)fParser;
298 TList *attributes = new TList;
299
300 if (p) {
301 for (const xmlChar **cur = p; cur && *cur; cur += 2) {
302 attributes->Add(new TXMLAttr((const char*)*cur,
303 (const char*)*(cur + 1)));
304 }
305 }
306
307 parser->OnStartElement((const char*) name, attributes);
308
309 attributes->Delete();
310 delete attributes;
311}
312
313////////////////////////////////////////////////////////////////////////////////
314/// EndElement callback function, where name is the name of the element.
315
317{
318 TSAXParser *parser = (TSAXParser*)fParser;
319 parser->OnEndElement((const char*) name);
320}
321
322////////////////////////////////////////////////////////////////////////////////
323/// Character callback function. It is called when there are characters that
324/// are outside of tags get parsed and the context will be stored in ch,
325/// len is the length of ch.
326
328 Int_t len)
329{
330 TSAXParser *parser = (TSAXParser*)fParser;
331
332 char *str = new char[len+1];
333 strlcpy(str, (const char*) ch, len+1);
334 str[len] = '\0';
335
336 parser->OnCharacters(str);
337
338 delete [] str;
339}
340
341////////////////////////////////////////////////////////////////////////////////
342/// Comment callback function.
343/// Comment of the xml file will be parsed to value.
344
346{
347 TSAXParser *parser = (TSAXParser*)fParser;
348 parser->OnComment((const char*) value);
349}
350
351////////////////////////////////////////////////////////////////////////////////
352/// Warning callback function. Warnings while parsing a xml file will
353/// be stored at fmt.
354
355void TSAXParserCallback::Warning(void * fParser, const char *va_(fmt), ...)
356{
357 TSAXParser *parser = (TSAXParser*)fParser;
358
359 va_list arg;
360 char buffer[2048];
361
362 va_start(arg, va_(fmt));
363 vsnprintf(buffer, 2048, va_(fmt), arg);
364 va_end(arg);
365
366 TString buff(buffer);
367
368 parser->OnWarning(buff.Data());
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Error callback function. Errors while parsing a xml file will be stored
373/// at fmt.
374
375void TSAXParserCallback::Error(void *fParser, const char *va_(fmt), ...)
376{
378 TSAXParser *parser = (TSAXParser*)fParser;
379
380 va_list arg;
381 char buffer[2048];
382
383 va_start(arg, va_(fmt));
384 vsnprintf(buffer, 2048, va_(fmt), arg);
385 va_end(arg);
386
387 TString buff(buffer);
388
389 errorcode = parser->OnError(buff.Data());
390 if (errorcode < 0) { //When error occurs, write fErrorCode
391 parser->SetParseCode(errorcode);
392 }
393
394 if (errorcode < 0 && parser->GetStopOnError()) {
395 //When GetStopOnError is enabled, stop the parse when an error occurs
396 parser->StopParser();
397 }
398}
399
400////////////////////////////////////////////////////////////////////////////////
401/// FactalError callback function. Factal errors while parsing a xml file
402/// will be stored at fmt.
403
404void TSAXParserCallback::FatalError(void *fParser, const char *va_(fmt), ...)
405{
407 TSAXParser *parser = (TSAXParser*)fParser;
408
409 va_list arg;
410 char buffer[2048];
411
412 va_start(arg, va_(fmt));
413 vsnprintf(buffer, 2048, va_(fmt), arg);
414 va_end(arg);
415
416 TString buff(buffer);
417
418 errorcode = parser->OnFatalError(buff);
419 if (errorcode < 0) {
420 parser->SetParseCode(errorcode);
421 parser->StopParser();
422 }
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// CdataBlock Callback function.
427
429 Int_t len)
430{
431 TSAXParser *parser = (TSAXParser*)fParser;
432 parser->OnCdataBlock((const char*)value, len);
433}
434
435////////////////////////////////////////////////////////////////////////////////
436/// A default TSAXParser to a user-defined Handler connection function.
437/// This function makes connection between various function from TSAXParser
438/// with the user-define SAX Handler, whose functions has to be exactly the
439/// same as in TSAXParser.
440///
441/// \param[in] handlerName User-defined SAX Handler class name
442/// \param[in] handler Pointer to the user-defined SAX Handler
443///
444/// See SAXHandler.C tutorial.
445
446void TSAXParser::ConnectToHandler(const char *handlerName, void *handler)
447{
448 const TString kFunctionsName [] = {
449 "OnStartDocument()",
450 "OnEndDocument()",
451 "OnStartElement(const char *, const TList *)",
452 "OnEndElement(const char *)",
453 "OnCharacters(const char *)",
454 "OnComment(const char *)",
455 "OnWarning(const char *)",
456 "OnError(const char *)",
457 "OnFatalError(const char *)",
458 "OnCdataBlock(const char *, Int_t)"
459 };
460
462
463 for (Int_t i = 0; i < 10; i++) {
464 if (CheckConnectArgs(this, this->IsA(), kFunctionsName[i],
465 cl, kFunctionsName[i]) != -1)
467 }
468}
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:89
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:84
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:2973
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:467
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:175
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:865
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:138
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.