1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*449ab281SAndrew Rist  * distributed with this work for additional information
6*449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*449ab281SAndrew Rist  *
11*449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*449ab281SAndrew Rist  *
13*449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist  * software distributed under the License is distributed on an
15*449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17*449ab281SAndrew Rist  * specific language governing permissions and limitations
18*449ab281SAndrew Rist  * under the License.
19*449ab281SAndrew Rist  *
20*449ab281SAndrew Rist  *************************************************************/
21*449ab281SAndrew Rist 
22*449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <inputsequencechecker.hxx>
28cdf0e10cSrcweir #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
29cdf0e10cSrcweir #include <com/sun/star/i18n/UnicodeType.hpp>
30cdf0e10cSrcweir #include <i18nutil/unicode.hxx>
31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir using namespace ::com::sun::star::uno;
34cdf0e10cSrcweir using namespace ::com::sun::star::lang;
35cdf0e10cSrcweir using namespace ::rtl;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
38cdf0e10cSrcweir 
InputSequenceCheckerImpl(const Reference<XMultiServiceFactory> & rxMSF)39cdf0e10cSrcweir InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
40cdf0e10cSrcweir {
41cdf0e10cSrcweir         serviceName = "com.sun.star.i18n.InputSequenceCheckerImpl";
42cdf0e10cSrcweir         cachedItem = NULL;
43cdf0e10cSrcweir }
44cdf0e10cSrcweir 
InputSequenceCheckerImpl()45cdf0e10cSrcweir InputSequenceCheckerImpl::InputSequenceCheckerImpl()
46cdf0e10cSrcweir {
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
~InputSequenceCheckerImpl()49cdf0e10cSrcweir InputSequenceCheckerImpl::~InputSequenceCheckerImpl()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir         // Clear lookuptable
52cdf0e10cSrcweir         for (size_t l = 0; l < lookupTable.size(); l++)
53cdf0e10cSrcweir             delete lookupTable[l];
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         lookupTable.clear();
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir sal_Bool SAL_CALL
checkInputSequence(const OUString & Text,sal_Int32 nStartPos,sal_Unicode inputChar,sal_Int16 inputCheckMode)59cdf0e10cSrcweir InputSequenceCheckerImpl::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
60cdf0e10cSrcweir         sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir         if (inputCheckMode == InputSequenceCheckMode::PASSTHROUGH)
63cdf0e10cSrcweir             return sal_True;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         if (language)
68cdf0e10cSrcweir             return getInputSequenceChecker(language)->checkInputSequence(Text, nStartPos, inputChar, inputCheckMode);
69cdf0e10cSrcweir         else
70cdf0e10cSrcweir             return sal_True; // not a checkable languages.
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir sal_Int32 SAL_CALL
correctInputSequence(OUString & Text,sal_Int32 nStartPos,sal_Unicode inputChar,sal_Int16 inputCheckMode)74cdf0e10cSrcweir InputSequenceCheckerImpl::correctInputSequence(OUString& Text, sal_Int32 nStartPos,
75cdf0e10cSrcweir         sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir         if (inputCheckMode != InputSequenceCheckMode::PASSTHROUGH) {
78cdf0e10cSrcweir             sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             if (language)
81cdf0e10cSrcweir                 return getInputSequenceChecker(language)->correctInputSequence(Text, nStartPos, inputChar, inputCheckMode);
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
84cdf0e10cSrcweir         return nStartPos;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir static ScriptTypeList typeList[] = {
88cdf0e10cSrcweir         //{ UnicodeScript_kHebrew,              UnicodeScript_kHebrew },        // 10,
89cdf0e10cSrcweir         //{ UnicodeScript_kArabic,              UnicodeScript_kArabic },        // 11,
90cdf0e10cSrcweir         { UnicodeScript_kDevanagari,UnicodeScript_kDevanagari,          UnicodeScript_kDevanagari },    // 14,
91cdf0e10cSrcweir         { UnicodeScript_kThai,  UnicodeScript_kThai,                  UnicodeScript_kThai },          // 24,
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         { UnicodeScript_kScriptCount,   UnicodeScript_kScriptCount,           UnicodeScript_kScriptCount }    // 88
94cdf0e10cSrcweir };
95cdf0e10cSrcweir 
96cdf0e10cSrcweir sal_Char* SAL_CALL
getLanguageByScripType(sal_Unicode cChar,sal_Unicode nChar)97cdf0e10cSrcweir InputSequenceCheckerImpl::getLanguageByScripType(sal_Unicode cChar, sal_Unicode nChar)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     sal_Int16 type = unicode::getUnicodeScriptType( cChar, typeList, UnicodeScript_kScriptCount );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     if (type != UnicodeScript_kScriptCount &&
102cdf0e10cSrcweir             type == unicode::getUnicodeScriptType( nChar, typeList, UnicodeScript_kScriptCount )) {
103cdf0e10cSrcweir         switch(type) {
104cdf0e10cSrcweir             case UnicodeScript_kThai:           return (sal_Char*)"th";
105cdf0e10cSrcweir             //case UnicodeScript_kArabic:       return (sal_Char*)"ar";
106cdf0e10cSrcweir             //case UnicodeScript_kHebrew:       return (sal_Char*)"he";
107cdf0e10cSrcweir             case UnicodeScript_kDevanagari:   return (sal_Char*)"hi";
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir     return NULL;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir Reference< XExtendedInputSequenceChecker >& SAL_CALL
getInputSequenceChecker(sal_Char * rLanguage)114cdf0e10cSrcweir InputSequenceCheckerImpl::getInputSequenceChecker(sal_Char* rLanguage) throw (RuntimeException)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir         if (cachedItem && cachedItem->aLanguage == rLanguage) {
117cdf0e10cSrcweir             return cachedItem->xISC;
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         else if (xMSF.is()) {
120cdf0e10cSrcweir             for (size_t l = 0; l < lookupTable.size(); l++) {
121cdf0e10cSrcweir                 cachedItem = lookupTable[l];
122cdf0e10cSrcweir                 if (cachedItem->aLanguage == rLanguage)
123cdf0e10cSrcweir                     return cachedItem->xISC;
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             Reference < uno::XInterface > xI = xMSF->createInstance(
127cdf0e10cSrcweir                         OUString::createFromAscii("com.sun.star.i18n.InputSequenceChecker_") +
128cdf0e10cSrcweir                         OUString::createFromAscii(rLanguage));
129cdf0e10cSrcweir 
130cdf0e10cSrcweir             if ( xI.is() ) {
131cdf0e10cSrcweir                 Reference< XExtendedInputSequenceChecker > xISC;
132cdf0e10cSrcweir                 xI->queryInterface( getCppuType((const Reference< XExtendedInputSequenceChecker>*)0) ) >>= xISC;
133cdf0e10cSrcweir                 if (xISC.is()) {
134cdf0e10cSrcweir                     lookupTable.push_back(cachedItem = new lookupTableItem(rLanguage, xISC));
135cdf0e10cSrcweir                     return cachedItem->xISC;
136cdf0e10cSrcweir                 }
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir         throw RuntimeException();
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir OUString SAL_CALL
getImplementationName(void)143cdf0e10cSrcweir InputSequenceCheckerImpl::getImplementationName(void) throw( RuntimeException )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir         return OUString::createFromAscii(serviceName);
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)149cdf0e10cSrcweir InputSequenceCheckerImpl::supportsService(const OUString& rServiceName) throw( RuntimeException )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir         return !rServiceName.compareToAscii(serviceName);
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir Sequence< OUString > SAL_CALL
getSupportedServiceNames(void)155cdf0e10cSrcweir InputSequenceCheckerImpl::getSupportedServiceNames(void) throw( RuntimeException )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir         Sequence< OUString > aRet(1);
158cdf0e10cSrcweir         aRet[0] = OUString::createFromAscii(serviceName);
159cdf0e10cSrcweir         return aRet;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir } } } }
163