Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / linguistic / source / hhconvdic.cxx
blob2e8969b0dff6c2c490e1e5d24ab944d96379fe1e
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 <unicode/uscript.h>
21 #include <i18nlangtag/lang.h>
22 #include <tools/stream.hxx>
23 #include <osl/mutex.hxx>
24 #include <ucbhelper/content.hxx>
26 #include <cppuhelper/factory.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <com/sun/star/registry/XRegistryKey.hpp>
32 #include "hhconvdic.hxx"
33 #include <linguistic/misc.hxx>
34 #include "defs.hxx"
36 using namespace utl;
37 using namespace osl;
38 using namespace com::sun::star;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::linguistic2;
42 using namespace linguistic;
45 #define SN_HH_CONV_DICTIONARY "com.sun.star.linguistic2.HangulHanjaConversionDictionary"
48 #include <i18nutil/unicode.hxx>
49 #include <com/sun/star/i18n/UnicodeScript.hpp>
51 using namespace i18n;
53 #define SCRIPT_OTHERS 0
54 #define SCRIPT_HANJA 1
55 #define SCRIPT_HANGUL 2
57 // from i18npool/source/textconversion/textconversion_ko.cxx
58 /// @throws RuntimeException
59 sal_Int16 checkScriptType(sal_Unicode c)
61 UErrorCode status = U_ZERO_ERROR;
63 UScriptCode scriptCode = uscript_getScript(c, &status);
65 if ( !U_SUCCESS(status) ) throw RuntimeException();
67 return scriptCode == USCRIPT_HANGUL ? SCRIPT_HANGUL :
68 scriptCode == USCRIPT_HAN ? SCRIPT_HANJA : SCRIPT_OTHERS;
72 bool TextIsAllScriptType( const OUString &rTxt, sal_Int16 nScriptType )
74 bool bIsAll = true;
75 for (sal_Int32 i = 0; i < rTxt.getLength() && bIsAll; ++i)
77 if (checkScriptType( rTxt[i]) != nScriptType)
78 bIsAll = false;
80 return bIsAll;
84 HHConvDic::HHConvDic( const OUString &rName, const OUString &rMainURL ) :
85 ConvDic( rName, LANGUAGE_KOREAN, ConversionDictionaryType::HANGUL_HANJA, true, rMainURL )
90 HHConvDic::~HHConvDic()
95 void SAL_CALL HHConvDic::addEntry(
96 const OUString& aLeftText,
97 const OUString& aRightText )
99 MutexGuard aGuard( GetLinguMutex() );
101 if ((aLeftText.getLength() != aRightText.getLength()) ||
102 !TextIsAllScriptType( aLeftText, SCRIPT_HANGUL ) ||
103 !TextIsAllScriptType( aRightText, SCRIPT_HANJA ))
104 throw IllegalArgumentException();
105 ConvDic::addEntry( aLeftText, aRightText );
109 OUString SAL_CALL HHConvDic::getImplementationName( )
111 return OUString( "com.sun.star.lingu2.HHConvDic" );
115 sal_Bool SAL_CALL HHConvDic::supportsService( const OUString& rServiceName )
117 return cppu::supportsService(this, rServiceName);
121 uno::Sequence< OUString > SAL_CALL HHConvDic::getSupportedServiceNames( )
123 return { SN_CONV_DICTIONARY, SN_HH_CONV_DICTIONARY };
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */