Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
25 :
26 : using namespace ::com::sun::star::uno;
27 : using namespace ::com::sun::star::lang;
28 :
29 : namespace com { namespace sun { namespace star { namespace i18n {
30 :
31 2 : InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
32 : {
33 2 : serviceName = "com.sun.star.i18n.InputSequenceChecker";
34 2 : cachedItem = NULL;
35 2 : }
36 :
37 0 : InputSequenceCheckerImpl::InputSequenceCheckerImpl(const char *pServiceName)
38 : : serviceName(pServiceName)
39 0 : , cachedItem(NULL)
40 : {
41 0 : }
42 :
43 6 : InputSequenceCheckerImpl::~InputSequenceCheckerImpl()
44 : {
45 : // Clear lookuptable
46 2 : for (size_t l = 0; l < lookupTable.size(); l++)
47 0 : delete lookupTable[l];
48 :
49 2 : lookupTable.clear();
50 4 : }
51 :
52 : sal_Bool SAL_CALL
53 0 : InputSequenceCheckerImpl::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
54 : sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException, std::exception)
55 : {
56 0 : if (inputCheckMode == InputSequenceCheckMode::PASSTHROUGH)
57 0 : return sal_True;
58 :
59 0 : sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
60 :
61 0 : if (language)
62 0 : return getInputSequenceChecker(language)->checkInputSequence(Text, nStartPos, inputChar, inputCheckMode);
63 : else
64 0 : return sal_True; // not a checkable languages.
65 : }
66 :
67 : sal_Int32 SAL_CALL
68 0 : InputSequenceCheckerImpl::correctInputSequence(OUString& Text, sal_Int32 nStartPos,
69 : sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(RuntimeException, std::exception)
70 : {
71 0 : if (inputCheckMode != InputSequenceCheckMode::PASSTHROUGH) {
72 0 : sal_Char* language = getLanguageByScripType(Text[nStartPos], inputChar);
73 :
74 0 : if (language)
75 0 : return getInputSequenceChecker(language)->correctInputSequence(Text, nStartPos, inputChar, inputCheckMode);
76 : }
77 0 : Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
78 0 : return nStartPos;
79 : }
80 :
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,
86 :
87 : { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, UnicodeScript_kScriptCount } // 88
88 : };
89 :
90 : sal_Char* SAL_CALL
91 0 : InputSequenceCheckerImpl::getLanguageByScripType(sal_Unicode cChar, sal_Unicode nChar)
92 : {
93 0 : sal_Int16 type = unicode::getUnicodeScriptType( cChar, typeList, UnicodeScript_kScriptCount );
94 :
95 0 : if (type != UnicodeScript_kScriptCount &&
96 0 : type == unicode::getUnicodeScriptType( nChar, typeList, UnicodeScript_kScriptCount )) {
97 0 : switch(type) {
98 0 : case UnicodeScript_kThai: return const_cast<sal_Char*>("th");
99 : //case UnicodeScript_kArabic: return (sal_Char*)"ar";
100 : //case UnicodeScript_kHebrew: return (sal_Char*)"he";
101 0 : case UnicodeScript_kDevanagari: return const_cast<sal_Char*>("hi");
102 : }
103 : }
104 0 : return NULL;
105 : }
106 :
107 : Reference< XExtendedInputSequenceChecker >& SAL_CALL
108 0 : InputSequenceCheckerImpl::getInputSequenceChecker(sal_Char* rLanguage) throw (RuntimeException)
109 : {
110 0 : if (cachedItem && cachedItem->aLanguage == rLanguage) {
111 0 : return cachedItem->xISC;
112 : }
113 : else {
114 0 : for (size_t l = 0; l < lookupTable.size(); l++) {
115 0 : cachedItem = lookupTable[l];
116 0 : if (cachedItem->aLanguage == rLanguage)
117 0 : return cachedItem->xISC;
118 : }
119 :
120 0 : Reference < uno::XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
121 0 : "com.sun.star.i18n.InputSequenceChecker_" +
122 0 : OUString::createFromAscii(rLanguage),
123 0 : m_xContext);
124 :
125 0 : if ( xI.is() ) {
126 0 : Reference< XExtendedInputSequenceChecker > xISC( xI, uno::UNO_QUERY );
127 0 : if (xISC.is()) {
128 0 : lookupTable.push_back(cachedItem = new lookupTableItem(rLanguage, xISC));
129 0 : return cachedItem->xISC;
130 0 : }
131 0 : }
132 : }
133 0 : throw RuntimeException();
134 : }
135 :
136 : OUString SAL_CALL
137 1 : InputSequenceCheckerImpl::getImplementationName() throw( RuntimeException, std::exception )
138 : {
139 1 : return OUString::createFromAscii(serviceName);
140 : }
141 :
142 : sal_Bool SAL_CALL
143 0 : InputSequenceCheckerImpl::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
144 : {
145 0 : return cppu::supportsService(this, rServiceName);
146 : }
147 :
148 : Sequence< OUString > SAL_CALL
149 1 : InputSequenceCheckerImpl::getSupportedServiceNames() throw( RuntimeException, std::exception )
150 : {
151 1 : Sequence< OUString > aRet(1);
152 1 : aRet[0] = OUString::createFromAscii(serviceName);
153 1 : return aRet;
154 : }
155 :
156 : } } } }
157 :
158 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
159 2 : com_sun_star_i18n_InputSequenceChecker_get_implementation(
160 : css::uno::XComponentContext *context,
161 : css::uno::Sequence<css::uno::Any> const &)
162 : {
163 2 : return cppu::acquire(new css::i18n::InputSequenceCheckerImpl(context));
164 : }
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|