1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tokenmap.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include "oox/token/tokenmap.hxx"
34 #include <rtl/strbuf.hxx>
35 #include <rtl/string.hxx>
37 #include "oox/helper/containerhelper.hxx"
40 using ::rtl::OUString
;
41 using ::com::sun::star::uno::Sequence
;
45 // ============================================================================
49 // include auto-generated token lists
51 #include "tokenwords.inc"
55 // ============================================================================
57 TokenMap::TokenMap() :
58 maTokenNames( static_cast< size_t >( XML_TOKEN_COUNT
) )
60 const sal_Char
* const* ppcTokenWord
= xmltokenwordlist
;
61 for( TokenNameVector::iterator aIt
= maTokenNames
.begin(), aEnd
= maTokenNames
.end(); aIt
!= aEnd
; ++aIt
, ++ppcTokenWord
)
63 OString
aUtf8Token( *ppcTokenWord
);
64 aIt
->maUniName
= OStringToOUString( aUtf8Token
, RTL_TEXTENCODING_UTF8
);
65 aIt
->maUtf8Name
= Sequence
< sal_Int8
>( reinterpret_cast< const sal_Int8
* >( aUtf8Token
.getStr() ), aUtf8Token
.getLength() );
68 #if OSL_DEBUG_LEVEL > 0
69 // check that the perfect_hash is in sync with the token name list
71 for( sal_Int32 nToken
= 0; bOk
&& (nToken
< XML_TOKEN_COUNT
); ++nToken
)
73 // check that the getIdentifier <-> getToken roundtrip works
74 OString aUtf8Name
= OUStringToOString( maTokenNames
[ nToken
].maUniName
, RTL_TEXTENCODING_UTF8
);
75 struct xmltoken
* pToken
= Perfect_Hash::in_word_set( aUtf8Name
.getStr(), aUtf8Name
.getLength() );
76 bOk
= pToken
&& (pToken
->nToken
== nToken
);
77 OSL_ENSURE( bOk
, ::rtl::OStringBuffer( "FastTokenHandler::FastTokenHandler - token list broken, #" ).
78 append( nToken
).append( ", '" ).append( aUtf8Name
).append( '\'' ).getStr() );
87 OUString
TokenMap::getUnicodeTokenName( sal_Int32 nToken
) const
89 const TokenName
* pTokenName
= ContainerHelper::getVectorElement( maTokenNames
, nToken
);
90 return pTokenName
? pTokenName
->maUniName
: OUString();
93 sal_Int32
TokenMap::getTokenFromUnicode( const OUString
& rUnicodeName
) const
95 OString aUtf8Name
= OUStringToOString( rUnicodeName
, RTL_TEXTENCODING_UTF8
);
96 struct xmltoken
* pToken
= Perfect_Hash::in_word_set( aUtf8Name
.getStr(), aUtf8Name
.getLength() );
97 return pToken
? pToken
->nToken
: XML_TOKEN_INVALID
;
100 Sequence
< sal_Int8
> TokenMap::getUtf8TokenName( sal_Int32 nToken
) const
102 const TokenName
* pTokenName
= ContainerHelper::getVectorElement( maTokenNames
, nToken
);
103 return pTokenName
? pTokenName
->maUtf8Name
: Sequence
< sal_Int8
>();
106 sal_Int32
TokenMap::getTokenFromUtf8( const Sequence
< sal_Int8
>& rUtf8Name
) const
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
;
113 // ============================================================================