xref: /trunk/main/linguistic/source/hhconvdic.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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)
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 {
116     MutexGuard  aGuard( GetLinguMutex() );
117 
118     if ((aLeftText.getLength() != aRightText.getLength()) ||
119         !TextIsAllScriptType( aLeftText,  SCRIPT_HANGUL ) ||
120         !TextIsAllScriptType( aRightText, SCRIPT_HANJA ))
121         throw IllegalArgumentException();
122     ConvDic::addEntry( aLeftText, aRightText );
123 }
124 
125 
getImplementationName()126 OUString SAL_CALL HHConvDic::getImplementationName(  )
127 {
128     MutexGuard  aGuard( GetLinguMutex() );
129     return getImplementationName_Static();
130 }
131 
132 
supportsService(const OUString & rServiceName)133 sal_Bool SAL_CALL HHConvDic::supportsService( const OUString& rServiceName )
134 {
135     MutexGuard  aGuard( GetLinguMutex() );
136     sal_Bool bRes = sal_False;
137     if (rServiceName.equalsAscii( SN_CONV_DICTIONARY )||
138         rServiceName.equalsAscii( SN_HH_CONV_DICTIONARY ))
139         bRes = sal_True;
140     return bRes;
141 }
142 
143 
getSupportedServiceNames()144 uno::Sequence< OUString > SAL_CALL HHConvDic::getSupportedServiceNames(  )
145 {
146     MutexGuard  aGuard( GetLinguMutex() );
147     return getSupportedServiceNames_Static();
148 }
149 
150 
getSupportedServiceNames_Static()151 uno::Sequence< OUString > HHConvDic::getSupportedServiceNames_Static()
152     throw()
153 {
154     uno::Sequence< OUString > aSNS( 2 );
155     aSNS.getArray()[0] = A2OU( SN_CONV_DICTIONARY );
156     aSNS.getArray()[1] = A2OU( SN_HH_CONV_DICTIONARY );
157     return aSNS;
158 }
159 
160 ///////////////////////////////////////////////////////////////////////////
161