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 #ifndef INCLUDED_I18NPOOL_TEXTSEARCH_HXX
25 #define INCLUDED_I18NPOOL_TEXTSEARCH_HXX
26 
27 
28 #include <com/sun/star/util/XTextSearch.hpp>
29 #include <com/sun/star/i18n/XBreakIterator.hpp>
30 #include <cppuhelper/implbase2.hxx>		// helper for implementations
31 #include <com/sun/star/i18n/XExtendedTransliteration.hpp>
32 #include <com/sun/star/i18n/XCharacterClassification.hpp>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 
35 #include <map>
36 
37 #include <unicode/regex.h>
38 using namespace U_ICU_NAMESPACE;
39 typedef U_ICU_NAMESPACE::UnicodeString IcuUniString;
40 
41 class WLevDistance;
42 typedef ::std::map< sal_Unicode, sal_Int32 > TextSearchJumpTable;
43 
44 //	----------------------------------------------------
45 //	class TextSearch
46 //	----------------------------------------------------
47 class TextSearch: public cppu::WeakImplHelper2
48 <
49 	::com::sun::star::util::XTextSearch,
50 	::com::sun::star::lang::XServiceInfo
51 >
52 {
53 	::com::sun::star::uno::Reference < ::com::sun::star::lang::XMultiServiceFactory > xMSF;
54 
55 	::com::sun::star::util::SearchOptions aSrchPara;
56 	::rtl::OUString sSrchStr;
57 	::rtl::OUString sSrchStr2;
58 
59     mutable com::sun::star::uno::Reference<
60         com::sun::star::i18n::XCharacterClassification > xCharClass;
61 
62     com::sun::star::uno::Reference<
63         com::sun::star::i18n::XExtendedTransliteration > xTranslit;
64     com::sun::star::uno::Reference<
65         com::sun::star::i18n::XExtendedTransliteration > xTranslit2;
66 
67 	// define a function pointer for the different search nethods
68     typedef ::com::sun::star::util::SearchResult
69 		(SAL_CALL TextSearch:: *FnSrch)( const ::rtl::OUString& searchStr,
70 								sal_Int32 startPos, sal_Int32 endPos );
71 
72 	FnSrch fnForward;
73 	FnSrch fnBackward;
74 
75 	// Members and methods for the normal (Boyer-Moore) search
76 	TextSearchJumpTable* pJumpTable;
77 	TextSearchJumpTable* pJumpTable2;
78 	bool bIsForwardTab;
79 	bool bUsePrimarySrchStr;
80 	void MakeForwardTab();
81 	void MakeForwardTab2();
82 	void MakeBackwardTab();
83 	void MakeBackwardTab2();
84 	sal_Int32 GetDiff( const sal_Unicode ) const;
85 	::com::sun::star::util::SearchResult SAL_CALL
86 		NSrchFrwrd( const ::rtl::OUString& searchStr,
87 								sal_Int32 startPos, sal_Int32 endPos )
88 							throw(::com::sun::star::uno::RuntimeException);
89 	::com::sun::star::util::SearchResult SAL_CALL
90 		NSrchBkwrd( const ::rtl::OUString& searchStr,
91 								sal_Int32 startPos, sal_Int32 endPos )
92 							throw(::com::sun::star::uno::RuntimeException);
93 
94 	// Members and methods for the regular expression search
95 	RegexMatcher* pRegexMatcher;
96 	::com::sun::star::util::SearchResult SAL_CALL
97 		RESrchFrwrd( const ::rtl::OUString& searchStr,
98 								sal_Int32 startPos, sal_Int32 endPos )
99 							throw(::com::sun::star::uno::RuntimeException);
100 	::com::sun::star::util::SearchResult SAL_CALL
101 		RESrchBkwrd( const ::rtl::OUString& searchStr,
102 								sal_Int32 startPos, sal_Int32 endPos )
103 							throw(::com::sun::star::uno::RuntimeException);
104 
105 	// Members and methods for the "Weight Levenshtein-Distance" search
106 	int nLimit;
107 	WLevDistance* pWLD;
108 	com::sun::star::uno::Reference < com::sun::star::i18n::XBreakIterator > xBreak;
109 	::com::sun::star::util::SearchResult SAL_CALL
110 		ApproxSrchFrwrd( const ::rtl::OUString& searchStr,
111 								sal_Int32 startPos, sal_Int32 endPos )
112 							throw(::com::sun::star::uno::RuntimeException);
113 	::com::sun::star::util::SearchResult SAL_CALL
114 		ApproxSrchBkwrd( const ::rtl::OUString& searchStr,
115 								sal_Int32 startPos, sal_Int32 endPos )
116 							throw(::com::sun::star::uno::RuntimeException);
117 
118 	bool IsDelimiter( const ::rtl::OUString& rStr, sal_Int32 nPos ) const;
119 
120 	sal_Bool checkCTLStart, checkCTLEnd;
121 	sal_Bool SAL_CALL isCellStart(const ::rtl::OUString& searchStr, sal_Int32 nPos)
122 							throw(::com::sun::star::uno::RuntimeException);
123 
124 public:
125 	TextSearch(
126 		const ::com::sun::star::uno::Reference < ::com::sun::star::lang::XMultiServiceFactory >& rxMSF );
127 
128 	virtual ~TextSearch();
129 
130     // Methods
131     virtual void SAL_CALL
132 		setOptions( const ::com::sun::star::util::SearchOptions& options )
133 							throw(::com::sun::star::uno::RuntimeException);
134     virtual ::com::sun::star::util::SearchResult SAL_CALL
135 		searchForward( const ::rtl::OUString& searchStr,
136 						sal_Int32 startPos, sal_Int32 endPos )
137 							throw(::com::sun::star::uno::RuntimeException);
138     virtual ::com::sun::star::util::SearchResult SAL_CALL
139 		searchBackward( const ::rtl::OUString& searchStr,
140 						sal_Int32 startPos, sal_Int32 endPos )
141 							throw(::com::sun::star::uno::RuntimeException);
142 
143     //XServiceInfo
144     virtual rtl::OUString SAL_CALL getImplementationName(void)
145                 throw( ::com::sun::star::uno::RuntimeException );
146     virtual sal_Bool SAL_CALL supportsService(const rtl::OUString& ServiceName)
147                 throw( ::com::sun::star::uno::RuntimeException );
148     virtual ::com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames(void)
149                 throw( ::com::sun::star::uno::RuntimeException );
150 };
151 
152 #endif
153