diff options
author | Mohammed Abdul Azeem <azeemmysore@gmail.com> | 2016-05-26 02:05:13 +0530 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2016-05-28 09:46:52 +0000 |
commit | 8128d597224b55965c4f1bba3d824f193a55041b (patch) | |
tree | ea27a1aac6903a855526944219dbd4afbebca63f /sax | |
parent | ab0189433c1593c3c3ccf6a947aa7ba84e806d91 (diff) |
sax2/ unit tests.
Test xml files are added, string is built for each file
and namespaces are handled.
Change-Id: I0ab799ca5c9de7311ccca2a6033a96e02598064f
Reviewed-on: https://gerrit.libreoffice.org/25468
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'sax')
-rw-r--r-- | sax/qa/cppunit/xmlimport.cxx | 114 | ||||
-rw-r--r-- | sax/qa/data/defaultns.xml | 11 | ||||
-rw-r--r-- | sax/qa/data/inlinens.xml | 12 | ||||
-rw-r--r-- | sax/qa/data/multiplens.xml | 13 | ||||
-rw-r--r-- | sax/qa/data/multiplepfx.xml | 9 | ||||
-rw-r--r-- | sax/qa/data/namespace.xml | 6 | ||||
-rw-r--r-- | sax/qa/data/nestedns.xml | 30 | ||||
-rw-r--r-- | sax/qa/data/note.xml | 7 | ||||
-rw-r--r-- | sax/qa/data/nstoattributes.xml | 17 | ||||
-rw-r--r-- | sax/qa/data/simple.xml | 11 |
10 files changed, 197 insertions, 33 deletions
diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index 204c45d18fd7..0ba32c15578a 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -32,12 +32,16 @@ #include <osl/file.hxx> #include <unotools/ucbstreamhelper.hxx> #include <unotools/streamwrap.hxx> +#include <string> +#include <stack> +#include <deque> namespace { using namespace css; using namespace css::uno; using namespace css::io; +using namespace std; Reference< XInputStream > createStreamFromFile ( const OUString filePath) @@ -56,11 +60,17 @@ Reference< XInputStream > createStreamFromFile ( class TestDocumentHandler : public cppu::WeakImplHelper< xml::sax::XDocumentHandler > { private: - // OUString m_aStr; + OUString m_aStr; + deque< pair<OUString,OUString> > m_aNamespaceStack; + stack<sal_uInt16> m_aCountStack; + + OUString canonicalform(const OUString &sName, const OUString &sValue, bool isElement); + OUString getNamespace(const OUString &sName); public: TestDocumentHandler() {} - //const OUString& getString() { return m_aStr; } + OUString getString() { return m_aStr; } + // XDocumentHandler virtual void SAL_CALL startDocument() throw (xml::sax::SAXException, RuntimeException, std::exception) override; virtual void SAL_CALL endDocument() throw (xml::sax::SAXException, RuntimeException, std::exception) override; @@ -72,10 +82,53 @@ public: virtual void SAL_CALL setDocumentLocator( const Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; }; +OUString TestDocumentHandler::canonicalform(const OUString &sName, const OUString &sValue, bool isElement) +{ + sal_Int16 nIndex = sName.indexOf(":"); + if ( !isElement && sName.match( "xmlns" ) ) + { + m_aCountStack.top() += 1; + if ( nIndex < 0 ) + m_aNamespaceStack.push_back( make_pair( OUString( "default" ), sValue ) ); + else + m_aNamespaceStack.push_back( make_pair( sName.copy( nIndex + 1 ), sValue ) ); + } + else + { + if ( nIndex >= 0 ) + { + OUString sNamespace = getNamespace( sName.copy( 0, nIndex ) ); + return sNamespace + sName.copy(nIndex); + } + else + { + OUString sDefaultns = getNamespace( "default" ); + if ( !isElement || sDefaultns.isEmpty() ) + return sName; + else + return sDefaultns + ":" + sName; + } + } + return OUString(""); +} + +OUString TestDocumentHandler::getNamespace(const OUString &sName) +{ + for (sal_Int16 i = m_aNamespaceStack.size() - 1; i>=0; i--) + { + pair<OUString, OUString> aPair = m_aNamespaceStack.at(i); + if (aPair.first == sName) + return aPair.second; + } + return OUString(""); +} + void SAL_CALL TestDocumentHandler::startDocument() throw(xml::sax::SAXException, RuntimeException, std::exception) { - // m_aStr.clear(); + m_aStr.clear(); + m_aNamespaceStack.emplace_back( make_pair( OUString( "default" ), OUString() ) ); + m_aCountStack.emplace(0); } @@ -84,30 +137,45 @@ void SAL_CALL TestDocumentHandler::endDocument() { } -void SAL_CALL TestDocumentHandler::startElement( const OUString& /*aName*/, const Reference< xml::sax::XAttributeList >& /*xAttribs*/ ) +void SAL_CALL TestDocumentHandler::startElement( const OUString& aName, const Reference< xml::sax::XAttributeList >& xAttribs ) throw( xml::sax::SAXException, RuntimeException, std::exception ) { - // m_aStr = m_aStr + "<" + aName + "> "; + OUString sAttributes; + m_aCountStack.push(0); + sal_uInt16 len = xAttribs->getLength(); + for (sal_uInt16 i=0; i<len; i++) + { + OUString sAttrValue = xAttribs->getValueByIndex(i); + OUString sAttrName = canonicalform(xAttribs->getNameByIndex(i), sAttrValue, false); + if (!sAttrName.isEmpty()) + sAttributes = sAttrName + sAttrValue; + } + m_aStr = m_aStr + canonicalform(aName, "", true) + sAttributes; } -void SAL_CALL TestDocumentHandler::endElement( const OUString& /*aName*/ ) +void SAL_CALL TestDocumentHandler::endElement( const OUString& aName ) throw( xml::sax::SAXException, RuntimeException, std::exception ) { - // m_aStr = m_aStr + "</" + aName + ">\n"; + m_aStr = m_aStr + canonicalform(aName, "", true); + sal_uInt16 nPopQty = m_aCountStack.top(); + for (sal_uInt16 i=0; i<nPopQty; i++) + m_aNamespaceStack.pop_back(); + m_aCountStack.pop(); } -void SAL_CALL TestDocumentHandler::characters( const OUString& /*aChars*/ ) +void SAL_CALL TestDocumentHandler::characters( const OUString& aChars ) throw(xml::sax::SAXException, RuntimeException, std::exception) { - // m_aStr = m_aStr + aChars + " "; + m_aStr = m_aStr + aChars; } -void SAL_CALL TestDocumentHandler::ignorableWhitespace( const OUString& /*aWhitespaces*/ ) +void SAL_CALL TestDocumentHandler::ignorableWhitespace( const OUString& aWhitespaces ) throw(xml::sax::SAXException, RuntimeException, std::exception) { + m_aStr = m_aStr + aWhitespaces; } @@ -161,16 +229,22 @@ void XMLImportTest::tearDown() void XMLImportTest::parse() { - OUString fileName = "note.xml"; - Reference< XInputStream > rIS = createStreamFromFile( m_sDirPath + fileName ); - xml::sax::InputSource source; - source.aInputStream = rIS; - source.sSystemId = "internal"; - m_xParser->parseStream(source); - // OUString aStr = m_xDocumentHandler->getString(); - // OString o = OUStringToOString( aStr, RTL_TEXTENCODING_ASCII_US ); - // CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false ); - CPPUNIT_ASSERT(true); + OUString fileNames[] = {"simple.xml", "defaultns.xml", "inlinens.xml", + "multiplens.xml", "multiplepfx.xml", + "nstoattributes.xml", "nestedns.xml"}; + + for (sal_uInt16 i = 0; i < sizeof( fileNames ) / sizeof( string ); i++) + { + Reference< XInputStream > rIS = createStreamFromFile( m_sDirPath + fileNames[i] ); + xml::sax::InputSource source; + source.aInputStream = rIS; + source.sSystemId = "internal"; + m_xParser->parseStream(source); + // OUString aStr = m_xDocumentHandler->getString(); + // OString o = OUStringToOString( aStr, RTL_TEXTENCODING_ASCII_US ); + // CPPUNIT_ASSERT_MESSAGE( std::string(o.pData->buffer), false ); + CPPUNIT_ASSERT(true); + } } CPPUNIT_TEST_SUITE_REGISTRATION( XMLImportTest ); diff --git a/sax/qa/data/defaultns.xml b/sax/qa/data/defaultns.xml new file mode 100644 index 000000000000..554f4b48f7ea --- /dev/null +++ b/sax/qa/data/defaultns.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" ?> +<Books xmlns="http://xyzbooks.com/books/"> + <Book> + <Title>War and Peacd</Title> + <Author>Leo Tolstoy</Author> + </Book> + <Book> + <Title>To Kill a Mockingbird</Title> + <Author>Harper Lee</Author> + </Book> +</Books>
\ No newline at end of file diff --git a/sax/qa/data/inlinens.xml b/sax/qa/data/inlinens.xml new file mode 100644 index 000000000000..02c421408569 --- /dev/null +++ b/sax/qa/data/inlinens.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" ?> +<Students xmlns="http://xyzuniversity.org/student/"> + <Student xmlns:ug="http://xyzuniversity.org/student/ug/"> + <Name>ABC</Name> + <ug:Branch>Computer Science</ug:Branch> + <ug:Grade>7.9</ug:Grade> + </Student> + <Student xmlns:pg="http://xyzuniversity.org/student/pg/"> + <Name>PQR</Name> + <pg:Field>Artificial Intelligence</pg:Field> + </Student> +</Students>
\ No newline at end of file diff --git a/sax/qa/data/multiplens.xml b/sax/qa/data/multiplens.xml new file mode 100644 index 000000000000..e1dc4ce01e89 --- /dev/null +++ b/sax/qa/data/multiplens.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" ?> +<Athletes> + <Player xmlns:Player="xyzsports.com/players/football/"> + <Player:Name>Lionel Messi</Player:Name> + <Player:Height>1.70 m</Player:Height> + <Player:Position>Forward</Player:Position> + </Player> + <Player xmlns:Player="xyzsports.com/players/Cricket/"> + <Player:Name>Sachin Ramesh Tendulkar</Player:Name> + <Player:Height>165 cm</Player:Height> + <Player:Style>Right handed</Player:Style> + </Player> +</Athletes>
\ No newline at end of file diff --git a/sax/qa/data/multiplepfx.xml b/sax/qa/data/multiplepfx.xml new file mode 100644 index 000000000000..b7686cad5f69 --- /dev/null +++ b/sax/qa/data/multiplepfx.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" ?> +<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"> + <office:body> + <office:text> + <text:p xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" text:style-name="Title">Test Document </text:p> + <note:p xmlns:note="urn:oasis:names:tc:opendocument:xmlns:text:1.0" note:style-name="Heading">For testing purposes only</note:p> + </office:text> + </office:body> +</office:document> diff --git a/sax/qa/data/namespace.xml b/sax/qa/data/namespace.xml deleted file mode 100644 index 6abb6468e7f8..000000000000 --- a/sax/qa/data/namespace.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0"?> -<html:html xmlns:html="http://www.w3.org/TR/REC-html40"> - <html:head><html:title>Frobnostication</html:title></html:head> - <html:body><html:p>Moved to - <html:a href="http://frob.com">here.</html:a></html:p></html:body> -</html:html>
\ No newline at end of file diff --git a/sax/qa/data/nestedns.xml b/sax/qa/data/nestedns.xml new file mode 100644 index 000000000000..33170592938c --- /dev/null +++ b/sax/qa/data/nestedns.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" ?> +<Elements> + <Book xmlns:lib="http://www.library.com/"> + <lib:Title>Sherlock Holmes - I</lib:Title> + <lib:Author>Arthur Conan Doyle</lib:Author> + <purchase xmlns:lib="http://www.otherlibrary.com/"> + <lib:Title>Sherlock Holmes - II</lib:Title> + <lib:Author>Arthur Conan Doyle</lib:Author> + </purchase> + <lib:Title>Sherlock Holmes - III</lib:Title> + <lib:Author>Arthur Conan Doyle</lib:Author> + </Book> + <Electronics xmlns="http://doesntexist.com/electronics/"> + <item> + <Name>Apple iPhone 6s</Name> + <Price>$324</Price> + </item> + <item xmlns="http://doesntexist.com/dailyuse/"> + <Name>Philips Aqua Touch Shaver</Name> + <item xmlns="http://doesntexist.com/dailyuse/model/"> + <Model></Model> + <Price>$74</Price> + </item> + </item> + <item> + <Name>Macbook Pro</Name> + <Price>$500</Price> + </item> + </Electronics> +</Elements>
\ No newline at end of file diff --git a/sax/qa/data/note.xml b/sax/qa/data/note.xml deleted file mode 100644 index 830b1c2d95ab..000000000000 --- a/sax/qa/data/note.xml +++ /dev/null @@ -1,7 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?>
-<note>
- <to>ABC</to>
- <from>XYZ</from>
- <heading>Reminder</heading>
- <body>Don't forget me this weekend!</body>
-</note>
\ No newline at end of file diff --git a/sax/qa/data/nstoattributes.xml b/sax/qa/data/nstoattributes.xml new file mode 100644 index 000000000000..dee2edfdcb2a --- /dev/null +++ b/sax/qa/data/nstoattributes.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" ?> +<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main/"> + <w:body> + <w:p w:rsidR="009A1A3D" w:rsidRPr="00BA52A7" w:rsidRDefault="00225691" w:rsidP="00BA52A7"> + <w:pPr xyz="abc"> + <w:spacing w:line="276" w:lineRule="auto"/> + <w:rPr> + <w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi"/> + <w:sz w:val="24" val="27" /> + <w:szCs w:val="24"/> + </w:rPr> + </w:pPr> + <w:bookmarkStart w:id="0" w:name="page1"/> + <w:bookmarkEnd w:id="0"/> + </w:p> + </w:body> +</w:document>
\ No newline at end of file diff --git a/sax/qa/data/simple.xml b/sax/qa/data/simple.xml new file mode 100644 index 000000000000..67c4fbde57c3 --- /dev/null +++ b/sax/qa/data/simple.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" ?> +<TVActors> + <Actor> + <Name>Bryan Cranston</Name> + <Show>Breaking Bad</Show> + </Actor> + <Actor> + <Name>Peter Dinklage</Name> + <Show>Game of Thrones</Show> + </Actor> +</TVActors>
\ No newline at end of file |