| 1 | // | 
| 2 | // ParserWriterTest.cpp | 
| 3 | // | 
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. | 
| 5 | // and Contributors. | 
| 6 | // | 
| 7 | // SPDX-License-Identifier:	BSL-1.0 | 
| 8 | // | 
| 9 |  | 
| 10 |  | 
| 11 | #include "ParserWriterTest.h" | 
| 12 | #include "Poco/CppUnit/TestCaller.h" | 
| 13 | #include "Poco/CppUnit/TestSuite.h" | 
| 14 | #include "Poco/DOM/DOMParser.h" | 
| 15 | #include "Poco/DOM/DOMWriter.h" | 
| 16 | #include "Poco/DOM/Document.h" | 
| 17 | #include "Poco/DOM/Element.h" | 
| 18 | #include "Poco/DOM/AutoPtr.h" | 
| 19 | #include "Poco/SAX/InputSource.h" | 
| 20 | #include "Poco/XML/XMLWriter.h" | 
| 21 | #include <sstream> | 
| 22 |  | 
| 23 |  | 
| 24 | using Poco::XML::DOMParser; | 
| 25 | using Poco::XML::DOMWriter; | 
| 26 | using Poco::XML::XMLReader; | 
| 27 | using Poco::XML::XMLWriter; | 
| 28 | using Poco::XML::Document; | 
| 29 | using Poco::XML::AutoPtr; | 
| 30 | using Poco::XML::InputSource; | 
| 31 |  | 
| 32 |  | 
| 33 | ParserWriterTest::ParserWriterTest(const std::string& name): CppUnit::TestCase(name) | 
| 34 | { | 
| 35 | } | 
| 36 |  | 
| 37 |  | 
| 38 | ParserWriterTest::~ParserWriterTest() | 
| 39 | { | 
| 40 | } | 
| 41 |  | 
| 42 |  | 
| 43 | void ParserWriterTest::testParseWriteXHTML() | 
| 44 | { | 
| 45 | 	std::ostringstream ostr; | 
| 46 | 	 | 
| 47 | 	DOMParser parser; | 
| 48 | 	parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, false); | 
| 49 | 	DOMWriter writer; | 
| 50 | 	AutoPtr<Document> pDoc = parser.parseString(XHTML); | 
| 51 | 	writer.writeNode(ostr, pDoc); | 
| 52 | 	 | 
| 53 | 	std::string xml = ostr.str(); | 
| 54 | 	assertTrue (xml == XHTML); | 
| 55 | } | 
| 56 |  | 
| 57 |  | 
| 58 | void ParserWriterTest::testParseWriteXHTML2() | 
| 59 | { | 
| 60 | 	std::ostringstream ostr; | 
| 61 | 	 | 
| 62 | 	DOMParser parser; | 
| 63 | 	parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); | 
| 64 | 	DOMWriter writer; | 
| 65 | 	AutoPtr<Document> pDoc = parser.parseString(XHTML2); | 
| 66 | 	writer.writeNode(ostr, pDoc); | 
| 67 | 	 | 
| 68 | 	std::string xml = ostr.str(); | 
| 69 | 	assertTrue (xml == XHTML2); | 
| 70 | } | 
| 71 |  | 
| 72 |  | 
| 73 | void ParserWriterTest::testParseWriteSimple() | 
| 74 | { | 
| 75 | 	static const std::string simple = | 
| 76 | 		"<config>\n"  | 
| 77 | 		"\t<prop1>value1</prop1>\n"  | 
| 78 | 		"\t<prop2>value2</prop2>\n"  | 
| 79 | 		"</config>\n" ; | 
| 80 |  | 
| 81 | 	std::istringstream istr(simple); | 
| 82 | 	std::ostringstream ostr; | 
| 83 |  | 
| 84 | 	DOMParser parser; | 
| 85 | 	parser.setFeature(DOMParser::FEATURE_FILTER_WHITESPACE, true); | 
| 86 | 	parser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, false); | 
| 87 | 	DOMWriter writer; | 
| 88 | 	writer.setNewLine("\n" ); | 
| 89 | 	writer.setOptions(XMLWriter::PRETTY_PRINT); | 
| 90 | 	InputSource source(istr); | 
| 91 | 	AutoPtr<Document> pDoc = parser.parse(&source); | 
| 92 | 	writer.writeNode(ostr, pDoc); | 
| 93 |  | 
| 94 | 	unsigned int numChildren = 0; | 
| 95 | 	Poco::XML::Node* child = pDoc->documentElement()->firstChild(); | 
| 96 | 	while (child) { | 
| 97 | 		numChildren++; | 
| 98 | 		child = child->nextSibling(); | 
| 99 | 	} | 
| 100 | 	assertTrue (numChildren == 2); | 
| 101 |  | 
| 102 | 	std::string xml = ostr.str(); | 
| 103 | 	assertTrue (xml == simple); | 
| 104 | } | 
| 105 |  | 
| 106 |  | 
| 107 | void ParserWriterTest::setUp() | 
| 108 | { | 
| 109 | } | 
| 110 |  | 
| 111 |  | 
| 112 | void ParserWriterTest::tearDown() | 
| 113 | { | 
| 114 | } | 
| 115 |  | 
| 116 |  | 
| 117 | CppUnit::Test* ParserWriterTest::suite() | 
| 118 | { | 
| 119 | 	CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ParserWriterTest" ); | 
| 120 |  | 
| 121 | 	CppUnit_addTest(pSuite, ParserWriterTest, testParseWriteXHTML); | 
| 122 | 	CppUnit_addTest(pSuite, ParserWriterTest, testParseWriteXHTML2); | 
| 123 | 	CppUnit_addTest(pSuite, ParserWriterTest, testParseWriteSimple); | 
| 124 |  | 
| 125 | 	return pSuite; | 
| 126 | } | 
| 127 |  | 
| 128 |  | 
| 129 | const std::string ParserWriterTest::XHTML = | 
| 130 | 	"<!--\n"  | 
| 131 | 	"\tThis is a comment.\n"  | 
| 132 | 	"-->"  | 
| 133 | 	"<ns1:html xml:lang=\"en\" xmlns:ns1=\"http://www.w3.org/1999/xhtml\">\n"  | 
| 134 | 	"\t<ns1:head>\n"  | 
| 135 | 	"\t\t<ns1:link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"  | 
| 136 | 	"\t\t<?xml-stylesheet href=\"styles.css\" type=\"text/css\"?>\n"  | 
| 137 | 	"\t\t<ns1:title>A XHTML Example</ns1:title>\n"  | 
| 138 | 	"\t</ns1:head>\n"  | 
| 139 | 	"\t<ns1:body>\n"  | 
| 140 | 	"\t\t<ns1:h1>XHTML Example</ns1:h1>\n"  | 
| 141 | 	"\t\t<ns1:p>This is a XHTML example page.</ns1:p>\n"  | 
| 142 | 	"\t\t<ns1:img alt=\"Example Picture\" border=\"0\" height=\"192\" src=\"example.gif\" width=\"256\"/>\n"  | 
| 143 | 	"\t\t<![CDATA[\n"  | 
| 144 | 	"\t\tThe following <tag attr=\"value\">is inside a CDATA section</tag>.\n"  | 
| 145 | 	"\t\t]]>\n"  | 
| 146 | 	"\t</ns1:body>\n"  | 
| 147 | 	"</ns1:html>" ; | 
| 148 |  | 
| 149 |  | 
| 150 | const std::string ParserWriterTest::XHTML2 = | 
| 151 | 	"<!--\n"  | 
| 152 | 	"\tThis is a comment.\n"  | 
| 153 | 	"-->"  | 
| 154 | 	"<xns:html xml:lang=\"en\" xmlns:xns=\"http://www.w3.org/1999/xhtml\">\n"  | 
| 155 | 	"\t<xns:head>\n"  | 
| 156 | 	"\t\t<xns:link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"  | 
| 157 | 	"\t\t<?xml-stylesheet href=\"styles.css\" type=\"text/css\"?>\n"  | 
| 158 | 	"\t\t<xns:title>A XHTML Example</xns:title>\n"  | 
| 159 | 	"\t</xns:head>\n"  | 
| 160 | 	"\t<xns:body>\n"  | 
| 161 | 	"\t\t<xns:h1>XHTML Example</xns:h1>\n"  | 
| 162 | 	"\t\t<xns:p>This is a XHTML example page.</xns:p>\n"  | 
| 163 | 	"\t\t<xns:img alt=\"Example Picture\" border=\"0\" height=\"192\" src=\"example.gif\" width=\"256\"/>\n"  | 
| 164 | 	"\t\t<![CDATA[\n"  | 
| 165 | 	"\t\tThe following <tag attr=\"value\">is inside a CDATA section</tag>.\n"  | 
| 166 | 	"\t\t]]>\n"  | 
| 167 | 	"\t</xns:body>\n"  | 
| 168 | 	"</xns:html>" ; | 
| 169 |  | 
| 170 |  |