update dev300-m58
[ooovba.git] / oox / source / token / tokenmap.cxx
blob5abeb9ab612400ec9d467729895001b81661c6f4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tokenmap.cxx,v $
10 * $Revision: 1.4 $
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 ************************************************************************/
31 #include <string.h>
32 #include <osl/mutex.hxx>
33 #include <rtl/strbuf.hxx>
34 #include <com/sun/star/xml/sax/FastToken.hpp>
35 #include "oox/core/fasttokenhandler.hxx"
36 #include "tokens.hxx"
38 using ::rtl::OString;
39 using ::rtl::OStringBuffer;
40 using ::rtl::OUString;
41 using ::rtl::OUStringToOString;
42 using ::osl::Mutex;
43 using ::osl::MutexGuard;
44 using ::com::sun::star::uno::Sequence;
45 using ::com::sun::star::uno::RuntimeException;
46 using ::com::sun::star::xml::sax::FastToken::DONTKNOW;
48 namespace oox {
50 #include "tokens.inc"
51 #include "tokenwords.inc"
53 // ============================================================================
55 namespace {
57 Mutex& lclGetTokenMutex()
59 static Mutex aMutex;
60 return aMutex;
63 } // namespace
65 // ============================================================================
67 FastTokenHandler::FastTokenHandler()
69 #if OSL_DEBUG_LEVEL > 0
70 MutexGuard aGuard( lclGetTokenMutex() );
71 bool bOk = true;
72 for( sal_Int32 nIdx = 0; bOk && (nIdx < XML_TOKEN_COUNT); ++nIdx )
74 // check that the getIdentifier <-> getToken roundtrip works
75 OUString aToken = getIdentifier( nIdx );
76 bOk = getToken( aToken ) == nIdx;
77 OSL_ENSURE( bOk, OStringBuffer( "FastTokenHandler::FastTokenHandler - token list broken, #" ).
78 append( nIdx ).append( ", '" ).
79 append( OUStringToOString( aToken, RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
81 #endif
84 FastTokenHandler::~FastTokenHandler()
88 sal_Int32 FastTokenHandler::getToken( const OUString& rIdentifier ) throw( RuntimeException )
90 MutexGuard aGuard( lclGetTokenMutex() );
92 OString aUTF8 = OUStringToOString( rIdentifier, RTL_TEXTENCODING_UTF8 );
94 struct xmltoken * t = Perfect_Hash::in_word_set( aUTF8.getStr(), aUTF8.getLength() );
95 return t ? t->nToken : DONTKNOW;
98 OUString FastTokenHandler::getIdentifier( sal_Int32 nToken ) throw( RuntimeException )
100 MutexGuard aGuard( lclGetTokenMutex() );
102 if( nToken >= XML_TOKEN_COUNT )
103 return OUString();
105 static OUString aTokens[XML_TOKEN_COUNT];
107 if( aTokens[nToken].getLength() == 0 )
108 aTokens[nToken] = OUString::createFromAscii( tokentowordlist[nToken] );
110 return aTokens[nToken];
113 Sequence< sal_Int8 > FastTokenHandler::getUTF8Identifier( sal_Int32 nToken ) throw( RuntimeException )
115 MutexGuard aGuard( lclGetTokenMutex() );
117 if( nToken >= XML_TOKEN_COUNT )
118 return Sequence< sal_Int8 >();
120 return Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8 *>(tokentowordlist[nToken]), strlen(tokentowordlist[nToken]));
123 sal_Int32 FastTokenHandler::getTokenFromUTF8( const Sequence< sal_Int8 >& rIdentifier ) throw( RuntimeException )
125 MutexGuard aGuard( lclGetTokenMutex() );
127 struct xmltoken * t = Perfect_Hash::in_word_set( reinterpret_cast< const char* >( rIdentifier.getConstArray() ), rIdentifier.getLength());
128 return t ? t->nToken : DONTKNOW;
131 // ============================================================================
133 } // namespace oox