1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <unicode/uscript.h>
21 #include <i18nlangtag/lang.h>
22 #include <osl/mutex.hxx>
24 #include <cppuhelper/factory.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include "hhconvdic.hxx"
30 #include <linguistic/misc.hxx>
33 using namespace com::sun::star
;
34 using namespace com::sun::star::lang
;
35 using namespace com::sun::star::uno
;
36 using namespace com::sun::star::linguistic2
;
37 using namespace linguistic
;
40 #define SN_HH_CONV_DICTIONARY "com.sun.star.linguistic2.HangulHanjaConversionDictionary"
43 #include <com/sun/star/i18n/UnicodeScript.hpp>
47 #define SCRIPT_OTHERS 0
48 #define SCRIPT_HANJA 1
49 #define SCRIPT_HANGUL 2
51 // from i18npool/source/textconversion/textconversion_ko.cxx
52 /// @throws RuntimeException
53 static sal_Int16
checkScriptType(sal_Unicode c
)
55 UErrorCode status
= U_ZERO_ERROR
;
57 UScriptCode scriptCode
= uscript_getScript(c
, &status
);
59 if ( !U_SUCCESS(status
) ) throw RuntimeException();
61 return scriptCode
== USCRIPT_HANGUL
? SCRIPT_HANGUL
:
62 scriptCode
== USCRIPT_HAN
? SCRIPT_HANJA
: SCRIPT_OTHERS
;
66 static bool TextIsAllScriptType( const OUString
&rTxt
, sal_Int16 nScriptType
)
69 for (sal_Int32 i
= 0; i
< rTxt
.getLength() && bIsAll
; ++i
)
71 if (checkScriptType( rTxt
[i
]) != nScriptType
)
78 HHConvDic::HHConvDic( const OUString
&rName
, const OUString
&rMainURL
) :
79 ConvDic( rName
, LANGUAGE_KOREAN
, ConversionDictionaryType::HANGUL_HANJA
, true, rMainURL
)
84 HHConvDic::~HHConvDic()
89 void SAL_CALL
HHConvDic::addEntry(
90 const OUString
& aLeftText
,
91 const OUString
& aRightText
)
93 MutexGuard
aGuard( GetLinguMutex() );
95 if ((aLeftText
.getLength() != aRightText
.getLength()) ||
96 !TextIsAllScriptType( aLeftText
, SCRIPT_HANGUL
) ||
97 !TextIsAllScriptType( aRightText
, SCRIPT_HANJA
))
98 throw IllegalArgumentException();
99 ConvDic::addEntry( aLeftText
, aRightText
);
103 OUString SAL_CALL
HHConvDic::getImplementationName( )
105 return "com.sun.star.lingu2.HHConvDic";
109 sal_Bool SAL_CALL
HHConvDic::supportsService( const OUString
& rServiceName
)
111 return cppu::supportsService(this, rServiceName
);
115 uno::Sequence
< OUString
> SAL_CALL
HHConvDic::getSupportedServiceNames( )
117 return { SN_CONV_DICTIONARY
, SN_HH_CONV_DICTIONARY
};
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */