1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
31 #include <inputsequencechecker.hxx>
32 #include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
33 #include <com/sun/star/i18n/UnicodeType.hpp>
34 #include <i18nutil/unicode.hxx>
35 #include <rtl/ustrbuf.hxx>
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::rtl
;
41 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
43 InputSequenceCheckerImpl::InputSequenceCheckerImpl( const Reference
< XMultiServiceFactory
>& rxMSF
) : xMSF( rxMSF
)
45 serviceName
= "com.sun.star.i18n.InputSequenceCheckerImpl";
49 InputSequenceCheckerImpl::InputSequenceCheckerImpl()
53 InputSequenceCheckerImpl::~InputSequenceCheckerImpl()
56 for (size_t l
= 0; l
< lookupTable
.size(); l
++)
57 delete lookupTable
[l
];
63 InputSequenceCheckerImpl::checkInputSequence(const OUString
& Text
, sal_Int32 nStartPos
,
64 sal_Unicode inputChar
, sal_Int16 inputCheckMode
) throw(RuntimeException
)
66 if (inputCheckMode
== InputSequenceCheckMode::PASSTHROUGH
)
69 sal_Char
* language
= getLanguageByScripType(Text
[nStartPos
], inputChar
);
72 return getInputSequenceChecker(language
)->checkInputSequence(Text
, nStartPos
, inputChar
, inputCheckMode
);
74 return sal_True
; // not a checkable languages.
78 InputSequenceCheckerImpl::correctInputSequence(OUString
& Text
, sal_Int32 nStartPos
,
79 sal_Unicode inputChar
, sal_Int16 inputCheckMode
) throw(RuntimeException
)
81 if (inputCheckMode
!= InputSequenceCheckMode::PASSTHROUGH
) {
82 sal_Char
* language
= getLanguageByScripType(Text
[nStartPos
], inputChar
);
85 return getInputSequenceChecker(language
)->correctInputSequence(Text
, nStartPos
, inputChar
, inputCheckMode
);
87 Text
= Text
.replaceAt(++nStartPos
, 0, OUString(inputChar
));
91 static ScriptTypeList typeList
[] = {
92 //{ UnicodeScript_kHebrew, UnicodeScript_kHebrew }, // 10,
93 //{ UnicodeScript_kArabic, UnicodeScript_kArabic }, // 11,
94 { UnicodeScript_kDevanagari
,UnicodeScript_kDevanagari
, UnicodeScript_kDevanagari
}, // 14,
95 { UnicodeScript_kThai
, UnicodeScript_kThai
, UnicodeScript_kThai
}, // 24,
97 { UnicodeScript_kScriptCount
, UnicodeScript_kScriptCount
, UnicodeScript_kScriptCount
} // 88
101 InputSequenceCheckerImpl::getLanguageByScripType(sal_Unicode cChar
, sal_Unicode nChar
)
103 sal_Int16 type
= unicode::getUnicodeScriptType( cChar
, typeList
, UnicodeScript_kScriptCount
);
105 if (type
!= UnicodeScript_kScriptCount
&&
106 type
== unicode::getUnicodeScriptType( nChar
, typeList
, UnicodeScript_kScriptCount
)) {
108 case UnicodeScript_kThai
: return (sal_Char
*)"th";
109 //case UnicodeScript_kArabic: return (sal_Char*)"ar";
110 //case UnicodeScript_kHebrew: return (sal_Char*)"he";
111 case UnicodeScript_kDevanagari
: return (sal_Char
*)"hi";
117 Reference
< XExtendedInputSequenceChecker
>& SAL_CALL
118 InputSequenceCheckerImpl::getInputSequenceChecker(sal_Char
* rLanguage
) throw (RuntimeException
)
120 if (cachedItem
&& cachedItem
->aLanguage
== rLanguage
) {
121 return cachedItem
->xISC
;
123 else if (xMSF
.is()) {
124 for (size_t l
= 0; l
< lookupTable
.size(); l
++) {
125 cachedItem
= lookupTable
[l
];
126 if (cachedItem
->aLanguage
== rLanguage
)
127 return cachedItem
->xISC
;
130 Reference
< uno::XInterface
> xI
= xMSF
->createInstance(
131 OUString::createFromAscii("com.sun.star.i18n.InputSequenceChecker_") +
132 OUString::createFromAscii(rLanguage
));
135 Reference
< XExtendedInputSequenceChecker
> xISC
;
136 xI
->queryInterface( getCppuType((const Reference
< XExtendedInputSequenceChecker
>*)0) ) >>= xISC
;
138 lookupTable
.push_back(cachedItem
= new lookupTableItem(rLanguage
, xISC
));
139 return cachedItem
->xISC
;
143 throw RuntimeException();
147 InputSequenceCheckerImpl::getImplementationName(void) throw( RuntimeException
)
149 return OUString::createFromAscii(serviceName
);
153 InputSequenceCheckerImpl::supportsService(const OUString
& rServiceName
) throw( RuntimeException
)
155 return !rServiceName
.compareToAscii(serviceName
);
158 Sequence
< OUString
> SAL_CALL
159 InputSequenceCheckerImpl::getSupportedServiceNames(void) throw( RuntimeException
)
161 Sequence
< OUString
> aRet(1);
162 aRet
[0] = OUString::createFromAscii(serviceName
);