xref: /aoo42x/main/linguistic/source/hhconvdic.cxx (revision 3b8558fd)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_linguistic.hxx"
26 #include <unicode/uscript.h>
27 #include <i18npool/lang.h>
28 #include <tools/urlobj.hxx>
29 #include <tools/debug.hxx>
30 #include <tools/fsys.hxx>
31 #include <tools/stream.hxx>
32 #include <tools/string.hxx>
33 #include <osl/mutex.hxx>
34 #include <unotools/processfactory.hxx>
35 #include <ucbhelper/content.hxx>
36 
37 #include <cppuhelper/factory.hxx>   // helper for factories
38 #include <com/sun/star/linguistic2/XConversionDictionary.hpp>
39 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
40 #include <com/sun/star/lang/Locale.hpp>
41 #ifndef _COM_SUN_STAR_UNO_REFERENCE_HPP_
42 #include <com/sun/star/uno/Reference.h>
43 #endif
44 #include <com/sun/star/registry/XRegistryKey.hpp>
45 
46 #include "hhconvdic.hxx"
47 #include "linguistic/misc.hxx"
48 #include "defs.hxx"
49 
50 using namespace utl;
51 using namespace osl;
52 using namespace rtl;
53 using namespace com::sun::star;
54 using namespace com::sun::star::lang;
55 using namespace com::sun::star::uno;
56 using namespace com::sun::star::linguistic2;
57 using namespace linguistic;
58 
59 #define SN_HH_CONV_DICTIONARY   "com.sun.star.linguistic2.HangulHanjaConversionDictionary"
60 
61 ///////////////////////////////////////////////////////////////////////////
62 
63 #include <i18nutil/unicode.hxx>
64 #include <com/sun/star/i18n/UnicodeScript.hpp>
65 
66 using namespace i18n;
67 
68 #define SCRIPT_OTHERS   0
69 #define SCRIPT_HANJA    1
70 #define SCRIPT_HANGUL   2
71 
72 // from i18npool/source/textconversion/textconversion_ko.cxx
checkScriptType(sal_Unicode c)73 sal_Int16 SAL_CALL checkScriptType(sal_Unicode c) throw (RuntimeException)
74 {
75   UErrorCode status = U_ZERO_ERROR;
76 
77   UScriptCode scriptCode = uscript_getScript(c, &status);
78 
79   if ( !U_SUCCESS(status) ) throw RuntimeException();
80 
81   return scriptCode == USCRIPT_HANGUL ? SCRIPT_HANGUL :
82             scriptCode == USCRIPT_HAN ? SCRIPT_HANJA : SCRIPT_OTHERS;
83 }
84 
85 
86 
TextIsAllScriptType(const OUString & rTxt,sal_Int16 nScriptType)87 sal_Bool TextIsAllScriptType( const OUString &rTxt, sal_Int16 nScriptType )
88 {
89     sal_Bool bIsAll = sal_True;
90     for (sal_Int32 i = 0;  i < rTxt.getLength() && bIsAll;  ++i)
91     {
92         if (checkScriptType( rTxt.getStr()[i]) != nScriptType)
93             bIsAll = sal_False;
94     }
95     return bIsAll;
96 }
97 
98 
99 ///////////////////////////////////////////////////////////////////////////
100 
HHConvDic(const String & rName,const String & rMainURL)101 HHConvDic::HHConvDic( const String &rName, const String &rMainURL ) :
102     ConvDic( rName, LANGUAGE_KOREAN, ConversionDictionaryType::HANGUL_HANJA, sal_True, rMainURL )
103 {
104 }
105 
106 
~HHConvDic()107 HHConvDic::~HHConvDic()
108 {
109 }
110 
111 
addEntry(const OUString & aLeftText,const OUString & aRightText)112 void SAL_CALL HHConvDic::addEntry(
113         const OUString& aLeftText,
114         const OUString& aRightText )
115     throw (IllegalArgumentException, container::ElementExistException, RuntimeException)
116 {
117     MutexGuard  aGuard( GetLinguMutex() );
118 
119     if ((aLeftText.getLength() != aRightText.getLength()) ||
120         !TextIsAllScriptType( aLeftText,  SCRIPT_HANGUL ) ||
121         !TextIsAllScriptType( aRightText, SCRIPT_HANJA ))
122         throw IllegalArgumentException();
123     ConvDic::addEntry( aLeftText, aRightText );
124 }
125 
126 
getImplementationName()127 OUString SAL_CALL HHConvDic::getImplementationName(  )
128     throw (RuntimeException)
129 {
130     MutexGuard  aGuard( GetLinguMutex() );
131     return getImplementationName_Static();
132 }
133 
134 
supportsService(const OUString & rServiceName)135 sal_Bool SAL_CALL HHConvDic::supportsService( const OUString& rServiceName )
136     throw (RuntimeException)
137 {
138     MutexGuard  aGuard( GetLinguMutex() );
139     sal_Bool bRes = sal_False;
140     if (rServiceName.equalsAscii( SN_CONV_DICTIONARY )||
141         rServiceName.equalsAscii( SN_HH_CONV_DICTIONARY ))
142         bRes = sal_True;
143     return bRes;
144 }
145 
146 
getSupportedServiceNames()147 uno::Sequence< OUString > SAL_CALL HHConvDic::getSupportedServiceNames(  )
148     throw (RuntimeException)
149 {
150     MutexGuard  aGuard( GetLinguMutex() );
151     return getSupportedServiceNames_Static();
152 }
153 
154 
getSupportedServiceNames_Static()155 uno::Sequence< OUString > HHConvDic::getSupportedServiceNames_Static()
156     throw()
157 {
158     uno::Sequence< OUString > aSNS( 2 );
159     aSNS.getArray()[0] = A2OU( SN_CONV_DICTIONARY );
160     aSNS.getArray()[1] = A2OU( SN_HH_CONV_DICTIONARY );
161     return aSNS;
162 }
163 
164 ///////////////////////////////////////////////////////////////////////////
165 
166