#include "TSAXParser.h"
#include "TXMLAttr.h"
#include "Varargs.h"
#include "TObjString.h"
#include "TList.h"
#include "TClass.h"
#include "snprintf.h"
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
class TSAXParserCallback {
public:
static void StartDocument(void *fParser);
static void EndDocument(void *fParser);
static void StartElement(void *fParser, const xmlChar *name, const xmlChar **p);
static void EndElement(void *fParser, const xmlChar *name);
static void Characters(void *fParser, const xmlChar *ch, Int_t len);
static void Comment(void *fParser, const xmlChar *value);
static void CdataBlock(void *fParser, const xmlChar *value, Int_t len);
static void Warning(void *fParser, const char *fmt, ...);
static void Error(void *fParser, const char *fmt, ...);
static void FatalError(void *fParser, const char *fmt, ...);
};
ClassImp(TSAXParser)
TSAXParser::TSAXParser()
{
fSAXHandler = new xmlSAXHandler;
memset(fSAXHandler, 0, sizeof(xmlSAXHandler));
fSAXHandler->startDocument =
(startDocumentSAXFunc)TSAXParserCallback::StartDocument;
fSAXHandler->endDocument =
(endDocumentSAXFunc)TSAXParserCallback::EndDocument;
fSAXHandler->startElement =
(startElementSAXFunc)TSAXParserCallback::StartElement;
fSAXHandler->endElement =
(endElementSAXFunc)TSAXParserCallback::EndElement;
fSAXHandler->characters =
(charactersSAXFunc)TSAXParserCallback::Characters;
fSAXHandler->comment =
(commentSAXFunc)TSAXParserCallback::Comment;
fSAXHandler->cdataBlock =
(cdataBlockSAXFunc)TSAXParserCallback::CdataBlock;
fSAXHandler->warning =
(warningSAXFunc)TSAXParserCallback::Warning;
fSAXHandler->error =
(errorSAXFunc)TSAXParserCallback::Error;
fSAXHandler->fatalError =
(fatalErrorSAXFunc)TSAXParserCallback::FatalError;
}
TSAXParser::~TSAXParser()
{
ReleaseUnderlying();
delete fSAXHandler;
}
void TSAXParser::OnStartDocument()
{
Emit("OnStartDocument()");
}
void TSAXParser::OnEndDocument()
{
Emit("OnEndDocument()");
}
void TSAXParser::OnStartElement(const char *name, const TList *attributes)
{
Long_t args[2];
args[0] = (Long_t)name;
args[1] = (Long_t)attributes;
Emit("OnStartElement(const char *, const TList *)", args);
}
void TSAXParser::OnEndElement(const char *name)
{
Emit("OnEndElement(const char *)", name);
}
void TSAXParser::OnCharacters(const char *characters)
{
Emit("OnCharacters(const char *)", characters);
}
void TSAXParser::OnComment(const char *text)
{
Emit("OnComment(const char *)", text);
}
void TSAXParser::OnWarning(const char *text)
{
Emit("OnWarning(const char *)", text);
}
Int_t TSAXParser::OnError(const char *text)
{
Emit("OnError(const char *)", text);
return -3;
}
Int_t TSAXParser::OnFatalError(const char *text)
{
Emit("OnFatalError(const char *)", text);
return -4;
}
void TSAXParser::OnCdataBlock(const char *text, Int_t len)
{
Long_t args[2];
args[0] = (Long_t)text;
args[1] = len;
Emit("OnCdataBlock(const char *, Int_t)", args);
}
Int_t TSAXParser::Parse()
{
if (!fContext) {
return -2;
}
xmlSAXHandlerPtr oldSAX = fContext->sax;
fContext->sax = fSAXHandler;
fContext->userData = this;
InitializeContext();
xmlParseDocument(fContext);
fContext->sax = oldSAX;
if (!fContext->wellFormed && fParseCode == 0) {
fParseCode = -5;
}
ReleaseUnderlying();
return fParseCode;
}
Int_t TSAXParser::ParseFile(const char *filename)
{
if (fContext) {
return -1;
}
fContext = xmlCreateFileParserCtxt(filename);
return Parse();
}
Int_t TSAXParser::ParseBuffer(const char *contents, Int_t len)
{
if (fContext) {
return -1;
}
fContext = xmlCreateMemoryParserCtxt(contents, len);
return Parse();
}
void TSAXParserCallback::StartDocument(void *fParser)
{
TSAXParser *parser = (TSAXParser*)fParser;
parser->OnStartDocument();
}
void TSAXParserCallback::EndDocument(void *fParser)
{
TSAXParser *parser = (TSAXParser*)fParser;
parser->OnEndDocument();
}
void TSAXParserCallback::StartElement(void *fParser, const xmlChar *name,
const xmlChar **p)
{
TSAXParser *parser = (TSAXParser*)fParser;
TList *attributes = new TList;
if (p) {
for (const xmlChar **cur = p; cur && *cur; cur += 2) {
attributes->Add(new TXMLAttr((const char*)*cur,
(const char*)*(cur + 1)));
}
}
parser->OnStartElement((const char*) name, attributes);
attributes->Delete();
delete attributes;
}
void TSAXParserCallback::EndElement(void *fParser, const xmlChar *name)
{
TSAXParser *parser = (TSAXParser*)fParser;
parser->OnEndElement((const char*) name);
}
void TSAXParserCallback::Characters(void *fParser, const xmlChar *ch,
Int_t len)
{
TSAXParser *parser = (TSAXParser*)fParser;
char *str = new char[len+1];
strncpy(str, (const char*) ch, len);
str[len] = '\0';
parser->OnCharacters(str);
delete [] str;
}
void TSAXParserCallback::Comment(void *fParser, const xmlChar *value)
{
TSAXParser *parser = (TSAXParser*)fParser;
parser->OnComment((const char*) value);
}
void TSAXParserCallback::Warning(void * fParser, const char *va_(fmt), ...)
{
TSAXParser *parser = (TSAXParser*)fParser;
va_list arg;
char buff[1024];
va_start(arg, va_(fmt));
vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), va_(fmt), arg);
va_end(arg);
parser->OnWarning(buff);
}
void TSAXParserCallback::Error(void *fParser, const char *va_(fmt), ...)
{
Int_t errorcode;
TSAXParser *parser = (TSAXParser*)fParser;
va_list arg;
char buff[1024];
va_start(arg, va_(fmt));
vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), va_(fmt), arg);
va_end(arg);
errorcode = parser->OnError(buff);
if (errorcode < 0) {
parser->SetParseCode(errorcode);
}
if (errorcode < 0 && parser->GetStopOnError()) {
parser->StopParser();
}
}
void TSAXParserCallback::FatalError(void *fParser, const char *va_(fmt), ...)
{
Int_t errorcode;
TSAXParser *parser = (TSAXParser*)fParser;
va_list arg;
char buff[1024];
va_start(arg, va_(fmt));
vsnprintf(buff, sizeof(buff)/sizeof(buff[0]), va_(fmt), arg);
va_end(arg);
errorcode = parser->OnFatalError(buff);
if (errorcode < 0) {
parser->SetParseCode(errorcode);
parser->StopParser();
}
}
void TSAXParserCallback::CdataBlock(void *fParser, const xmlChar *value,
Int_t len)
{
TSAXParser *parser = (TSAXParser*)fParser;
parser->OnCdataBlock((const char*)value, len);
}
void TSAXParser::ConnectToHandler(const char *handlerName, void *handler)
{
const TString kFunctionsName [] = {
"OnStartDocument()",
"OnEndDocument()",
"OnStartElement(const char *, const TList *)",
"OnEndElement(const char *)",
"OnCharacters(const char *)",
"OnComment(const char *)",
"OnWarning(const char *)",
"OnError(const char *)",
"OnFatalError(const char *)",
"OnCdataBlock(const char *, Int_t)"
};
TClass *cl = TClass::GetClass(handlerName);
for (Int_t i = 0; i < 10; i++) {
if (CheckConnectArgs(this, this->IsA(), kFunctionsName[i],
cl, kFunctionsName[i]) != -1)
Connect(kFunctionsName[i], handlerName, handler, kFunctionsName[i]);
}
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.