xref: /aoo42x/main/oox/source/token/tokenmap.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/token/tokenmap.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <string.h>
27cdf0e10cSrcweir #include <rtl/strbuf.hxx>
28cdf0e10cSrcweir #include <rtl/string.hxx>
29cdf0e10cSrcweir #include "oox/token/tokens.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace oox {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // ============================================================================
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
36cdf0e10cSrcweir using ::rtl::OString;
37cdf0e10cSrcweir using ::rtl::OUString;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // ============================================================================
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace {
42cdf0e10cSrcweir // include auto-generated Perfect_Hash
43cdf0e10cSrcweir #include "tokenhash.inc"
44cdf0e10cSrcweir } // namespace
45cdf0e10cSrcweir 
46cdf0e10cSrcweir // ============================================================================
47cdf0e10cSrcweir 
48cdf0e10cSrcweir TokenMap::TokenMap() :
49cdf0e10cSrcweir     maTokenNames( static_cast< size_t >( XML_TOKEN_COUNT ) )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     static const sal_Char* sppcTokenNames[] =
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir // include auto-generated C array with token names as C strings
54cdf0e10cSrcweir #include "tokennames.inc"
55cdf0e10cSrcweir         ""
56cdf0e10cSrcweir     };
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     const sal_Char* const* ppcTokenName = sppcTokenNames;
59cdf0e10cSrcweir     for( TokenNameVector::iterator aIt = maTokenNames.begin(), aEnd = maTokenNames.end(); aIt != aEnd; ++aIt, ++ppcTokenName )
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         OString aUtf8Token( *ppcTokenName );
62cdf0e10cSrcweir         aIt->maUniName = OStringToOUString( aUtf8Token, RTL_TEXTENCODING_UTF8 );
63cdf0e10cSrcweir         aIt->maUtf8Name = Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aUtf8Token.getStr() ), aUtf8Token.getLength() );
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
67cdf0e10cSrcweir     // check that the perfect_hash is in sync with the token name list
68cdf0e10cSrcweir     bool bOk = true;
69cdf0e10cSrcweir     for( sal_Int32 nToken = 0; bOk && (nToken < XML_TOKEN_COUNT); ++nToken )
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         // check that the getIdentifier <-> getToken roundtrip works
72cdf0e10cSrcweir         OString aUtf8Name = OUStringToOString( maTokenNames[ nToken ].maUniName, RTL_TEXTENCODING_UTF8 );
73cdf0e10cSrcweir         struct xmltoken* pToken = Perfect_Hash::in_word_set( aUtf8Name.getStr(), aUtf8Name.getLength() );
74cdf0e10cSrcweir         bOk = pToken && (pToken->nToken == nToken);
75cdf0e10cSrcweir         OSL_ENSURE( bOk, ::rtl::OStringBuffer( "TokenMap::TokenMap - token list broken, #" ).
76cdf0e10cSrcweir             append( nToken ).append( ", '" ).append( aUtf8Name ).append( '\'' ).getStr() );
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir #endif
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir TokenMap::~TokenMap()
82cdf0e10cSrcweir {
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir OUString TokenMap::getUnicodeTokenName( sal_Int32 nToken ) const
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
88cdf0e10cSrcweir         return maTokenNames[ static_cast< size_t >( nToken ) ].maUniName;
89cdf0e10cSrcweir     return OUString();
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir sal_Int32 TokenMap::getTokenFromUnicode( const OUString& rUnicodeName ) const
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     OString aUtf8Name = OUStringToOString( rUnicodeName, RTL_TEXTENCODING_UTF8 );
95cdf0e10cSrcweir     struct xmltoken* pToken = Perfect_Hash::in_word_set( aUtf8Name.getStr(), aUtf8Name.getLength() );
96cdf0e10cSrcweir     return pToken ? pToken->nToken : XML_TOKEN_INVALID;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir Sequence< sal_Int8 > TokenMap::getUtf8TokenName( sal_Int32 nToken ) const
100cdf0e10cSrcweir {
101cdf0e10cSrcweir     if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
102cdf0e10cSrcweir         return maTokenNames[ static_cast< size_t >( nToken ) ].maUtf8Name;
103cdf0e10cSrcweir     return Sequence< sal_Int8 >();
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir sal_Int32 TokenMap::getTokenFromUtf8( const Sequence< sal_Int8 >& rUtf8Name ) const
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     struct xmltoken* pToken = Perfect_Hash::in_word_set(
109cdf0e10cSrcweir         reinterpret_cast< const char* >( rUtf8Name.getConstArray() ), rUtf8Name.getLength() );
110cdf0e10cSrcweir     return pToken ? pToken->nToken : XML_TOKEN_INVALID;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir // ============================================================================
114cdf0e10cSrcweir 
115cdf0e10cSrcweir } // namespace oox
116