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 <sal/log.hxx>
18 #include <xmloff/dllapi.h>
19 
20 namespace xmloff::token {
21 
22 class TokenMap
23 {
24 public:
25  explicit TokenMap();
26  ~TokenMap();
27 
29  css::uno::Sequence< sal_Int8 > const & getUtf8TokenName( sal_Int32 nToken ) const
30  {
31  SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter");
32  if( 0 <= nToken && nToken < XML_TOKEN_COUNT )
33  return maTokenNamesUtf8[ nToken ];
34  return EMPTY_BYTE_SEQ;
35  }
36 
37  const OUString& getTokenName( sal_Int32 nToken ) const
38  {
39  SAL_WARN_IF(nToken < 0 || nToken >= XML_TOKEN_COUNT, "xmloff", "Wrong nToken parameter");
40  if( 0 <= nToken && nToken < XML_TOKEN_COUNT )
41  return maTokenNames[ nToken ];
42  return EMPTY_STRING;
43  }
44 
46  static sal_Int32 getTokenFromUtf8( const css::uno::Sequence< sal_Int8 >& rUtf8Name )
47  {
48  return getTokenFromUTF8( reinterpret_cast< const char* >(
49  rUtf8Name.getConstArray() ), rUtf8Name.getLength() );
50  }
51 
53  static sal_Int32 getTokenFromUTF8( const char *pToken, sal_Int32 nLength )
54  {
55  return getTokenPerfectHash( pToken, nLength );
56  }
57 
58 private:
59  static sal_Int32 getTokenPerfectHash( const char *pToken, sal_Int32 nLength );
60 
61  std::vector< css::uno::Sequence< sal_Int8 > > maTokenNamesUtf8;
62  std::vector< OUString > maTokenNames;
63 
64  static const css::uno::Sequence< sal_Int8 > EMPTY_BYTE_SEQ;
65  static const OUString EMPTY_STRING;
66 };
67 
69 
70 class FastTokenHandler final :
72 {
73 public:
74  explicit FastTokenHandler();
75  virtual ~FastTokenHandler() override;
76 
77  // XFastTokenHandler
78  virtual css::uno::Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 nToken ) override;
79  virtual sal_Int32 SAL_CALL getTokenFromUTF8( const css::uno::Sequence< sal_Int8 >& Identifier ) override;
80 
81  const OUString & getIdentifier( sal_Int32 nToken ) const;
82 
83  // Much faster direct C++ shortcut to the method that matters
84  virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const override;
85 
86 private:
88 };
89 
90 } // namespace xmloff::token
91 
92 #endif
93 
94 /* 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:38
TokenMap & StaticTokenMap()
static sal_Int32 getTokenFromUTF8(const char *pToken, sal_Int32 nLength)
Returns the token identifier for a UTF-8 string.