diff options
author | Daniel Sikeler <d.sikeler94@gmail.com> | 2016-08-12 10:26:28 +0000 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2016-08-16 20:31:31 +0000 |
commit | f577a422079c5333caa52e69ad09eb2b83de9134 (patch) | |
tree | 09dec5fb643532a9217bda0fb93d55046e437ad7 /include | |
parent | 10c6bef34d8f40173f2d87037327706cb20ed33e (diff) |
GSoC - implement global tokenhandler for odf-tokens
This generates perfect hash for odf-tokens and use them with the tokenhandler.
With added test case to check to and fro mapping between tokens.
This is taken from Daniel's work in feature/fastparser branch.
Change-Id: I7cf77c1eb6c9dd68fd78108c6e0726507c7672e1
Reviewed-on: https://gerrit.libreoffice.org/28073
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Eike Rathke <erack@redhat.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/xmloff/fasttokenhandler.hxx | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/include/xmloff/fasttokenhandler.hxx b/include/xmloff/fasttokenhandler.hxx new file mode 100644 index 000000000000..9cda5e3f8b6f --- /dev/null +++ b/include/xmloff/fasttokenhandler.hxx @@ -0,0 +1,88 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_XMLOFF_FASTTOKENHANDLER_HXX +#define INCLUDED_XMLOFF_FASTTOKENHANDLER_HXX + +#include <com/sun/star/xml/sax/XFastTokenHandler.hpp> +#include <cppuhelper/implbase1.hxx> +#include <sax/fastattribs.hxx> +#include <xmloff/token/tokens.hxx> +#include <rtl/instance.hxx> +#include <xmloff/dllapi.h> + +namespace xmloff { +namespace token { + +class TokenMap +{ +public: + explicit TokenMap(); + ~TokenMap(); + + /** Returns the token identifier for the passed Unicode token name. */ + sal_Int32 getTokenFromUnicode( const OUString& rUnicodeName ) const; + + /** Returns the UTF8 name of the passed token identifier as byte sequence. */ + css::uno::Sequence< sal_Int8 > getUtf8TokenName( sal_Int32 nToken ) const + { + SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter"); + if( 0 <= nToken && nToken < XML_TOKEN_COUNT ) + return maTokenNames[ nToken ]; + return css::uno::Sequence< sal_Int8 >(); + } + + /** Returns the token identifier for the passed UTF8 token name. */ + sal_Int32 getTokenFromUtf8( const css::uno::Sequence< sal_Int8 >& rUtf8Name ) const + { + return getTokenFromUTF8( reinterpret_cast< const char* >( + rUtf8Name.getConstArray() ), rUtf8Name.getLength() ); + } + + /** Returns the token identifier for a UTF8 string passed in pToken */ + sal_Int32 getTokenFromUTF8( const char *pToken, sal_Int32 nLength ) const + { + return getTokenPerfectHash( pToken, nLength ); + } + +private: + sal_Int32 getTokenPerfectHash( const char *pToken, sal_Int32 nLength ) const; + + std::vector< css::uno::Sequence< sal_Int8 > > maTokenNames; +}; + +struct StaticTokenMap : public rtl::Static< TokenMap, StaticTokenMap > {}; + +class XMLOFF_DLLPUBLIC FastTokenHandler : public cppu::WeakImplHelper1< + css::xml::sax::XFastTokenHandler >, + public sax_fastparser::FastTokenHandlerBase +{ +public: + explicit FastTokenHandler(); + virtual ~FastTokenHandler(); + + // XFastTokenHandler + virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 nToken ) + throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE; + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const css::uno::Sequence< sal_Int8 >& Identifier ) + throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE; + + // Much faster direct C++ shortcut to the method that matters + virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const SAL_OVERRIDE; + +private: + TokenMap& mrTokenMap; +}; + +} // namespace token +} // namespace xmloff + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |