Commit f117b69f authored by Sebastien Barre's avatar Sebastien Barre

ENH: allow user to specify an encoding for the XML parser, and allow...

ENH: allow user to specify an encoding for the XML parser, and allow developpers to override the creation of the parser itself
parent bf3d326e
......@@ -33,7 +33,7 @@
#include <ctype.h>
#include <sys/stat.h>
vtkCxxRevisionMacro(vtkXMLParser, "1.28");
vtkCxxRevisionMacro(vtkXMLParser, "1.29");
vtkStandardNewMacro(vtkXMLParser);
//----------------------------------------------------------------------------
......@@ -42,6 +42,7 @@ vtkXMLParser::vtkXMLParser()
this->Stream = 0;
this->Parser = 0;
this->FileName = 0;
this->Encoding = 0;
this->InputString = 0;
this->InputStringLength = 0;
this->ParseError = 0;
......@@ -53,6 +54,7 @@ vtkXMLParser::~vtkXMLParser()
{
this->SetStream(0);
this->SetFileName(0);
this->SetEncoding(0);
}
//----------------------------------------------------------------------------
......@@ -72,6 +74,8 @@ void vtkXMLParser::PrintSelf(ostream& os, vtkIndent indent)
os << indent << "IgnoreCharacterData: "
<< (this->IgnoreCharacterData?"On":"Off")
<< endl;
os << indent << "Encoding: " << (this->Encoding? this->Encoding : "(none)")
<< "\n";
}
//----------------------------------------------------------------------------
......@@ -229,7 +233,8 @@ int vtkXMLParser::Parse()
}
// Create the expat XML parser.
this->Parser = XML_ParserCreate(0);
this->CreateParser();
XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
&vtkXMLParserStartElement,
&vtkXMLParserEndElement);
......@@ -270,17 +275,30 @@ int vtkXMLParser::Parse()
return result;
}
//----------------------------------------------------------------------------
int vtkXMLParser::CreateParser()
{
if (this->Parser)
{
vtkErrorMacro("Parser already created");
return 0;
}
// Create the expat XML parser.
this->Parser = XML_ParserCreate(this->Encoding);
return this->Parser ? 1 : 0;
}
//----------------------------------------------------------------------------
int vtkXMLParser::InitializeParser()
{
if ( this->Parser )
// Create the expat XML parser.
if (!this->CreateParser())
{
vtkErrorMacro("Parser already initialized");
this->ParseError = 1;
return 0;
}
// Create the expat XML parser.
this->Parser = XML_ParserCreate(0);
XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
&vtkXMLParserStartElement,
&vtkXMLParserEndElement);
......
......@@ -88,6 +88,14 @@ public:
vtkSetMacro(IgnoreCharacterData, int);
vtkGetMacro(IgnoreCharacterData, int);
// Description:
// Set and get the encoding the parser should expect (NULL defaults to
// Expat's own default encoder, i.e UTF-8).
// This should be set before parsing (i.e. a call to Parse()) or
// even initializing the parser (i.e. a call to InitializeParser())
vtkSetStringMacro(Encoding);
vtkGetStringMacro(Encoding);
protected:
vtkXMLParser();
~vtkXMLParser();
......@@ -98,6 +106,9 @@ protected:
// File name to parse
char* FileName;
// Encoding
char* Encoding;
// This variable is true if there was a parse error while parsing in
// chunks.
int ParseError;
......@@ -109,6 +120,9 @@ protected:
// Expat parser structure. Exists only during call to Parse().
void* Parser;
// Create/Allocate the internal parser (can be overriden by subclasses).
virtual int CreateParser();
// Called by Parse() to read the stream and call ParseBuffer. Can
// be replaced by subclasses to change how input is read.
virtual int ParseXML();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment