? unotools/unotools.vpj
Index: unotools/inc/unotools/textsearch.hxx
===================================================================
RCS file: /cvs/util/unotools/inc/unotools/textsearch.hxx,v
retrieving revision 1.8
diff -u -r1.8 textsearch.hxx
--- unotools/inc/unotools/textsearch.hxx	19 Jun 2006 14:02:47 -0000	1.8
+++ unotools/inc/unotools/textsearch.hxx	29 Aug 2007 17:16:41 -0000
@@ -54,6 +54,7 @@
 #ifndef _COM_SUN_STAR_UTIL_XTEXTSEARCH_HPP_
 #include <com/sun/star/util/XTextSearch.hpp>
 #endif
+#include <com/sun/star/util/SearchOptions.hpp>
 
 // Forward-Deklaration
 class CharClass;
@@ -63,7 +64,6 @@
 		namespace star {
 			namespace util {
 				struct SearchResult;
-				struct SearchOptions;
 			}
 		}
 	}
@@ -148,6 +148,18 @@
 
 class UNOTOOLS_DLLPUBLIC TextSearch
 {
+    struct CachedTextSearch
+    {
+        ::osl::Mutex mutex;
+        ::com::sun::star::util::SearchOptions Options;
+        ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch > xTextSearch;
+    };
+
+    static CachedTextSearch maCache;
+
+    static ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch >
+        getXTextSearch( const ::com::sun::star::util::SearchOptions& rPara );
+
 	com::sun::star::uno::Reference < com::sun::star::util::XTextSearch >
 			xTextSearch;
 
Index: unotools/source/i18n/textsearch.cxx
===================================================================
RCS file: /cvs/util/unotools/source/i18n/textsearch.cxx,v
retrieving revision 1.13
diff -u -r1.13 textsearch.cxx
--- unotools/source/i18n/textsearch.cxx	26 Mar 2007 12:46:42 -0000	1.13
+++ unotools/source/i18n/textsearch.cxx	29 Aug 2007 17:16:41 -0000
@@ -119,6 +119,46 @@
 //  ( Die Unterscheidung der Gross/Klein-Schreibung kann mit einen Flag
 //  unterdrueckt werden )
 
+TextSearch::CachedTextSearch TextSearch::maCache;
+
+static bool lcl_Equals( const SearchOptions& rSO1, const SearchOptions& rSO2 )
+{
+    return rSO1.algorithmType == rSO2.algorithmType &&
+        rSO1.searchFlag == rSO2.searchFlag &&
+        rSO1.searchString.equals(rSO2.searchString) &&
+        rSO1.replaceString.equals(rSO2.replaceString) &&
+        rSO1.changedChars == rSO2.changedChars &&
+        rSO1.deletedChars == rSO2.deletedChars &&
+        rSO1.insertedChars == rSO2.insertedChars &&
+        rSO1.Locale.Language == rSO2.Locale.Language &&
+        rSO1.Locale.Country == rSO2.Locale.Country &&
+        rSO1.Locale.Variant == rSO2.Locale.Variant &&
+        rSO1.transliterateFlags == rSO2.transliterateFlags;
+}
+
+Reference<XTextSearch> TextSearch::getXTextSearch( const SearchOptions& rPara )
+{
+    osl::MutexGuard aGuard(maCache.mutex);
+
+    if ( lcl_Equals(maCache.Options, rPara) )
+        return maCache.xTextSearch;
+
+    try
+    {
+        Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
+        maCache.xTextSearch.set( xMSF->createInstance(
+            ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
+                        "com.sun.star.util.TextSearch" ) ) ), UNO_QUERY_THROW );
+        maCache.xTextSearch->setOptions( rPara );
+        maCache.Options = rPara;
+    }
+    catch ( Exception& )
+    {
+        DBG_ERRORFILE( "TextSearch ctor: Exception caught!" );
+    }
+    return maCache.xTextSearch;
+}
+
 TextSearch::TextSearch(const SearchParam & rParam, LanguageType eLang )
 {
 	if( LANGUAGE_NONE == eLang )
@@ -136,18 +176,7 @@
 
 TextSearch::TextSearch( const SearchOptions& rPara )
 {
-	try
-	{
-		Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
-		xTextSearch = Reference< XTextSearch > ( xMSF->createInstance(
-			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
-                        "com.sun.star.util.TextSearch" ) ) ), UNO_QUERY_THROW );
-		xTextSearch->setOptions( rPara );
-	}
-	catch ( Exception& )
-	{
-		DBG_ERRORFILE( "TextSearch ctor: Exception caught!" );
-	}
+    xTextSearch = getXTextSearch( rPara );
 }
 
 void TextSearch::Init( const SearchParam & rParam,
@@ -191,18 +220,7 @@
         aSOpt.transliterateFlags |= ::com::sun::star::i18n::TransliterationModules_IGNORE_CASE;
     }
 
-	try
-	{
-		Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
-		xTextSearch = Reference< XTextSearch > ( xMSF->createInstance(
-			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
-                        "com.sun.star.util.TextSearch" ) ) ), UNO_QUERY_THROW );
-		xTextSearch->setOptions( aSOpt );
-	}
-	catch ( Exception& )
-	{
-		DBG_ERRORFILE( "TextSearch ctor: Exception caught!" );
-	}
+    xTextSearch = getXTextSearch( aSOpt );
 }
 
 void TextSearch::SetLocale( const ::com::sun::star::util::SearchOptions& rOptions,
@@ -212,18 +230,7 @@
     SearchOptions aSOpt( rOptions );
     aSOpt.Locale = rLocale;
 
-    try
-    {
-        Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
-        xTextSearch = Reference< XTextSearch > ( xMSF->createInstance(
-            ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
-                        "com.sun.star.util.TextSearch" ) ) ), UNO_QUERY_THROW );
-        xTextSearch->setOptions( aSOpt );
-    }
-    catch ( Exception& )
-    {
-        DBG_ERRORFILE( "TextSearch ctor: Exception caught!" );
-    }
+    xTextSearch = getXTextSearch( aSOpt );
 }
 
 
