Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / inputchecker / inputsequencechecker.cxx
blobed0b5ccf410a5ed318f9011f94af968af2c34bd9
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 .
20 #include <inputsequencechecker.hxx>
21 #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
22 #include <com/sun/star/i18n/UnicodeType.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <i18nutil/unicode.hxx>
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::lang;
29 namespace com { namespace sun { namespace star { namespace i18n {
31 InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
33 serviceName = "com.sun.star.i18n.InputSequenceChecker";
34 cachedItem = NULL;
37 InputSequenceCheckerImpl::InputSequenceCheckerImpl(const char *pServiceName)
38 : serviceName(pServiceName)
39 , cachedItem(NULL)
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, std::exception)
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, std::exception)
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 {
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 = m_xContext->getServiceManager()->createInstanceWithContext(
121 OUString("com.sun.star.i18n.InputSequenceChecker_") +
122 OUString::createFromAscii(rLanguage),
123 m_xContext);
125 if ( xI.is() ) {
126 Reference< XExtendedInputSequenceChecker > xISC( xI, uno::UNO_QUERY );
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, std::exception )
139 return OUString::createFromAscii(serviceName);
142 sal_Bool SAL_CALL
143 InputSequenceCheckerImpl::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
145 return cppu::supportsService(this, rServiceName);
148 Sequence< OUString > SAL_CALL
149 InputSequenceCheckerImpl::getSupportedServiceNames(void) throw( RuntimeException, std::exception )
151 Sequence< OUString > aRet(1);
152 aRet[0] = OUString::createFromAscii(serviceName);
153 return aRet;
156 } } } }
158 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
159 com_sun_star_i18n_InputSequenceChecker_get_implementation(
160 css::uno::XComponentContext *context,
161 css::uno::Sequence<css::uno::Any> const &)
163 return cppu::acquire(new css::i18n::InputSequenceCheckerImpl(context));
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */