Add support for DOCTYPE to wxXmlDocument.

Allow retrieving the DOCTYPE specified in an existing document as well as
specifying the DOCTYPE to use when writing a new one.

Closes #13779.
This commit is contained in:
Nick Matthews
2015-04-26 15:34:18 +02:00
committed by Vadim Zeitlin
parent 41b1ed9c2e
commit d13278ecc3
5 changed files with 336 additions and 2 deletions

View File

@@ -82,6 +82,8 @@ private:
CPPUNIT_TEST( AppendToProlog );
CPPUNIT_TEST( SetRoot );
CPPUNIT_TEST( CopyNode );
CPPUNIT_TEST( CopyDocument );
CPPUNIT_TEST( Doctype );
CPPUNIT_TEST_SUITE_END();
void InsertChild();
@@ -94,6 +96,8 @@ private:
void AppendToProlog();
void SetRoot();
void CopyNode();
void CopyDocument();
void Doctype();
wxDECLARE_NO_COPY_CLASS(XmlTestCase);
};
@@ -207,6 +211,7 @@ void XmlTestCase::LoadSave()
const char *xmlTextProlog =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE resource PUBLIC \"Public-ID\" 'System\"ID\"'>\n"
"<!-- Prolog comment -->\n"
"<?xml-stylesheet href=\"style.css\" type=\"text/css\"?>\n"
"<resource xmlns=\"http://www.wxwidgets.org/wxxrc\" version=\"2.3.0.1\">\n"
@@ -507,3 +512,103 @@ void XmlTestCase::CopyNode()
;
CPPUNIT_ASSERT_EQUAL( xmlTextResult, sos.GetString() );
}
void XmlTestCase::CopyDocument()
{
const char *xmlText =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE resource PUBLIC \"Public-ID\" \"System'ID'\">\n"
"<!-- 1st prolog entry -->\n"
"<root>\n"
" <first>Text</first>\n"
" <second/>\n"
"</root>\n"
;
wxXmlDocument doc1;
wxStringInputStream sis(xmlText);
CPPUNIT_ASSERT( doc1.Load(sis) );
wxXmlDocument doc2 = doc1;
wxStringOutputStream sos;
CPPUNIT_ASSERT(doc2.Save(sos));
CPPUNIT_ASSERT_EQUAL( xmlText, sos.GetString() );
}
void XmlTestCase::Doctype()
{
const char *xmlText =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE root PUBLIC \"Public-ID\" 'System\"ID\"'>\n"
"<root>\n"
" <content/>\n"
"</root>\n"
;
wxStringInputStream sis(xmlText);
wxXmlDocument doc;
CPPUNIT_ASSERT( doc.Load(sis) );
wxXmlDoctype dt = doc.GetDoctype();
CPPUNIT_ASSERT_EQUAL( "root", dt.GetRootName() );
CPPUNIT_ASSERT_EQUAL( "System\"ID\"", dt.GetSystemId() );
CPPUNIT_ASSERT_EQUAL( "Public-ID", dt.GetPublicId() );
CPPUNIT_ASSERT( dt.IsValid() );
CPPUNIT_ASSERT_EQUAL( "root PUBLIC \"Public-ID\" 'System\"ID\"'", dt.GetFullString() );
dt = wxXmlDoctype( dt.GetRootName(), dt.GetSystemId() );
CPPUNIT_ASSERT( dt.IsValid() );
CPPUNIT_ASSERT_EQUAL( "root SYSTEM 'System\"ID\"'", dt.GetFullString() );
dt = wxXmlDoctype( dt.GetRootName() );
CPPUNIT_ASSERT( dt.IsValid() );
CPPUNIT_ASSERT_EQUAL( "root", dt.GetFullString() );
doc.SetDoctype(dt);
wxStringOutputStream sos;
CPPUNIT_ASSERT(doc.Save(sos));
const char *xmlText1 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE root>\n"
"<root>\n"
" <content/>\n"
"</root>\n"
;
CPPUNIT_ASSERT_EQUAL( xmlText1, sos.GetString() );
doc.SetDoctype(wxXmlDoctype());
wxStringOutputStream sos2;
CPPUNIT_ASSERT(doc.Save(sos2));
const char *xmlText2 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<root>\n"
" <content/>\n"
"</root>\n"
;
CPPUNIT_ASSERT_EQUAL( xmlText2, sos2.GetString() );
doc.SetDoctype(wxXmlDoctype("root", "Sys'id"));
wxStringOutputStream sos3;
CPPUNIT_ASSERT(doc.Save(sos3));
const char *xmlText3 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE root SYSTEM \"Sys'id\">\n"
"<root>\n"
" <content/>\n"
"</root>\n"
;
CPPUNIT_ASSERT_EQUAL( xmlText3, sos3.GetString() );
dt = wxXmlDoctype( "", "System\"ID\"", "Public-ID" );
CPPUNIT_ASSERT( !dt.IsValid() );
CPPUNIT_ASSERT_EQUAL( "", dt.GetFullString() );
// Strictly speaking, this is illegal for XML but is legal for SGML.
dt = wxXmlDoctype( "root", "", "Public-ID" );
CPPUNIT_ASSERT( dt.IsValid() );
CPPUNIT_ASSERT_EQUAL( "root PUBLIC \"Public-ID\"", dt.GetFullString() );
// Using both single and double quotes in system ID is not allowed.
dt = wxXmlDoctype( "root", "O'Reilly (\"editor\")", "Public-ID" );
CPPUNIT_ASSERT( !dt.IsValid() );
}