mirror of
https://github.com/AxioDL/tinyxml2.git
synced 2025-05-14 19:31:23 +00:00
new element loop
This commit is contained in:
parent
d627776dd3
commit
46a14cfec7
344
tinyxml2.cpp
344
tinyxml2.cpp
@ -18,17 +18,29 @@ static const char CR = CARRIAGE_RETURN;
|
|||||||
static const char SINGLE_QUOTE = '\'';
|
static const char SINGLE_QUOTE = '\'';
|
||||||
static const char DOUBLE_QUOTE = '\"';
|
static const char DOUBLE_QUOTE = '\"';
|
||||||
|
|
||||||
// Bunch of unicode info at:
|
// Bunch of unicode info at:
|
||||||
// http://www.unicode.org/faq/utf_bom.html
|
// http://www.unicode.org/faq/utf_bom.html
|
||||||
// ef bb bf (Microsoft "lead bytes") - designates UTF-8
|
// ef bb bf (Microsoft "lead bytes") - designates UTF-8
|
||||||
|
|
||||||
static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
|
static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
|
||||||
static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
|
static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
|
||||||
static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
|
static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
|
||||||
|
|
||||||
|
|
||||||
#define DELETE_NODE( node ) { MemPool* pool = node->memPool; node->~XMLNode(); pool->Free( node ); }
|
#define DELETE_NODE( node ) { \
|
||||||
#define DELETE_ATTRIBUTE( attrib ) { MemPool* pool = attrib->memPool; attrib->~XMLAttribute(); pool->Free( attrib ); }
|
if ( node ) { \
|
||||||
|
MemPool* pool = node->memPool; \
|
||||||
|
node->~XMLNode(); \
|
||||||
|
pool->Free( node ); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
#define DELETE_ATTRIBUTE( attrib ) { \
|
||||||
|
if ( attrib ) { \
|
||||||
|
MemPool* pool = attrib->memPool; \
|
||||||
|
attrib->~XMLAttribute(); \
|
||||||
|
pool->Free( attrib ); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
struct Entity {
|
struct Entity {
|
||||||
const char* pattern;
|
const char* pattern;
|
||||||
@ -229,114 +241,114 @@ const char* XMLUtil::ReadBOM( const char* p, bool* bom )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
|
void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
|
||||||
{
|
{
|
||||||
const unsigned long BYTE_MASK = 0xBF;
|
const unsigned long BYTE_MASK = 0xBF;
|
||||||
const unsigned long BYTE_MARK = 0x80;
|
const unsigned long BYTE_MARK = 0x80;
|
||||||
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
||||||
|
|
||||||
if (input < 0x80)
|
if (input < 0x80)
|
||||||
*length = 1;
|
*length = 1;
|
||||||
else if ( input < 0x800 )
|
else if ( input < 0x800 )
|
||||||
*length = 2;
|
*length = 2;
|
||||||
else if ( input < 0x10000 )
|
else if ( input < 0x10000 )
|
||||||
*length = 3;
|
*length = 3;
|
||||||
else if ( input < 0x200000 )
|
else if ( input < 0x200000 )
|
||||||
*length = 4;
|
*length = 4;
|
||||||
else
|
else
|
||||||
{ *length = 0; return; } // This code won't covert this correctly anyway.
|
{ *length = 0; return; } // This code won't covert this correctly anyway.
|
||||||
|
|
||||||
output += *length;
|
output += *length;
|
||||||
|
|
||||||
// Scary scary fall throughs.
|
// Scary scary fall throughs.
|
||||||
switch (*length)
|
switch (*length)
|
||||||
{
|
{
|
||||||
case 4:
|
case 4:
|
||||||
--output;
|
--output;
|
||||||
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
||||||
input >>= 6;
|
input >>= 6;
|
||||||
case 3:
|
case 3:
|
||||||
--output;
|
--output;
|
||||||
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
||||||
input >>= 6;
|
input >>= 6;
|
||||||
case 2:
|
case 2:
|
||||||
--output;
|
--output;
|
||||||
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
||||||
input >>= 6;
|
input >>= 6;
|
||||||
case 1:
|
case 1:
|
||||||
--output;
|
--output;
|
||||||
*output = (char)(input | FIRST_BYTE_MARK[*length]);
|
*output = (char)(input | FIRST_BYTE_MARK[*length]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
|
const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
|
||||||
{
|
{
|
||||||
// Presume an entity, and pull it out.
|
// Presume an entity, and pull it out.
|
||||||
*length = 0;
|
*length = 0;
|
||||||
|
|
||||||
if ( *(p+1) == '#' && *(p+2) )
|
if ( *(p+1) == '#' && *(p+2) )
|
||||||
{
|
{
|
||||||
unsigned long ucs = 0;
|
unsigned long ucs = 0;
|
||||||
ptrdiff_t delta = 0;
|
ptrdiff_t delta = 0;
|
||||||
unsigned mult = 1;
|
unsigned mult = 1;
|
||||||
|
|
||||||
if ( *(p+2) == 'x' )
|
if ( *(p+2) == 'x' )
|
||||||
{
|
{
|
||||||
// Hexadecimal.
|
// Hexadecimal.
|
||||||
if ( !*(p+3) ) return 0;
|
if ( !*(p+3) ) return 0;
|
||||||
|
|
||||||
const char* q = p+3;
|
const char* q = p+3;
|
||||||
q = strchr( q, ';' );
|
q = strchr( q, ';' );
|
||||||
|
|
||||||
if ( !q || !*q ) return 0;
|
if ( !q || !*q ) return 0;
|
||||||
|
|
||||||
delta = q-p;
|
delta = q-p;
|
||||||
--q;
|
--q;
|
||||||
|
|
||||||
while ( *q != 'x' )
|
while ( *q != 'x' )
|
||||||
{
|
{
|
||||||
if ( *q >= '0' && *q <= '9' )
|
if ( *q >= '0' && *q <= '9' )
|
||||||
ucs += mult * (*q - '0');
|
ucs += mult * (*q - '0');
|
||||||
else if ( *q >= 'a' && *q <= 'f' )
|
else if ( *q >= 'a' && *q <= 'f' )
|
||||||
ucs += mult * (*q - 'a' + 10);
|
ucs += mult * (*q - 'a' + 10);
|
||||||
else if ( *q >= 'A' && *q <= 'F' )
|
else if ( *q >= 'A' && *q <= 'F' )
|
||||||
ucs += mult * (*q - 'A' + 10 );
|
ucs += mult * (*q - 'A' + 10 );
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
mult *= 16;
|
mult *= 16;
|
||||||
--q;
|
--q;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Decimal.
|
// Decimal.
|
||||||
if ( !*(p+2) ) return 0;
|
if ( !*(p+2) ) return 0;
|
||||||
|
|
||||||
const char* q = p+2;
|
const char* q = p+2;
|
||||||
q = strchr( q, ';' );
|
q = strchr( q, ';' );
|
||||||
|
|
||||||
if ( !q || !*q ) return 0;
|
if ( !q || !*q ) return 0;
|
||||||
|
|
||||||
delta = q-p;
|
delta = q-p;
|
||||||
--q;
|
--q;
|
||||||
|
|
||||||
while ( *q != '#' )
|
while ( *q != '#' )
|
||||||
{
|
{
|
||||||
if ( *q >= '0' && *q <= '9' )
|
if ( *q >= '0' && *q <= '9' )
|
||||||
ucs += mult * (*q - '0');
|
ucs += mult * (*q - '0');
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
mult *= 10;
|
mult *= 10;
|
||||||
--q;
|
--q;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// convert the UCS to UTF-8
|
// convert the UCS to UTF-8
|
||||||
ConvertUTF32ToUTF8( ucs, value, length );
|
ConvertUTF32ToUTF8( ucs, value, length );
|
||||||
return p + delta + 1;
|
return p + delta + 1;
|
||||||
}
|
}
|
||||||
return p+1;
|
return p+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char* XMLDocument::Identify( char* p, XMLNode** node )
|
char* XMLDocument::Identify( char* p, XMLNode** node )
|
||||||
@ -397,6 +409,11 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
|
|||||||
returnNode = new (elementPool.Alloc()) XMLElement( this );
|
returnNode = new (elementPool.Alloc()) XMLElement( this );
|
||||||
returnNode->memPool = &elementPool;
|
returnNode->memPool = &elementPool;
|
||||||
p += elementHeaderLen;
|
p += elementHeaderLen;
|
||||||
|
|
||||||
|
p = XMLUtil::SkipWhiteSpace( p );
|
||||||
|
if ( p && *p == '/' ) {
|
||||||
|
((XMLElement*)returnNode)->closingType = XMLElement::CLOSING;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
returnNode = new (textPool.Alloc()) XMLText( this );
|
returnNode = new (textPool.Alloc()) XMLText( this );
|
||||||
@ -587,20 +604,75 @@ const XMLElement* XMLNode::LastChildElement( const char* value ) const
|
|||||||
|
|
||||||
char* XMLNode::ParseDeep( char* p )
|
char* XMLNode::ParseDeep( char* p )
|
||||||
{
|
{
|
||||||
|
// This is a recursive method, but thinking about it "at the current level"
|
||||||
|
// it is a pretty simple flat list:
|
||||||
|
// <foo/>
|
||||||
|
// <!-- comment -->
|
||||||
|
//
|
||||||
|
// With a special case:
|
||||||
|
// <foo>
|
||||||
|
// </foo>
|
||||||
|
// <!-- comment -->
|
||||||
|
//
|
||||||
|
// Where the closing element (/foo) *must* be the next thing after the opening
|
||||||
|
// element, and the names must match. BUT the tricky bit is that the closing
|
||||||
|
// element will be read by the child.
|
||||||
|
|
||||||
while( p && *p ) {
|
while( p && *p ) {
|
||||||
XMLNode* node = 0;
|
XMLNode* node = 0;
|
||||||
p = document->Identify( p, &node );
|
char* mark = p;
|
||||||
if ( p && node ) {
|
|
||||||
p = node->ParseDeep( p );
|
|
||||||
|
|
||||||
if ( node->IsClosingElement() ) {
|
p = document->Identify( p, &node );
|
||||||
if ( !XMLUtil::StringEqual( Value(), node->Value() )) {
|
if ( p == 0 ) {
|
||||||
document->SetError( ERROR_MISMATCHED_ELEMENT, Value(), 0 );
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We read the end tag. Back up and return.
|
||||||
|
if ( node && node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
|
||||||
|
DELETE_NODE( node );
|
||||||
|
return mark;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( node ) {
|
||||||
|
p = node->ParseDeep( p );
|
||||||
|
if ( !p ) {
|
||||||
DELETE_NODE( node );
|
DELETE_NODE( node );
|
||||||
return p;
|
node = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLElement* ele = node->ToElement();
|
||||||
|
if ( ele && ele->ClosingType() == XMLElement::OPEN ) {
|
||||||
|
XMLNode* closingNode = 0;
|
||||||
|
p = document->Identify( p, &closingNode );
|
||||||
|
XMLElement* closingEle = closingNode ? closingNode->ToElement() : 0;
|
||||||
|
|
||||||
|
if ( closingEle == 0 ) {
|
||||||
|
document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
||||||
|
p = 0;
|
||||||
|
}
|
||||||
|
else if ( closingEle->ClosingType() != XMLElement::CLOSING ) {
|
||||||
|
document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
||||||
|
p = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p = closingEle->ParseDeep( p );
|
||||||
|
if ( !XMLUtil::StringEqual( closingEle->Value(), node->Value() )) {
|
||||||
|
document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
||||||
|
p = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Else everything is fine, but we need to throw away the node.
|
||||||
|
DELETE_NODE( closingNode );
|
||||||
|
if ( p == 0 ) {
|
||||||
|
DELETE_NODE( node );
|
||||||
|
node = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( node ) {
|
||||||
|
this->InsertEndChild( node );
|
||||||
}
|
}
|
||||||
this->InsertEndChild( node );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -736,7 +808,7 @@ char* XMLAttribute::ParseDeep( char* p )
|
|||||||
char endTag[2] = { *p, 0 };
|
char endTag[2] = { *p, 0 };
|
||||||
++p;
|
++p;
|
||||||
p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
|
p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
|
||||||
if ( value.Empty() ) return 0;
|
//if ( value.Empty() ) return 0;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -842,9 +914,8 @@ void XMLAttribute::SetAttribute( float v )
|
|||||||
|
|
||||||
// --------- XMLElement ---------- //
|
// --------- XMLElement ---------- //
|
||||||
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
||||||
closing( false ),
|
closingType( 0 ),
|
||||||
rootAttribute( 0 )
|
rootAttribute( 0 )
|
||||||
//lastAttribute( 0 )
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -937,10 +1008,9 @@ void XMLElement::DeleteAttribute( const char* name )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char* XMLElement::ParseAttributes( char* p, bool* closedElement )
|
char* XMLElement::ParseAttributes( char* p )
|
||||||
{
|
{
|
||||||
const char* start = p;
|
const char* start = p;
|
||||||
*closedElement = false;
|
|
||||||
|
|
||||||
// Read the attributes.
|
// Read the attributes.
|
||||||
while( p ) {
|
while( p ) {
|
||||||
@ -965,11 +1035,7 @@ char* XMLElement::ParseAttributes( char* p, bool* closedElement )
|
|||||||
}
|
}
|
||||||
// end of the tag
|
// end of the tag
|
||||||
else if ( *p == '/' && *(p+1) == '>' ) {
|
else if ( *p == '/' && *(p+1) == '>' ) {
|
||||||
if ( closing ) {
|
closingType = CLOSED;
|
||||||
document->SetError( ERROR_PARSING_ELEMENT, start, p );
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
*closedElement = true;
|
|
||||||
return p+2; // done; sealed element.
|
return p+2; // done; sealed element.
|
||||||
}
|
}
|
||||||
// end of the tag
|
// end of the tag
|
||||||
@ -1001,7 +1067,7 @@ char* XMLElement::ParseDeep( char* p )
|
|||||||
// parsed just like a regular element then deleted from
|
// parsed just like a regular element then deleted from
|
||||||
// the DOM.
|
// the DOM.
|
||||||
if ( *p == '/' ) {
|
if ( *p == '/' ) {
|
||||||
closing = true;
|
closingType = CLOSING;
|
||||||
++p;
|
++p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1009,8 +1075,8 @@ char* XMLElement::ParseDeep( char* p )
|
|||||||
if ( value.Empty() ) return 0;
|
if ( value.Empty() ) return 0;
|
||||||
|
|
||||||
bool elementClosed=false;
|
bool elementClosed=false;
|
||||||
p = ParseAttributes( p, &elementClosed );
|
p = ParseAttributes( p );
|
||||||
if ( !p || !*p || elementClosed || closing )
|
if ( !p || !*p || closingType )
|
||||||
return p;
|
return p;
|
||||||
|
|
||||||
p = XMLNode::ParseDeep( p );
|
p = XMLNode::ParseDeep( p );
|
||||||
|
12
tinyxml2.h
12
tinyxml2.h
@ -463,7 +463,6 @@ public:
|
|||||||
virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
||||||
|
|
||||||
virtual char* ParseDeep( char* );
|
virtual char* ParseDeep( char* );
|
||||||
virtual bool IsClosingElement() const { return false; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
XMLNode( XMLDocument* );
|
XMLNode( XMLDocument* );
|
||||||
@ -681,7 +680,12 @@ public:
|
|||||||
const char* GetText() const;
|
const char* GetText() const;
|
||||||
|
|
||||||
// internal:
|
// internal:
|
||||||
virtual bool IsClosingElement() const { return closing; }
|
enum {
|
||||||
|
OPEN, // <foo>
|
||||||
|
CLOSED, // <foo/>
|
||||||
|
CLOSING // </foo>
|
||||||
|
};
|
||||||
|
int ClosingType() const { return closingType; }
|
||||||
char* ParseDeep( char* p );
|
char* ParseDeep( char* p );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -693,9 +697,9 @@ private:
|
|||||||
XMLAttribute* FindAttribute( const char* name );
|
XMLAttribute* FindAttribute( const char* name );
|
||||||
XMLAttribute* FindOrCreateAttribute( const char* name );
|
XMLAttribute* FindOrCreateAttribute( const char* name );
|
||||||
void LinkAttribute( XMLAttribute* attrib );
|
void LinkAttribute( XMLAttribute* attrib );
|
||||||
char* ParseAttributes( char* p, bool *closedElement );
|
char* ParseAttributes( char* p );
|
||||||
|
|
||||||
bool closing;
|
int closingType;
|
||||||
XMLAttribute* rootAttribute;
|
XMLAttribute* rootAttribute;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
534
xmltest.cpp
534
xmltest.cpp
@ -129,7 +129,7 @@ int main( int argc, const char* argv )
|
|||||||
printf( "----------------------------------------------\n" );
|
printf( "----------------------------------------------\n" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#if 1
|
||||||
{
|
{
|
||||||
static const char* test = "<!--hello world\n"
|
static const char* test = "<!--hello world\n"
|
||||||
" line 2\r"
|
" line 2\r"
|
||||||
@ -358,283 +358,285 @@ int main( int argc, const char* argv )
|
|||||||
XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------GetText()-----------
|
// --------GetText()-----------
|
||||||
{
|
{
|
||||||
const char* str = "<foo>This is text</foo>";
|
const char* str = "<foo>This is text</foo>";
|
||||||
XMLDocument doc;
|
XMLDocument doc;
|
||||||
doc.Parse( str );
|
doc.Parse( str );
|
||||||
const XMLElement* element = doc.RootElement();
|
const XMLElement* element = doc.RootElement();
|
||||||
|
|
||||||
XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
||||||
|
|
||||||
str = "<foo><b>This is text</b></foo>";
|
str = "<foo><b>This is text</b></foo>";
|
||||||
doc.Parse( str );
|
doc.Parse( str );
|
||||||
element = doc.RootElement();
|
element = doc.RootElement();
|
||||||
|
|
||||||
XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------- CDATA ---------------
|
// ---------- CDATA ---------------
|
||||||
{
|
{
|
||||||
const char* str = "<xmlElement>"
|
const char* str = "<xmlElement>"
|
||||||
"<![CDATA["
|
"<![CDATA["
|
||||||
"I am > the rules!\n"
|
"I am > the rules!\n"
|
||||||
"...since I make symbolic puns"
|
"...since I make symbolic puns"
|
||||||
"]]>"
|
"]]>"
|
||||||
"</xmlElement>";
|
"</xmlElement>";
|
||||||
XMLDocument doc;
|
XMLDocument doc;
|
||||||
doc.Parse( str );
|
doc.Parse( str );
|
||||||
doc.Print();
|
doc.Print();
|
||||||
|
|
||||||
XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
|
||||||
"I am > the rules!\n...since I make symbolic puns",
|
|
||||||
false );
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------- CDATA -------------
|
XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
||||||
{
|
"I am > the rules!\n...since I make symbolic puns",
|
||||||
const char* str = "<xmlElement>"
|
|
||||||
"<![CDATA["
|
|
||||||
"<b>I am > the rules!</b>\n"
|
|
||||||
"...since I make symbolic puns"
|
|
||||||
"]]>"
|
|
||||||
"</xmlElement>";
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( str );
|
|
||||||
doc.Print();
|
|
||||||
|
|
||||||
XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
|
||||||
"<b>I am > the rules!</b>\n...since I make symbolic puns",
|
|
||||||
false );
|
false );
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertAfterChild causes crash.
|
// ----------- CDATA -------------
|
||||||
{
|
{
|
||||||
// InsertBeforeChild and InsertAfterChild causes crash.
|
const char* str = "<xmlElement>"
|
||||||
XMLDocument doc;
|
"<![CDATA["
|
||||||
XMLElement* parent = doc.NewElement( "Parent" );
|
"<b>I am > the rules!</b>\n"
|
||||||
doc.InsertFirstChild( parent );
|
"...since I make symbolic puns"
|
||||||
|
"]]>"
|
||||||
XMLElement* childText0 = doc.NewElement( "childText0" );
|
"</xmlElement>";
|
||||||
XMLElement* childText1 = doc.NewElement( "childText1" );
|
XMLDocument doc;
|
||||||
|
doc.Parse( str );
|
||||||
XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
doc.Print();
|
||||||
XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
|
||||||
|
XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
||||||
XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
"<b>I am > the rules!</b>\n...since I make symbolic puns",
|
||||||
}
|
false );
|
||||||
|
}
|
||||||
{
|
|
||||||
// Entities not being written correctly.
|
// InsertAfterChild causes crash.
|
||||||
// From Lynn Allen
|
{
|
||||||
|
// InsertBeforeChild and InsertAfterChild causes crash.
|
||||||
const char* passages =
|
XMLDocument doc;
|
||||||
"<?xml version=\"1.0\" standalone=\"no\" ?>"
|
XMLElement* parent = doc.NewElement( "Parent" );
|
||||||
"<passages count=\"006\" formatversion=\"20020620\">"
|
doc.InsertFirstChild( parent );
|
||||||
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
|
||||||
" It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
XMLElement* childText0 = doc.NewElement( "childText0" );
|
||||||
"</passages>";
|
XMLElement* childText1 = doc.NewElement( "childText1" );
|
||||||
|
|
||||||
XMLDocument doc;
|
XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
||||||
doc.Parse( passages );
|
XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
||||||
XMLElement* psg = doc.RootElement()->FirstChildElement();
|
|
||||||
const char* context = psg->Attribute( "context" );
|
XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
||||||
const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
|
}
|
||||||
|
|
||||||
XMLTest( "Entity transformation: read. ", expected, context, true );
|
|
||||||
|
|
||||||
FILE* textfile = fopen( "textfile.txt", "w" );
|
|
||||||
if ( textfile )
|
|
||||||
{
|
|
||||||
XMLStreamer streamer( textfile );
|
|
||||||
psg->Accept( &streamer );
|
|
||||||
fclose( textfile );
|
|
||||||
}
|
|
||||||
textfile = fopen( "textfile.txt", "r" );
|
|
||||||
TIXMLASSERT( textfile );
|
|
||||||
if ( textfile )
|
|
||||||
{
|
|
||||||
char buf[ 1024 ];
|
|
||||||
fgets( buf, 1024, textfile );
|
|
||||||
XMLTest( "Entity transformation: write. ",
|
|
||||||
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
|
||||||
" It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
|
||||||
buf, false );
|
|
||||||
}
|
|
||||||
fclose( textfile );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
|
||||||
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( test );
|
|
||||||
XMLTest( "dot in names", doc.Error(), 0);
|
|
||||||
XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
|
||||||
XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
|
||||||
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( test );
|
|
||||||
|
|
||||||
XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
|
||||||
XMLTest( "Entity with one digit.",
|
|
||||||
text->Value(), "1.1 Start easy ignore fin thickness\n",
|
|
||||||
false );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
// DOCTYPE not preserved (950171)
|
|
||||||
//
|
|
||||||
const char* doctype =
|
|
||||||
"<?xml version=\"1.0\" ?>"
|
|
||||||
"<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
|
||||||
"<!ELEMENT title (#PCDATA)>"
|
|
||||||
"<!ELEMENT books (title,authors)>"
|
|
||||||
"<element />";
|
|
||||||
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( doctype );
|
|
||||||
doc.SaveFile( "test7.xml" );
|
|
||||||
doc.DeleteChild( doc.RootElement() );
|
|
||||||
doc.LoadFile( "test7.xml" );
|
|
||||||
doc.Print();
|
|
||||||
|
|
||||||
const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
|
||||||
XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// Comments do not stream out correctly.
|
// Entities not being written correctly.
|
||||||
const char* doctype =
|
// From Lynn Allen
|
||||||
"<!-- Somewhat<evil> -->";
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( doctype );
|
|
||||||
|
|
||||||
XMLComment* comment = doc.FirstChild()->ToComment();
|
|
||||||
|
|
||||||
XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
|
||||||
}
|
|
||||||
{
|
|
||||||
// Double attributes
|
|
||||||
const char* doctype = "<element attr='red' attr='blue' />";
|
|
||||||
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( doctype );
|
|
||||||
|
|
||||||
XMLTest( "Parsing repeated attributes.", ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
// Embedded null in stream.
|
|
||||||
const char* doctype = "<element att\0r='red' attr='blue' />";
|
|
||||||
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( doctype );
|
|
||||||
XMLTest( "Embedded null throws error.", true, doc.Error() );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
// Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
|
|
||||||
const char* str = " ";
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( str );
|
|
||||||
XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
const char* passages =
|
||||||
// Low entities
|
"<?xml version=\"1.0\" standalone=\"no\" ?>"
|
||||||
XMLDocument doc;
|
"<passages count=\"006\" formatversion=\"20020620\">"
|
||||||
doc.Parse( "<test></test>" );
|
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
||||||
const char result[] = { 0x0e, 0 };
|
" It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
||||||
XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
"</passages>";
|
||||||
doc.Print();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
XMLDocument doc;
|
||||||
// Attribute values with trailing quotes not handled correctly
|
doc.Parse( passages );
|
||||||
XMLDocument doc;
|
XMLElement* psg = doc.RootElement()->FirstChildElement();
|
||||||
doc.Parse( "<foo attribute=bar\" />" );
|
const char* context = psg->Attribute( "context" );
|
||||||
XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
|
||||||
}
|
|
||||||
|
|
||||||
{
|
XMLTest( "Entity transformation: read. ", expected, context, true );
|
||||||
// [ 1663758 ] Failure to report error on bad XML
|
|
||||||
XMLDocument xml;
|
|
||||||
xml.Parse("<x>");
|
|
||||||
XMLTest("Missing end tag at end of input", xml.Error(), true);
|
|
||||||
xml.Parse("<x> ");
|
|
||||||
XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
|
||||||
xml.Parse("<x></y>");
|
|
||||||
XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
{
|
|
||||||
// [ 1475201 ] TinyXML parses entities in comments
|
|
||||||
XMLDocument xml;
|
|
||||||
xml.Parse("<!-- declarations for <head> & <body> -->"
|
|
||||||
"<!-- far & away -->" );
|
|
||||||
|
|
||||||
XMLNode* e0 = xml.FirstChild();
|
|
||||||
XMLNode* e1 = e0->NextSibling();
|
|
||||||
XMLComment* c0 = e0->ToComment();
|
|
||||||
XMLComment* c1 = e1->ToComment();
|
|
||||||
|
|
||||||
XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
|
||||||
XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
XMLDocument xml;
|
|
||||||
xml.Parse( "<Parent>"
|
|
||||||
"<child1 att=''/>"
|
|
||||||
"<!-- With this comment, child2 will not be parsed! -->"
|
|
||||||
"<child2 att=''/>"
|
|
||||||
"</Parent>" );
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
|
||||||
ele;
|
|
||||||
ele = ele->NextSibling() )
|
|
||||||
{
|
|
||||||
++count;
|
|
||||||
}
|
|
||||||
|
|
||||||
XMLTest( "Comments iterate correctly.", 3, count );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
// trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
|
||||||
unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
|
||||||
buf[60] = 239;
|
|
||||||
buf[61] = 0;
|
|
||||||
|
|
||||||
XMLDocument doc;
|
|
||||||
doc.Parse( (const char*)buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
{
|
|
||||||
// bug 1827248 Error while parsing a little bit malformed file
|
|
||||||
// Actually not malformed - should work.
|
|
||||||
XMLDocument xml;
|
|
||||||
xml.Parse( "<attributelist> </attributelist >" );
|
|
||||||
XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
// This one must not result in an infinite loop
|
|
||||||
XMLDocument xml;
|
|
||||||
xml.Parse( "<infinite>loop" );
|
|
||||||
XMLTest( "Infinite loop test.", true, true );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
FILE* textfile = fopen( "textfile.txt", "w" );
|
||||||
|
if ( textfile )
|
||||||
|
{
|
||||||
|
XMLStreamer streamer( textfile );
|
||||||
|
psg->Accept( &streamer );
|
||||||
|
fclose( textfile );
|
||||||
|
}
|
||||||
|
textfile = fopen( "textfile.txt", "r" );
|
||||||
|
TIXMLASSERT( textfile );
|
||||||
|
if ( textfile )
|
||||||
|
{
|
||||||
|
char buf[ 1024 ];
|
||||||
|
fgets( buf, 1024, textfile );
|
||||||
|
XMLTest( "Entity transformation: write. ",
|
||||||
|
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
||||||
|
" It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
||||||
|
buf, false );
|
||||||
|
}
|
||||||
|
fclose( textfile );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
||||||
|
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( test );
|
||||||
|
XMLTest( "dot in names", doc.Error(), 0);
|
||||||
|
XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
||||||
|
XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
||||||
|
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( test );
|
||||||
|
|
||||||
|
XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
||||||
|
XMLTest( "Entity with one digit.",
|
||||||
|
text->Value(), "1.1 Start easy ignore fin thickness\n",
|
||||||
|
false );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// DOCTYPE not preserved (950171)
|
||||||
|
//
|
||||||
|
const char* doctype =
|
||||||
|
"<?xml version=\"1.0\" ?>"
|
||||||
|
"<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
||||||
|
"<!ELEMENT title (#PCDATA)>"
|
||||||
|
"<!ELEMENT books (title,authors)>"
|
||||||
|
"<element />";
|
||||||
|
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( doctype );
|
||||||
|
doc.SaveFile( "test7.xml" );
|
||||||
|
doc.DeleteChild( doc.RootElement() );
|
||||||
|
doc.LoadFile( "test7.xml" );
|
||||||
|
doc.Print();
|
||||||
|
|
||||||
|
const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
||||||
|
XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Comments do not stream out correctly.
|
||||||
|
const char* doctype =
|
||||||
|
"<!-- Somewhat<evil> -->";
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( doctype );
|
||||||
|
|
||||||
|
XMLComment* comment = doc.FirstChild()->ToComment();
|
||||||
|
|
||||||
|
XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// Double attributes
|
||||||
|
const char* doctype = "<element attr='red' attr='blue' />";
|
||||||
|
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( doctype );
|
||||||
|
|
||||||
|
XMLTest( "Parsing repeated attributes.", ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Embedded null in stream.
|
||||||
|
const char* doctype = "<element att\0r='red' attr='blue' />";
|
||||||
|
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( doctype );
|
||||||
|
XMLTest( "Embedded null throws error.", true, doc.Error() );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Empty documents should return TIXML_ERROR_PARSING_EMPTY, bug 1070717
|
||||||
|
const char* str = " ";
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( str );
|
||||||
|
XMLTest( "Empty document error", ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Low entities
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( "<test></test>" );
|
||||||
|
const char result[] = { 0x0e, 0 };
|
||||||
|
XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
||||||
|
doc.Print();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Attribute values with trailing quotes not handled correctly
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( "<foo attribute=bar\" />" );
|
||||||
|
XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// [ 1663758 ] Failure to report error on bad XML
|
||||||
|
XMLDocument xml;
|
||||||
|
xml.Parse("<x>");
|
||||||
|
XMLTest("Missing end tag at end of input", xml.Error(), true);
|
||||||
|
xml.Parse("<x> ");
|
||||||
|
XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
||||||
|
xml.Parse("<x></y>");
|
||||||
|
XMLTest("Mismatched tags", xml.ErrorID(), ERROR_MISMATCHED_ELEMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
// [ 1475201 ] TinyXML parses entities in comments
|
||||||
|
XMLDocument xml;
|
||||||
|
xml.Parse("<!-- declarations for <head> & <body> -->"
|
||||||
|
"<!-- far & away -->" );
|
||||||
|
|
||||||
|
XMLNode* e0 = xml.FirstChild();
|
||||||
|
XMLNode* e1 = e0->NextSibling();
|
||||||
|
XMLComment* c0 = e0->ToComment();
|
||||||
|
XMLComment* c1 = e1->ToComment();
|
||||||
|
|
||||||
|
XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
||||||
|
XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
XMLDocument xml;
|
||||||
|
xml.Parse( "<Parent>"
|
||||||
|
"<child1 att=''/>"
|
||||||
|
"<!-- With this comment, child2 will not be parsed! -->"
|
||||||
|
"<child2 att=''/>"
|
||||||
|
"</Parent>" );
|
||||||
|
xml.Print();
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
||||||
|
ele;
|
||||||
|
ele = ele->NextSibling() )
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLTest( "Comments iterate correctly.", 3, count );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
||||||
|
unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
||||||
|
buf[60] = 239;
|
||||||
|
buf[61] = 0;
|
||||||
|
|
||||||
|
XMLDocument doc;
|
||||||
|
doc.Parse( (const char*)buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
// bug 1827248 Error while parsing a little bit malformed file
|
||||||
|
// Actually not malformed - should work.
|
||||||
|
XMLDocument xml;
|
||||||
|
xml.Parse( "<attributelist> </attributelist >" );
|
||||||
|
XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// This one must not result in an infinite loop
|
||||||
|
XMLDocument xml;
|
||||||
|
xml.Parse( "<infinite>loop" );
|
||||||
|
XMLTest( "Infinite loop test.", true, true );
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#if defined( WIN32 )
|
#if defined( WIN32 )
|
||||||
_CrtMemCheckpoint( &endMemState );
|
_CrtMemCheckpoint( &endMemState );
|
||||||
//_CrtMemDumpStatistics( &endMemState );
|
//_CrtMemDumpStatistics( &endMemState );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user