merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / inputchecker / inputsequencechecker.cxx
blobb0965db445632024eccd6307ecbc9a19719e66f1
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: inputsequencechecker.cxx,v $
10 * $Revision: 1.10 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include <inputsequencechecker.hxx>
35 #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
36 #include <com/sun/star/i18n/UnicodeType.hpp>
37 #include <i18nutil/unicode.hxx>
38 #include <rtl/ustrbuf.hxx>
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::rtl;
44 namespace com { namespace sun { namespace star { namespace i18n {
46 InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
48 serviceName = "com.sun.star.i18n.InputSequenceCheckerImpl";
49 cachedItem = NULL;
52 InputSequenceCheckerImpl::InputSequenceCheckerImpl()
56 InputSequenceCheckerImpl::~InputSequenceCheckerImpl()
58 // Clear lookuptable
59 for (size_t l = 0; l < lookupTable.size(); l++)
60 delete lookupTable[l];
62 lookupTable.clear();
65 sal_Bool SAL_CALL
66 InputSequenceCheckerImpl::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
67 sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException)
69 if (inputCheckMode == InputSequenceCheckMode::PASSTHROUGH)
70 return sal_True;
72 sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
74 if (language)
75 return getInputSequenceChecker(language)->checkInputSequence(Text, nStartPos, inputChar, inputCheckMode);
76 else
77 return sal_True; // not a checkable languages.
80 sal_Int32 SAL_CALL
81 InputSequenceCheckerImpl::correctInputSequence(OUString& Text, sal_Int32 nStartPos,
82 sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException)
84 if (inputCheckMode != InputSequenceCheckMode::PASSTHROUGH) {
85 sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
87 if (language)
88 return getInputSequenceChecker(language)->correctInputSequence(Text, nStartPos, inputChar, inputCheckMode);
90 Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
91 return nStartPos;
94 static ScriptTypeList typeList[] = {
95 //{ UnicodeScript_kHebrew, UnicodeScript_kHebrew }, // 10,
96 //{ UnicodeScript_kArabic, UnicodeScript_kArabic }, // 11,
97 { UnicodeScript_kDevanagari,UnicodeScript_kDevanagari, UnicodeScript_kDevanagari }, // 14,
98 { UnicodeScript_kThai, UnicodeScript_kThai, UnicodeScript_kThai }, // 24,
100 { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, UnicodeScript_kScriptCount } // 88
103 sal_Char* SAL_CALL
104 InputSequenceCheckerImpl::getLanguageByScripType(sal_Unicode cChar, sal_Unicode nChar)
106 sal_Int16 type = unicode::getUnicodeScriptType( cChar, typeList, UnicodeScript_kScriptCount );
108 if (type != UnicodeScript_kScriptCount &&
109 type == unicode::getUnicodeScriptType( nChar, typeList, UnicodeScript_kScriptCount )) {
110 switch(type) {
111 case UnicodeScript_kThai: return (sal_Char*)"th";
112 //case UnicodeScript_kArabic: return (sal_Char*)"ar";
113 //case UnicodeScript_kHebrew: return (sal_Char*)"he";
114 case UnicodeScript_kDevanagari: return (sal_Char*)"hi";
117 return NULL;
120 Reference< XExtendedInputSequenceChecker >& SAL_CALL
121 InputSequenceCheckerImpl::getInputSequenceChecker(sal_Char* rLanguage) throw (RuntimeException)
123 if (cachedItem && cachedItem->aLanguage == rLanguage) {
124 return cachedItem->xISC;
126 else if (xMSF.is()) {
127 for (size_t l = 0; l < lookupTable.size(); l++) {
128 cachedItem = lookupTable[l];
129 if (cachedItem->aLanguage == rLanguage)
130 return cachedItem->xISC;
133 Reference < uno::XInterface > xI = xMSF->createInstance(
134 OUString::createFromAscii("com.sun.star.i18n.InputSequenceChecker_") +
135 OUString::createFromAscii(rLanguage));
137 if ( xI.is() ) {
138 Reference< XExtendedInputSequenceChecker > xISC;
139 xI->queryInterface( getCppuType((const Reference< XExtendedInputSequenceChecker>*)0) ) >>= xISC;
140 if (xISC.is()) {
141 lookupTable.push_back(cachedItem = new lookupTableItem(rLanguage, xISC));
142 return cachedItem->xISC;
146 throw RuntimeException();
149 OUString SAL_CALL
150 InputSequenceCheckerImpl::getImplementationName(void) throw( RuntimeException )
152 return OUString::createFromAscii(serviceName);
155 sal_Bool SAL_CALL
156 InputSequenceCheckerImpl::supportsService(const OUString& rServiceName) throw( RuntimeException )
158 return !rServiceName.compareToAscii(serviceName);
161 Sequence< OUString > SAL_CALL
162 InputSequenceCheckerImpl::getSupportedServiceNames(void) throw( RuntimeException )
164 Sequence< OUString > aRet(1);
165 aRet[0] = OUString::createFromAscii(serviceName);
166 return aRet;
169 } } } }