43 #include <libxml/parser.h>
44 #include <libxml/parserInternals.h>
47 class TSAXParserCallback {
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, ...);
69 fSAXHandler =
new xmlSAXHandler;
70 memset(fSAXHandler, 0,
sizeof(xmlSAXHandler));
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 =
90 fSAXHandler->fatalError =
91 (fatalErrorSAXFunc)TSAXParserCallback::FatalError;
109 Emit(
"OnStartDocument()");
117 Emit(
"OnEndDocument()");
130 args[1] = (
Long_t)attributes;
132 Emit(
"OnStartElement(const char *, const TList *)", args);
140 Emit(
"OnEndElement(const char *)", name);
149 Emit(
"OnCharacters(const char *)", characters);
157 Emit(
"OnComment(const char *)", text);
165 Emit(
"OnWarning(const char *)", text);
174 Emit(
"OnError(const char *)", text);
184 Emit(
"OnFatalError(const char *)", text);
197 Emit(
"OnCdataBlock(const char *, Int_t)", args);
212 xmlSAXHandlerPtr oldSAX =
fContext->sax;
243 fContext = xmlCreateFileParserCtxt(filename);
260 fContext = xmlCreateMemoryParserCtxt(contents, len);
270 void TSAXParserCallback::StartDocument(
void *fParser)
279 void TSAXParserCallback::EndDocument(
void *fParser)
289 void TSAXParserCallback::StartElement(
void *fParser,
const xmlChar *name,
296 for (
const xmlChar **cur = p; cur && *cur; cur += 2) {
298 (
const char*)*(cur + 1)));
311 void TSAXParserCallback::EndElement(
void *fParser,
const xmlChar *name)
322 void TSAXParserCallback::Characters(
void *fParser,
const xmlChar *ch,
327 char *str =
new char[len+1];
328 strlcpy(str, (
const char*) ch, len+1);
340 void TSAXParserCallback::Comment(
void *fParser,
const xmlChar *value)
357 va_start(arg,
va_(fmt));
358 vsnprintf(buffer, 2048,
va_(fmt), arg);
378 va_start(arg,
va_(fmt));
379 vsnprintf(buffer, 2048,
va_(fmt), arg);
384 errorcode = parser->
OnError(buff.Data());
389 if (errorcode < 0 && parser->GetStopOnError()) {
399 void TSAXParserCallback::FatalError(
void *fParser,
const char *
va_(fmt), ...)
407 va_start(arg,
va_(fmt));
408 vsnprintf(buffer, 2048,
va_(fmt), arg);
423 void TSAXParserCallback::CdataBlock(
void *fParser,
const xmlChar *value,
443 const TString kFunctionsName [] = {
446 "OnStartElement(const char *, const TList *)",
447 "OnEndElement(const char *)",
448 "OnCharacters(const char *)",
449 "OnComment(const char *)",
450 "OnWarning(const char *)",
451 "OnError(const char *)",
452 "OnFatalError(const char *)",
453 "OnCdataBlock(const char *, Int_t)"
458 for (
Int_t i = 0; i < 10; i++) {
460 cl, kFunctionsName[i]) != -1)
461 Connect(kFunctionsName[i], handlerName, handler, kFunctionsName[i]);
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
virtual void OnComment(const char *text)
Emit a signal for OnComment, where text is the comment.
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.
virtual void InitializeContext()
Initialize parser parameters, such as, disactivate non-standards libxml1 features, on/off validation, clear error and warning messages.
virtual Int_t ParseBuffer(const char *contents, Int_t len)
It parse the contents, instead of a file.
_xmlSAXHandler * fSAXHandler
libxml2 SAX handler
TSAXParser is a subclass of TXMLParser, it is a wraper class to libxml library.
virtual void OnStartDocument()
Emit a signal for OnStartDocument.
static const char * filename()
virtual void ReleaseUnderlying()
To release any existing document.
Int_t fParseCode
To keep track of the errorcodes.
_xmlParserCtxt * fContext
Parse the xml file.
virtual void OnWarning(const char *text)
Emit a signal for OnWarning, where text is the warning.
virtual Int_t OnError(const char *text)
Emit a signal for OnError, where text is the error and it returns the Parse Error Code...
virtual void SetParseCode(Int_t code)
Set the parse code:
virtual ~TSAXParser()
TSAXParser desctructor.
virtual void OnEndElement(const char *name)
Emit a signal for OnEndElement, where name is the Element's name.
virtual void StopParser()
Stops parsing.
void Error(const char *location, const char *msgfmt,...)
void Emit(const char *signal)
Acitvate signal without args.
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...
virtual Int_t Parse()
This function parses the xml file, by initializing the parser and checks whether the parse context is...
virtual void OnEndDocument()
Emit a signal for OnEndDocument.
The ROOT global object gROOT contains a list of all defined classes.
TXMLAttribute is the attribute of an Element.
void Warning(const char *location, const char *msgfmt,...)
virtual void OnCdataBlock(const char *text, Int_t len)
Emit a signal for OnCdataBlock.
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.
ClassImp(TSAXParser) TSAXParser
Create SAX parser.
virtual void ConnectToHandler(const char *handlerName, void *handler)
A default TSAXParser to a user-defined Handler connection function.
virtual void OnCharacters(const char *characters)
Emit a signal for OnCharacters, where characters are the characters outside of tags.
virtual void Add(TObject *obj)
virtual Int_t OnFatalError(const char *text)
Emit a signal for OnFactalError, where text is the error and it returns the Parse Error Code...
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 Int_t ParseFile(const char *filename)
It creates the parse context of the xml file, where the xml file name is filename.