1*3b8558fdSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*3b8558fdSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*3b8558fdSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*3b8558fdSAndrew Rist * distributed with this work for additional information
6*3b8558fdSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*3b8558fdSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*3b8558fdSAndrew Rist * "License"); you may not use this file except in compliance
9*3b8558fdSAndrew Rist * with the License. You may obtain a copy of the License at
10*3b8558fdSAndrew Rist *
11*3b8558fdSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*3b8558fdSAndrew Rist *
13*3b8558fdSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*3b8558fdSAndrew Rist * software distributed under the License is distributed on an
15*3b8558fdSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3b8558fdSAndrew Rist * KIND, either express or implied. See the License for the
17*3b8558fdSAndrew Rist * specific language governing permissions and limitations
18*3b8558fdSAndrew Rist * under the License.
19*3b8558fdSAndrew Rist *
20*3b8558fdSAndrew Rist *************************************************************/
21*3b8558fdSAndrew Rist
22*3b8558fdSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
28cdf0e10cSrcweir #include <i18npool/lang.h>
29cdf0e10cSrcweir #include <osl/mutex.hxx>
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir #include <tools/fsys.hxx>
32cdf0e10cSrcweir #include <tools/stream.hxx>
33cdf0e10cSrcweir #include <tools/stream.hxx>
34cdf0e10cSrcweir #include <tools/string.hxx>
35cdf0e10cSrcweir #include <tools/urlobj.hxx>
36cdf0e10cSrcweir #include <ucbhelper/content.hxx>
37cdf0e10cSrcweir #include <unotools/processfactory.hxx>
38cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
39cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include <com/sun/star/linguistic2/XConversionDictionary.hpp>
42cdf0e10cSrcweir #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
43cdf0e10cSrcweir #include <com/sun/star/linguistic2/XConversionPropertyType.hpp>
44cdf0e10cSrcweir #include <com/sun/star/linguistic2/ConversionPropertyType.hpp>
45cdf0e10cSrcweir #include <com/sun/star/util/XFlushable.hpp>
46cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
47cdf0e10cSrcweir #include <com/sun/star/lang/EventObject.hpp>
48cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
49cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
50cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
51cdf0e10cSrcweir #include <com/sun/star/util/XFlushListener.hpp>
52cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
53cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
54cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
55cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
56cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
57cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp>
58cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
59cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
60cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
61cdf0e10cSrcweir
62cdf0e10cSrcweir
63cdf0e10cSrcweir #include "convdic.hxx"
64cdf0e10cSrcweir #include "convdicxml.hxx"
65cdf0e10cSrcweir #include "linguistic/misc.hxx"
66cdf0e10cSrcweir #include "defs.hxx"
67cdf0e10cSrcweir
68cdf0e10cSrcweir using namespace std;
69cdf0e10cSrcweir using namespace utl;
70cdf0e10cSrcweir using namespace osl;
71cdf0e10cSrcweir using namespace rtl;
72cdf0e10cSrcweir using namespace com::sun::star;
73cdf0e10cSrcweir using namespace com::sun::star::lang;
74cdf0e10cSrcweir using namespace com::sun::star::uno;
75cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
76cdf0e10cSrcweir using namespace linguistic;
77cdf0e10cSrcweir
78cdf0e10cSrcweir #define SN_CONV_DICTIONARY "com.sun.star.linguistic2.ConversionDictionary"
79cdf0e10cSrcweir #define SN_HCD_CONV_DICTIONARY "com.sun.star.linguistic2.HangulHanjaConversionDictionary"
80cdf0e10cSrcweir
81cdf0e10cSrcweir
82cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
ReadThroughDic(const String & rMainURL,ConvDicXMLImport & rImport)83cdf0e10cSrcweir void ReadThroughDic( const String &rMainURL, ConvDicXMLImport &rImport )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir if (rMainURL.Len() == 0)
86cdf0e10cSrcweir return;
87cdf0e10cSrcweir DBG_ASSERT(!INetURLObject( rMainURL ).HasError(), "invalid URL");
88cdf0e10cSrcweir
89cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xServiceFactory( utl::getProcessServiceFactory() );
90cdf0e10cSrcweir
91cdf0e10cSrcweir // get xInputStream stream
92cdf0e10cSrcweir uno::Reference< io::XInputStream > xIn;
93cdf0e10cSrcweir try
94cdf0e10cSrcweir {
95cdf0e10cSrcweir uno::Reference< ucb::XSimpleFileAccess > xAccess( xServiceFactory->createInstance(
96cdf0e10cSrcweir A2OU( "com.sun.star.ucb.SimpleFileAccess" ) ), uno::UNO_QUERY_THROW );
97cdf0e10cSrcweir xIn = xAccess->openFileRead( rMainURL );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir catch (uno::Exception & e)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir DBG_ASSERT( 0, "failed to get input stream" );
102cdf0e10cSrcweir (void) e;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir if (!xIn.is())
105cdf0e10cSrcweir return;
106cdf0e10cSrcweir
107cdf0e10cSrcweir SvStreamPtr pStream = SvStreamPtr( utl::UcbStreamHelper::CreateStream( xIn ) );
108cdf0e10cSrcweir
109cdf0e10cSrcweir sal_uLong nError = sal::static_int_cast< sal_uLong >(-1);
110cdf0e10cSrcweir
111cdf0e10cSrcweir // prepare ParserInputSource
112cdf0e10cSrcweir xml::sax::InputSource aParserInput;
113cdf0e10cSrcweir aParserInput.aInputStream = xIn;
114cdf0e10cSrcweir
115cdf0e10cSrcweir // get parser
116cdf0e10cSrcweir uno::Reference< xml::sax::XParser > xParser;
117cdf0e10cSrcweir try
118cdf0e10cSrcweir {
119cdf0e10cSrcweir xParser = uno::Reference< xml::sax::XParser >( xServiceFactory->createInstance(
120cdf0e10cSrcweir A2OU( "com.sun.star.xml.sax.Parser" ) ), UNO_QUERY );
121cdf0e10cSrcweir }
122cdf0e10cSrcweir catch (uno::Exception &)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir }
125cdf0e10cSrcweir DBG_ASSERT( xParser.is(), "Can't create parser" );
126cdf0e10cSrcweir if (!xParser.is())
127cdf0e10cSrcweir return;
128cdf0e10cSrcweir
129cdf0e10cSrcweir // get filter
130cdf0e10cSrcweir //ConvDicXMLImport *pImport = new ConvDicXMLImport( this, rMainURL );
131cdf0e10cSrcweir //!! keep a reference until everything is done to
132cdf0e10cSrcweir //!! ensure the proper lifetime of the object
133cdf0e10cSrcweir uno::Reference < xml::sax::XDocumentHandler > xFilter(
134cdf0e10cSrcweir (xml::sax::XExtendedDocumentHandler *) &rImport, UNO_QUERY );
135cdf0e10cSrcweir
136cdf0e10cSrcweir // connect parser and filter
137cdf0e10cSrcweir xParser->setDocumentHandler( xFilter );
138cdf0e10cSrcweir
139cdf0e10cSrcweir // finally, parser the stream
140cdf0e10cSrcweir try
141cdf0e10cSrcweir {
142cdf0e10cSrcweir xParser->parseStream( aParserInput ); // implicitly calls ConvDicXMLImport::CreateContext
143cdf0e10cSrcweir if (rImport.GetSuccess())
144cdf0e10cSrcweir nError = 0;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir catch( xml::sax::SAXParseException& )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir // if( bEncrypted )
149cdf0e10cSrcweir // nError = ERRCODE_SFX_WRONGPASSWORD;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir catch( xml::sax::SAXException& )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir // if( bEncrypted )
154cdf0e10cSrcweir // nError = ERRCODE_SFX_WRONGPASSWORD;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir catch( io::IOException& )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir }
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
IsConvDic(const String & rFileURL,sal_Int16 & nLang,sal_Int16 & nConvType)161cdf0e10cSrcweir sal_Bool IsConvDic( const String &rFileURL, sal_Int16 &nLang, sal_Int16 &nConvType )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir sal_Bool bRes = sal_False;
164cdf0e10cSrcweir
165cdf0e10cSrcweir if (rFileURL.Len() == 0)
166cdf0e10cSrcweir return bRes;
167cdf0e10cSrcweir
168cdf0e10cSrcweir // check if file extension matches CONV_DIC_EXT
169cdf0e10cSrcweir String aExt;
170cdf0e10cSrcweir xub_StrLen nPos = rFileURL.SearchBackward( '.' );
171cdf0e10cSrcweir if (STRING_NOTFOUND != nPos)
172cdf0e10cSrcweir aExt = rFileURL.Copy( nPos + 1 );
173cdf0e10cSrcweir aExt.ToLowerAscii();
174cdf0e10cSrcweir if (!aExt.EqualsAscii( CONV_DIC_EXT ))
175cdf0e10cSrcweir return bRes;
176cdf0e10cSrcweir
177cdf0e10cSrcweir // first argument being 0 should stop the file from being parsed
178cdf0e10cSrcweir // up to the end (reading all entries) when the required
179cdf0e10cSrcweir // data (language, conversion type) is found.
180cdf0e10cSrcweir ConvDicXMLImport *pImport = new ConvDicXMLImport( 0, rFileURL );
181cdf0e10cSrcweir
182cdf0e10cSrcweir //!! keep a first reference to ensure the lifetime of the object !!
183cdf0e10cSrcweir uno::Reference< XInterface > xRef( (document::XFilter *) pImport, UNO_QUERY );
184cdf0e10cSrcweir
185cdf0e10cSrcweir ReadThroughDic( rFileURL, *pImport ); // will implicitly add the entries
186cdf0e10cSrcweir bRes = pImport->GetLanguage() != LANGUAGE_NONE &&
187cdf0e10cSrcweir pImport->GetConversionType() != -1;
188cdf0e10cSrcweir DBG_ASSERT( bRes, "conversion dictionary corrupted?" );
189cdf0e10cSrcweir
190cdf0e10cSrcweir if (bRes)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir nLang = pImport->GetLanguage();
193cdf0e10cSrcweir nConvType = pImport->GetConversionType();
194cdf0e10cSrcweir }
195cdf0e10cSrcweir
196cdf0e10cSrcweir return bRes;
197cdf0e10cSrcweir }
198cdf0e10cSrcweir
199cdf0e10cSrcweir
200cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
201cdf0e10cSrcweir
ConvDic(const String & rName,sal_Int16 nLang,sal_Int16 nConvType,sal_Bool bBiDirectional,const String & rMainURL)202cdf0e10cSrcweir ConvDic::ConvDic(
203cdf0e10cSrcweir const String &rName,
204cdf0e10cSrcweir sal_Int16 nLang,
205cdf0e10cSrcweir sal_Int16 nConvType,
206cdf0e10cSrcweir sal_Bool bBiDirectional,
207cdf0e10cSrcweir const String &rMainURL) :
208cdf0e10cSrcweir aFlushListeners( GetLinguMutex() )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir aName = rName;
211cdf0e10cSrcweir nLanguage = nLang;
212cdf0e10cSrcweir nConversionType = nConvType;
213cdf0e10cSrcweir aMainURL = rMainURL;
214cdf0e10cSrcweir
215cdf0e10cSrcweir if (bBiDirectional)
216cdf0e10cSrcweir pFromRight = std::auto_ptr< ConvMap >( new ConvMap );
217cdf0e10cSrcweir if (nLang == LANGUAGE_CHINESE_SIMPLIFIED || nLang == LANGUAGE_CHINESE_TRADITIONAL)
218cdf0e10cSrcweir pConvPropType = std::auto_ptr< PropTypeMap >( new PropTypeMap );
219cdf0e10cSrcweir
220cdf0e10cSrcweir nMaxLeftCharCount = nMaxRightCharCount = 0;
221cdf0e10cSrcweir bMaxCharCountIsValid = sal_True;
222cdf0e10cSrcweir
223cdf0e10cSrcweir bNeedEntries = sal_True;
224cdf0e10cSrcweir bIsModified = bIsActive = sal_False;
225cdf0e10cSrcweir bIsReadonly = sal_False;
226cdf0e10cSrcweir
227cdf0e10cSrcweir if( rMainURL.Len() > 0 )
228cdf0e10cSrcweir {
229cdf0e10cSrcweir sal_Bool bExists = sal_False;
230cdf0e10cSrcweir bIsReadonly = IsReadOnly( rMainURL, &bExists );
231cdf0e10cSrcweir
232cdf0e10cSrcweir if( !bExists ) // new empty dictionary
233cdf0e10cSrcweir {
234cdf0e10cSrcweir bNeedEntries = sal_False;
235cdf0e10cSrcweir //! create physical representation of an **empty** dictionary
236cdf0e10cSrcweir //! that could be found by the dictionary-list implementation
237cdf0e10cSrcweir // (Note: empty dictionaries are not just empty files!)
238cdf0e10cSrcweir Save();
239cdf0e10cSrcweir bIsReadonly = IsReadOnly( rMainURL ); // will be sal_False if Save was succesfull
240cdf0e10cSrcweir }
241cdf0e10cSrcweir }
242cdf0e10cSrcweir else
243cdf0e10cSrcweir {
244cdf0e10cSrcweir bNeedEntries = sal_False;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir }
247cdf0e10cSrcweir
248cdf0e10cSrcweir
~ConvDic()249cdf0e10cSrcweir ConvDic::~ConvDic()
250cdf0e10cSrcweir {
251cdf0e10cSrcweir }
252cdf0e10cSrcweir
253cdf0e10cSrcweir
Load()254cdf0e10cSrcweir void ConvDic::Load()
255cdf0e10cSrcweir {
256cdf0e10cSrcweir DBG_ASSERT( !bIsModified, "dictionary is modified. Really do 'Load'?" );
257cdf0e10cSrcweir
258cdf0e10cSrcweir //!! prevent function from being called recursively via HasEntry, AddEntry
259cdf0e10cSrcweir bNeedEntries = sal_False;
260cdf0e10cSrcweir ConvDicXMLImport *pImport = new ConvDicXMLImport( this, aMainURL );
261cdf0e10cSrcweir //!! keep a first reference to ensure the lifetime of the object !!
262cdf0e10cSrcweir uno::Reference< XInterface > xRef( (document::XFilter *) pImport, UNO_QUERY );
263cdf0e10cSrcweir ReadThroughDic( aMainURL, *pImport ); // will implicitly add the entries
264cdf0e10cSrcweir bIsModified = sal_False;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir
267cdf0e10cSrcweir
Save()268cdf0e10cSrcweir void ConvDic::Save()
269cdf0e10cSrcweir {
270cdf0e10cSrcweir DBG_ASSERT( !bNeedEntries, "saving while entries missing" );
271cdf0e10cSrcweir if (aMainURL.Len() == 0 || bNeedEntries)
272cdf0e10cSrcweir return;
273cdf0e10cSrcweir DBG_ASSERT(!INetURLObject( aMainURL ).HasError(), "invalid URL");
274cdf0e10cSrcweir
275cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xServiceFactory( utl::getProcessServiceFactory() );
276cdf0e10cSrcweir
277cdf0e10cSrcweir // get XOutputStream stream
278cdf0e10cSrcweir uno::Reference< io::XStream > xStream;
279cdf0e10cSrcweir try
280cdf0e10cSrcweir {
281cdf0e10cSrcweir uno::Reference< ucb::XSimpleFileAccess > xAccess( xServiceFactory->createInstance(
282cdf0e10cSrcweir A2OU( "com.sun.star.ucb.SimpleFileAccess" ) ), uno::UNO_QUERY_THROW );
283cdf0e10cSrcweir xStream = xAccess->openFileReadWrite( aMainURL );
284cdf0e10cSrcweir }
285cdf0e10cSrcweir catch (uno::Exception & e)
286cdf0e10cSrcweir {
287cdf0e10cSrcweir DBG_ASSERT( 0, "failed to get input stream" );
288cdf0e10cSrcweir (void) e;
289cdf0e10cSrcweir }
290cdf0e10cSrcweir if (!xStream.is())
291cdf0e10cSrcweir return;
292cdf0e10cSrcweir
293cdf0e10cSrcweir SvStreamPtr pStream = SvStreamPtr( utl::UcbStreamHelper::CreateStream( xStream ) );
294cdf0e10cSrcweir
295cdf0e10cSrcweir // get XML writer
296cdf0e10cSrcweir uno::Reference< io::XActiveDataSource > xSaxWriter;
297cdf0e10cSrcweir if (xServiceFactory.is())
298cdf0e10cSrcweir {
299cdf0e10cSrcweir try
300cdf0e10cSrcweir {
301cdf0e10cSrcweir xSaxWriter = uno::Reference< io::XActiveDataSource >(
302cdf0e10cSrcweir xServiceFactory->createInstance(
303cdf0e10cSrcweir OUString::createFromAscii( "com.sun.star.xml.sax.Writer" ) ), UNO_QUERY );
304cdf0e10cSrcweir }
305cdf0e10cSrcweir catch (uno::Exception &)
306cdf0e10cSrcweir {
307cdf0e10cSrcweir }
308cdf0e10cSrcweir }
309cdf0e10cSrcweir DBG_ASSERT( xSaxWriter.is(), "can't instantiate XML writer" );
310cdf0e10cSrcweir
311cdf0e10cSrcweir if (xSaxWriter.is() && xStream.is())
312cdf0e10cSrcweir {
313cdf0e10cSrcweir // connect XML writer to output stream
314cdf0e10cSrcweir xSaxWriter->setOutputStream( xStream->getOutputStream() );
315cdf0e10cSrcweir
316cdf0e10cSrcweir // prepare arguments (prepend doc handler to given arguments)
317cdf0e10cSrcweir uno::Reference< xml::sax::XDocumentHandler > xDocHandler( xSaxWriter, UNO_QUERY );
318cdf0e10cSrcweir ConvDicXMLExport *pExport = new ConvDicXMLExport( *this, aMainURL, xDocHandler );
319cdf0e10cSrcweir //!! keep a first(!) reference until everything is done to
320cdf0e10cSrcweir //!! ensure the proper lifetime of the object
321cdf0e10cSrcweir uno::Reference< document::XFilter > aRef( (document::XFilter *) pExport );
322cdf0e10cSrcweir sal_Bool bRet = pExport->Export(); // write entries to file
323cdf0e10cSrcweir DBG_ASSERT( !pStream->GetError(), "I/O error while writing to stream" );
324cdf0e10cSrcweir if (bRet)
325cdf0e10cSrcweir bIsModified = sal_False;
326cdf0e10cSrcweir }
327cdf0e10cSrcweir DBG_ASSERT( !bIsModified, "dictionary still modified after save. Save failed?" );
328cdf0e10cSrcweir }
329cdf0e10cSrcweir
330cdf0e10cSrcweir
GetEntry(ConvMap & rMap,const rtl::OUString & rFirstText,const rtl::OUString & rSecondText)331cdf0e10cSrcweir ConvMap::iterator ConvDic::GetEntry( ConvMap &rMap, const rtl::OUString &rFirstText, const rtl::OUString &rSecondText )
332cdf0e10cSrcweir {
333cdf0e10cSrcweir pair< ConvMap::iterator, ConvMap::iterator > aRange =
334cdf0e10cSrcweir rMap.equal_range( rFirstText );
335cdf0e10cSrcweir ConvMap::iterator aPos = rMap.end();
336cdf0e10cSrcweir for (ConvMap::iterator aIt = aRange.first;
337cdf0e10cSrcweir aIt != aRange.second && aPos == rMap.end();
338cdf0e10cSrcweir ++aIt)
339cdf0e10cSrcweir {
340cdf0e10cSrcweir if ((*aIt).second == rSecondText)
341cdf0e10cSrcweir aPos = aIt;
342cdf0e10cSrcweir }
343cdf0e10cSrcweir return aPos;
344cdf0e10cSrcweir }
345cdf0e10cSrcweir
346cdf0e10cSrcweir
HasEntry(const OUString & rLeftText,const OUString & rRightText)347cdf0e10cSrcweir sal_Bool ConvDic::HasEntry( const OUString &rLeftText, const OUString &rRightText )
348cdf0e10cSrcweir {
349cdf0e10cSrcweir if (bNeedEntries)
350cdf0e10cSrcweir Load();
351cdf0e10cSrcweir ConvMap::iterator aIt = GetEntry( aFromLeft, rLeftText, rRightText );
352cdf0e10cSrcweir return aIt != aFromLeft.end();
353cdf0e10cSrcweir }
354cdf0e10cSrcweir
355cdf0e10cSrcweir
AddEntry(const OUString & rLeftText,const OUString & rRightText)356cdf0e10cSrcweir void ConvDic::AddEntry( const OUString &rLeftText, const OUString &rRightText )
357cdf0e10cSrcweir {
358cdf0e10cSrcweir if (bNeedEntries)
359cdf0e10cSrcweir Load();
360cdf0e10cSrcweir
361cdf0e10cSrcweir DBG_ASSERT(!HasEntry( rLeftText, rRightText), "entry already exists" );
362cdf0e10cSrcweir aFromLeft .insert( ConvMap::value_type( rLeftText, rRightText ) );
363cdf0e10cSrcweir if (pFromRight.get())
364cdf0e10cSrcweir pFromRight->insert( ConvMap::value_type( rRightText, rLeftText ) );
365cdf0e10cSrcweir
366cdf0e10cSrcweir if (bMaxCharCountIsValid)
367cdf0e10cSrcweir {
368cdf0e10cSrcweir if (rLeftText.getLength() > nMaxLeftCharCount)
369cdf0e10cSrcweir nMaxLeftCharCount = (sal_Int16) rLeftText.getLength();
370cdf0e10cSrcweir if (pFromRight.get() && rRightText.getLength() > nMaxRightCharCount)
371cdf0e10cSrcweir nMaxRightCharCount = (sal_Int16) rRightText.getLength();
372cdf0e10cSrcweir }
373cdf0e10cSrcweir
374cdf0e10cSrcweir bIsModified = sal_True;
375cdf0e10cSrcweir }
376cdf0e10cSrcweir
377cdf0e10cSrcweir
RemoveEntry(const OUString & rLeftText,const OUString & rRightText)378cdf0e10cSrcweir void ConvDic::RemoveEntry( const OUString &rLeftText, const OUString &rRightText )
379cdf0e10cSrcweir {
380cdf0e10cSrcweir if (bNeedEntries)
381cdf0e10cSrcweir Load();
382cdf0e10cSrcweir
383cdf0e10cSrcweir ConvMap::iterator aLeftIt = GetEntry( aFromLeft, rLeftText, rRightText );
384cdf0e10cSrcweir DBG_ASSERT( aLeftIt != aFromLeft.end(), "left map entry missing" );
385cdf0e10cSrcweir aFromLeft .erase( aLeftIt );
386cdf0e10cSrcweir
387cdf0e10cSrcweir if (pFromRight.get())
388cdf0e10cSrcweir {
389cdf0e10cSrcweir ConvMap::iterator aRightIt = GetEntry( *pFromRight, rRightText, rLeftText );
390cdf0e10cSrcweir DBG_ASSERT( aRightIt != pFromRight->end(), "right map entry missing" );
391cdf0e10cSrcweir pFromRight->erase( aRightIt );
392cdf0e10cSrcweir }
393cdf0e10cSrcweir
394cdf0e10cSrcweir bIsModified = sal_True;
395cdf0e10cSrcweir bMaxCharCountIsValid = sal_False;
396cdf0e10cSrcweir }
397cdf0e10cSrcweir
398cdf0e10cSrcweir
getName()399cdf0e10cSrcweir OUString SAL_CALL ConvDic::getName( )
400cdf0e10cSrcweir throw (RuntimeException)
401cdf0e10cSrcweir {
402cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
403cdf0e10cSrcweir return aName;
404cdf0e10cSrcweir }
405cdf0e10cSrcweir
406cdf0e10cSrcweir
getLocale()407cdf0e10cSrcweir Locale SAL_CALL ConvDic::getLocale( )
408cdf0e10cSrcweir throw (RuntimeException)
409cdf0e10cSrcweir {
410cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
411cdf0e10cSrcweir return CreateLocale( nLanguage );
412cdf0e10cSrcweir }
413cdf0e10cSrcweir
414cdf0e10cSrcweir
getConversionType()415cdf0e10cSrcweir sal_Int16 SAL_CALL ConvDic::getConversionType( )
416cdf0e10cSrcweir throw (RuntimeException)
417cdf0e10cSrcweir {
418cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
419cdf0e10cSrcweir return nConversionType;
420cdf0e10cSrcweir }
421cdf0e10cSrcweir
422cdf0e10cSrcweir
setActive(sal_Bool bActivate)423cdf0e10cSrcweir void SAL_CALL ConvDic::setActive( sal_Bool bActivate )
424cdf0e10cSrcweir throw (RuntimeException)
425cdf0e10cSrcweir {
426cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
427cdf0e10cSrcweir bIsActive = bActivate;
428cdf0e10cSrcweir }
429cdf0e10cSrcweir
430cdf0e10cSrcweir
isActive()431cdf0e10cSrcweir sal_Bool SAL_CALL ConvDic::isActive( )
432cdf0e10cSrcweir throw (RuntimeException)
433cdf0e10cSrcweir {
434cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
435cdf0e10cSrcweir return bIsActive;
436cdf0e10cSrcweir }
437cdf0e10cSrcweir
438cdf0e10cSrcweir
clear()439cdf0e10cSrcweir void SAL_CALL ConvDic::clear( )
440cdf0e10cSrcweir throw (RuntimeException)
441cdf0e10cSrcweir {
442cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
443cdf0e10cSrcweir aFromLeft .clear();
444cdf0e10cSrcweir if (pFromRight.get())
445cdf0e10cSrcweir pFromRight->clear();
446cdf0e10cSrcweir bNeedEntries = sal_False;
447cdf0e10cSrcweir bIsModified = sal_True;
448cdf0e10cSrcweir nMaxLeftCharCount = 0;
449cdf0e10cSrcweir nMaxRightCharCount = 0;
450cdf0e10cSrcweir bMaxCharCountIsValid = sal_True;
451cdf0e10cSrcweir }
452cdf0e10cSrcweir
453cdf0e10cSrcweir
getConversions(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,ConversionDirection eDirection,sal_Int32)454cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL ConvDic::getConversions(
455cdf0e10cSrcweir const OUString& aText,
456cdf0e10cSrcweir sal_Int32 nStartPos,
457cdf0e10cSrcweir sal_Int32 nLength,
458cdf0e10cSrcweir ConversionDirection eDirection,
459cdf0e10cSrcweir sal_Int32 /*nTextConversionOptions*/ )
460cdf0e10cSrcweir throw (IllegalArgumentException, RuntimeException)
461cdf0e10cSrcweir {
462cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
463cdf0e10cSrcweir
464cdf0e10cSrcweir if (!pFromRight.get() && eDirection == ConversionDirection_FROM_RIGHT)
465cdf0e10cSrcweir return uno::Sequence< OUString >();
466cdf0e10cSrcweir
467cdf0e10cSrcweir if (bNeedEntries)
468cdf0e10cSrcweir Load();
469cdf0e10cSrcweir
470cdf0e10cSrcweir OUString aLookUpText( aText.copy(nStartPos, nLength) );
471cdf0e10cSrcweir ConvMap &rConvMap = eDirection == ConversionDirection_FROM_LEFT ?
472cdf0e10cSrcweir aFromLeft : *pFromRight;
473cdf0e10cSrcweir pair< ConvMap::iterator, ConvMap::iterator > aRange =
474cdf0e10cSrcweir rConvMap.equal_range( aLookUpText );
475cdf0e10cSrcweir
476cdf0e10cSrcweir sal_Int32 nCount = 0;
477cdf0e10cSrcweir ConvMap::iterator aIt;
478cdf0e10cSrcweir for (aIt = aRange.first; aIt != aRange.second; ++aIt)
479cdf0e10cSrcweir ++nCount;
480cdf0e10cSrcweir
481cdf0e10cSrcweir uno::Sequence< OUString > aRes( nCount );
482cdf0e10cSrcweir OUString *pRes = aRes.getArray();
483cdf0e10cSrcweir sal_Int32 i = 0;
484cdf0e10cSrcweir for (aIt = aRange.first; aIt != aRange.second; ++aIt)
485cdf0e10cSrcweir pRes[i++] = (*aIt).second;
486cdf0e10cSrcweir
487cdf0e10cSrcweir return aRes;
488cdf0e10cSrcweir }
489cdf0e10cSrcweir
490cdf0e10cSrcweir
lcl_SeqHasEntry(const OUString * pSeqStart,sal_Int32 nToCheck,const OUString & rText)491cdf0e10cSrcweir static sal_Bool lcl_SeqHasEntry(
492cdf0e10cSrcweir const OUString *pSeqStart, // first element to check
493cdf0e10cSrcweir sal_Int32 nToCheck, // number of elements to check
494cdf0e10cSrcweir const OUString &rText)
495cdf0e10cSrcweir {
496cdf0e10cSrcweir sal_Bool bRes = sal_False;
497cdf0e10cSrcweir if (pSeqStart && nToCheck > 0)
498cdf0e10cSrcweir {
499cdf0e10cSrcweir const OUString *pDone = pSeqStart + nToCheck; // one behind last to check
500cdf0e10cSrcweir while (!bRes && pSeqStart != pDone)
501cdf0e10cSrcweir {
502cdf0e10cSrcweir if (*pSeqStart++ == rText)
503cdf0e10cSrcweir bRes = sal_True;
504cdf0e10cSrcweir }
505cdf0e10cSrcweir }
506cdf0e10cSrcweir return bRes;
507cdf0e10cSrcweir }
508cdf0e10cSrcweir
getConversionEntries(ConversionDirection eDirection)509cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL ConvDic::getConversionEntries(
510cdf0e10cSrcweir ConversionDirection eDirection )
511cdf0e10cSrcweir throw (RuntimeException)
512cdf0e10cSrcweir {
513cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
514cdf0e10cSrcweir
515cdf0e10cSrcweir if (!pFromRight.get() && eDirection == ConversionDirection_FROM_RIGHT)
516cdf0e10cSrcweir return uno::Sequence< OUString >();
517cdf0e10cSrcweir
518cdf0e10cSrcweir if (bNeedEntries)
519cdf0e10cSrcweir Load();
520cdf0e10cSrcweir
521cdf0e10cSrcweir ConvMap &rConvMap = eDirection == ConversionDirection_FROM_LEFT ?
522cdf0e10cSrcweir aFromLeft : *pFromRight;
523cdf0e10cSrcweir uno::Sequence< OUString > aRes( rConvMap.size() );
524cdf0e10cSrcweir OUString *pRes = aRes.getArray();
525cdf0e10cSrcweir ConvMap::iterator aIt = rConvMap.begin();
526cdf0e10cSrcweir sal_Int32 nIdx = 0;
527cdf0e10cSrcweir while (aIt != rConvMap.end())
528cdf0e10cSrcweir {
529cdf0e10cSrcweir OUString aCurEntry( (*aIt).first );
530cdf0e10cSrcweir // skip duplicate entries ( duplicate = duplicate entries
531cdf0e10cSrcweir // respective to the evaluated side (FROM_LEFT or FROM_RIGHT).
532cdf0e10cSrcweir // Thus if FROM_LEFT is evaluated for pairs (A,B) and (A,C)
533cdf0e10cSrcweir // only one entry for A will be returned in the result)
534cdf0e10cSrcweir if (nIdx == 0 || !lcl_SeqHasEntry( pRes, nIdx, aCurEntry ))
535cdf0e10cSrcweir pRes[ nIdx++ ] = aCurEntry;
536cdf0e10cSrcweir ++aIt;
537cdf0e10cSrcweir }
538cdf0e10cSrcweir aRes.realloc( nIdx );
539cdf0e10cSrcweir
540cdf0e10cSrcweir return aRes;
541cdf0e10cSrcweir }
542cdf0e10cSrcweir
543cdf0e10cSrcweir
addEntry(const OUString & aLeftText,const OUString & aRightText)544cdf0e10cSrcweir void SAL_CALL ConvDic::addEntry(
545cdf0e10cSrcweir const OUString& aLeftText,
546cdf0e10cSrcweir const OUString& aRightText )
547cdf0e10cSrcweir throw (IllegalArgumentException, container::ElementExistException, RuntimeException)
548cdf0e10cSrcweir {
549cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
550cdf0e10cSrcweir if (bNeedEntries)
551cdf0e10cSrcweir Load();
552cdf0e10cSrcweir if (HasEntry( aLeftText, aRightText ))
553cdf0e10cSrcweir throw container::ElementExistException();
554cdf0e10cSrcweir AddEntry( aLeftText, aRightText );
555cdf0e10cSrcweir }
556cdf0e10cSrcweir
557cdf0e10cSrcweir
removeEntry(const OUString & aLeftText,const OUString & aRightText)558cdf0e10cSrcweir void SAL_CALL ConvDic::removeEntry(
559cdf0e10cSrcweir const OUString& aLeftText,
560cdf0e10cSrcweir const OUString& aRightText )
561cdf0e10cSrcweir throw (container::NoSuchElementException, RuntimeException)
562cdf0e10cSrcweir {
563cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
564cdf0e10cSrcweir if (bNeedEntries)
565cdf0e10cSrcweir Load();
566cdf0e10cSrcweir if (!HasEntry( aLeftText, aRightText ))
567cdf0e10cSrcweir throw container::NoSuchElementException();
568cdf0e10cSrcweir RemoveEntry( aLeftText, aRightText );
569cdf0e10cSrcweir }
570cdf0e10cSrcweir
571cdf0e10cSrcweir
getMaxCharCount(ConversionDirection eDirection)572cdf0e10cSrcweir sal_Int16 SAL_CALL ConvDic::getMaxCharCount( ConversionDirection eDirection )
573cdf0e10cSrcweir throw (RuntimeException)
574cdf0e10cSrcweir {
575cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
576cdf0e10cSrcweir
577cdf0e10cSrcweir if (!pFromRight.get() && eDirection == ConversionDirection_FROM_RIGHT)
578cdf0e10cSrcweir {
579cdf0e10cSrcweir DBG_ASSERT( nMaxRightCharCount == 0, "max right char count should be 0" );
580cdf0e10cSrcweir return 0;
581cdf0e10cSrcweir }
582cdf0e10cSrcweir
583cdf0e10cSrcweir if (bNeedEntries)
584cdf0e10cSrcweir Load();
585cdf0e10cSrcweir
586cdf0e10cSrcweir if (!bMaxCharCountIsValid)
587cdf0e10cSrcweir {
588cdf0e10cSrcweir nMaxLeftCharCount = 0;
589cdf0e10cSrcweir ConvMap::iterator aIt = aFromLeft.begin();
590cdf0e10cSrcweir while (aIt != aFromLeft.end())
591cdf0e10cSrcweir {
592cdf0e10cSrcweir sal_Int16 nTmp = (sal_Int16) (*aIt).first.getLength();
593cdf0e10cSrcweir if (nTmp > nMaxLeftCharCount)
594cdf0e10cSrcweir nMaxLeftCharCount = nTmp;
595cdf0e10cSrcweir ++aIt;
596cdf0e10cSrcweir }
597cdf0e10cSrcweir
598cdf0e10cSrcweir nMaxRightCharCount = 0;
599cdf0e10cSrcweir if (pFromRight.get())
600cdf0e10cSrcweir {
601cdf0e10cSrcweir aIt = pFromRight->begin();
602cdf0e10cSrcweir while (aIt != pFromRight->end())
603cdf0e10cSrcweir {
604cdf0e10cSrcweir sal_Int16 nTmp = (sal_Int16) (*aIt).first.getLength();
605cdf0e10cSrcweir if (nTmp > nMaxRightCharCount)
606cdf0e10cSrcweir nMaxRightCharCount = nTmp;
607cdf0e10cSrcweir ++aIt;
608cdf0e10cSrcweir }
609cdf0e10cSrcweir }
610cdf0e10cSrcweir
611cdf0e10cSrcweir bMaxCharCountIsValid = sal_True;
612cdf0e10cSrcweir }
613cdf0e10cSrcweir sal_Int16 nRes = eDirection == ConversionDirection_FROM_LEFT ?
614cdf0e10cSrcweir nMaxLeftCharCount : nMaxRightCharCount;
615cdf0e10cSrcweir DBG_ASSERT( nRes >= 0, "invalid MaxCharCount" );
616cdf0e10cSrcweir return nRes;
617cdf0e10cSrcweir }
618cdf0e10cSrcweir
619cdf0e10cSrcweir
setPropertyType(const OUString & rLeftText,const OUString & rRightText,sal_Int16 nPropertyType)620cdf0e10cSrcweir void SAL_CALL ConvDic::setPropertyType(
621cdf0e10cSrcweir const OUString& rLeftText,
622cdf0e10cSrcweir const OUString& rRightText,
623cdf0e10cSrcweir sal_Int16 nPropertyType )
624cdf0e10cSrcweir throw (container::NoSuchElementException, IllegalArgumentException, RuntimeException)
625cdf0e10cSrcweir {
626cdf0e10cSrcweir sal_Bool bHasElement = HasEntry( rLeftText, rRightText);
627cdf0e10cSrcweir if (!bHasElement)
628cdf0e10cSrcweir throw container::NoSuchElementException();
629cdf0e10cSrcweir
630cdf0e10cSrcweir // currently we assume that entries with the same left text have the
631cdf0e10cSrcweir // same PropertyType even if the right text is different...
632cdf0e10cSrcweir if (pConvPropType.get())
633cdf0e10cSrcweir pConvPropType->insert( PropTypeMap::value_type( rLeftText, nPropertyType ) );
634cdf0e10cSrcweir bIsModified = sal_True;
635cdf0e10cSrcweir }
636cdf0e10cSrcweir
637cdf0e10cSrcweir
getPropertyType(const OUString & rLeftText,const OUString & rRightText)638cdf0e10cSrcweir sal_Int16 SAL_CALL ConvDic::getPropertyType(
639cdf0e10cSrcweir const OUString& rLeftText,
640cdf0e10cSrcweir const OUString& rRightText )
641cdf0e10cSrcweir throw (container::NoSuchElementException, RuntimeException)
642cdf0e10cSrcweir {
643cdf0e10cSrcweir sal_Bool bHasElement = HasEntry( rLeftText, rRightText);
644cdf0e10cSrcweir if (!bHasElement)
645cdf0e10cSrcweir throw container::NoSuchElementException();
646cdf0e10cSrcweir
647cdf0e10cSrcweir sal_Int16 nRes = ConversionPropertyType::NOT_DEFINED;
648cdf0e10cSrcweir if (pConvPropType.get())
649cdf0e10cSrcweir {
650cdf0e10cSrcweir // still assuming that entries with same left text have same PropertyType
651cdf0e10cSrcweir // even if they have different right text...
652cdf0e10cSrcweir PropTypeMap::iterator aIt = pConvPropType->find( rLeftText );
653cdf0e10cSrcweir if (aIt != pConvPropType->end())
654cdf0e10cSrcweir nRes = (*aIt).second;
655cdf0e10cSrcweir }
656cdf0e10cSrcweir return nRes;
657cdf0e10cSrcweir }
658cdf0e10cSrcweir
659cdf0e10cSrcweir
flush()660cdf0e10cSrcweir void SAL_CALL ConvDic::flush( )
661cdf0e10cSrcweir throw (RuntimeException)
662cdf0e10cSrcweir {
663cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
664cdf0e10cSrcweir
665cdf0e10cSrcweir if (!bIsModified)
666cdf0e10cSrcweir return;
667cdf0e10cSrcweir
668cdf0e10cSrcweir Save();
669cdf0e10cSrcweir
670cdf0e10cSrcweir // notify listeners
671cdf0e10cSrcweir EventObject aEvtObj;
672cdf0e10cSrcweir aEvtObj.Source = uno::Reference< XFlushable >( this );
673cdf0e10cSrcweir cppu::OInterfaceIteratorHelper aIt( aFlushListeners );
674cdf0e10cSrcweir while (aIt.hasMoreElements())
675cdf0e10cSrcweir {
676cdf0e10cSrcweir uno::Reference< util::XFlushListener > xRef( aIt.next(), UNO_QUERY );
677cdf0e10cSrcweir if (xRef.is())
678cdf0e10cSrcweir xRef->flushed( aEvtObj );
679cdf0e10cSrcweir }
680cdf0e10cSrcweir }
681cdf0e10cSrcweir
682cdf0e10cSrcweir
addFlushListener(const uno::Reference<util::XFlushListener> & rxListener)683cdf0e10cSrcweir void SAL_CALL ConvDic::addFlushListener(
684cdf0e10cSrcweir const uno::Reference< util::XFlushListener >& rxListener )
685cdf0e10cSrcweir throw (RuntimeException)
686cdf0e10cSrcweir {
687cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
688cdf0e10cSrcweir if (rxListener.is())
689cdf0e10cSrcweir aFlushListeners.addInterface( rxListener );
690cdf0e10cSrcweir }
691cdf0e10cSrcweir
692cdf0e10cSrcweir
removeFlushListener(const uno::Reference<util::XFlushListener> & rxListener)693cdf0e10cSrcweir void SAL_CALL ConvDic::removeFlushListener(
694cdf0e10cSrcweir const uno::Reference< util::XFlushListener >& rxListener )
695cdf0e10cSrcweir throw (RuntimeException)
696cdf0e10cSrcweir {
697cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
698cdf0e10cSrcweir if (rxListener.is())
699cdf0e10cSrcweir aFlushListeners.removeInterface( rxListener );
700cdf0e10cSrcweir }
701cdf0e10cSrcweir
702cdf0e10cSrcweir
getImplementationName()703cdf0e10cSrcweir OUString SAL_CALL ConvDic::getImplementationName( )
704cdf0e10cSrcweir throw (RuntimeException)
705cdf0e10cSrcweir {
706cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
707cdf0e10cSrcweir return getImplementationName_Static();
708cdf0e10cSrcweir }
709cdf0e10cSrcweir
710cdf0e10cSrcweir
supportsService(const OUString & rServiceName)711cdf0e10cSrcweir sal_Bool SAL_CALL ConvDic::supportsService( const OUString& rServiceName )
712cdf0e10cSrcweir throw (RuntimeException)
713cdf0e10cSrcweir {
714cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
715cdf0e10cSrcweir sal_Bool bRes = sal_False;
716cdf0e10cSrcweir if (rServiceName.equalsAscii( SN_CONV_DICTIONARY ))
717cdf0e10cSrcweir bRes = sal_True;
718cdf0e10cSrcweir return bRes;
719cdf0e10cSrcweir }
720cdf0e10cSrcweir
721cdf0e10cSrcweir
getSupportedServiceNames()722cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL ConvDic::getSupportedServiceNames( )
723cdf0e10cSrcweir throw (RuntimeException)
724cdf0e10cSrcweir {
725cdf0e10cSrcweir MutexGuard aGuard( GetLinguMutex() );
726cdf0e10cSrcweir return getSupportedServiceNames_Static();
727cdf0e10cSrcweir }
728cdf0e10cSrcweir
729cdf0e10cSrcweir
getSupportedServiceNames_Static()730cdf0e10cSrcweir uno::Sequence< OUString > ConvDic::getSupportedServiceNames_Static()
731cdf0e10cSrcweir throw()
732cdf0e10cSrcweir {
733cdf0e10cSrcweir uno::Sequence< OUString > aSNS( 1 );
734cdf0e10cSrcweir aSNS.getArray()[0] = A2OU( SN_CONV_DICTIONARY );
735cdf0e10cSrcweir return aSNS;
736cdf0e10cSrcweir }
737cdf0e10cSrcweir
738cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
739cdf0e10cSrcweir
740cdf0e10cSrcweir
741