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_TEXTSEARCH_HXX
26 #define _UNOTOOLS_TEXTSEARCH_HXX
27 #include <i18npool/lang.h>
28 #include <tools/string.hxx>
29 #include <com/sun/star/uno/Reference.h>
30 #include <com/sun/star/lang/Locale.hpp>
31 #include <com/sun/star/util/XTextSearch.hpp>
32 #include <com/sun/star/util/SearchOptions.hpp>
33 
34 // Forward-Deklaration
35 class CharClass;
36 
37 namespace com {
38 	namespace sun {
39 		namespace star {
40 			namespace util {
41 				struct SearchResult;
42 			}
43 		}
44 	}
45 }
46 
47 // ............................................................................
48 namespace utl
49 {
50 // ............................................................................
51 
52 // SS - Klasse fuers Suchen
53 class UNOTOOLS_DLLPUBLIC SearchParam
54 {
55 public:
56 	enum SearchType{ SRCH_NORMAL, SRCH_REGEXP, SRCH_LEVDIST };
57 
58 private:
59 	String sSrchStr;            // the search string
60 	String sReplaceStr;			// the replace string
61 
62 	SearchType eSrchType;       // search normal/regular/LevDist
63 
64 	int bWordOnly	: 1;		// used by normal search
65 	int bSrchInSel	: 1;		// search only in the selection
66 	int bCaseSense  : 1;        //
67 
68 	// values for the "weight Levenshtein-Distance"
69 	int bLEV_Relaxed : 1;
70 	int nLEV_OtherX;
71 	int nLEV_ShorterY;
72 	int nLEV_LongerZ;
73 
74 	// asian flags - used for the transliteration
75 	long nTransliterationFlags;
76 
77 public:
78 	SearchParam( const String &rText,
79 					SearchType eSrchType = SearchParam::SRCH_NORMAL,
80 					sal_Bool bCaseSens = sal_True,
81 					sal_Bool bWrdOnly = sal_False,
82 					sal_Bool bSrchInSel = sal_False );
83 	SearchParam( const SearchParam& );
84 
GetSrchStr() const85 	const String& 	GetSrchStr() const			{ return sSrchStr; }
GetReplaceStr() const86 	const String& 	GetReplaceStr() const		{ return sReplaceStr; }
GetSrchType() const87 	SearchType      GetSrchType() const         { return eSrchType; }
88 
IsCaseSensitive() const89 	int             IsCaseSensitive() const     { return bCaseSense; }
IsSrchInSelection() const90 	int				IsSrchInSelection() const	{ return bSrchInSel; }
IsSrchWordOnly() const91 	int 			IsSrchWordOnly() const		{ return bWordOnly; }
92 
93 
SetSrchStr(const String & rStr)94 	void SetSrchStr( const String& rStr ) 		{ sSrchStr = rStr; }
SetReplaceStr(const String & rStr)95 	void SetReplaceStr( const String& rStr )	{ sReplaceStr = rStr; }
SetSrchType(SearchType eType)96 	void SetSrchType( SearchType eType )        { eSrchType = eType; }
97 
SetCaseSensitive(int bFlag)98 	void SetCaseSensitive( int bFlag )          { bCaseSense = bFlag; }
SetSrchInSelection(int bFlag)99 	void SetSrchInSelection( int bFlag )        { bSrchInSel = bFlag; }
SetSrchWordOnly(int bFlag)100 	void SetSrchWordOnly( int bFlag )           { bWordOnly = bFlag; }
101 
IsSrchRelaxed() const102 	int 			IsSrchRelaxed() const		{ return bLEV_Relaxed; }
GetLEVOther() const103 	int				GetLEVOther() const			{ return nLEV_OtherX; }
GetLEVShorter() const104 	int				GetLEVShorter() const		{ return nLEV_ShorterY; }
GetLEVLonger() const105 	int				GetLEVLonger() const		{ return nLEV_LongerZ; }
106 
SetSrchRelaxed(int bFlag)107 	void SetSrchRelaxed( int bFlag )            { bLEV_Relaxed = bFlag; }
SetLEVOther(int nValue)108 	void SetLEVOther( int nValue )				{ nLEV_OtherX = nValue; }
SetLEVShorter(int nValue)109 	void SetLEVShorter( int nValue )			{ nLEV_ShorterY = nValue; }
SetLEVLonger(int nValue)110 	void SetLEVLonger( int nValue )				{ nLEV_LongerZ = nValue; }
111 
GetTransliterationFlags() const112 	long GetTransliterationFlags() const		{ return nTransliterationFlags; }
SetTransliterationFlags(long nValue)113 	void SetTransliterationFlags( long nValue )	{ nTransliterationFlags = nValue; }
114 };
115 
116 //	Klasse zum Suchen eines Strings in einem String.
117 //	Unterstuetzt werden folgende Verfahren:
118 //		- normalen Text (Bayer/Moore)
119 //		- regulaere Ausdruecke
120 // 		- gewichtete Levenshtein Distanz
121 //
122 //	Es kann Vorwaerts und Rueckwaerts gesucht werden!
123 
124 class UNOTOOLS_DLLPUBLIC TextSearch
125 {
126     static ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch >
127         getXTextSearch( const ::com::sun::star::util::SearchOptions& rPara );
128 
129 	com::sun::star::uno::Reference < com::sun::star::util::XTextSearch >
130 			xTextSearch;
131 
132 	void Init( const SearchParam & rParam,
133 			   const ::com::sun::star::lang::Locale& rLocale );
134 
135 public:
136 	// rText ist der zusuchende String
137 	// this first two CTORs are deprecated!
138 	TextSearch( const SearchParam & rPara, LanguageType nLanguage );
139 	TextSearch( const SearchParam & rPara, const CharClass& rCClass );
140 
141 	TextSearch( const ::com::sun::star::util::SearchOptions& rPara );
142 	~TextSearch();
143 
144 	/* search in the (selected) text the search string:
145 		rScrTxt - the text, in in which we search
146 		pStart	- start position for the search
147 		pEnde   - end position for the search
148 
149 		RETURN values   ==  sal_True: something is found
150 						- pStart start pos of the found text,
151 						- pStart end pos of the found text,
152 						- pSrchResult - the search result with all found
153 						     positions. Is only filled with more positions
154 							 if the regular expression handles groups.
155 
156 						== sal_False: nothing found, pStart,pEnde unchanged.
157 
158 		Definitions: start pos always inclusive, end pos always exclusive!
159 					 The position must always in the right direction!
160 					search forward: start <= end
161 					search backward: end <= start
162 	*/
163 	int SearchFrwrd( const String &rStr,
164 					xub_StrLen* pStart, xub_StrLen* pEnde,
165 					::com::sun::star::util::SearchResult* pSrchResult = 0 );
166 	int SearchBkwrd( const String &rStr,
167 					xub_StrLen* pStart, xub_StrLen* pEnde,
168 					::com::sun::star::util::SearchResult* pSrchResult = 0 );
169 
170     void SetLocale( const ::com::sun::star::util::SearchOptions& rOpt,
171                     const ::com::sun::star::lang::Locale& rLocale );
172 
173     /* replace back references in the replace string by the sub expressions from the search result */
174     void ReplaceBackReferences( String& rReplaceStr, const String &rStr, const ::com::sun::star::util::SearchResult& rResult );
175 
176 };
177 
178 // ............................................................................
179 }	// namespace utl
180 // ............................................................................
181 
182 #endif
183 
184