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