LibreOffice Module xmloff (master)  1
fasttokenhandler.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #ifndef XMLOFF_INC_FASTTOKENHANDLER_HXX
11 #define XMLOFF_INC_FASTTOKENHANDLER_HXX
12 
13 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
14 #include <cppuhelper/implbase.hxx>
15 #include <sax/fastattribs.hxx>
16 #include <xmloff/token/tokens.hxx>
17 #include <rtl/instance.hxx>
18 #include <sal/log.hxx>
19 #include <xmloff/dllapi.h>
20 
21 namespace xmloff::token {
22 
23 class TokenMap
24 {
25 public:
26  explicit TokenMap();
27  ~TokenMap();
28 
30  css::uno::Sequence< sal_Int8 > const & getUtf8TokenName( sal_Int32 nToken ) const
31  {
32  SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter");
33  if( 0 <= nToken && nToken < XML_TOKEN_COUNT )
34  return maTokenNamesUtf8[ nToken ];
35  return EMPTY_BYTE_SEQ;
36  }
37 
38  const OUString& getTokenName( sal_Int32 nToken ) const
39  {
40  SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter");
41  if( 0 <= nToken && nToken < XML_TOKEN_COUNT )
42  return maTokenNames[ nToken ];
43  return EMPTY_STRING;
44  }
45 
47  static sal_Int32 getTokenFromUtf8( const css::uno::Sequence< sal_Int8 >& rUtf8Name )
48  {
49  return getTokenFromUTF8( reinterpret_cast< const char* >(
50  rUtf8Name.getConstArray() ), rUtf8Name.getLength() );
51  }
52 
54  static sal_Int32 getTokenFromUTF8( const char *pToken, sal_Int32 nLength )
55  {
56  return getTokenPerfectHash( pToken, nLength );
57  }
58 
59 private:
60  static sal_Int32 getTokenPerfectHash( const char *pToken, sal_Int32 nLength );
61 
62  std::vector< css::uno::Sequence< sal_Int8 > > maTokenNamesUtf8;
63  std::vector< OUString > maTokenNames;
64 
65  static const css::uno::Sequence< sal_Int8 > EMPTY_BYTE_SEQ;
66  static const OUString EMPTY_STRING;
67 };
68 
69 struct StaticTokenMap : public rtl::Static< TokenMap, StaticTokenMap > {};
70 
71 class FastTokenHandler final :
73 {
74 public:
75  explicit FastTokenHandler();
76  virtual ~FastTokenHandler() override;
77 
78  // XFastTokenHandler
79  virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 nToken ) override;
80  virtual sal_Int32 SAL_CALL getTokenFromUTF8( const css::uno::Sequence< sal_Int8 >& Identifier ) override;
81 
82  const OUString & getIdentifier( sal_Int32 nToken ) const;
83 
84  // Much faster direct C++ shortcut to the method that matters
85  virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const override;
86 
87 private:
89 };
90 
91 } // namespace xmloff::token
92 
93 #endif
94 
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int8 > const & getUtf8TokenName(sal_Int32 nToken) const
Returns the UTF-8 name of the passed token identifier as byte sequence.
static sal_Int32 getTokenPerfectHash(const char *pToken, sal_Int32 nLength)
static const OUString EMPTY_STRING
static const css::uno::Sequence< sal_Int8 > EMPTY_BYTE_SEQ
const OUString & getTokenName(sal_Int32 nToken) const
virtual sal_Int32 getTokenDirect(const char *pToken, sal_Int32 nLength) const override
virtual sal_Int32 SAL_CALL getTokenFromUTF8(const css::uno::Sequence< sal_Int8 > &Identifier) override
static sal_Int32 getTokenFromUtf8(const css::uno::Sequence< sal_Int8 > &rUtf8Name)
Returns the token identifier for the passed UTF-8 token name.
std::vector< css::uno::Sequence< sal_Int8 > > maTokenNamesUtf8
const OUString & getIdentifier(sal_Int32 nToken) const
DefTokenId nToken
#define SAL_WARN_IF(condition, area, stream)
Handling of tokens in XML:
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier(sal_Int32 nToken) override
std::vector< OUString > maTokenNames
sal_Int32 nLength
Definition: xmltoken.cxx:36
static sal_Int32 getTokenFromUTF8(const char *pToken, sal_Int32 nLength)
Returns the token identifier for a UTF-8 string.