Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / inputchecker / inputsequencechecker.cxx
blob0f8ca2305e0f51756091af202fd6634c1fecc7fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <inputsequencechecker.hxx>
22 #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
23 #include <com/sun/star/i18n/UnicodeType.hpp>
24 #include <i18nutil/unicode.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star::uno;
28 using namespace ::com::sun::star::lang;
29 using namespace ::rtl;
31 namespace com { namespace sun { namespace star { namespace i18n {
33 InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
35 serviceName = "com.sun.star.i18n.InputSequenceCheckerImpl";
36 cachedItem = NULL;
39 InputSequenceCheckerImpl::InputSequenceCheckerImpl()
43 InputSequenceCheckerImpl::~InputSequenceCheckerImpl()
45 // Clear lookuptable
46 for (size_t l = 0; l < lookupTable.size(); l++)
47 delete lookupTable[l];
49 lookupTable.clear();
52 sal_Bool SAL_CALL
53 InputSequenceCheckerImpl::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
54 sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException)
56 if (inputCheckMode == InputSequenceCheckMode::PASSTHROUGH)
57 return sal_True;
59 sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
61 if (language)
62 return getInputSequenceChecker(language)->checkInputSequence(Text, nStartPos, inputChar, inputCheckMode);
63 else
64 return sal_True; // not a checkable languages.
67 sal_Int32 SAL_CALL
68 InputSequenceCheckerImpl::correctInputSequence(OUString& Text, sal_Int32 nStartPos,
69 sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException)
71 if (inputCheckMode != InputSequenceCheckMode::PASSTHROUGH) {
72 sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
74 if (language)
75 return getInputSequenceChecker(language)->correctInputSequence(Text, nStartPos, inputChar, inputCheckMode);
77 Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
78 return nStartPos;
81 static ScriptTypeList typeList[] = {
82 //{ UnicodeScript_kHebrew, UnicodeScript_kHebrew }, // 10,
83 //{ UnicodeScript_kArabic, UnicodeScript_kArabic }, // 11,
84 { UnicodeScript_kDevanagari,UnicodeScript_kDevanagari, UnicodeScript_kDevanagari }, // 14,
85 { UnicodeScript_kThai, UnicodeScript_kThai, UnicodeScript_kThai }, // 24,
87 { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, UnicodeScript_kScriptCount } // 88
90 sal_Char* SAL_CALL
91 InputSequenceCheckerImpl::getLanguageByScripType(sal_Unicode cChar, sal_Unicode nChar)
93 sal_Int16 type = unicode::getUnicodeScriptType( cChar, typeList, UnicodeScript_kScriptCount );
95 if (type != UnicodeScript_kScriptCount &&
96 type == unicode::getUnicodeScriptType( nChar, typeList, UnicodeScript_kScriptCount )) {
97 switch(type) {
98 case UnicodeScript_kThai: return (sal_Char*)"th";
99 //case UnicodeScript_kArabic: return (sal_Char*)"ar";
100 //case UnicodeScript_kHebrew: return (sal_Char*)"he";
101 case UnicodeScript_kDevanagari: return (sal_Char*)"hi";
104 return NULL;
107 Reference< XExtendedInputSequenceChecker >& SAL_CALL
108 InputSequenceCheckerImpl::getInputSequenceChecker(sal_Char* rLanguage) throw (RuntimeException)
110 if (cachedItem && cachedItem->aLanguage == rLanguage) {
111 return cachedItem->xISC;
113 else if (xMSF.is()) {
114 for (size_t l = 0; l < lookupTable.size(); l++) {
115 cachedItem = lookupTable[l];
116 if (cachedItem->aLanguage == rLanguage)
117 return cachedItem->xISC;
120 Reference < uno::XInterface > xI = xMSF->createInstance(
121 OUString("com.sun.star.i18n.InputSequenceChecker_") +
122 OUString::createFromAscii(rLanguage));
124 if ( xI.is() ) {
125 Reference< XExtendedInputSequenceChecker > xISC;
126 xI->queryInterface( getCppuType((const Reference< XExtendedInputSequenceChecker>*)0) ) >>= xISC;
127 if (xISC.is()) {
128 lookupTable.push_back(cachedItem = new lookupTableItem(rLanguage, xISC));
129 return cachedItem->xISC;
133 throw RuntimeException();
136 OUString SAL_CALL
137 InputSequenceCheckerImpl::getImplementationName(void) throw( RuntimeException )
139 return OUString::createFromAscii(serviceName);
142 sal_Bool SAL_CALL
143 InputSequenceCheckerImpl::supportsService(const OUString& rServiceName) throw( RuntimeException )
145 return !rServiceName.compareToAscii(serviceName);
148 Sequence< OUString > SAL_CALL
149 InputSequenceCheckerImpl::getSupportedServiceNames(void) throw( RuntimeException )
151 Sequence< OUString > aRet(1);
152 aRet[0] = OUString::createFromAscii(serviceName);
153 return aRet;
156 } } } }
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */