1*bae3752eSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*bae3752eSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*bae3752eSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*bae3752eSAndrew Rist * distributed with this work for additional information 6*bae3752eSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*bae3752eSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*bae3752eSAndrew Rist * "License"); you may not use this file except in compliance 9*bae3752eSAndrew Rist * with the License. You may obtain a copy of the License at 10*bae3752eSAndrew Rist * 11*bae3752eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*bae3752eSAndrew Rist * 13*bae3752eSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*bae3752eSAndrew Rist * software distributed under the License is distributed on an 15*bae3752eSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*bae3752eSAndrew Rist * KIND, either express or implied. See the License for the 17*bae3752eSAndrew Rist * specific language governing permissions and limitations 18*bae3752eSAndrew Rist * under the License. 19*bae3752eSAndrew Rist * 20*bae3752eSAndrew Rist *************************************************************/ 21*bae3752eSAndrew Rist 22*bae3752eSAndrew Rist 23cdf0e10cSrcweir #include "unotools/unotoolsdllapi.h" 24cdf0e10cSrcweir 25cdf0e10cSrcweir #ifndef _UNOTOOLS_TEXTSEARCH_HXX 26cdf0e10cSrcweir #define _UNOTOOLS_TEXTSEARCH_HXX 27cdf0e10cSrcweir #include <i18npool/lang.h> 28cdf0e10cSrcweir #include <tools/string.hxx> 29cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h> 30cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp> 31cdf0e10cSrcweir #include <com/sun/star/util/XTextSearch.hpp> 32cdf0e10cSrcweir #include <com/sun/star/util/SearchOptions.hpp> 33cdf0e10cSrcweir 34cdf0e10cSrcweir // Forward-Deklaration 35cdf0e10cSrcweir class CharClass; 36cdf0e10cSrcweir 37cdf0e10cSrcweir namespace com { 38cdf0e10cSrcweir namespace sun { 39cdf0e10cSrcweir namespace star { 40cdf0e10cSrcweir namespace util { 41cdf0e10cSrcweir struct SearchResult; 42cdf0e10cSrcweir } 43cdf0e10cSrcweir } 44cdf0e10cSrcweir } 45cdf0e10cSrcweir } 46cdf0e10cSrcweir 47cdf0e10cSrcweir // ............................................................................ 48cdf0e10cSrcweir namespace utl 49cdf0e10cSrcweir { 50cdf0e10cSrcweir // ............................................................................ 51cdf0e10cSrcweir 52cdf0e10cSrcweir // SS - Klasse fuers Suchen 53cdf0e10cSrcweir class UNOTOOLS_DLLPUBLIC SearchParam 54cdf0e10cSrcweir { 55cdf0e10cSrcweir public: 56cdf0e10cSrcweir enum SearchType{ SRCH_NORMAL, SRCH_REGEXP, SRCH_LEVDIST }; 57cdf0e10cSrcweir 58cdf0e10cSrcweir private: 59cdf0e10cSrcweir String sSrchStr; // the search string 60cdf0e10cSrcweir String sReplaceStr; // the replace string 61cdf0e10cSrcweir 62cdf0e10cSrcweir SearchType eSrchType; // search normal/regular/LevDist 63cdf0e10cSrcweir 64cdf0e10cSrcweir int bWordOnly : 1; // used by normal search 65cdf0e10cSrcweir int bSrchInSel : 1; // search only in the selection 66cdf0e10cSrcweir int bCaseSense : 1; // 67cdf0e10cSrcweir 68cdf0e10cSrcweir // values for the "weight Levenshtein-Distance" 69cdf0e10cSrcweir int bLEV_Relaxed : 1; 70cdf0e10cSrcweir int nLEV_OtherX; 71cdf0e10cSrcweir int nLEV_ShorterY; 72cdf0e10cSrcweir int nLEV_LongerZ; 73cdf0e10cSrcweir 74cdf0e10cSrcweir // asian flags - used for the transliteration 75cdf0e10cSrcweir long nTransliterationFlags; 76cdf0e10cSrcweir 77cdf0e10cSrcweir public: 78cdf0e10cSrcweir SearchParam( const String &rText, 79cdf0e10cSrcweir SearchType eSrchType = SearchParam::SRCH_NORMAL, 80cdf0e10cSrcweir sal_Bool bCaseSens = sal_True, 81cdf0e10cSrcweir sal_Bool bWrdOnly = sal_False, 82cdf0e10cSrcweir sal_Bool bSrchInSel = sal_False ); 83cdf0e10cSrcweir SearchParam( const SearchParam& ); 84cdf0e10cSrcweir GetSrchStr() const85cdf0e10cSrcweir const String& GetSrchStr() const { return sSrchStr; } GetReplaceStr() const86cdf0e10cSrcweir const String& GetReplaceStr() const { return sReplaceStr; } GetSrchType() const87cdf0e10cSrcweir SearchType GetSrchType() const { return eSrchType; } 88cdf0e10cSrcweir IsCaseSensitive() const89cdf0e10cSrcweir int IsCaseSensitive() const { return bCaseSense; } IsSrchInSelection() const90cdf0e10cSrcweir int IsSrchInSelection() const { return bSrchInSel; } IsSrchWordOnly() const91cdf0e10cSrcweir int IsSrchWordOnly() const { return bWordOnly; } 92cdf0e10cSrcweir 93cdf0e10cSrcweir SetSrchStr(const String & rStr)94cdf0e10cSrcweir void SetSrchStr( const String& rStr ) { sSrchStr = rStr; } SetReplaceStr(const String & rStr)95cdf0e10cSrcweir void SetReplaceStr( const String& rStr ) { sReplaceStr = rStr; } SetSrchType(SearchType eType)96cdf0e10cSrcweir void SetSrchType( SearchType eType ) { eSrchType = eType; } 97cdf0e10cSrcweir SetCaseSensitive(int bFlag)98cdf0e10cSrcweir void SetCaseSensitive( int bFlag ) { bCaseSense = bFlag; } SetSrchInSelection(int bFlag)99cdf0e10cSrcweir void SetSrchInSelection( int bFlag ) { bSrchInSel = bFlag; } SetSrchWordOnly(int bFlag)100cdf0e10cSrcweir void SetSrchWordOnly( int bFlag ) { bWordOnly = bFlag; } 101cdf0e10cSrcweir IsSrchRelaxed() const102cdf0e10cSrcweir int IsSrchRelaxed() const { return bLEV_Relaxed; } GetLEVOther() const103cdf0e10cSrcweir int GetLEVOther() const { return nLEV_OtherX; } GetLEVShorter() const104cdf0e10cSrcweir int GetLEVShorter() const { return nLEV_ShorterY; } GetLEVLonger() const105cdf0e10cSrcweir int GetLEVLonger() const { return nLEV_LongerZ; } 106cdf0e10cSrcweir SetSrchRelaxed(int bFlag)107cdf0e10cSrcweir void SetSrchRelaxed( int bFlag ) { bLEV_Relaxed = bFlag; } SetLEVOther(int nValue)108cdf0e10cSrcweir void SetLEVOther( int nValue ) { nLEV_OtherX = nValue; } SetLEVShorter(int nValue)109cdf0e10cSrcweir void SetLEVShorter( int nValue ) { nLEV_ShorterY = nValue; } SetLEVLonger(int nValue)110cdf0e10cSrcweir void SetLEVLonger( int nValue ) { nLEV_LongerZ = nValue; } 111cdf0e10cSrcweir GetTransliterationFlags() const112cdf0e10cSrcweir long GetTransliterationFlags() const { return nTransliterationFlags; } SetTransliterationFlags(long nValue)113cdf0e10cSrcweir void SetTransliterationFlags( long nValue ) { nTransliterationFlags = nValue; } 114cdf0e10cSrcweir }; 115cdf0e10cSrcweir 116cdf0e10cSrcweir // Klasse zum Suchen eines Strings in einem String. 117cdf0e10cSrcweir // Unterstuetzt werden folgende Verfahren: 118cdf0e10cSrcweir // - normalen Text (Bayer/Moore) 119cdf0e10cSrcweir // - regulaere Ausdruecke 120cdf0e10cSrcweir // - gewichtete Levenshtein Distanz 121cdf0e10cSrcweir // 122cdf0e10cSrcweir // Es kann Vorwaerts und Rueckwaerts gesucht werden! 123cdf0e10cSrcweir 124cdf0e10cSrcweir class UNOTOOLS_DLLPUBLIC TextSearch 125cdf0e10cSrcweir { 126cdf0e10cSrcweir static ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch > 127cdf0e10cSrcweir getXTextSearch( const ::com::sun::star::util::SearchOptions& rPara ); 128cdf0e10cSrcweir 129cdf0e10cSrcweir com::sun::star::uno::Reference < com::sun::star::util::XTextSearch > 130cdf0e10cSrcweir xTextSearch; 131cdf0e10cSrcweir 132cdf0e10cSrcweir void Init( const SearchParam & rParam, 133cdf0e10cSrcweir const ::com::sun::star::lang::Locale& rLocale ); 134cdf0e10cSrcweir 135cdf0e10cSrcweir public: 136cdf0e10cSrcweir // rText ist der zusuchende String 137cdf0e10cSrcweir // this first two CTORs are deprecated! 138cdf0e10cSrcweir TextSearch( const SearchParam & rPara, LanguageType nLanguage ); 139cdf0e10cSrcweir TextSearch( const SearchParam & rPara, const CharClass& rCClass ); 140cdf0e10cSrcweir 141cdf0e10cSrcweir TextSearch( const ::com::sun::star::util::SearchOptions& rPara ); 142cdf0e10cSrcweir ~TextSearch(); 143cdf0e10cSrcweir 144cdf0e10cSrcweir /* search in the (selected) text the search string: 145cdf0e10cSrcweir rScrTxt - the text, in in which we search 146cdf0e10cSrcweir pStart - start position for the search 147cdf0e10cSrcweir pEnde - end position for the search 148cdf0e10cSrcweir 149cdf0e10cSrcweir RETURN values == sal_True: something is found 150cdf0e10cSrcweir - pStart start pos of the found text, 151cdf0e10cSrcweir - pStart end pos of the found text, 152cdf0e10cSrcweir - pSrchResult - the search result with all found 153cdf0e10cSrcweir positions. Is only filled with more positions 154cdf0e10cSrcweir if the regular expression handles groups. 155cdf0e10cSrcweir 156cdf0e10cSrcweir == sal_False: nothing found, pStart,pEnde unchanged. 157cdf0e10cSrcweir 158cdf0e10cSrcweir Definitions: start pos always inclusive, end pos always exclusive! 159cdf0e10cSrcweir The position must always in the right direction! 160cdf0e10cSrcweir search forward: start <= end 161cdf0e10cSrcweir search backward: end <= start 162cdf0e10cSrcweir */ 163cdf0e10cSrcweir int SearchFrwrd( const String &rStr, 164cdf0e10cSrcweir xub_StrLen* pStart, xub_StrLen* pEnde, 165cdf0e10cSrcweir ::com::sun::star::util::SearchResult* pSrchResult = 0 ); 166cdf0e10cSrcweir int SearchBkwrd( const String &rStr, 167cdf0e10cSrcweir xub_StrLen* pStart, xub_StrLen* pEnde, 168cdf0e10cSrcweir ::com::sun::star::util::SearchResult* pSrchResult = 0 ); 169cdf0e10cSrcweir 170cdf0e10cSrcweir void SetLocale( const ::com::sun::star::util::SearchOptions& rOpt, 171cdf0e10cSrcweir const ::com::sun::star::lang::Locale& rLocale ); 172cdf0e10cSrcweir 173cdf0e10cSrcweir /* replace back references in the replace string by the sub expressions from the search result */ 174cdf0e10cSrcweir void ReplaceBackReferences( String& rReplaceStr, const String &rStr, const ::com::sun::star::util::SearchResult& rResult ); 175cdf0e10cSrcweir 176cdf0e10cSrcweir }; 177cdf0e10cSrcweir 178cdf0e10cSrcweir // ............................................................................ 179cdf0e10cSrcweir } // namespace utl 180cdf0e10cSrcweir // ............................................................................ 181cdf0e10cSrcweir 182cdf0e10cSrcweir #endif 183cdf0e10cSrcweir 184