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 #include "unotools/unotoolsdllapi.h"
24 
25 #ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
26 #define _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
27 #include <tools/string.hxx>
28 #include <tools/solar.h>
29 #include <com/sun/star/i18n/XExtendedTransliteration.hpp>
30 
31 namespace com { namespace sun { namespace star {
32 	namespace lang {
33 		class XMultiServiceFactory;
34 	}
35 }}}
36 
37 namespace utl
38 {
39 
40 class UNOTOOLS_DLLPUBLIC TransliterationWrapper
41 {
42 	::com::sun::star::uno::Reference<
43 					::com::sun::star::lang::XMultiServiceFactory > xSMgr;
44 	::com::sun::star::uno::Reference<
45         ::com::sun::star::i18n::XExtendedTransliteration > xTrans;
46 	::com::sun::star::lang::Locale aLocale;
47 	sal_uInt32 nType;
48 	sal_uInt16 nLanguage;
49     mutable sal_Bool bFirstCall;
50 
51 								// not implemented, prevent usage
52 	TransliterationWrapper( const TransliterationWrapper& );
53 	TransliterationWrapper&	operator=( const TransliterationWrapper& );
54 
55     void loadModuleImpl() const;
56     void setLanguageLocaleImpl( sal_uInt16 nLang );
57 
58 public:
59 	TransliterationWrapper( const ::com::sun::star::uno::Reference<
60 					::com::sun::star::lang::XMultiServiceFactory > & xSF,
61 					sal_uInt32 nType );
62 
63 	~TransliterationWrapper();
64 
65 	// get current Locale / Language
getLocale() const66 	const ::com::sun::star::lang::Locale& getLocale() const	{ return aLocale;}
getLanguage() const67 	sal_uInt16 getLanguage() const { return nLanguage; }
68 
getType() const69 	sal_uInt32 getType() const { return nType; }
70 
71 	sal_Bool needLanguageForTheMode() const;
72 
73     /** set a new language and load the corresponding transliteration module if
74         needed for the mode set with nType in the ctor */
75     void loadModuleIfNeeded( sal_uInt16 nLang );
76 
77     /** Load the transliteration module specified by rModuleName, which has to
78         be the UNO service implementation name that is expanded to the full UNO
79         service implementation name, for example, "NumToCharKanjiShort_ja_JP"
80         expands to
81         "com.sun.star.i18n.Transliteration.NumToCharKanjiShort_ja_JP".
82         @ATTENTION!
83         This method ignores the mode type set with the constructor and
84         interferes with the loadModuleIfNeeded() method and the transliterate()
85         method that gets a LanguageType passed as parameter.  Using one of
86         those may load a different module and overwrite this setting. Only the
87         transliterate() method that takes no LanguageType parameter may be used
88         for a specific module loaded with this method.  */
89     void loadModuleByImplName( const String& rModuleName, sal_uInt16 nLang );
90 
91     /** This transliteration method corresponds with the loadModuleByImplName()
92         method. It relies on a module being loaded and does not try load one.
93         If for any reason the string can't be transliterated the original
94         string is returned.  */
95 	String transliterate( const String& rStr,
96 						xub_StrLen nStart, xub_StrLen nLen,
97 						::com::sun::star::uno::Sequence <sal_Int32>* pOffset ) const;
98 
99 	// Wrapper implementations of class Transliteration
100 	String transliterate( const String& rStr, sal_uInt16 nLanguage,
101 						xub_StrLen nStart, xub_StrLen nLen,
102 						::com::sun::star::uno::Sequence <sal_Int32>* pOffset );
103 
104     /** If two strings are equal per this transliteration.
105         Returns the number of matched code points in any case, even if strings
106         are not equal, for example:
107         equals( "a", 0, 1, nMatch1, "aaa", 0, 3, nMatch2 )
108         returns false and nMatch:=1 and nMatch2:=1
109         equals( "aab", 0, 3, nMatch1, "aaa", 0, 3, nMatch2 )
110         returns false and nMatch:=2 and nMatch2:=2
111      */
112     sal_Bool equals(
113         const String& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
114         const String& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const;
115 
116     sal_Int32 compareSubstring(
117         const String& rStr1, sal_Int32 nOff1, sal_Int32 nLen1,
118         const String& rStr2, sal_Int32 nOff2, sal_Int32 nLen2 ) const;
119 
120     sal_Int32 compareString( const String& rStr1, const String& rStr2 ) const;
121 
122 
123     // helpers
124 
125     /** If two strings are really equal as per this translation, and not just
126         one string is matching the start of the other. Use this method instead
127         of compareString()==0 because it is much faster.
128      */
129     sal_Bool isEqual( const String& rStr1, const String& rStr2 ) const;
130 
131     /** If string rStr1 matches the start of string rStr2, i.e. "a" in "aaa"
132      */
133     sal_Bool isMatch( const String& rStr1, const String& rStr2 ) const;
134 
135 };
136 
137 // ............................................................................
138 }       // namespace utl
139 // ............................................................................
140 
141 #endif
142