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