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