1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/ref.hxx>
21 #include <sal/log.hxx>
22 #include <sax/fastattribs.hxx>
23 #include <comphelper/seqstream.hxx>
24 #include <cppuhelper/implbase.hxx>
25 #include <cppunit/extensions/HelperMacros.h>
26 #include <cppunit/plugin/TestPlugIn.h>
27 #include <test/bootstrapfixture.hxx>
29 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
30 #include <com/sun/star/xml/sax/FastToken.hpp>
31 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
32 #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
33 #include <com/sun/star/xml/sax/SAXParseException.hpp>
35 using namespace ::comphelper
;
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::uno
;
38 using css::xml::dom::XDocumentBuilder
;
43 const char validTestFile
[] =
44 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
45 <office:document-content \
46 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
47 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
48 office:version=\"1.0\"> \
51 <office:automatic-styles teststyle=\"test\"/> \
53 some text \303\266\303\244\303\274 \
54 </office:document-content> \
57 // generates a warning: unknown xml:space
59 const char warningTestFile
[] =
60 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
61 <office:document-content \
62 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
64 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
65 office:version=\"1.0\"> \
68 <office:automatic-styles teststyle=\"test\"/> \
70 some text \303\266\303\244\303\274 \
71 </office:document-content> \
74 // <?xml not at start of file
75 const char errorTestFile
[] =
76 " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
77 <office:document-content \
78 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
79 office:version=\"1.0\"> \
81 <office:automatic-styles/> \
82 </office:document-content> \
86 : public ::cppu::WeakImplHelper
< xml::sax::XErrorHandler
>
88 sal_uInt32 mnErrCount
;
89 // sal_uInt32 mnFatalCount; // No fatal error counter, as lib2xml doesn't distinguish between error and fatal error
90 // (see See xmlFatalErrMsg from lib2xml/parse.c and __xmlRaiseError from lib2xml/error.c)
91 sal_uInt32 mnWarnCount
;
93 bool noErrors() const { return !mnErrCount
/*&& !mnFatalCount*/ && !mnWarnCount
; }
95 ErrorHandler() : mnErrCount(0), /*mnFatalCount(0),*/ mnWarnCount(0)
98 virtual void SAL_CALL
error( const uno::Any
& ) override
103 // Just implement FatalError function as it is in XErrorHandler
104 // This function is never used, as lib2xml doesn't distinguish between error and fatalerror and calls error functions in both cases
105 virtual void SAL_CALL
fatalError( const uno::Any
& ) override
110 virtual void SAL_CALL
warning( const uno::Any
& ) override
116 struct DocumentHandler
117 : public ::cppu::WeakImplHelper
< xml::sax::XFastDocumentHandler
>
119 // XFastContextHandler
120 virtual void SAL_CALL
startFastElement( ::sal_Int32 Element
, const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
124 "Seen element: " << (Element
& 0xFFFF) << " with namespace "
125 << (Element
& 0xFFFF0000));
128 virtual void SAL_CALL
startUnknownElement( const OUString
& , const OUString
& , const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
132 virtual void SAL_CALL
endFastElement( ::sal_Int32
) override
136 virtual void SAL_CALL
endUnknownElement( const OUString
&, const OUString
& ) override
140 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext( ::sal_Int32
, const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
145 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext( const OUString
& , const OUString
& , const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
150 virtual void SAL_CALL
characters( const OUString
& ) override
154 // XFastDocumentHandler
155 virtual void SAL_CALL
startDocument( ) override
159 virtual void SAL_CALL
endDocument( ) override
163 virtual void SAL_CALL
processingInstruction( const OUString
& /*rTarget*/, const OUString
& /*rData*/ ) override
167 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& ) override
172 struct TokenHandler
: public sax_fastparser::FastTokenHandlerBase
174 virtual ::sal_Int32 SAL_CALL
getTokenFromUTF8( const uno::Sequence
< ::sal_Int8
>& Identifier
) override
176 return Identifier
.hasElements() ? Identifier
[0] : 0;
179 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getUTF8Identifier( ::sal_Int32
) override
181 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
183 return uno::Sequence
<sal_Int8
>();
186 virtual sal_Int32
getTokenDirect( const char * /* pToken */, sal_Int32
/* nLength */ ) const override
192 struct BasicTest
: public test::BootstrapFixture
194 uno::Reference
<XDocumentBuilder
> mxDomBuilder
;
195 rtl::Reference
<ErrorHandler
> mxErrHandler
;
196 rtl::Reference
<SequenceInputStream
> mxValidInStream
;
197 rtl::Reference
<SequenceInputStream
> mxWarningInStream
;
198 rtl::Reference
<SequenceInputStream
> mxErrorInStream
;
200 virtual void setUp() override
202 test::BootstrapFixture::setUp();
204 mxErrHandler
.set( new ErrorHandler() );
205 uno::Reference
<XDocumentBuilder
> xDB( getMultiServiceFactory()->createInstance("com.sun.star.xml.dom.DocumentBuilder"), uno::UNO_QUERY_THROW
);
206 mxDomBuilder
.set( xDB
);
207 mxValidInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(validTestFile
), std::size(validTestFile
))) );
208 mxWarningInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(warningTestFile
), std::size(warningTestFile
))) );
209 mxErrorInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(errorTestFile
), std::size(errorTestFile
))) );
210 mxDomBuilder
->setErrorHandler(mxErrHandler
);
213 void validInputTest()
217 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #1",
218 mxDomBuilder
->parse(mxValidInStream
).is());
219 CPPUNIT_ASSERT_MESSAGE("Valid input file resulted in parse errors",
220 mxErrHandler
->noErrors());
222 catch (const css::xml::sax::SAXParseException
&)
224 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument (exception thrown)", false);
228 void warningInputTest()
232 // We DON'T expect exception here, as mxWarningInStream is valid XML Doc
233 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #2",
234 mxDomBuilder
->parse(mxWarningInStream
).is());
236 catch (const css::xml::sax::SAXParseException
& )
238 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #2 (exception thrown)", false);
240 CPPUNIT_ASSERT_MESSAGE("No parse warnings in unclean input file",
241 mxErrHandler
->mnWarnCount
);
242 CPPUNIT_ASSERT_MESSAGE("No parse warnings in unclean input file",
243 !mxErrHandler
->mnErrCount
/*&& !mxErrHandler->mnFatalCount*/);
246 void errorInputTest()
250 // We expect exception here, as mxErrorInStream is invalid XML Doc
251 CPPUNIT_ASSERT_MESSAGE("Invalid input file result in XDocument #2!",
252 !mxDomBuilder
->parse(mxErrorInStream
).is());
253 CPPUNIT_ASSERT_MESSAGE("No exception is thrown in unclean input file", false);
255 catch (const css::xml::sax::SAXParseException
&)
257 // It's OK to catch an exception here as we parse incorrect XML file
259 CPPUNIT_ASSERT_MESSAGE("No parse errors in unclean input file",
260 !mxErrHandler
->mnWarnCount
);
261 CPPUNIT_ASSERT_MESSAGE("No parse errors in unclean input file",
262 mxErrHandler
->mnErrCount
/*&& !mxErrHandler->mnFatalCount*/);
265 // Change the following lines only, if you add, remove or rename
266 // member functions of the current class,
267 // because these macros are need by auto register mechanism.
268 CPPUNIT_TEST_SUITE(BasicTest
);
269 CPPUNIT_TEST(validInputTest
);
270 CPPUNIT_TEST(warningInputTest
);
271 CPPUNIT_TEST(errorInputTest
);
272 CPPUNIT_TEST_SUITE_END();
275 struct SerializerTest
: public test::BootstrapFixture
277 uno::Reference
<XDocumentBuilder
> mxDomBuilder
;
278 rtl::Reference
<ErrorHandler
> mxErrHandler
;
279 rtl::Reference
<SequenceInputStream
> mxInStream
;
280 rtl::Reference
<DocumentHandler
> mxHandler
;
281 rtl::Reference
<TokenHandler
> mxTokHandler
;
282 uno::Sequence
< beans::Pair
< OUString
, sal_Int32
> > maRegisteredNamespaces
;
284 void setUp() override
286 test::BootstrapFixture::setUp();
288 mxErrHandler
.set( new ErrorHandler() );
289 uno::Reference
<XDocumentBuilder
> xDB( getMultiServiceFactory()->createInstance("com.sun.star.xml.dom.DocumentBuilder"), uno::UNO_QUERY_THROW
);
290 mxDomBuilder
.set( xDB
);
291 mxInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(validTestFile
), std::size(validTestFile
))) );
292 mxDomBuilder
->setErrorHandler(mxErrHandler
);
293 mxHandler
.set( new DocumentHandler
);
294 mxTokHandler
.set( new TokenHandler
);
296 maRegisteredNamespaces
= {
298 OUString( "urn:oasis:names:tc:opendocument:xmlns:office:1.0" ),
299 xml::sax::FastToken::NAMESPACE
),
301 OUString( "http://www.w3.org/1999/xlink" ),
302 2*xml::sax::FastToken::NAMESPACE
)
306 void serializerTest ()
310 uno::Reference
< xml::dom::XDocument
> xDoc
=
311 mxDomBuilder
->parse(mxInStream
);
312 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument",
314 CPPUNIT_ASSERT_MESSAGE("Valid input file resulted in parse errors",
315 mxErrHandler
->noErrors());
317 uno::Reference
< xml::sax::XSAXSerializable
> xSaxSerializer(
318 xDoc
, uno::UNO_QUERY
);
319 CPPUNIT_ASSERT_MESSAGE("XSAXSerializable not supported",
320 xSaxSerializer
.is());
322 uno::Reference
< xml::sax::XFastSAXSerializable
> xFastSaxSerializer(
323 xDoc
, uno::UNO_QUERY
);
324 CPPUNIT_ASSERT_MESSAGE("XFastSAXSerializable not supported",
325 xSaxSerializer
.is());
327 xFastSaxSerializer
->fastSerialize(mxHandler
,
329 uno::Sequence
< beans::StringPair
>(),
330 maRegisteredNamespaces
);
332 catch (const css::xml::sax::SAXParseException
&)
334 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument (exception thrown)", false);
338 // Change the following lines only, if you add, remove or rename
339 // member functions of the current class,
340 // because these macros are need by auto register mechanism.
342 CPPUNIT_TEST_SUITE(SerializerTest
);
343 CPPUNIT_TEST(serializerTest
);
344 CPPUNIT_TEST_SUITE_END();
347 CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest
);
348 CPPUNIT_TEST_SUITE_REGISTRATION(SerializerTest
);
351 // this macro creates an empty function, which will called by the RegisterAllFunctions()
352 // to let the user the possibility to also register some functions by hand.
353 CPPUNIT_PLUGIN_IMPLEMENT();
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */