1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "unotools/unotoolsdllapi.h"
28 
29 #ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
30 #define _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
31 #include <tools/string.hxx>
32 #include <tools/solar.h>
33 #include <com/sun/star/i18n/XExtendedTransliteration.hpp>
34 
35 namespace com { namespace sun { namespace star {
36 	namespace lang {
37 		class XMultiServiceFactory;
38 	}
39 }}}
40 
41 namespace utl
42 {
43 
44 class UNOTOOLS_DLLPUBLIC TransliterationWrapper
45 {
46 	::com::sun::star::uno::Reference<
47 					::com::sun::star::lang::XMultiServiceFactory > xSMgr;
48 	::com::sun::star::uno::Reference<
49         ::com::sun::star::i18n::XExtendedTransliteration > xTrans;
50 	::com::sun::star::lang::Locale aLocale;
51 	sal_uInt32 nType;
52 	sal_uInt16 nLanguage;
53     mutable sal_Bool bFirstCall;
54 
55 								// not implemented, prevent usage
56 	TransliterationWrapper( const TransliterationWrapper& );
57 	TransliterationWrapper&	operator=( const TransliterationWrapper& );
58 
59     void loadModuleImpl() const;
60     void setLanguageLocaleImpl( sal_uInt16 nLang );
61 
62 public:
63 	TransliterationWrapper( const ::com::sun::star::uno::Reference<
64 					::com::sun::star::lang::XMultiServiceFactory > & xSF,
65 					sal_uInt32 nType );
66 
67 	~TransliterationWrapper();
68 
69 	// get current Locale / Language
70 	const ::com::sun::star::lang::Locale& getLocale() const	{ return aLocale;}
71 	sal_uInt16 getLanguage() const { return nLanguage; }
72 
73 	sal_uInt32 getType() const { return nType; }
74 
75 	sal_Bool needLanguageForTheMode() const;
76 
77     /** set a new language and load the corresponding transliteration module if
78         needed for the mode set with nType in the ctor */
79     void loadModuleIfNeeded( sal_uInt16 nLang );
80 
81     /** Load the transliteration module specified by rModuleName, which has to
82         be the UNO service implementation name that is expanded to the full UNO
83         service implementation name, for example, "NumToCharKanjiShort_ja_JP"
84         expands to
85         "com.sun.star.i18n.Transliteration.NumToCharKanjiShort_ja_JP".
86         @ATTENTION!
87         This method ignores the mode type set with the constructor and
88         interferes with the loadModuleIfNeeded() method and the transliterate()
89         method that gets a LanguageType passed as parameter.  Using one of
90         those may load a different module and overwrite this setting. Only the
91         transliterate() method that takes no LanguageType parameter may be used
92         for a specific module loaded with this method.  */
93     void loadModuleByImplName( const String& rModuleName, sal_uInt16 nLang );
94 
95     /** This transliteration method corresponds with the loadModuleByImplName()
96         method. It relies on a module being loaded and does not try load one.
97         If for any reason the string can't be transliterated the original
98         string is returned.  */
99 	String transliterate( const String& rStr,
100 						xub_StrLen nStart, xub_StrLen nLen,
101 						::com::sun::star::uno::Sequence <sal_Int32>* pOffset ) const;
102 
103 	// Wrapper implementations of class Transliteration
104 	String transliterate( const String& rStr, sal_uInt16 nLanguage,
105 						xub_StrLen nStart, xub_StrLen nLen,
106 						::com::sun::star::uno::Sequence <sal_Int32>* pOffset );
107 
108     /** If two strings are equal per this transliteration.
109         Returns the number of matched code points in any case, even if strings
110         are not equal, for example:
111         equals( "a", 0, 1, nMatch1, "aaa", 0, 3, nMatch2 )
112         returns false and nMatch:=1 and nMatch2:=1
113         equals( "aab", 0, 3, nMatch1, "aaa", 0, 3, nMatch2 )
114         returns false and nMatch:=2 and nMatch2:=2
115      */
116     sal_Bool equals(
117         const String& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
118         const String& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const;
119 
120     sal_Int32 compareSubstring(
121         const String& rStr1, sal_Int32 nOff1, sal_Int32 nLen1,
122         const String& rStr2, sal_Int32 nOff2, sal_Int32 nLen2 ) const;
123 
124     sal_Int32 compareString( const String& rStr1, const String& rStr2 ) const;
125 
126 
127     // helpers
128 
129     /** If two strings are really equal as per this translation, and not just
130         one string is matching the start of the other. Use this method instead
131         of compareString()==0 because it is much faster.
132      */
133     sal_Bool isEqual( const String& rStr1, const String& rStr2 ) const;
134 
135     /** If string rStr1 matches the start of string rStr2, i.e. "a" in "aaa"
136      */
137     sal_Bool isMatch( const String& rStr1, const String& rStr2 ) const;
138 
139 };
140 
141 // ............................................................................
142 }       // namespace utl
143 // ............................................................................
144 
145 #endif
146