xref: /aoo42x/main/oox/source/token/tokenmap.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/token/tokenmap.hxx"
29 
30 #include <string.h>
31 #include <rtl/strbuf.hxx>
32 #include <rtl/string.hxx>
33 #include "oox/token/tokens.hxx"
34 
35 namespace oox {
36 
37 // ============================================================================
38 
39 using ::com::sun::star::uno::Sequence;
40 using ::rtl::OString;
41 using ::rtl::OUString;
42 
43 // ============================================================================
44 
45 namespace {
46 // include auto-generated Perfect_Hash
47 #include "tokenhash.inc"
48 } // namespace
49 
50 // ============================================================================
51 
52 TokenMap::TokenMap() :
53     maTokenNames( static_cast< size_t >( XML_TOKEN_COUNT ) )
54 {
55     static const sal_Char* sppcTokenNames[] =
56     {
57 // include auto-generated C array with token names as C strings
58 #include "tokennames.inc"
59         ""
60     };
61 
62     const sal_Char* const* ppcTokenName = sppcTokenNames;
63     for( TokenNameVector::iterator aIt = maTokenNames.begin(), aEnd = maTokenNames.end(); aIt != aEnd; ++aIt, ++ppcTokenName )
64     {
65         OString aUtf8Token( *ppcTokenName );
66         aIt->maUniName = OStringToOUString( aUtf8Token, RTL_TEXTENCODING_UTF8 );
67         aIt->maUtf8Name = Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aUtf8Token.getStr() ), aUtf8Token.getLength() );
68     }
69 
70 #if OSL_DEBUG_LEVEL > 0
71     // check that the perfect_hash is in sync with the token name list
72     bool bOk = true;
73     for( sal_Int32 nToken = 0; bOk && (nToken < XML_TOKEN_COUNT); ++nToken )
74     {
75         // check that the getIdentifier <-> getToken roundtrip works
76         OString aUtf8Name = OUStringToOString( maTokenNames[ nToken ].maUniName, RTL_TEXTENCODING_UTF8 );
77         struct xmltoken* pToken = Perfect_Hash::in_word_set( aUtf8Name.getStr(), aUtf8Name.getLength() );
78         bOk = pToken && (pToken->nToken == nToken);
79         OSL_ENSURE( bOk, ::rtl::OStringBuffer( "TokenMap::TokenMap - token list broken, #" ).
80             append( nToken ).append( ", '" ).append( aUtf8Name ).append( '\'' ).getStr() );
81     }
82 #endif
83 }
84 
85 TokenMap::~TokenMap()
86 {
87 }
88 
89 OUString TokenMap::getUnicodeTokenName( sal_Int32 nToken ) const
90 {
91     if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
92         return maTokenNames[ static_cast< size_t >( nToken ) ].maUniName;
93     return OUString();
94 }
95 
96 sal_Int32 TokenMap::getTokenFromUnicode( const OUString& rUnicodeName ) const
97 {
98     OString aUtf8Name = OUStringToOString( rUnicodeName, RTL_TEXTENCODING_UTF8 );
99     struct xmltoken* pToken = Perfect_Hash::in_word_set( aUtf8Name.getStr(), aUtf8Name.getLength() );
100     return pToken ? pToken->nToken : XML_TOKEN_INVALID;
101 }
102 
103 Sequence< sal_Int8 > TokenMap::getUtf8TokenName( sal_Int32 nToken ) const
104 {
105     if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
106         return maTokenNames[ static_cast< size_t >( nToken ) ].maUtf8Name;
107     return Sequence< sal_Int8 >();
108 }
109 
110 sal_Int32 TokenMap::getTokenFromUtf8( const Sequence< sal_Int8 >& rUtf8Name ) const
111 {
112     struct xmltoken* pToken = Perfect_Hash::in_word_set(
113         reinterpret_cast< const char* >( rUtf8Name.getConstArray() ), rUtf8Name.getLength() );
114     return pToken ? pToken->nToken : XML_TOKEN_INVALID;
115 }
116 
117 // ============================================================================
118 
119 } // namespace oox
120