xref: /trunk/main/oox/source/token/tokenmap.cxx (revision b63233d8)
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 
43 // include auto-generated Perfect_Hash class
44 #include <token/tokenhash.inc>
45 
46 } // namespace
47 
48 // ============================================================================
49 
TokenMap()50 TokenMap::TokenMap() :
51     maTokenNames( static_cast< size_t >( XML_TOKEN_COUNT ) )
52 {
53     static const sal_Char* sppcTokenNames[] =
54     {
55 // include auto-generated C array with token names as C strings
56 #include <token/tokennames.inc>
57         ""
58     };
59 
60     const sal_Char* const* ppcTokenName = sppcTokenNames;
61     for( TokenNameVector::iterator aIt = maTokenNames.begin(), aEnd = maTokenNames.end(); aIt != aEnd; ++aIt, ++ppcTokenName )
62     {
63         OString aUtf8Token( *ppcTokenName );
64         aIt->maUniName = OStringToOUString( aUtf8Token, RTL_TEXTENCODING_UTF8 );
65         aIt->maUtf8Name = Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aUtf8Token.getStr() ), aUtf8Token.getLength() );
66     }
67 
68 #if OSL_DEBUG_LEVEL > 0
69     // check that the Perfect_Hash is in sync with the token name list
70     bool bOk = true;
71     for( sal_Int32 nToken = 0; bOk && (nToken < XML_TOKEN_COUNT); ++nToken )
72     {
73         // check that the getIdentifier <-> getToken roundtrip works
74         OString aUtf8Name = OUStringToOString( maTokenNames[ nToken ].maUniName, RTL_TEXTENCODING_UTF8 );
75         const XMLTokenInfo* pTokenInfo = Perfect_Hash::getTokenInfo( aUtf8Name.getStr(), aUtf8Name.getLength() );
76         bOk = pTokenInfo && (pTokenInfo->mnToken == nToken);
77         OSL_ENSURE( bOk, ::rtl::OStringBuffer( "TokenMap::TokenMap - token list broken, #" ).
78             append( nToken ).append( ", '" ).append( aUtf8Name ).append( '\'' ).getStr() );
79     }
80 #endif
81 }
82 
~TokenMap()83 TokenMap::~TokenMap()
84 {
85 }
86 
getUnicodeTokenName(sal_Int32 nToken) const87 OUString TokenMap::getUnicodeTokenName( sal_Int32 nToken ) const
88 {
89     if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
90         return maTokenNames[ static_cast< size_t >( nToken ) ].maUniName;
91     return OUString();
92 }
93 
getTokenFromUnicode(const OUString & rUnicodeName) const94 sal_Int32 TokenMap::getTokenFromUnicode( const OUString& rUnicodeName ) const
95 {
96     OString aUtf8Name = OUStringToOString( rUnicodeName, RTL_TEXTENCODING_UTF8 );
97     const XMLTokenInfo* pTokenInfo = Perfect_Hash::getTokenInfo( aUtf8Name.getStr(), aUtf8Name.getLength() );
98     return pTokenInfo ? pTokenInfo->mnToken : XML_TOKEN_INVALID;
99 }
100 
getUtf8TokenName(sal_Int32 nToken) const101 Sequence< sal_Int8 > TokenMap::getUtf8TokenName( sal_Int32 nToken ) const
102 {
103     if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
104         return maTokenNames[ static_cast< size_t >( nToken ) ].maUtf8Name;
105     return Sequence< sal_Int8 >();
106 }
107 
getTokenFromUtf8(const Sequence<sal_Int8> & rUtf8Name) const108 sal_Int32 TokenMap::getTokenFromUtf8( const Sequence< sal_Int8 >& rUtf8Name ) const
109 {
110     const XMLTokenInfo* pTokenInfo = Perfect_Hash::getTokenInfo(
111         reinterpret_cast< const char* >( rUtf8Name.getConstArray() ), rUtf8Name.getLength() );
112     return pTokenInfo ? pTokenInfo->mnToken : XML_TOKEN_INVALID;
113 }
114 
115 // ============================================================================
116 
117 } // namespace oox
118